free.c 8.0 KB

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