free.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * libc/stdlib/malloc/free.c -- free function
  3. *
  4. * Copyright (C) 2002,03 NEC Electronics Corporation
  5. * Copyright (C) 2002,03 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 <unistd.h>
  15. #include <sys/mman.h>
  16. #include "malloc.h"
  17. #include "heap.h"
  18. static void
  19. free_to_heap (void *mem, struct heap *heap)
  20. {
  21. size_t size;
  22. struct heap_free_area *fa;
  23. /* Check for special cases. */
  24. if (unlikely (! mem))
  25. return;
  26. /* Normal free. */
  27. MALLOC_DEBUG (1, "free: 0x%lx (base = 0x%lx, total_size = %d)",
  28. (long)mem, (long)MALLOC_BASE (mem), MALLOC_SIZE (mem));
  29. size = MALLOC_SIZE (mem);
  30. mem = MALLOC_BASE (mem);
  31. __heap_lock (heap);
  32. /* Put MEM back in the heap, and get the free-area it was placed in. */
  33. fa = __heap_free (heap, mem, size);
  34. /* See if the free-area FA has grown big enough that it should be
  35. unmapped. */
  36. if (HEAP_FREE_AREA_SIZE (fa) < MALLOC_UNMAP_THRESHOLD)
  37. /* Nope, nothing left to do, just release the lock. */
  38. __heap_unlock (heap);
  39. else
  40. /* Yup, try to unmap FA. */
  41. {
  42. unsigned long start = (unsigned long)HEAP_FREE_AREA_START (fa);
  43. unsigned long end = (unsigned long)HEAP_FREE_AREA_END (fa);
  44. #ifndef MALLOC_USE_SBRK
  45. # ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
  46. struct malloc_mmb *mmb, *prev_mmb;
  47. unsigned long mmb_start, mmb_end;
  48. # else /* !__UCLIBC_UCLINUX_BROKEN_MUNMAP__ */
  49. unsigned long unmap_start, unmap_end;
  50. # endif /* __UCLIBC_UCLINUX_BROKEN_MUNMAP__ */
  51. #endif /* !MALLOC_USE_SBRK */
  52. #ifdef MALLOC_USE_SBRK
  53. /* Get the sbrk lock so that the two possible calls to sbrk below
  54. are guaranteed to be contiguous. */
  55. __malloc_lock_sbrk ();
  56. /* When using sbrk, we only shrink the heap from the end. It would
  57. be possible to allow _both_ -- shrinking via sbrk when possible,
  58. and otherwise shrinking via munmap, but this results in holes in
  59. memory that prevent the brk from every growing back down; since
  60. we only ever grow the heap via sbrk, this tends to produce a
  61. continuously growing brk (though the actual memory is unmapped),
  62. which could eventually run out of address space. Note that
  63. `sbrk(0)' shouldn't normally do a system call, so this test is
  64. reasonably cheap. */
  65. if ((void *)end != sbrk (0))
  66. {
  67. MALLOC_DEBUG (-1, "not unmapping: 0x%lx - 0x%lx (%ld bytes)",
  68. start, end, end - start);
  69. __malloc_unlock_sbrk ();
  70. __heap_unlock (heap);
  71. return;
  72. }
  73. #endif
  74. MALLOC_DEBUG (0, "unmapping: 0x%lx - 0x%lx (%ld bytes)",
  75. start, end, end - start);
  76. /* Remove FA from the heap. */
  77. __heap_delete (heap, fa);
  78. if (__heap_is_empty (heap))
  79. /* We want to avoid the heap from losing all memory, so reserve
  80. a bit. This test is only a heuristic -- the existance of
  81. another free area, even if it's smaller than
  82. MALLOC_MIN_SIZE, will cause us not to reserve anything. */
  83. {
  84. /* Put the reserved memory back in the heap; we asssume that
  85. MALLOC_UNMAP_THRESHOLD is greater than MALLOC_MIN_SIZE, so
  86. we use the latter unconditionally here. */
  87. __heap_free (heap, (void *)start, MALLOC_MIN_SIZE);
  88. start += MALLOC_MIN_SIZE;
  89. }
  90. #ifdef MALLOC_USE_SBRK
  91. /* Release the heap lock; we're still holding the sbrk lock. */
  92. __heap_unlock (heap);
  93. /* Lower the brk. */
  94. sbrk (start - end);
  95. /* Release the sbrk lock too; now we hold no locks. */
  96. __malloc_unlock_sbrk ();
  97. #else /* !MALLOC_USE_SBRK */
  98. # ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
  99. /* Using the uClinux broken munmap, we have to only munmap blocks
  100. exactly as we got them from mmap, so scan through our list of
  101. mmapped blocks, and return them in order. */
  102. MALLOC_MMB_DEBUG (1, "walking mmb list for region 0x%x[%d]...",
  103. start, end - start);
  104. prev_mmb = 0;
  105. mmb = __malloc_mmapped_blocks;
  106. while (mmb
  107. && ((mmb_end = (mmb_start = (unsigned long)mmb->mem) + mmb->size)
  108. <= end))
  109. {
  110. MALLOC_MMB_DEBUG (1, "considering mmb at 0x%x: 0x%x[%d]",
  111. (unsigned)mmb, mmb_start, mmb_end - mmb_start);
  112. if (mmb_start >= start
  113. /* If the space between START and MMB_START is non-zero, but
  114. too small to return to the heap, we can't unmap MMB. */
  115. && (start == mmb_start
  116. || mmb_start - start > HEAP_MIN_FREE_AREA_SIZE))
  117. {
  118. struct malloc_mmb *next_mmb = mmb->next;
  119. if (mmb_end != end && mmb_end + HEAP_MIN_FREE_AREA_SIZE > end)
  120. /* There's too little space left at the end to deallocate
  121. this block, so give up. */
  122. break;
  123. MALLOC_MMB_DEBUG (1, "unmapping mmb at 0x%x: 0x%x[%d]",
  124. (unsigned)mmb, mmb_start, mmb_end - mmb_start);
  125. if (mmb_start != start)
  126. /* We're going to unmap a part of the heap that begins after
  127. start, so put the intervening region back into the heap. */
  128. {
  129. MALLOC_MMB_DEBUG (0, "putting intervening region back into heap: 0x%x[%d]",
  130. start, mmb_start - start);
  131. __heap_free (heap, (void *)start, mmb_start - start);
  132. }
  133. MALLOC_MMB_DEBUG_INDENT (-1);
  134. /* Unlink MMB from the list. */
  135. if (prev_mmb)
  136. prev_mmb->next = next_mmb;
  137. else
  138. __malloc_mmapped_blocks = next_mmb;
  139. /* Start searching again from the end of this block. */
  140. start = mmb_end;
  141. /* We have to unlock the heap before we recurse to free the mmb
  142. descriptor, because we might be unmapping from the mmb
  143. heap. */
  144. __heap_unlock (heap);
  145. /* Release the descriptor block we used. */
  146. free_to_heap (mmb, &__malloc_mmb_heap);
  147. /* Do the actual munmap. */
  148. munmap ((void *)mmb_start, mmb_end - mmb_start);
  149. __heap_lock (heap);
  150. # ifdef __UCLIBC_HAS_THREADS__
  151. /* In a multi-threaded program, it's possible that PREV_MMB has
  152. been invalidated by another thread when we released the
  153. heap lock to do the munmap system call, so just start over
  154. from the beginning of the list. It sucks, but oh well;
  155. it's probably not worth the bother to do better. */
  156. prev_mmb = 0;
  157. mmb = __malloc_mmapped_blocks;
  158. # else
  159. mmb = next_mmb;
  160. # endif
  161. }
  162. else
  163. {
  164. prev_mmb = mmb;
  165. mmb = mmb->next;
  166. }
  167. MALLOC_MMB_DEBUG_INDENT (-1);
  168. }
  169. if (start != end)
  170. /* Hmm, well there's something we couldn't unmap, so put it back
  171. into the heap. */
  172. {
  173. MALLOC_MMB_DEBUG (0, "putting tail region back into heap: 0x%x[%d]",
  174. start, end - start);
  175. __heap_free (heap, (void *)start, end - start);
  176. }
  177. /* Finally release the lock for good. */
  178. __heap_unlock (heap);
  179. MALLOC_MMB_DEBUG_INDENT (-1);
  180. # else /* !__UCLIBC_UCLINUX_BROKEN_MUNMAP__ */
  181. /* MEM/LEN may not be page-aligned, so we have to page-align them,
  182. and return any left-over bits on the end to the heap. */
  183. unmap_start = MALLOC_ROUND_UP_TO_PAGE_SIZE (start);
  184. unmap_end = MALLOC_ROUND_DOWN_TO_PAGE_SIZE (end);
  185. /* We have to be careful that any left-over bits are large enough to
  186. return. Note that we _don't check_ to make sure there's room to
  187. grow/shrink the start/end by another page, we just assume that
  188. the unmap threshold is high enough so that this is always safe
  189. (i.e., it should probably be at least 3 pages). */
  190. if (unmap_start > start)
  191. {
  192. if (unmap_start - start < HEAP_MIN_FREE_AREA_SIZE)
  193. unmap_start += MALLOC_PAGE_SIZE;
  194. __heap_free (heap, (void *)start, unmap_start - start);
  195. }
  196. if (end > unmap_end)
  197. {
  198. if (end - unmap_end < HEAP_MIN_FREE_AREA_SIZE)
  199. unmap_end -= MALLOC_PAGE_SIZE;
  200. __heap_free (heap, (void *)unmap_end, end - unmap_end);
  201. }
  202. /* Release the heap lock before we do the system call. */
  203. __heap_unlock (heap);
  204. if (unmap_end > unmap_start)
  205. /* Finally, actually unmap the memory. */
  206. munmap ((void *)unmap_start, unmap_end - unmap_start);
  207. # endif /* __UCLIBC_UCLINUX_BROKEN_MUNMAP__ */
  208. #endif /* MALLOC_USE_SBRK */
  209. }
  210. MALLOC_DEBUG_INDENT (-1);
  211. }
  212. void
  213. free (void *mem)
  214. {
  215. free_to_heap (mem, &__malloc_heap);
  216. }