heap.h 8.1 KB

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