heap_free.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * libc/stdlib/malloc/heap_free.c -- return memory to a heap
  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. /* Return the memory area MEM of size SIZE to HEAP. */
  16. struct heap_free_area *
  17. __heap_free (struct heap *heap, void *mem, size_t size)
  18. {
  19. struct heap_free_area *prev_fa, *fa;
  20. void *end = (char *)mem + size;
  21. __heap_lock (heap);
  22. HEAP_DEBUG (heap, "before __heap_free");
  23. /* Find an adjacent free-list entry. */
  24. for (prev_fa = 0, fa = heap->free_areas; fa; prev_fa = fa, fa = fa->next)
  25. {
  26. size_t fa_size = fa->size;
  27. void *fa_mem = HEAP_FREE_AREA_START (fa);
  28. if (end == fa_mem)
  29. /* FA is just after MEM, grow down to encompass it. */
  30. {
  31. fa_size += size;
  32. /* See if FA can now be merged with its predecessor. */
  33. if (prev_fa && fa_mem - size == HEAP_FREE_AREA_END (prev_fa))
  34. /* Yup; merge PREV_FA's info into FA. */
  35. {
  36. fa_size += prev_fa->size;
  37. __heap_link_free_area_after (heap, fa, prev_fa->prev);
  38. }
  39. fa->size = fa_size;
  40. goto done;
  41. }
  42. else if (HEAP_FREE_AREA_END (fa) == mem)
  43. /* FA is just before MEM, expand to encompass it. */
  44. {
  45. struct heap_free_area *next_fa = fa->next;
  46. fa_size += size;
  47. /* See if FA can now be merged with its successor. */
  48. if (next_fa && mem + size == HEAP_FREE_AREA_START (next_fa))
  49. {
  50. /* Yup; merge FA's info into NEXT_FA. */
  51. fa_size += next_fa->size;
  52. __heap_link_free_area_after (heap, next_fa, prev_fa);
  53. fa = next_fa;
  54. }
  55. else
  56. /* FA can't be merged; move the descriptor for it to the tail-end
  57. of the memory block. */
  58. {
  59. /* The new descriptor is at the end of the extended block,
  60. SIZE bytes later than the old descriptor. */
  61. fa = (struct heap_free_area *)((char *)fa + size);
  62. /* Update links with the neighbors in the list. */
  63. __heap_link_free_area (heap, fa, prev_fa, next_fa);
  64. }
  65. fa->size = fa_size;
  66. goto done;
  67. }
  68. else if (fa_mem > mem)
  69. /* We've reached the right spot in the free-list without finding an
  70. adjacent free-area, so continue below to add a new free area. */
  71. break;
  72. }
  73. /* Make MEM into a new free-list entry. */
  74. fa = __heap_add_free_area (heap, mem, size, prev_fa, fa);
  75. done:
  76. HEAP_DEBUG (heap, "after __heap_free");
  77. __heap_unlock (heap);
  78. return fa;
  79. }