free.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Free a block of memory allocated by `malloc'.
  2. Copyright 1990, 1991, 1992, 1994 Free Software Foundation, Inc.
  3. Written May 1989 by Mike Haertel.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; see the file COPYING.LIB. If
  14. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  15. Cambridge, MA 02139, USA.
  16. The author may be reached (Email) at the address mike@ai.mit.edu,
  17. or (US mail) as Mike Haertel c/o Free Software Foundation. */
  18. #ifndef _MALLOC_INTERNAL
  19. #define _MALLOC_INTERNAL
  20. #include <malloc.h>
  21. #endif
  22. /* Debugging hook for free. */
  23. void (*__free_hook) __P ((__ptr_t __ptr));
  24. /* List of blocks allocated by memalign. */
  25. struct alignlist *_aligned_blocks = NULL;
  26. /* Return memory to the heap.
  27. Like `free' but don't call a __free_hook if there is one. */
  28. void
  29. _free_internal (ptr)
  30. __ptr_t ptr;
  31. {
  32. int type;
  33. __malloc_size_t block, blocks;
  34. register __malloc_size_t i;
  35. struct list *prev, *next;
  36. block = BLOCK (ptr);
  37. type = _heapinfo[block].busy.type;
  38. switch (type)
  39. {
  40. case 0:
  41. /* Get as many statistics as early as we can. */
  42. --_chunks_used;
  43. _bytes_used -= _heapinfo[block].busy.info.size * BLOCKSIZE;
  44. _bytes_free += _heapinfo[block].busy.info.size * BLOCKSIZE;
  45. /* Find the free cluster previous to this one in the free list.
  46. Start searching at the last block referenced; this may benefit
  47. programs with locality of allocation. */
  48. i = _heapindex;
  49. if (i > block)
  50. while (i > block)
  51. i = _heapinfo[i].free.prev;
  52. else
  53. {
  54. do
  55. i = _heapinfo[i].free.next;
  56. while (i > 0 && i < block);
  57. i = _heapinfo[i].free.prev;
  58. }
  59. /* Determine how to link this block into the free list. */
  60. if (block == i + _heapinfo[i].free.size)
  61. {
  62. /* Coalesce this block with its predecessor. */
  63. _heapinfo[i].free.size += _heapinfo[block].busy.info.size;
  64. block = i;
  65. }
  66. else
  67. {
  68. /* Really link this block back into the free list. */
  69. _heapinfo[block].free.size = _heapinfo[block].busy.info.size;
  70. _heapinfo[block].free.next = _heapinfo[i].free.next;
  71. _heapinfo[block].free.prev = i;
  72. _heapinfo[i].free.next = block;
  73. _heapinfo[_heapinfo[block].free.next].free.prev = block;
  74. ++_chunks_free;
  75. }
  76. /* Now that the block is linked in, see if we can coalesce it
  77. with its successor (by deleting its successor from the list
  78. and adding in its size). */
  79. if (block + _heapinfo[block].free.size == _heapinfo[block].free.next)
  80. {
  81. _heapinfo[block].free.size
  82. += _heapinfo[_heapinfo[block].free.next].free.size;
  83. _heapinfo[block].free.next
  84. = _heapinfo[_heapinfo[block].free.next].free.next;
  85. _heapinfo[_heapinfo[block].free.next].free.prev = block;
  86. --_chunks_free;
  87. }
  88. /* Now see if we can return stuff to the system. */
  89. blocks = _heapinfo[block].free.size;
  90. if (blocks >= FINAL_FREE_BLOCKS && block + blocks == _heaplimit
  91. && (*__morecore) (0) == ADDRESS (block + blocks))
  92. {
  93. register __malloc_size_t bytes = blocks * BLOCKSIZE;
  94. _heaplimit -= blocks;
  95. (*__morecore) (-bytes);
  96. _heapinfo[_heapinfo[block].free.prev].free.next
  97. = _heapinfo[block].free.next;
  98. _heapinfo[_heapinfo[block].free.next].free.prev
  99. = _heapinfo[block].free.prev;
  100. block = _heapinfo[block].free.prev;
  101. --_chunks_free;
  102. _bytes_free -= bytes;
  103. }
  104. /* Set the next search to begin at this block. */
  105. _heapindex = block;
  106. break;
  107. default:
  108. /* Do some of the statistics. */
  109. --_chunks_used;
  110. _bytes_used -= 1 << type;
  111. ++_chunks_free;
  112. _bytes_free += 1 << type;
  113. /* Get the address of the first free fragment in this block. */
  114. prev = (struct list *) ((char *) ADDRESS (block) +
  115. (_heapinfo[block].busy.info.frag.first << type));
  116. if (_heapinfo[block].busy.info.frag.nfree == (BLOCKSIZE >> type) - 1)
  117. {
  118. /* If all fragments of this block are free, remove them
  119. from the fragment list and free the whole block. */
  120. next = prev;
  121. for (i = 1; i < (__malloc_size_t) (BLOCKSIZE >> type); ++i)
  122. next = next->next;
  123. prev->prev->next = next;
  124. if (next != NULL)
  125. next->prev = prev->prev;
  126. _heapinfo[block].busy.type = 0;
  127. _heapinfo[block].busy.info.size = 1;
  128. /* Keep the statistics accurate. */
  129. ++_chunks_used;
  130. _bytes_used += BLOCKSIZE;
  131. _chunks_free -= BLOCKSIZE >> type;
  132. _bytes_free -= BLOCKSIZE;
  133. free (ADDRESS (block));
  134. }
  135. else if (_heapinfo[block].busy.info.frag.nfree != 0)
  136. {
  137. /* If some fragments of this block are free, link this
  138. fragment into the fragment list after the first free
  139. fragment of this block. */
  140. next = (struct list *) ptr;
  141. next->next = prev->next;
  142. next->prev = prev;
  143. prev->next = next;
  144. if (next->next != NULL)
  145. next->next->prev = next;
  146. ++_heapinfo[block].busy.info.frag.nfree;
  147. }
  148. else
  149. {
  150. /* No fragments of this block are free, so link this
  151. fragment into the fragment list and announce that
  152. it is the first free fragment of this block. */
  153. prev = (struct list *) ptr;
  154. _heapinfo[block].busy.info.frag.nfree = 1;
  155. _heapinfo[block].busy.info.frag.first = (unsigned long int)
  156. ((unsigned long int) ((char *) ptr - (char *) NULL)
  157. % BLOCKSIZE >> type);
  158. prev->next = _fraghead[type].next;
  159. prev->prev = &_fraghead[type];
  160. prev->prev->next = prev;
  161. if (prev->next != NULL)
  162. prev->next->prev = prev;
  163. }
  164. break;
  165. }
  166. }
  167. /* Return memory to the heap. */
  168. void
  169. free (ptr)
  170. __ptr_t ptr;
  171. {
  172. register struct alignlist *l;
  173. if (ptr == NULL)
  174. return;
  175. for (l = _aligned_blocks; l != NULL; l = l->next)
  176. if (l->aligned == ptr)
  177. {
  178. l->aligned = NULL; /* Mark the slot in the list as free. */
  179. ptr = l->exact;
  180. break;
  181. }
  182. if (__free_hook != NULL)
  183. (*__free_hook) (ptr);
  184. else
  185. _free_internal (ptr);
  186. }