1 /*
2 * libc/stdlib/malloc/heap_alloc_at.c -- allocate at a specific address
3 *
4 * Copyright (C) 2002 NEC Corporation
5 * Copyright (C) 2002 Miles Bader <miles@gnu.org>
6 *
7 * This file is subject to the terms and conditions of the GNU Lesser
8 * General Public License. See the file COPYING.LIB in the main
9 * directory of this archive for more details.
10 *
11 * Written by Miles Bader <miles@gnu.org>
12 */
13
14 #include <stdlib.h>
15
16 #include "heap.h"
17
18
19 /* Allocate SIZE bytes at address MEM in HEAP. Return the actual size
20 allocated, or 0 if we failed. */
21 size_t
__heap_alloc_at(struct heap_free_area ** heap,void * mem,size_t size)22 __heap_alloc_at (struct heap_free_area **heap, void *mem, size_t size)
23 {
24 struct heap_free_area *fa;
25 size_t alloced = 0;
26
27 size = HEAP_ADJUST_SIZE (size);
28
29 HEAP_DEBUG (*heap, "before __heap_alloc_at");
30
31 /* Look for a free area that can contain SIZE bytes. */
32 for (fa = *heap; fa; fa = fa->next)
33 {
34 void *fa_mem = HEAP_FREE_AREA_START (fa);
35 if (fa_mem <= mem)
36 {
37 if (fa_mem == mem && fa->size >= size)
38 /* FA has the right addr, and is big enough! */
39 alloced = __heap_free_area_alloc (heap, fa, size);
40 break;
41 }
42 }
43
44 HEAP_DEBUG (*heap, "after __heap_alloc_at");
45
46 return alloced;
47 }
48