heap_alloc_at.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. #include <stdlib.h>
  14. #include "heap.h"
  15. /* Allocate SIZE bytes at address MEM in HEAP. Return the actual size
  16. allocated, or 0 if we failed. */
  17. size_t
  18. __heap_alloc_at (struct heap_free_area **heap, void *mem, size_t size)
  19. {
  20. struct heap_free_area *fa;
  21. size_t alloced = 0;
  22. size = HEAP_ADJUST_SIZE (size);
  23. HEAP_DEBUG (*heap, "before __heap_alloc_at");
  24. /* Look for a free area that can contain SIZE bytes. */
  25. for (fa = *heap; fa; fa = fa->next)
  26. {
  27. void *fa_mem = HEAP_FREE_AREA_START (fa);
  28. if (fa_mem <= mem)
  29. {
  30. if (fa_mem == mem && fa->size >= size)
  31. /* FA has the right addr, and is big enough! */
  32. alloced = __heap_free_area_alloc (heap, fa, size);
  33. break;
  34. }
  35. }
  36. HEAP_DEBUG (*heap, "after __heap_alloc_at");
  37. return alloced;
  38. }