free.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * libc/stdlib/malloc/free.c -- free function
  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 <unistd.h>
  15. #include <sys/mman.h>
  16. #include "malloc.h"
  17. #include "heap.h"
  18. void
  19. free (void *mem)
  20. {
  21. size_t size;
  22. struct heap_free_area *fa;
  23. struct heap *heap = &__malloc_heap;
  24. /* Check for special cases. */
  25. if (unlikely (! mem))
  26. return;
  27. /* Normal free. */
  28. MALLOC_DEBUG ("free: 0x%lx (base = 0x%lx, total_size = %d)\n",
  29. (long)mem, (long)MALLOC_BASE (mem), MALLOC_SIZE (mem));
  30. size = MALLOC_SIZE (mem);
  31. mem = MALLOC_BASE (mem);
  32. __malloc_lock ();
  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. __malloc_unlock ();
  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. unsigned long unmap_start, unmap_end;
  47. #endif
  48. #ifdef MALLOC_USE_SBRK
  49. /* Get the sbrk lock so that the two possible calls to sbrk below
  50. are guaranteed to be contiguous. */
  51. __malloc_lock_sbrk ();
  52. /* When using sbrk, we only shrink the heap from the end. It would
  53. be possible to allow _both_ -- shrinking via sbrk when possible,
  54. and otherwise shrinking via munmap, but this results in holes in
  55. memory that prevent the brk from every growing back down; since
  56. we only ever grow the heap via sbrk, this tends to produce a
  57. continuously growing brk (though the actual memory is unmapped),
  58. which could eventually run out of address space. Note that
  59. `sbrk(0)' shouldn't normally do a system call, so this test is
  60. reasonably cheap. */
  61. if ((void *)end != sbrk (0))
  62. {
  63. MALLOC_DEBUG (" not unmapping: 0x%lx - 0x%lx (%ld bytes)\n",
  64. start, end, end - start);
  65. __malloc_unlock_sbrk ();
  66. __malloc_unlock ();
  67. return;
  68. }
  69. #endif
  70. MALLOC_DEBUG (" unmapping: 0x%lx - 0x%lx (%ld bytes)\n",
  71. start, end, end - start);
  72. /* Remove FA from the heap. */
  73. __heap_delete (heap, fa);
  74. if (__heap_is_empty (heap))
  75. /* We want to avoid the heap from losing all memory, so reserve
  76. a bit. This test is only a heuristic -- the existance of
  77. another free area, even if it's smaller than
  78. MALLOC_MIN_SIZE, will cause us not to reserve anything. */
  79. {
  80. /* Put the reserved memory back in the heap; we asssume that
  81. MALLOC_UNMAP_THRESHOLD is greater than MALLOC_MIN_SIZE, so
  82. we use the latter unconditionally here. */
  83. __heap_free (heap, (void *)start, MALLOC_MIN_SIZE);
  84. start += MALLOC_MIN_SIZE;
  85. }
  86. #ifdef MALLOC_USE_SBRK
  87. /* Release the main lock; we're still holding the sbrk lock. */
  88. __malloc_unlock ();
  89. /* Lower the brk. */
  90. sbrk (start - end);
  91. /* Release the sbrk lock too; now we hold no locks. */
  92. __malloc_unlock_sbrk ();
  93. #else /* !MALLOC_USE_SBRK */
  94. /* MEM/LEN may not be page-aligned, so we have to page-align them,
  95. and return any left-over bits on the end to the heap. */
  96. unmap_start = MALLOC_ROUND_UP_TO_PAGE_SIZE (start);
  97. unmap_end = MALLOC_ROUND_DOWN_TO_PAGE_SIZE (end);
  98. /* We have to be careful that any left-over bits are large enough to
  99. return. Note that we _don't check_ to make sure there's room to
  100. grow/shrink the start/end by another page, we just assume that
  101. the unmap threshold is high enough so that this is always safe
  102. (i.e., it should probably be at least 3 pages). */
  103. if (unmap_start > start)
  104. {
  105. if (unmap_start - start < HEAP_MIN_FREE_AREA_SIZE)
  106. unmap_start += MALLOC_PAGE_SIZE;
  107. __heap_free (heap, (void *)start, unmap_start - start);
  108. }
  109. if (end > unmap_end)
  110. {
  111. if (end - unmap_end < HEAP_MIN_FREE_AREA_SIZE)
  112. unmap_end -= MALLOC_PAGE_SIZE;
  113. __heap_free (heap, (void *)unmap_end, end - unmap_end);
  114. }
  115. /* Release the malloc lock before we do the system call. */
  116. __malloc_unlock ();
  117. if (unmap_end > unmap_start)
  118. /* Finally, actually unmap the memory. */
  119. munmap ((void *)unmap_start, unmap_end - unmap_start);
  120. #endif /* MALLOC_USE_SBRK */
  121. }
  122. }