heap.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 when the variable __heap_debug is set to true. */
  68. #ifdef HEAP_DEBUGGING
  69. extern int __heap_debug;
  70. #define HEAP_DEBUG(heap, str) (__heap_debug ? __heap_dump (heap, str) : 0)
  71. #else
  72. #define HEAP_DEBUG(heap, str) (void)0
  73. #endif
  74. /* Output a text representation of HEAP to stderr, labelling it with STR. */
  75. extern void __heap_dump (struct heap *heap, const char *str);
  76. /* Do some consistency checks on HEAP. If they fail, output an error
  77. message to stderr, and exit. STR is printed with the failure message. */
  78. extern void __heap_check (struct heap *heap, const char *str);
  79. /* Delete the free-area FA from HEAP. */
  80. extern inline void
  81. __heap_delete (struct heap *heap, struct heap_free_area *fa)
  82. {
  83. if (fa->next)
  84. fa->next->prev = fa->prev;
  85. if (fa->prev)
  86. fa->prev->next = fa->next;
  87. else
  88. heap->free_areas = fa->next;
  89. }
  90. /* Link the free-area FA between the existing free-area's PREV and NEXT in
  91. HEAP. PREV and NEXT may be 0; if PREV is 0, FA is installed as the
  92. first free-area. */
  93. extern inline void
  94. __heap_link_free_area (struct heap *heap, struct heap_free_area *fa,
  95. struct heap_free_area *prev,
  96. struct heap_free_area *next)
  97. {
  98. fa->next = next;
  99. fa->prev = prev;
  100. if (prev)
  101. prev->next = fa;
  102. else
  103. heap->free_areas = fa;
  104. if (next)
  105. next->prev = fa;
  106. }
  107. /* Update the mutual links between the free-areas PREV and FA in HEAP.
  108. PREV may be 0, in which case FA is installed as the first free-area (but
  109. FA may not be 0). */
  110. extern inline void
  111. __heap_link_free_area_after (struct heap *heap,
  112. struct heap_free_area *fa,
  113. struct heap_free_area *prev)
  114. {
  115. if (prev)
  116. prev->next = fa;
  117. else
  118. heap->free_areas = fa;
  119. fa->prev = prev;
  120. }
  121. /* Add a new free-area MEM, of length SIZE, in between the existing
  122. free-area's PREV and NEXT in HEAP, and return a pointer to its header.
  123. PREV and NEXT may be 0; if PREV is 0, MEM is installed as the first
  124. free-area. */
  125. extern inline struct heap_free_area *
  126. __heap_add_free_area (struct heap *heap, void *mem, size_t size,
  127. struct heap_free_area *prev,
  128. struct heap_free_area *next)
  129. {
  130. struct heap_free_area *fa = (struct heap_free_area *)
  131. ((char *)mem + size - sizeof (struct heap_free_area));
  132. fa->size = size;
  133. __heap_link_free_area (heap, fa, prev, next);
  134. return fa;
  135. }
  136. /* Allocate SIZE bytes from the front of the free-area FA in HEAP, and
  137. return the amount actually allocated (which may be more than SIZE). */
  138. extern inline size_t
  139. __heap_free_area_alloc (struct heap *heap,
  140. struct heap_free_area *fa, size_t size)
  141. {
  142. size_t fa_size = fa->size;
  143. if (fa_size < size + HEAP_MIN_FREE_AREA_SIZE)
  144. /* There's not enough room left over in FA after allocating the block, so
  145. just use the whole thing, removing it from the list of free areas. */
  146. {
  147. __heap_delete (heap, fa);
  148. /* Remember that we've alloced the whole area. */
  149. size = fa_size;
  150. }
  151. else
  152. /* Reduce size of FA to account for this allocation. */
  153. fa->size = fa_size - size;
  154. return size;
  155. }
  156. /* Allocate and return a block at least *SIZE bytes long from HEAP.
  157. *SIZE is adjusted to reflect the actual amount allocated (which may be
  158. greater than requested). */
  159. extern void *__heap_alloc (struct heap *heap, size_t *size);
  160. /* Allocate SIZE bytes at address MEM in HEAP. Return the actual size
  161. allocated, or 0 if we failed. */
  162. extern size_t __heap_alloc_at (struct heap *heap, void *mem, size_t size);
  163. /* Return the memory area MEM of size SIZE to HEAP.
  164. Returns the heap free area into which the memory was placed. */
  165. extern struct heap_free_area *__heap_free (struct heap *heap,
  166. void *mem, size_t size);
  167. /* Return true if HEAP contains absolutely no memory. */
  168. #define __heap_is_empty(heap) (! (heap)->free_areas)