malloc.h 8.0 KB

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