heap.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * libc/stdlib/malloc/heap.h -- heap allocator used for malloc
  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 <features.h>
  14. /* The heap allocates in multiples of, and aligned to, HEAP_GRANULARITY.
  15. HEAP_GRANULARITY must be a power of 2. Malloc depends on this being the
  16. same as MALLOC_ALIGNMENT. */
  17. #define HEAP_GRANULARITY (sizeof (double))
  18. /* A heap is a collection of memory blocks, from which smaller blocks
  19. of memory can be allocated. */
  20. struct heap
  21. {
  22. /* A list of memory in the heap available for allocation. */
  23. struct heap_free_area *free_areas;
  24. };
  25. #define HEAP_INIT { 0 }
  26. /* A free-list area `header'. These are actually stored at the _ends_ of
  27. free areas (to make allocating from the beginning of the area simpler),
  28. so one might call it a `footer'. */
  29. struct heap_free_area
  30. {
  31. size_t size;
  32. struct heap_free_area *next, *prev;
  33. };
  34. /* Return the address of the end of the frea area FA. */
  35. #define HEAP_FREE_AREA_END(fa) ((void *)(fa + 1))
  36. /* Return the address of the beginning of the frea area FA. FA is
  37. evaulated multiple times. */
  38. #define HEAP_FREE_AREA_START(fa) ((void *)((char *)(fa + 1) - (fa)->size))
  39. /* Return the size of the frea area FA. */
  40. #define HEAP_FREE_AREA_SIZE(fa) ((fa)->size)
  41. /* Rounds SZ up to be a multiple of HEAP_GRANULARITY. */
  42. #define HEAP_ADJUST_SIZE(sz) \
  43. (((sz) + HEAP_GRANULARITY - 1) & ~(HEAP_GRANULARITY - 1))
  44. /* The minimum allocatable size. */
  45. #define HEAP_MIN_SIZE HEAP_ADJUST_SIZE (sizeof (struct heap_free_area))
  46. /* The minimum size of a free area; if allocating memory from a free-area
  47. would make the free-area smaller than this, the allocation is simply
  48. given the whole free-area instead. It must include at least enough room
  49. to hold a struct heap_free_area, plus some extra to avoid excessive heap
  50. fragmentation (thus increasing speed). This is only a heuristic -- it's
  51. possible for smaller free-areas than this to exist (say, by realloc
  52. returning the tail-end of a previous allocation), but __heap_alloc will
  53. try to get rid of them when possible. */
  54. #define HEAP_MIN_FREE_AREA_SIZE \
  55. HEAP_ADJUST_SIZE (sizeof (struct heap_free_area) + 32)
  56. /* branch-prediction macros; they may already be defined by libc. */
  57. #ifndef likely
  58. #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
  59. #define likely(cond) __builtin_expect(!!(int)(cond), 1)
  60. #define unlikely(cond) __builtin_expect((int)(cond), 0)
  61. #else
  62. #define likely(cond) (cond)
  63. #define unlikely(cond) (cond)
  64. #endif
  65. #endif /* !likely */
  66. /* Define HEAP_DEBUGGING to cause the heap routines to emit debugging info
  67. to stderr. */
  68. #ifdef HEAP_DEBUGGING
  69. #include <stdio.h>
  70. static void HEAP_DEBUG (struct heap *heap, const char *str)
  71. {
  72. static int recursed = 0;
  73. if (! recursed)
  74. {
  75. struct heap_free_area *fa, *prev;
  76. recursed = 1;
  77. fprintf (stderr, " %s: heap @0x%lx:\n", str, (long)heap);
  78. for (prev = 0, fa = heap->free_areas; fa; prev = fa, fa = fa->next)
  79. {
  80. fprintf (stderr,
  81. " 0x%lx: 0x%lx - 0x%lx (%d)\tP=0x%lx, N=0x%lx\n",
  82. (long)fa,
  83. (long)HEAP_FREE_AREA_START (fa),
  84. (long)HEAP_FREE_AREA_END (fa),
  85. fa->size,
  86. (long)fa->prev,
  87. (long)fa->next);
  88. if (fa->prev != prev)
  89. fprintf (stderr,
  90. " PREV POINTER CORRUPTED!!!! P=0x%lx should be 0x%lx\n",
  91. (long)fa->prev, (long)prev);
  92. }
  93. recursed = 0;
  94. }
  95. }
  96. #else
  97. #define HEAP_DEBUG(heap, str) (void)0
  98. #endif
  99. /* Delete the free-area FA from HEAP. */
  100. extern inline void
  101. __heap_delete (struct heap *heap, struct heap_free_area *fa)
  102. {
  103. if (fa->next)
  104. fa->next->prev = fa->prev;
  105. if (fa->prev)
  106. fa->prev->next = fa->next;
  107. else
  108. heap->free_areas = fa->next;
  109. }
  110. /* Link the free-area FA between the existing free-area's PREV and NEXT in
  111. HEAP. PREV and NEXT may be 0; if PREV is 0, FA is installed as the
  112. first free-area. */
  113. extern inline void
  114. __heap_link_free_area (struct heap *heap, struct heap_free_area *fa,
  115. struct heap_free_area *prev,
  116. struct heap_free_area *next)
  117. {
  118. fa->next = next;
  119. fa->prev = prev;
  120. if (prev)
  121. prev->next = fa;
  122. else
  123. heap->free_areas = fa;
  124. if (next)
  125. next->prev = fa;
  126. }
  127. /* Update the mutual links between the free-areas PREV and FA in HEAP.
  128. PREV may be 0, in which case FA is installed as the first free-area (but
  129. FA may not be 0). */
  130. extern inline void
  131. __heap_link_free_area_after (struct heap *heap,
  132. struct heap_free_area *fa,
  133. struct heap_free_area *prev)
  134. {
  135. if (prev)
  136. prev->next = fa;
  137. else
  138. heap->free_areas = fa;
  139. fa->prev = prev;
  140. }
  141. /* Add a new free-area MEM, of length SIZE, in between the existing
  142. free-area's PREV and NEXT in HEAP, and return a pointer to its header.
  143. PREV and NEXT may be 0; if PREV is 0, MEM is installed as the first
  144. free-area. */
  145. extern inline struct heap_free_area *
  146. __heap_add_free_area (struct heap *heap, void *mem, size_t size,
  147. struct heap_free_area *prev,
  148. struct heap_free_area *next)
  149. {
  150. struct heap_free_area *fa = (struct heap_free_area *)
  151. ((char *)mem + size - sizeof (struct heap_free_area));
  152. fa->size = size;
  153. __heap_link_free_area (heap, fa, prev, next);
  154. return fa;
  155. }
  156. /* Allocate SIZE bytes from the front of the free-area FA in HEAP, and
  157. return the amount actually allocated (which may be more than SIZE). */
  158. extern inline size_t
  159. __heap_free_area_alloc (struct heap *heap,
  160. struct heap_free_area *fa, size_t size)
  161. {
  162. size_t fa_size = fa->size;
  163. if (fa_size < size + HEAP_MIN_FREE_AREA_SIZE)
  164. /* There's not enough room left over in FA after allocating the block, so
  165. just use the whole thing, removing it from the list of free areas. */
  166. {
  167. __heap_delete (heap, fa);
  168. /* Remember that we've alloced the whole area. */
  169. size = fa_size;
  170. }
  171. else
  172. /* Reduce size of FA to account for this allocation. */
  173. fa->size = fa_size - size;
  174. return size;
  175. }
  176. /* Allocate and return a block at least *SIZE bytes long from HEAP.
  177. *SIZE is adjusted to reflect the actual amount allocated (which may be
  178. greater than requested). */
  179. extern void *__heap_alloc (struct heap *heap, size_t *size);
  180. /* Allocate SIZE bytes at address MEM in HEAP. Return the actual size
  181. allocated, or 0 if we failed. */
  182. extern size_t __heap_alloc_at (struct heap *heap, void *mem, size_t size);
  183. /* Return the memory area MEM of size SIZE to HEAP.
  184. Returns the heap free area into which the memory was placed. */
  185. extern struct heap_free_area *__heap_free (struct heap *heap,
  186. void *mem, size_t size);
  187. /* Return true if HEAP contains absolutely no memory. */
  188. #define __heap_is_empty(heap) (! (heap)->free_areas)