free.c 4.5 KB

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