heap.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. /* Define HEAP_DEBUGGING to cause the heap routines to emit debugging info
  57. to stderr. */
  58. #ifdef HEAP_DEBUGGING
  59. #include <stdio.h>
  60. static void HEAP_DEBUG (struct heap *heap, const char *str)
  61. {
  62. static int recursed = 0;
  63. if (! recursed)
  64. {
  65. struct heap_free_area *fa, *prev;
  66. recursed = 1;
  67. fprintf (stderr, " %s: heap @0x%lx:\n", str, (long)heap);
  68. for (prev = 0, fa = heap->free_areas; fa; prev = fa, fa = fa->next)
  69. {
  70. fprintf (stderr,
  71. " 0x%lx: 0x%lx - 0x%lx (%d)\tP=0x%lx, N=0x%lx\n",
  72. (long)fa,
  73. (long)HEAP_FREE_AREA_START (fa),
  74. (long)HEAP_FREE_AREA_END (fa),
  75. fa->size,
  76. (long)fa->prev,
  77. (long)fa->next);
  78. if (fa->prev != prev)
  79. fprintf (stderr,
  80. " PREV POINTER CORRUPTED!!!! P=0x%lx should be 0x%lx\n",
  81. (long)fa->prev, (long)prev);
  82. }
  83. recursed = 0;
  84. }
  85. }
  86. #else
  87. #define HEAP_DEBUG(heap, str) (void)0
  88. #endif
  89. /* Remove the free-area FA from HEAP. */
  90. extern inline void
  91. __heap_unlink_free_area (struct heap *heap, struct heap_free_area *fa)
  92. {
  93. if (fa->next)
  94. fa->next->prev = fa->prev;
  95. if (fa->prev)
  96. fa->prev->next = fa->next;
  97. else
  98. heap->free_areas = fa->next;
  99. }
  100. /* Link the free-area FA between the existing free-area's PREV and NEXT in
  101. HEAP. PREV and NEXT may be 0; if PREV is 0, FA is installed as the
  102. first free-area. */
  103. extern inline void
  104. __heap_link_free_area (struct heap *heap, struct heap_free_area *fa,
  105. struct heap_free_area *prev,
  106. struct heap_free_area *next)
  107. {
  108. fa->next = next;
  109. fa->prev = prev;
  110. if (prev)
  111. prev->next = fa;
  112. else
  113. heap->free_areas = fa;
  114. if (next)
  115. next->prev = fa;
  116. }
  117. /* Update the mutual links between the free-areas PREV and FA in HEAP.
  118. PREV may be 0, in which case FA is installed as the first free-area (but
  119. FA may not be 0). */
  120. extern inline void
  121. __heap_link_free_area_after (struct heap *heap,
  122. struct heap_free_area *fa,
  123. struct heap_free_area *prev)
  124. {
  125. if (prev)
  126. prev->next = fa;
  127. else
  128. heap->free_areas = fa;
  129. fa->prev = prev;
  130. }
  131. /* Add a new free-area MEM, of length SIZE, in between the existing
  132. free-area's PREV and NEXT in HEAP, and return a pointer to its header.
  133. PREV and NEXT may be 0; if PREV is 0, MEM is installed as the first
  134. free-area. */
  135. extern inline struct heap_free_area *
  136. __heap_add_free_area (struct heap *heap, void *mem, size_t size,
  137. struct heap_free_area *prev,
  138. struct heap_free_area *next)
  139. {
  140. struct heap_free_area *fa = (struct heap_free_area *)
  141. ((char *)mem + size - sizeof (struct heap_free_area));
  142. fa->size = size;
  143. __heap_link_free_area (heap, fa, prev, next);
  144. return fa;
  145. }
  146. /* Allocate SIZE bytes from the front of the free-area FA in HEAP, and
  147. return the amount actually allocated (which may be more than SIZE). */
  148. extern inline size_t
  149. __heap_free_area_alloc (struct heap *heap,
  150. struct heap_free_area *fa, size_t size)
  151. {
  152. size_t fa_size = fa->size;
  153. if (fa_size < size + HEAP_MIN_FREE_AREA_SIZE)
  154. /* There's not enough room left over in FA after allocating the block, so
  155. just use the whole thing, removing it from the list of free areas. */
  156. {
  157. __heap_unlink_free_area (heap, fa);
  158. /* Remember that we've alloced the whole area. */
  159. size = fa_size;
  160. }
  161. else
  162. /* Reduce size of FA to account for this allocation. */
  163. fa->size = fa_size - size;
  164. return size;
  165. }
  166. /* Allocate and return a block at least *SIZE bytes long from HEAP.
  167. *SIZE is adjusted to reflect the actual amount allocated (which may be
  168. greater than requested). */
  169. extern void *__heap_alloc (struct heap *heap, size_t *size);
  170. /* Allocate SIZE bytes at address MEM in HEAP. Return the actual size
  171. allocated, or 0 if we failed. */
  172. extern size_t __heap_alloc_at (struct heap *heap, void *mem, size_t size);
  173. /* Return the memory area MEM of size SIZE to HEAP.
  174. Returns the heap free area into which the memory was placed. */
  175. extern struct heap_free_area *__heap_free (struct heap *heap,
  176. void *mem, size_t size);