malloc.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 we assume; we really ought to get it with
  16. getpagesize, but gee, how annoying. */
  17. #define MALLOC_PAGE_SIZE 4096
  18. /* The minimum size of block we request from the the system to extend the
  19. heap for small allocations (we may request a bigger block if necessary to
  20. satisfy a particularly big request). */
  21. #define MALLOC_HEAP_EXTEND_SIZE MALLOC_PAGE_SIZE
  22. /* When a heap free-area grows above this size, try to unmap it, releasing
  23. the memory back to the system. */
  24. #define MALLOC_UNMAP_THRESHOLD (8*MALLOC_PAGE_SIZE)
  25. /* When unmapping a free-area, retain this many bytes if it's the only one,
  26. to avoid completely emptying the heap. This is only a heuristic -- the
  27. existance of another free area, even if it's smaller than
  28. MALLOC_MIN_SIZE, will cause us not to reserve anything. */
  29. #define MALLOC_MIN_SIZE (2*MALLOC_PAGE_SIZE)
  30. /* When realloc shrinks an allocation, it only does so if more than this
  31. many bytes will be freed; it must at at least HEAP_MIN_SIZE. Larger
  32. values increase speed (by reducing heap fragmentation) at the expense of
  33. space. */
  34. #define MALLOC_REALLOC_MIN_FREE_SIZE (HEAP_MIN_SIZE + 16)
  35. /* For systems with an MMU, use sbrk to map/unmap memory for the malloc
  36. heap, instead of mmap/munmap. This is a tradeoff -- sbrk is faster than
  37. mmap/munmap, and guarantees contiguous allocation, but is also less
  38. flexible, and causes the heap to only be shrinkable from the end. */
  39. #ifdef __UCLIBC_HAS_MMU__
  40. # define MALLOC_USE_SBRK
  41. #endif
  42. /* The size of a malloc allocation is stored in a size_t word
  43. MALLOC_ALIGNMENT bytes prior to the start address of the allocation:
  44. +--------+---------+-------------------+
  45. | SIZE |(unused) | allocation ... |
  46. +--------+---------+-------------------+
  47. ^ BASE ^ ADDR
  48. ^ ADDR - MALLOC_ALIGN
  49. */
  50. /* The amount of extra space used by the malloc header. */
  51. #define MALLOC_HEADER_SIZE MALLOC_ALIGNMENT
  52. /* Set up the malloc header, and return the user address of a malloc block. */
  53. #define MALLOC_SETUP(base, size) \
  54. (MALLOC_SET_SIZE (base, size), (void *)((char *)base + MALLOC_HEADER_SIZE))
  55. /* Set the size of a malloc allocation, given the base address. */
  56. #define MALLOC_SET_SIZE(base, size) (*(size_t *)(base) = (size))
  57. /* Return base-address of a malloc allocation, given the user address. */
  58. #define MALLOC_BASE(addr) ((void *)((char *)addr - MALLOC_HEADER_SIZE))
  59. /* Return the size of a malloc allocation, given the user address. */
  60. #define MALLOC_SIZE(addr) (*(size_t *)MALLOC_BASE(addr))
  61. /* Locking for multithreaded apps. */
  62. #ifdef __UCLIBC_HAS_THREADS__
  63. # include <pthread.h>
  64. # define MALLOC_USE_LOCKING
  65. typedef pthread_mutex_t malloc_mutex_t;
  66. # define MALLOC_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
  67. /* The main malloc lock. This must be hold while accessing __malloc_heap,
  68. and in order to gain __malloc_sbrk_lock. */
  69. extern malloc_mutex_t __malloc_lock;
  70. # define __malloc_lock() pthread_mutex_lock (&__malloc_lock)
  71. # define __malloc_unlock() pthread_mutex_unlock (&__malloc_lock)
  72. # ifdef MALLOC_USE_SBRK
  73. /* This lock is used to serialize uses of the `sbrk' function (in both
  74. malloc and free, sbrk may be used several times in succession, and
  75. things will break if these multiple calls are interleaved with another
  76. thread's use of sbrk!). */
  77. extern malloc_mutex_t __malloc_sbrk_lock;
  78. # define __malloc_lock_sbrk() pthread_mutex_lock (&__malloc_sbrk_lock)
  79. # define __malloc_unlock_sbrk() pthread_mutex_unlock (&__malloc_sbrk_lock)
  80. # endif /* MALLOC_USE_SBRK */
  81. #else /* !__UCLIBC_HAS_THREADS__ */
  82. /* Without threads, mutex operations are a nop. */
  83. # define __malloc_lock() (void)0
  84. # define __malloc_unlock() (void)0
  85. # define __malloc_lock_sbrk() (void)0
  86. # define __malloc_unlock_sbrk() (void)0
  87. #endif /* __UCLIBC_HAS_THREADS__ */
  88. /* branch-prediction macros; they may already be defined by libc. */
  89. #ifndef likely
  90. #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
  91. #define likely(cond) __builtin_expect(!!(int)(cond), 1)
  92. #define unlikely(cond) __builtin_expect((int)(cond), 0)
  93. #else
  94. #define likely(cond) (cond)
  95. #define unlikely(cond) (cond)
  96. #endif
  97. #endif /* !likely */
  98. /* Define MALLOC_DEBUGGING to cause malloc to emit debugging info to stderr
  99. when the variable __malloc_debug is set to true. */
  100. #ifdef MALLOC_DEBUGGING
  101. #include <stdio.h>
  102. extern int __malloc_debug;
  103. #define MALLOC_DEBUG(fmt, args...) \
  104. (__malloc_debug ? fprintf (stderr, fmt , ##args) : 0)
  105. #else
  106. #define MALLOC_DEBUG(fmt, args...) (void)0
  107. #endif
  108. /* Return SZ rounded down to POWER_OF_2_SIZE (which must be power of 2). */
  109. #define MALLOC_ROUND_DOWN(sz, power_of_2_size) \
  110. ((sz) & ~(power_of_2_size - 1))
  111. /* Return SZ rounded to POWER_OF_2_SIZE (which must be power of 2). */
  112. #define MALLOC_ROUND_UP(sz, power_of_2_size) \
  113. MALLOC_ROUND_DOWN ((sz) + (power_of_2_size - 1), (power_of_2_size))
  114. /* Return SZ rounded down to a multiple MALLOC_PAGE_SIZE. */
  115. #define MALLOC_ROUND_DOWN_TO_PAGE_SIZE(sz) \
  116. MALLOC_ROUND_DOWN (sz, MALLOC_PAGE_SIZE)
  117. /* Return SZ rounded up to a multiple MALLOC_PAGE_SIZE. */
  118. #define MALLOC_ROUND_UP_TO_PAGE_SIZE(sz) \
  119. MALLOC_ROUND_UP (sz, MALLOC_PAGE_SIZE)
  120. /* The malloc heap. */
  121. extern struct heap __malloc_heap;