heap_alloc_at.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * libc/stdlib/malloc-zarg/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 *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. mutex_lock (heap->lock);
  24. HEAP_DEBUG (heap, "before __heap_alloc_at");
  25. /* Look for a free area that can contain SIZE bytes. */
  26. for (fa = heap->free_areas; fa; fa = fa->next)
  27. {
  28. void *fa_mem = HEAP_FREE_AREA_START (fa);
  29. if (fa_mem <= mem)
  30. {
  31. if (fa_mem == mem && fa->size >= size)
  32. /* FA has the right addr, and is big enough! */
  33. alloced = __heap_free_area_alloc (heap, fa, size);
  34. break;
  35. }
  36. }
  37. HEAP_DEBUG (heap, "after __heap_alloc_at");
  38. mutex_unlock (heap->lock);
  39. return alloced;
  40. }