free.c 4.0 KB

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