heap.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #ifdef __UCLIBC_HAS_THREADS__
  15. #include <pthread.h>
  16. typedef pthread_mutex_t heap_mutex_t;
  17. # define HEAP_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
  18. # define __heap_lock(heap) pthread_mutex_lock (&(heap)->lock)
  19. # define __heap_unlock(heap) pthread_mutex_unlock (&(heap)->lock);
  20. #else
  21. /* Without threads, Mutex operations are a nop. */
  22. typedef int heap_mutex_t;
  23. # define HEAP_MUTEX_INIT 0
  24. # define __heap_lock(heap)
  25. # define __heap_unlock(heap)
  26. #endif
  27. /* The heap allocates in multiples of, and aligned to, HEAP_GRANULARITY.
  28. HEAP_GRANULARITY must be a power of 2. Malloc depends on this being the
  29. same as MALLOC_ALIGNMENT. */
  30. #define HEAP_GRANULARITY (sizeof (double))
  31. /* A heap is a collection of memory blocks, from which smaller blocks
  32. of memory can be allocated. */
  33. struct heap
  34. {
  35. /* A list of memory in the heap available for allocation. */
  36. struct heap_free_area *free_areas;
  37. heap_mutex_t lock;
  38. };
  39. #define HEAP_INIT { 0, HEAP_MUTEX_INIT }
  40. /* A free-list area `header'. These are actually stored at the _ends_ of
  41. free areas (to make allocating from the beginning of the area simpler),
  42. so one might call it a `footer'. */
  43. struct heap_free_area
  44. {
  45. size_t size;
  46. struct heap_free_area *next, *prev;
  47. };
  48. /* Return the address of the end of the frea area FA. */
  49. #define HEAP_FREE_AREA_END(fa) ((void *)(fa + 1))
  50. /* Return the address of the beginning of the frea area FA. FA is
  51. evaulated multiple times. */
  52. #define HEAP_FREE_AREA_START(fa) ((void *)((char *)(fa + 1) - (fa)->size))
  53. /* Return the size of the frea area FA. */
  54. #define HEAP_FREE_AREA_SIZE(fa) ((fa)->size)
  55. /* Rounds SZ up to be a multiple of HEAP_GRANULARITY. */
  56. #define HEAP_ADJUST_SIZE(sz) \
  57. (((sz) + HEAP_GRANULARITY - 1) & ~(HEAP_GRANULARITY - 1))
  58. /* The minimum size of a free area. It must include at least enough room
  59. to hold a struct heap_free_area, plus enough extra to be usefully
  60. allocated. */
  61. #define HEAP_MIN_FREE_AREA_SIZE \
  62. (sizeof (struct heap_free_area) + HEAP_ADJUST_SIZE (1))
  63. /* Change this to `#if 1' to cause the heap routines to emit debugging info
  64. to stderr. */
  65. #if 0
  66. #include <stdio.h>
  67. static void HEAP_DEBUG (struct heap *heap, const char *str)
  68. {
  69. static int recursed = 0;
  70. if (! recursed)
  71. {
  72. struct heap_free_area *fa, *prev;
  73. recursed = 1;
  74. fprintf (stderr, " %s: heap @0x%lx:\n", str, (long)heap);
  75. for (prev = 0, fa = heap->free_areas; fa; prev = fa, fa = fa->next)
  76. {
  77. fprintf (stderr,
  78. " 0x%lx: 0x%lx - 0x%lx (%d)\tP=0x%lx, N=0x%lx\n",
  79. (long)fa,
  80. (long)HEAP_FREE_AREA_START (fa),
  81. (long)HEAP_FREE_AREA_END (fa),
  82. fa->size,
  83. (long)fa->prev,
  84. (long)fa->next);
  85. if (fa->prev != prev)
  86. fprintf (stderr,
  87. " PREV POINTER CORRUPTED!!!! P=0x%lx should be 0x%lx\n",
  88. (long)fa->prev, (long)prev);
  89. }
  90. recursed = 0;
  91. }
  92. }
  93. #else
  94. #define HEAP_DEBUG(heap, str) (void)0
  95. #endif
  96. /* Remove the free-area FA from HEAP. */
  97. extern inline void
  98. __heap_unlink_free_area (struct heap *heap, struct heap_free_area *fa)
  99. {
  100. if (fa->next)
  101. fa->next->prev = fa->prev;
  102. if (fa->prev)
  103. fa->prev->next = fa->next;
  104. else
  105. heap->free_areas = fa->next;
  106. }
  107. /* Link the free-area FA between the existing free-area's PREV and NEXT in
  108. HEAP. PREV and NEXT may be 0; if PREV is 0, FA is installed as the
  109. first free-area. */
  110. extern inline void
  111. __heap_link_free_area (struct heap *heap, struct heap_free_area *fa,
  112. struct heap_free_area *prev,
  113. struct heap_free_area *next)
  114. {
  115. fa->next = next;
  116. fa->prev = prev;
  117. if (prev)
  118. prev->next = fa;
  119. else
  120. heap->free_areas = fa;
  121. if (next)
  122. next->prev = fa;
  123. }
  124. /* Update the mutual links between the free-areas PREV and FA in HEAP.
  125. PREV may be 0, in which case FA is installed as the first free-area (but
  126. FA may not be 0). */
  127. extern inline void
  128. __heap_link_free_area_after (struct heap *heap,
  129. struct heap_free_area *fa,
  130. struct heap_free_area *prev)
  131. {
  132. if (prev)
  133. prev->next = fa;
  134. else
  135. heap->free_areas = fa;
  136. fa->prev = prev;
  137. }
  138. /* Add a new free-area MEM, of length SIZE, in between the existing
  139. free-area's PREV and NEXT in HEAP, and return a pointer to its header.
  140. PREV and NEXT may be 0; if PREV is 0, MEM is installed as the first
  141. free-area. */
  142. extern inline struct heap_free_area *
  143. __heap_add_free_area (struct heap *heap, void *mem, size_t size,
  144. struct heap_free_area *prev,
  145. struct heap_free_area *next)
  146. {
  147. struct heap_free_area *fa = (struct heap_free_area *)
  148. ((char *)mem + size - sizeof (struct heap_free_area));
  149. fa->size = size;
  150. __heap_link_free_area (heap, fa, prev, next);
  151. return fa;
  152. }
  153. /* Allocate SIZE bytes from the front of the free-area FA in HEAP, and
  154. return the amount actually allocated (which may be more than SIZE). */
  155. extern inline size_t
  156. __heap_free_area_alloc (struct heap *heap,
  157. struct heap_free_area *fa, size_t size)
  158. {
  159. size_t fa_size = fa->size;
  160. if (fa_size < size + HEAP_MIN_FREE_AREA_SIZE)
  161. /* There's not enough room left over in FA after allocating the block, so
  162. just use the whole thing, removing it from the list of free areas. */
  163. {
  164. __heap_unlink_free_area (heap, fa);
  165. /* Remember that we've alloced the whole area. */
  166. size = fa_size;
  167. }
  168. else
  169. /* Reduce size of FA to account for this allocation. */
  170. fa->size = fa_size - size;
  171. return size;
  172. }
  173. /* Allocate and return a block at least *SIZE bytes long from HEAP.
  174. *SIZE is adjusted to reflect the actual amount allocated (which may be
  175. greater than requested). */
  176. extern void *__heap_alloc (struct heap *heap, size_t *size);
  177. /* Allocate SIZE bytes at address MEM in HEAP. Return the actual size
  178. allocated, or 0 if we failed. */
  179. extern size_t __heap_alloc_at (struct heap *heap, void *mem, size_t size);
  180. /* Return the memory area MEM of size SIZE to HEAP.
  181. Returns the heap free area into which the memory was placed. */
  182. extern struct heap_free_area *__heap_free (struct heap *heap,
  183. void *mem, size_t size);