heap.h 7.4 KB

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