malloc.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * libc/stdlib/malloc/malloc.h -- small malloc implementation
  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. /* The alignment we guarantee for malloc return values. We prefer this
  14. to be at least sizeof (size_t) bytes because (a) we have to allocate
  15. that many bytes for the header anyway and (b) guaranteeing word
  16. alignment can be a significant win on targets like m68k and Coldfire,
  17. where __alignof__(double) == 2. */
  18. #define MALLOC_ALIGNMENT \
  19. (__alignof__ (double) > sizeof (size_t) ? __alignof__ (double) : sizeof (size_t))
  20. /* The system pagesize... */
  21. #define MALLOC_PAGE_SIZE sysconf(_SC_PAGESIZE)
  22. /* The minimum size of block we request from the the system to extend the
  23. heap for small allocations (we may request a bigger block if necessary to
  24. satisfy a particularly big request). */
  25. #define MALLOC_HEAP_EXTEND_SIZE MALLOC_PAGE_SIZE
  26. /* When a heap free-area grows above this size, try to unmap it, releasing
  27. the memory back to the system. */
  28. #define MALLOC_UNMAP_THRESHOLD (8*MALLOC_PAGE_SIZE)
  29. /* When unmapping a free-area, retain this many bytes if it's the only one,
  30. to avoid completely emptying the heap. This is only a heuristic -- the
  31. existance of another free area, even if it's smaller than
  32. MALLOC_MIN_SIZE, will cause us not to reserve anything. */
  33. #define MALLOC_MIN_SIZE (2*MALLOC_PAGE_SIZE)
  34. /* When realloc shrinks an allocation, it only does so if more than this
  35. many bytes will be freed; it must at at least HEAP_MIN_SIZE. Larger
  36. values increase speed (by reducing heap fragmentation) at the expense of
  37. space. */
  38. #define MALLOC_REALLOC_MIN_FREE_SIZE (HEAP_MIN_SIZE + 16)
  39. /* For systems with an MMU, use sbrk to map/unmap memory for the malloc
  40. heap, instead of mmap/munmap. This is a tradeoff -- sbrk is faster than
  41. mmap/munmap, and guarantees contiguous allocation, but is also less
  42. flexible, and causes the heap to only be shrinkable from the end. */
  43. #ifdef __ARCH_USE_MMU__
  44. # define MALLOC_USE_SBRK
  45. #endif
  46. /* The current implementation of munmap in uClinux doesn't work correctly:
  47. it requires that ever call to munmap exactly match a corresponding call
  48. to mmap (that is, it doesn't allow you to unmap only part of a
  49. previously allocated block, or to unmap two contiguous blocks with a
  50. single call to munmap). This behavior is broken, and uClinux should be
  51. fixed; however, until it is, we add code to work around the problem in
  52. malloc. */
  53. #ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
  54. /* A structure recording a block of memory mmapped by malloc. */
  55. struct malloc_mmb
  56. {
  57. void *mem; /* the mmapped block */
  58. size_t size; /* its size */
  59. struct malloc_mmb *next;
  60. };
  61. /* A list of all malloc_mmb structures describing blocks that malloc has
  62. mmapped, ordered by the block address. */
  63. extern struct malloc_mmb *__malloc_mmapped_blocks;
  64. /* A heap used for allocating malloc_mmb structures. We could allocate
  65. them from the main heap, but that tends to cause heap fragmentation in
  66. annoying ways. */
  67. extern struct heap_free_area *__malloc_mmb_heap;
  68. /* Define MALLOC_MMB_DEBUGGING to cause malloc to emit debugging info about
  69. about mmap block allocation/freeing by the `uclinux broken munmap' code
  70. to stderr, when the variable __malloc_mmb_debug is set to true. */
  71. #ifdef MALLOC_MMB_DEBUGGING
  72. # include <stdio.h>
  73. extern int __malloc_mmb_debug;
  74. # define MALLOC_MMB_DEBUG(indent, fmt, args...) \
  75. (__malloc_mmb_debug ? __malloc_debug_printf (indent, fmt , ##args) : 0)
  76. # define MALLOC_MMB_DEBUG_INDENT(indent) \
  77. (__malloc_mmb_debug ? __malloc_debug_indent (indent) : 0)
  78. # ifndef MALLOC_DEBUGGING
  79. # define MALLOC_DEBUGGING
  80. # endif
  81. #else /* !MALLOC_MMB_DEBUGGING */
  82. # define MALLOC_MMB_DEBUG(fmt, args...) (void)0
  83. # define MALLOC_MMB_DEBUG_INDENT(indent) (void)0
  84. #endif /* MALLOC_MMB_DEBUGGING */
  85. #endif /* __UCLIBC_UCLINUX_BROKEN_MUNMAP__ */
  86. /* The size of a malloc allocation is stored in a size_t word
  87. MALLOC_HEADER_SIZE bytes prior to the start address of the allocation:
  88. +--------+---------+-------------------+
  89. | SIZE |(unused) | allocation ... |
  90. +--------+---------+-------------------+
  91. ^ BASE ^ ADDR
  92. ^ ADDR - MALLOC_HEADER_SIZE
  93. */
  94. /* The amount of extra space used by the malloc header. */
  95. #define MALLOC_HEADER_SIZE \
  96. (MALLOC_ALIGNMENT < sizeof (size_t) \
  97. ? sizeof (size_t) \
  98. : MALLOC_ALIGNMENT)
  99. /* Set up the malloc header, and return the user address of a malloc block. */
  100. #define MALLOC_SETUP(base, size) \
  101. (MALLOC_SET_SIZE (base, size), (void *)((char *)base + MALLOC_HEADER_SIZE))
  102. /* Set the size of a malloc allocation, given the base address. */
  103. #define MALLOC_SET_SIZE(base, size) (*(size_t *)(base) = (size))
  104. /* Return base-address of a malloc allocation, given the user address. */
  105. #define MALLOC_BASE(addr) ((void *)((char *)addr - MALLOC_HEADER_SIZE))
  106. /* Return the size of a malloc allocation, given the user address. */
  107. #define MALLOC_SIZE(addr) (*(size_t *)MALLOC_BASE(addr))
  108. #include <bits/uClibc_mutex.h>
  109. #ifdef __UCLIBC_HAS_THREADS__
  110. # define MALLOC_USE_LOCKING
  111. #endif
  112. #ifdef MALLOC_USE_SBRK
  113. /* This lock is used to serialize uses of the `sbrk' function (in both
  114. malloc and free, sbrk may be used several times in succession, and
  115. things will break if these multiple calls are interleaved with another
  116. thread's use of sbrk!). */
  117. __UCLIBC_MUTEX_EXTERN(__malloc_sbrk_lock) attribute_hidden;
  118. # define __malloc_lock_sbrk() __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE (__malloc_sbrk_lock)
  119. # define __malloc_unlock_sbrk() __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE (__malloc_sbrk_lock)
  120. #else
  121. # define __malloc_lock_sbrk() (void)0
  122. # define __malloc_unlock_sbrk() (void)0
  123. #endif /* MALLOC_USE_SBRK */
  124. /* Define MALLOC_DEBUGGING to cause malloc to emit debugging info to stderr
  125. when the variable __malloc_debug is set to true. */
  126. #ifdef MALLOC_DEBUGGING
  127. extern void __malloc_debug_init (void) attribute_hidden;
  128. /* The number of spaces in a malloc debug indent level. */
  129. #define MALLOC_DEBUG_INDENT_SIZE 3
  130. extern int __malloc_debug attribute_hidden, __malloc_check attribute_hidden;
  131. # define MALLOC_DEBUG(indent, fmt, args...) \
  132. (__malloc_debug ? __malloc_debug_printf (indent, fmt , ##args) : 0)
  133. # define MALLOC_DEBUG_INDENT(indent) \
  134. (__malloc_debug ? __malloc_debug_indent (indent) : 0)
  135. extern int __malloc_debug_cur_indent attribute_hidden;
  136. /* Print FMT and args indented at the current debug print level, followed
  137. by a newline, and change the level by INDENT. */
  138. extern void __malloc_debug_printf (int indent, const char *fmt, ...) attribute_hidden;
  139. /* Change the current debug print level by INDENT, and return the value. */
  140. #define __malloc_debug_indent(indent) (__malloc_debug_cur_indent += indent)
  141. /* Set the current debug print level to LEVEL. */
  142. #define __malloc_debug_set_indent(level) (__malloc_debug_cur_indent = level)
  143. #else /* !MALLOC_DEBUGGING */
  144. # define MALLOC_DEBUG(fmt, args...) (void)0
  145. # define MALLOC_DEBUG_INDENT(indent) (void)0
  146. #endif /* MALLOC_DEBUGGING */
  147. /* Return SZ rounded down to POWER_OF_2_SIZE (which must be power of 2). */
  148. #define MALLOC_ROUND_DOWN(sz, power_of_2_size) \
  149. ((sz) & ~(power_of_2_size - 1))
  150. /* Return SZ rounded to POWER_OF_2_SIZE (which must be power of 2). */
  151. #define MALLOC_ROUND_UP(sz, power_of_2_size) \
  152. MALLOC_ROUND_DOWN ((sz) + (power_of_2_size - 1), (power_of_2_size))
  153. /* Return SZ rounded down to a multiple MALLOC_PAGE_SIZE. */
  154. #define MALLOC_ROUND_DOWN_TO_PAGE_SIZE(sz) \
  155. MALLOC_ROUND_DOWN (sz, MALLOC_PAGE_SIZE)
  156. /* Return SZ rounded up to a multiple MALLOC_PAGE_SIZE. */
  157. #define MALLOC_ROUND_UP_TO_PAGE_SIZE(sz) \
  158. MALLOC_ROUND_UP (sz, MALLOC_PAGE_SIZE)
  159. /* The malloc heap. */
  160. extern struct heap_free_area *__malloc_heap attribute_hidden;
  161. #ifdef __UCLIBC_HAS_THREADS__
  162. __UCLIBC_MUTEX_EXTERN(__malloc_heap_lock)
  163. # ifndef __LINUXTHREADS_OLD__
  164. attribute_hidden
  165. # endif
  166. ;
  167. # ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
  168. __UCLIBC_MUTEX_EXTERN(__malloc_mmb_heap_lock)
  169. # ifndef __LINUXTHREADS_OLD__
  170. attribute_hidden
  171. # endif
  172. ;
  173. # endif
  174. #endif