heap.h 7.8 KB

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