heap.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * libc/stdlib/malloc-zarg/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 mutex_t;
  17. # define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
  18. # define mutex_lock(x) pthread_mutex_lock(&(x))
  19. # define mutex_unlock(x) pthread_mutex_unlock(&(x));
  20. #else
  21. /* Mutex operations are currently a nop. */
  22. typedef int mutex_t;
  23. # define MUTEX_INITIALIZER 0
  24. # define mutex_lock(x)
  25. # define mutex_unlock(x)
  26. #endif
  27. /* The unit in which allocation is done, due to alignment constraints, etc.
  28. All allocation requests are rounded up to a multiple of this size.
  29. Must be a power of 2. */
  30. #define HEAP_GRANULARITY (sizeof (double))
  31. struct heap
  32. {
  33. struct heap_free_area *free_areas;
  34. mutex_t lock;
  35. };
  36. #define HEAP_INIT { 0, MUTEX_INITIALIZER }
  37. /* A free-list area `header'. These are actually stored at the _ends_ of
  38. free areas (to make allocating from the beginning of the area simpler),
  39. so one might call it a `footer'. */
  40. struct heap_free_area
  41. {
  42. size_t size;
  43. struct heap_free_area *next, *prev;
  44. };
  45. /* Return the address of the end of the frea area FA. */
  46. #define HEAP_FREE_AREA_END(fa) ((void *)(fa + 1))
  47. /* Return the address of the beginning of the frea area FA. FA is
  48. evaulated multiple times. */
  49. #define HEAP_FREE_AREA_START(fa) ((void *)((char *)(fa + 1) - (fa)->size))
  50. /* Rounds SZ up to be a multiple of HEAP_GRANULARITY. */
  51. #define HEAP_ADJUST_SIZE(sz) \
  52. (((sz) + HEAP_GRANULARITY - 1) & ~(HEAP_GRANULARITY - 1))
  53. /* The minimum size of a free area. It must include at least enough room
  54. to hold a struct heap_free_area, plus enough extra to be usefully
  55. allocated. */
  56. #define HEAP_MIN_FREE_AREA_SIZE \
  57. (sizeof (struct heap_free_area) + HEAP_ADJUST_SIZE (1))
  58. #if 0
  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;
  66. recursed = 1;
  67. fprintf (stderr, " %s: heap @0x%lx:\n", str, (long)heap);
  68. for (fa = heap->free_areas; fa; fa = fa->next)
  69. fprintf (stderr,
  70. " 0x%lx: 0x%lx - 0x%lx (%d)\tN=0x%lx, P=0x%lx\n",
  71. (long)fa,
  72. (long)HEAP_FREE_AREA_START (fa),
  73. (long)HEAP_FREE_AREA_END (fa),
  74. fa->size,
  75. (long)fa->prev,
  76. (long)fa->next);
  77. recursed = 0;
  78. }
  79. }
  80. #else
  81. #define HEAP_DEBUG(heap, str) (void)0
  82. #endif
  83. /* Allocate SIZE bytes from the front of the free-area FA in HEAP, and
  84. return the amount actually allocated (which may be more than SIZE). */
  85. extern inline size_t
  86. __heap_free_area_alloc (struct heap *heap,
  87. struct heap_free_area *fa, size_t size)
  88. {
  89. size_t fa_size = fa->size;
  90. if (fa_size < size + HEAP_MIN_FREE_AREA_SIZE)
  91. /* There's not enough room left over in FA after allocating the block, so
  92. just use the whole thing, removing it from the list of free areas. */
  93. {
  94. if (fa->next)
  95. fa->next->prev = fa->prev;
  96. if (fa->prev)
  97. fa->prev->next = fa->next;
  98. else
  99. heap->free_areas = fa->next;
  100. /* Remember that we've alloced the whole area. */
  101. size = fa_size;
  102. }
  103. else
  104. /* Reduce size of FA to account for this allocation. */
  105. fa->size = fa_size - size;
  106. return size;
  107. }
  108. /* Allocate and return a block at least *SIZE bytes long from HEAP.
  109. *SIZE is adjusted to reflect the actual amount allocated (which may be
  110. greater than requested). */
  111. extern void *__heap_alloc (struct heap *heap, size_t *size);
  112. /* Allocate SIZE bytes at address MEM in HEAP. Return the actual size
  113. allocated, or 0 if we failed. */
  114. extern size_t __heap_alloc_at (struct heap *heap, void *mem, size_t size);
  115. /* Return the memory area MEM of size SIZE to HEAP. */
  116. extern void __heap_free (struct heap *heap, void *mem, size_t size);
  117. /* If the memory area MEM, of size SIZE, immediately follows an existing
  118. free-area in HEAP, use it to extend that free-area, and return true;
  119. otherwise return false. */
  120. extern int __heap_append_free (struct heap *heap, void *mem, size_t size);