free.c 4.5 KB

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