heap.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * libc/stdlib/malloc/heap.h -- heap allocator used for malloc
  3. *
  4. * Copyright (C) 2002,03 NEC Electronics Corporation
  5. * Copyright (C) 2002,03 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. /* On multi-threaded systems, the heap includes a lock. */
  15. #ifdef __UCLIBC_HAS_THREADS__
  16. # include <pthread.h>
  17. # define HEAP_USE_LOCKING
  18. #endif
  19. /* The heap allocates in multiples of, and aligned to, HEAP_GRANULARITY.
  20. HEAP_GRANULARITY must be a power of 2. Malloc depends on this being the
  21. same as MALLOC_ALIGNMENT. */
  22. #define HEAP_GRANULARITY_TYPE double
  23. #define HEAP_GRANULARITY (sizeof (HEAP_GRANULARITY_TYPE))
  24. /* A heap is a collection of memory blocks, from which smaller blocks
  25. of memory can be allocated. */
  26. struct heap
  27. {
  28. /* A list of memory in the heap available for allocation. */
  29. struct heap_free_area *free_areas;
  30. #ifdef HEAP_USE_LOCKING
  31. /* A lock that can be used by callers to control access to the heap.
  32. The heap code _does not_ use this lock, it's merely here for the
  33. convenience of users! */
  34. pthread_mutex_t lock;
  35. #endif
  36. };
  37. /* The HEAP_INIT macro can be used as a static initializer for a heap
  38. variable. The HEAP_INIT_WITH_FA variant is used to initialize a heap
  39. with an initial static free-area; its argument FA should be declared
  40. using HEAP_DECLARE_STATIC_FREE_AREA. */
  41. #ifdef HEAP_USE_LOCKING
  42. # define HEAP_INIT { 0, PTHREAD_MUTEX_INITIALIZER }
  43. # define HEAP_INIT_WITH_FA(fa) { &fa._fa, PTHREAD_MUTEX_INITIALIZER }
  44. #else
  45. # define HEAP_INIT { 0 }
  46. # define HEAP_INIT_WITH_FA(fa) { &fa._fa }
  47. #endif
  48. /* A free-list area `header'. These are actually stored at the _ends_ of
  49. free areas (to make allocating from the beginning of the area simpler),
  50. so one might call it a `footer'. */
  51. struct heap_free_area
  52. {
  53. size_t size;
  54. struct heap_free_area *next, *prev;
  55. };
  56. /* Return the address of the end of the frea area FA. */
  57. #define HEAP_FREE_AREA_END(fa) ((void *)(fa + 1))
  58. /* Return the address of the beginning of the frea area FA. FA is
  59. evaulated multiple times. */
  60. #define HEAP_FREE_AREA_START(fa) ((void *)((char *)(fa + 1) - (fa)->size))
  61. /* Return the size of the frea area FA. */
  62. #define HEAP_FREE_AREA_SIZE(fa) ((fa)->size)
  63. /* This rather clumsy macro allows one to declare a static free-area for
  64. passing to HEAP_INIT_WITH_FA initializer macro. This is only use for
  65. which NAME is allowed. */
  66. #define HEAP_DECLARE_STATIC_FREE_AREA(name, size) \
  67. static struct \
  68. { \
  69. HEAP_GRANULARITY_TYPE space[((size) - sizeof (struct heap_free_area)) \
  70. / HEAP_GRANULARITY]; \
  71. struct heap_free_area _fa; \
  72. } name = { { (HEAP_GRANULARITY_TYPE)0 }, { (size), 0, 0 } }
  73. /* Rounds SZ up to be a multiple of HEAP_GRANULARITY. */
  74. #define HEAP_ADJUST_SIZE(sz) \
  75. (((sz) + HEAP_GRANULARITY - 1) & ~(HEAP_GRANULARITY - 1))
  76. /* The minimum allocatable size. */
  77. #define HEAP_MIN_SIZE HEAP_ADJUST_SIZE (sizeof (struct heap_free_area))
  78. /* The minimum size of a free area; if allocating memory from a free-area
  79. would make the free-area smaller than this, the allocation is simply
  80. given the whole free-area instead. It must include at least enough room
  81. to hold a struct heap_free_area, plus some extra to avoid excessive heap
  82. fragmentation (thus increasing speed). This is only a heuristic -- it's
  83. possible for smaller free-areas than this to exist (say, by realloc
  84. returning the tail-end of a previous allocation), but __heap_alloc will
  85. try to get rid of them when possible. */
  86. #define HEAP_MIN_FREE_AREA_SIZE \
  87. HEAP_ADJUST_SIZE (sizeof (struct heap_free_area) + 32)
  88. /* branch-prediction macros; they may already be defined by libc. */
  89. #ifndef likely
  90. #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
  91. #define likely(cond) __builtin_expect(!!(int)(cond), 1)
  92. #define unlikely(cond) __builtin_expect((int)(cond), 0)
  93. #else
  94. #define likely(cond) (cond)
  95. #define unlikely(cond) (cond)
  96. #endif
  97. #endif /* !likely */
  98. /* Define HEAP_DEBUGGING to cause the heap routines to emit debugging info
  99. to stderr when the variable __heap_debug is set to true. */
  100. #ifdef HEAP_DEBUGGING
  101. extern int __heap_debug;
  102. #define HEAP_DEBUG(heap, str) (__heap_debug ? __heap_dump (heap, str) : 0)
  103. #else
  104. #define HEAP_DEBUG(heap, str) (void)0
  105. #endif
  106. /* Output a text representation of HEAP to stderr, labelling it with STR. */
  107. extern void __heap_dump (struct heap *heap, const char *str);
  108. /* Do some consistency checks on HEAP. If they fail, output an error
  109. message to stderr, and exit. STR is printed with the failure message. */
  110. extern void __heap_check (struct heap *heap, const char *str);
  111. #ifdef HEAP_USE_LOCKING
  112. # define __heap_lock(heap) pthread_mutex_lock (&(heap)->lock)
  113. # define __heap_unlock(heap) pthread_mutex_unlock (&(heap)->lock)
  114. #else /* !__UCLIBC_HAS_THREADS__ */
  115. /* Without threads, mutex operations are a nop. */
  116. # define __heap_lock(heap) (void)0
  117. # define __heap_unlock(heap) (void)0
  118. #endif /* HEAP_USE_LOCKING */
  119. /* Delete the free-area FA from HEAP. */
  120. static inline void
  121. __heap_delete (struct heap *heap, struct heap_free_area *fa)
  122. {
  123. if (fa->next)
  124. fa->next->prev = fa->prev;
  125. if (fa->prev)
  126. fa->prev->next = fa->next;
  127. else
  128. heap->free_areas = fa->next;
  129. }
  130. /* Link the free-area FA between the existing free-area's PREV and NEXT in
  131. HEAP. PREV and NEXT may be 0; if PREV is 0, FA is installed as the
  132. first free-area. */
  133. static inline void
  134. __heap_link_free_area (struct heap *heap, struct heap_free_area *fa,
  135. struct heap_free_area *prev,
  136. struct heap_free_area *next)
  137. {
  138. fa->next = next;
  139. fa->prev = prev;
  140. if (prev)
  141. prev->next = fa;
  142. else
  143. heap->free_areas = fa;
  144. if (next)
  145. next->prev = fa;
  146. }
  147. /* Update the mutual links between the free-areas PREV and FA in HEAP.
  148. PREV may be 0, in which case FA is installed as the first free-area (but
  149. FA may not be 0). */
  150. static inline void
  151. __heap_link_free_area_after (struct heap *heap,
  152. struct heap_free_area *fa,
  153. struct heap_free_area *prev)
  154. {
  155. if (prev)
  156. prev->next = fa;
  157. else
  158. heap->free_areas = fa;
  159. fa->prev = prev;
  160. }
  161. /* Add a new free-area MEM, of length SIZE, in between the existing
  162. free-area's PREV and NEXT in HEAP, and return a pointer to its header.
  163. PREV and NEXT may be 0; if PREV is 0, MEM is installed as the first
  164. free-area. */
  165. static inline struct heap_free_area *
  166. __heap_add_free_area (struct heap *heap, void *mem, size_t size,
  167. struct heap_free_area *prev,
  168. struct heap_free_area *next)
  169. {
  170. struct heap_free_area *fa = (struct heap_free_area *)
  171. ((char *)mem + size - sizeof (struct heap_free_area));
  172. fa->size = size;
  173. __heap_link_free_area (heap, fa, prev, next);
  174. return fa;
  175. }
  176. /* Allocate SIZE bytes from the front of the free-area FA in HEAP, and
  177. return the amount actually allocated (which may be more than SIZE). */
  178. static inline size_t
  179. __heap_free_area_alloc (struct heap *heap,
  180. struct heap_free_area *fa, size_t size)
  181. {
  182. size_t fa_size = fa->size;
  183. if (fa_size < size + HEAP_MIN_FREE_AREA_SIZE)
  184. /* There's not enough room left over in FA after allocating the block, so
  185. just use the whole thing, removing it from the list of free areas. */
  186. {
  187. __heap_delete (heap, fa);
  188. /* Remember that we've alloced the whole area. */
  189. size = fa_size;
  190. }
  191. else
  192. /* Reduce size of FA to account for this allocation. */
  193. fa->size = fa_size - size;
  194. return size;
  195. }
  196. /* Allocate and return a block at least *SIZE bytes long from HEAP.
  197. *SIZE is adjusted to reflect the actual amount allocated (which may be
  198. greater than requested). */
  199. extern void *__heap_alloc (struct heap *heap, size_t *size);
  200. /* Allocate SIZE bytes at address MEM in HEAP. Return the actual size
  201. allocated, or 0 if we failed. */
  202. extern size_t __heap_alloc_at (struct heap *heap, void *mem, size_t size);
  203. /* Return the memory area MEM of size SIZE to HEAP.
  204. Returns the heap free area into which the memory was placed. */
  205. extern struct heap_free_area *__heap_free (struct heap *heap,
  206. void *mem, size_t size);
  207. /* Return true if HEAP contains absolutely no memory. */
  208. #define __heap_is_empty(heap) (! (heap)->free_areas)