zend_alloc.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. | Dmitry Stogov <dmitry@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #ifndef ZEND_ALLOC_H
  21. #define ZEND_ALLOC_H
  22. #include <stdio.h>
  23. #include "../TSRM/TSRM.h"
  24. #include "zend.h"
  25. #ifndef ZEND_MM_ALIGNMENT
  26. # define ZEND_MM_ALIGNMENT Z_UL(8)
  27. # define ZEND_MM_ALIGNMENT_LOG2 Z_L(3)
  28. #elif ZEND_MM_ALIGNMENT < 4
  29. # undef ZEND_MM_ALIGNMENT
  30. # undef ZEND_MM_ALIGNMENT_LOG2
  31. # define ZEND_MM_ALIGNMENT Z_UL(4)
  32. # define ZEND_MM_ALIGNMENT_LOG2 Z_L(2)
  33. #endif
  34. #define ZEND_MM_ALIGNMENT_MASK ~(ZEND_MM_ALIGNMENT - 1)
  35. #define ZEND_MM_ALIGNED_SIZE(size) (((size) + ZEND_MM_ALIGNMENT - 1) & ZEND_MM_ALIGNMENT_MASK)
  36. #define ZEND_MM_ALIGNED_SIZE_EX(size, alignment) \
  37. (((size) + ((alignment) - 1)) & ~((alignment) - 1))
  38. typedef struct _zend_leak_info {
  39. void *addr;
  40. size_t size;
  41. const char *filename;
  42. const char *orig_filename;
  43. uint32_t lineno;
  44. uint32_t orig_lineno;
  45. } zend_leak_info;
  46. #if ZEND_DEBUG
  47. typedef struct _zend_mm_debug_info {
  48. size_t size;
  49. const char *filename;
  50. const char *orig_filename;
  51. uint32_t lineno;
  52. uint32_t orig_lineno;
  53. } zend_mm_debug_info;
  54. # define ZEND_MM_OVERHEAD ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))
  55. #else
  56. # define ZEND_MM_OVERHEAD 0
  57. #endif
  58. BEGIN_EXTERN_C()
  59. ZEND_API char* ZEND_FASTCALL zend_strndup(const char *s, size_t length) ZEND_ATTRIBUTE_MALLOC;
  60. ZEND_API void* ZEND_FASTCALL _emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE(1);
  61. ZEND_API void* ZEND_FASTCALL _safe_emalloc(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
  62. ZEND_API void* ZEND_FASTCALL _safe_malloc(size_t nmemb, size_t size, size_t offset) ZEND_ATTRIBUTE_MALLOC;
  63. ZEND_API void ZEND_FASTCALL _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
  64. ZEND_API void* ZEND_FASTCALL _ecalloc(size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE2(1,2);
  65. ZEND_API void* ZEND_FASTCALL _erealloc(void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_ALLOC_SIZE(2);
  66. ZEND_API void* ZEND_FASTCALL _erealloc2(void *ptr, size_t size, size_t copy_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_ALLOC_SIZE(2);
  67. ZEND_API void* ZEND_FASTCALL _safe_erealloc(void *ptr, size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
  68. ZEND_API void* ZEND_FASTCALL _safe_realloc(void *ptr, size_t nmemb, size_t size, size_t offset);
  69. ZEND_API char* ZEND_FASTCALL _estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
  70. ZEND_API char* ZEND_FASTCALL _estrndup(const char *s, size_t length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
  71. ZEND_API size_t ZEND_FASTCALL _zend_mem_block_size(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
  72. #include "zend_alloc_sizes.h"
  73. /* _emalloc() & _efree() specialization */
  74. #if !ZEND_DEBUG && defined(HAVE_BUILTIN_CONSTANT_P)
  75. # define _ZEND_BIN_ALLOCATOR_DEF(_num, _size, _elements, _pages, x, y) \
  76. ZEND_API void* ZEND_FASTCALL _emalloc_ ## _size(void) ZEND_ATTRIBUTE_MALLOC;
  77. ZEND_MM_BINS_INFO(_ZEND_BIN_ALLOCATOR_DEF, x, y)
  78. ZEND_API void* ZEND_FASTCALL _emalloc_large(size_t size) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE(1);
  79. ZEND_API void* ZEND_FASTCALL _emalloc_huge(size_t size) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE(1);
  80. # define _ZEND_BIN_ALLOCATOR_SELECTOR_START(_num, _size, _elements, _pages, size, y) \
  81. ((size <= _size) ? _emalloc_ ## _size() :
  82. # define _ZEND_BIN_ALLOCATOR_SELECTOR_END(_num, _size, _elements, _pages, size, y) \
  83. )
  84. # define ZEND_ALLOCATOR(size) \
  85. ZEND_MM_BINS_INFO(_ZEND_BIN_ALLOCATOR_SELECTOR_START, size, y) \
  86. ((size <= ZEND_MM_MAX_LARGE_SIZE) ? _emalloc_large(size) : _emalloc_huge(size)) \
  87. ZEND_MM_BINS_INFO(_ZEND_BIN_ALLOCATOR_SELECTOR_END, size, y)
  88. # define _emalloc(size) \
  89. (__builtin_constant_p(size) ? \
  90. ZEND_ALLOCATOR(size) \
  91. : \
  92. _emalloc(size) \
  93. )
  94. # define _ZEND_BIN_DEALLOCATOR_DEF(_num, _size, _elements, _pages, x, y) \
  95. ZEND_API void ZEND_FASTCALL _efree_ ## _size(void *);
  96. ZEND_MM_BINS_INFO(_ZEND_BIN_DEALLOCATOR_DEF, x, y)
  97. ZEND_API void ZEND_FASTCALL _efree_large(void *, size_t size);
  98. ZEND_API void ZEND_FASTCALL _efree_huge(void *, size_t size);
  99. # define _ZEND_BIN_DEALLOCATOR_SELECTOR_START(_num, _size, _elements, _pages, ptr, size) \
  100. if (size <= _size) { _efree_ ## _size(ptr); } else
  101. # define ZEND_DEALLOCATOR(ptr, size) \
  102. ZEND_MM_BINS_INFO(_ZEND_BIN_DEALLOCATOR_SELECTOR_START, ptr, size) \
  103. if (size <= ZEND_MM_MAX_LARGE_SIZE) { _efree_large(ptr, size); } \
  104. else { _efree_huge(ptr, size); }
  105. # define efree_size(ptr, size) do { \
  106. if (__builtin_constant_p(size)) { \
  107. ZEND_DEALLOCATOR(ptr, size) \
  108. } else { \
  109. _efree(ptr); \
  110. } \
  111. } while (0)
  112. # define efree_size_rel(ptr, size) \
  113. efree_size(ptr, size)
  114. #else
  115. # define efree_size(ptr, size) \
  116. efree(ptr)
  117. # define efree_size_rel(ptr, size) \
  118. efree_rel(ptr)
  119. #define _emalloc_large _emalloc
  120. #define _emalloc_huge _emalloc
  121. #define _efree_large _efree
  122. #define _efree_huge _efree
  123. #endif
  124. /* Standard wrapper macros */
  125. #define emalloc(size) _emalloc((size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  126. #define emalloc_large(size) _emalloc_large((size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  127. #define emalloc_huge(size) _emalloc_huge((size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  128. #define safe_emalloc(nmemb, size, offset) _safe_emalloc((nmemb), (size), (offset) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  129. #define efree(ptr) _efree((ptr) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  130. #define efree_large(ptr) _efree_large((ptr) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  131. #define efree_huge(ptr) _efree_huge((ptr) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  132. #define ecalloc(nmemb, size) _ecalloc((nmemb), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  133. #define erealloc(ptr, size) _erealloc((ptr), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  134. #define erealloc2(ptr, size, copy_size) _erealloc2((ptr), (size), (copy_size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  135. #define safe_erealloc(ptr, nmemb, size, offset) _safe_erealloc((ptr), (nmemb), (size), (offset) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  136. #define erealloc_recoverable(ptr, size) _erealloc((ptr), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  137. #define erealloc2_recoverable(ptr, size, copy_size) _erealloc2((ptr), (size), (copy_size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  138. #define estrdup(s) _estrdup((s) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  139. #define estrndup(s, length) _estrndup((s), (length) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  140. #define zend_mem_block_size(ptr) _zend_mem_block_size((ptr) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  141. /* Relay wrapper macros */
  142. #define emalloc_rel(size) _emalloc((size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  143. #define safe_emalloc_rel(nmemb, size, offset) _safe_emalloc((nmemb), (size), (offset) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  144. #define efree_rel(ptr) _efree((ptr) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  145. #define ecalloc_rel(nmemb, size) _ecalloc((nmemb), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  146. #define erealloc_rel(ptr, size) _erealloc((ptr), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  147. #define erealloc2_rel(ptr, size, copy_size) _erealloc2((ptr), (size), (copy_size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  148. #define erealloc_recoverable_rel(ptr, size) _erealloc((ptr), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  149. #define erealloc2_recoverable_rel(ptr, size, copy_size) _erealloc2((ptr), (size), (copy_size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  150. #define safe_erealloc_rel(ptr, nmemb, size, offset) _safe_erealloc((ptr), (nmemb), (size), (offset) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  151. #define estrdup_rel(s) _estrdup((s) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  152. #define estrndup_rel(s, length) _estrndup((s), (length) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  153. #define zend_mem_block_size_rel(ptr) _zend_mem_block_size((ptr) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  154. ZEND_API void * __zend_malloc(size_t len) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE(1);
  155. ZEND_API void * __zend_calloc(size_t nmemb, size_t len) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE2(1,2);
  156. ZEND_API void * __zend_realloc(void *p, size_t len) ZEND_ATTRIBUTE_ALLOC_SIZE(2);
  157. /* Selective persistent/non persistent allocation macros */
  158. #define pemalloc(size, persistent) ((persistent)?__zend_malloc(size):emalloc(size))
  159. #define safe_pemalloc(nmemb, size, offset, persistent) ((persistent)?_safe_malloc(nmemb, size, offset):safe_emalloc(nmemb, size, offset))
  160. #define pefree(ptr, persistent) ((persistent)?free(ptr):efree(ptr))
  161. #define pefree_size(ptr, size, persistent) do { \
  162. if (persistent) { \
  163. free(ptr); \
  164. } else { \
  165. efree_size(ptr, size);\
  166. } \
  167. } while (0)
  168. #define pecalloc(nmemb, size, persistent) ((persistent)?__zend_calloc((nmemb), (size)):ecalloc((nmemb), (size)))
  169. #define perealloc(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc((ptr), (size)))
  170. #define perealloc2(ptr, size, copy_size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc2((ptr), (size), (copy_size)))
  171. #define safe_perealloc(ptr, nmemb, size, offset, persistent) ((persistent)?_safe_realloc((ptr), (nmemb), (size), (offset)):safe_erealloc((ptr), (nmemb), (size), (offset)))
  172. #define perealloc_recoverable(ptr, size, persistent) ((persistent)?realloc((ptr), (size)):erealloc_recoverable((ptr), (size)))
  173. #define perealloc2_recoverable(ptr, size, persistent) ((persistent)?realloc((ptr), (size)):erealloc2_recoverable((ptr), (size), (copy_size)))
  174. #define pestrdup(s, persistent) ((persistent)?strdup(s):estrdup(s))
  175. #define pestrndup(s, length, persistent) ((persistent)?zend_strndup((s),(length)):estrndup((s),(length)))
  176. #define pemalloc_rel(size, persistent) ((persistent)?__zend_malloc(size):emalloc_rel(size))
  177. #define pefree_rel(ptr, persistent) ((persistent)?free(ptr):efree_rel(ptr))
  178. #define pecalloc_rel(nmemb, size, persistent) ((persistent)?__zend_calloc((nmemb), (size)):ecalloc_rel((nmemb), (size)))
  179. #define perealloc_rel(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc_rel((ptr), (size)))
  180. #define perealloc2_rel(ptr, size, copy_size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc2_rel((ptr), (size), (copy_size)))
  181. #define perealloc_recoverable_rel(ptr, size, persistent) ((persistent)?realloc((ptr), (size)):erealloc_recoverable_rel((ptr), (size)))
  182. #define perealloc2_recoverable_rel(ptr, size, copy_size, persistent) ((persistent)?realloc((ptr), (size)):erealloc2_recoverable_rel((ptr), (size), (copy_size)))
  183. #define pestrdup_rel(s, persistent) ((persistent)?strdup(s):estrdup_rel(s))
  184. ZEND_API zend_result zend_set_memory_limit(size_t memory_limit);
  185. ZEND_API bool zend_alloc_in_memory_limit_error_reporting(void);
  186. ZEND_API void start_memory_manager(void);
  187. ZEND_API void shutdown_memory_manager(bool silent, bool full_shutdown);
  188. ZEND_API bool is_zend_mm(void);
  189. ZEND_API bool is_zend_ptr(const void *ptr);
  190. ZEND_API size_t zend_memory_usage(bool real_usage);
  191. ZEND_API size_t zend_memory_peak_usage(bool real_usage);
  192. /* fast cache for HashTables */
  193. #define ALLOC_HASHTABLE(ht) \
  194. (ht) = (HashTable *) emalloc(sizeof(HashTable))
  195. #define FREE_HASHTABLE(ht) \
  196. efree_size(ht, sizeof(HashTable))
  197. #define ALLOC_HASHTABLE_REL(ht) \
  198. (ht) = (HashTable *) emalloc_rel(sizeof(HashTable))
  199. #define FREE_HASHTABLE_REL(ht) \
  200. efree_size_rel(ht, sizeof(HashTable))
  201. /* Heap functions */
  202. typedef struct _zend_mm_heap zend_mm_heap;
  203. ZEND_API zend_mm_heap *zend_mm_startup(void);
  204. ZEND_API void zend_mm_shutdown(zend_mm_heap *heap, bool full_shutdown, bool silent);
  205. ZEND_API void* ZEND_FASTCALL _zend_mm_alloc(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
  206. ZEND_API void ZEND_FASTCALL _zend_mm_free(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
  207. ZEND_API void* ZEND_FASTCALL _zend_mm_realloc(zend_mm_heap *heap, void *p, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
  208. ZEND_API void* ZEND_FASTCALL _zend_mm_realloc2(zend_mm_heap *heap, void *p, size_t size, size_t copy_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
  209. ZEND_API size_t ZEND_FASTCALL _zend_mm_block_size(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
  210. #define zend_mm_alloc(heap, size) _zend_mm_alloc((heap), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  211. #define zend_mm_free(heap, p) _zend_mm_free((heap), (p) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  212. #define zend_mm_realloc(heap, p, size) _zend_mm_realloc((heap), (p), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  213. #define zend_mm_realloc2(heap, p, size, copy_size) _zend_mm_realloc2((heap), (p), (size), (copy_size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  214. #define zend_mm_block_size(heap, p) _zend_mm_block_size((heap), (p) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  215. #define zend_mm_alloc_rel(heap, size) _zend_mm_alloc((heap), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  216. #define zend_mm_free_rel(heap, p) _zend_mm_free((heap), (p) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  217. #define zend_mm_realloc_rel(heap, p, size) _zend_mm_realloc((heap), (p), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  218. #define zend_mm_realloc2_rel(heap, p, size, copy_size) _zend_mm_realloc2((heap), (p), (size), (copy_size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  219. #define zend_mm_block_size_rel(heap, p) _zend_mm_block_size((heap), (p) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  220. ZEND_API zend_mm_heap *zend_mm_set_heap(zend_mm_heap *new_heap);
  221. ZEND_API zend_mm_heap *zend_mm_get_heap(void);
  222. ZEND_API size_t zend_mm_gc(zend_mm_heap *heap);
  223. #define ZEND_MM_CUSTOM_HEAP_NONE 0
  224. #define ZEND_MM_CUSTOM_HEAP_STD 1
  225. #define ZEND_MM_CUSTOM_HEAP_DEBUG 2
  226. ZEND_API bool zend_mm_is_custom_heap(zend_mm_heap *new_heap);
  227. ZEND_API void zend_mm_set_custom_handlers(zend_mm_heap *heap,
  228. void* (*_malloc)(size_t),
  229. void (*_free)(void*),
  230. void* (*_realloc)(void*, size_t));
  231. ZEND_API void zend_mm_get_custom_handlers(zend_mm_heap *heap,
  232. void* (**_malloc)(size_t),
  233. void (**_free)(void*),
  234. void* (**_realloc)(void*, size_t));
  235. #if ZEND_DEBUG
  236. ZEND_API void zend_mm_set_custom_debug_handlers(zend_mm_heap *heap,
  237. void* (*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
  238. void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
  239. void* (*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC));
  240. #endif
  241. typedef struct _zend_mm_storage zend_mm_storage;
  242. typedef void* (*zend_mm_chunk_alloc_t)(zend_mm_storage *storage, size_t size, size_t alignment);
  243. typedef void (*zend_mm_chunk_free_t)(zend_mm_storage *storage, void *chunk, size_t size);
  244. typedef bool (*zend_mm_chunk_truncate_t)(zend_mm_storage *storage, void *chunk, size_t old_size, size_t new_size);
  245. typedef bool (*zend_mm_chunk_extend_t)(zend_mm_storage *storage, void *chunk, size_t old_size, size_t new_size);
  246. typedef struct _zend_mm_handlers {
  247. zend_mm_chunk_alloc_t chunk_alloc;
  248. zend_mm_chunk_free_t chunk_free;
  249. zend_mm_chunk_truncate_t chunk_truncate;
  250. zend_mm_chunk_extend_t chunk_extend;
  251. } zend_mm_handlers;
  252. struct _zend_mm_storage {
  253. const zend_mm_handlers handlers;
  254. void *data;
  255. };
  256. ZEND_API zend_mm_storage *zend_mm_get_storage(zend_mm_heap *heap);
  257. ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_handlers *handlers, void *data, size_t data_size);
  258. /*
  259. // The following example shows how to use zend_mm_heap API with custom storage
  260. static zend_mm_heap *apc_heap = NULL;
  261. static HashTable *apc_ht = NULL;
  262. typedef struct _apc_data {
  263. void *mem;
  264. uint32_t free_pages;
  265. } apc_data;
  266. static void *apc_chunk_alloc(zend_mm_storage *storage, size_t size, size_t alignment)
  267. {
  268. apc_data *data = (apc_data*)(storage->data);
  269. size_t real_size = ((size + (ZEND_MM_CHUNK_SIZE-1)) & ~(ZEND_MM_CHUNK_SIZE-1));
  270. uint32_t count = real_size / ZEND_MM_CHUNK_SIZE;
  271. uint32_t first, last, i;
  272. ZEND_ASSERT(alignment == ZEND_MM_CHUNK_SIZE);
  273. for (first = 0; first < 32; first++) {
  274. if (!(data->free_pages & (1 << first))) {
  275. last = first;
  276. do {
  277. if (last - first == count - 1) {
  278. for (i = first; i <= last; i++) {
  279. data->free_pages |= (1 << i);
  280. }
  281. return (void *)(((char*)(data->mem)) + ZEND_MM_CHUNK_SIZE * (1 << first));
  282. }
  283. last++;
  284. } while (last < 32 && !(data->free_pages & (1 << last)));
  285. first = last;
  286. }
  287. }
  288. return NULL;
  289. }
  290. static void apc_chunk_free(zend_mm_storage *storage, void *chunk, size_t size)
  291. {
  292. apc_data *data = (apc_data*)(storage->data);
  293. uint32_t i;
  294. ZEND_ASSERT(((uintptr_t)chunk & (ZEND_MM_CHUNK_SIZE - 1)) == 0);
  295. i = ((uintptr_t)chunk - (uintptr_t)(data->mem)) / ZEND_MM_CHUNK_SIZE;
  296. while (1) {
  297. data->free_pages &= ~(1 << i);
  298. if (size <= ZEND_MM_CHUNK_SIZE) {
  299. break;
  300. }
  301. size -= ZEND_MM_CHUNK_SIZE;
  302. }
  303. }
  304. static void apc_init_heap(void)
  305. {
  306. zend_mm_handlers apc_handlers = {
  307. apc_chunk_alloc,
  308. apc_chunk_free,
  309. NULL,
  310. NULL,
  311. };
  312. apc_data tmp_data;
  313. zend_mm_heap *old_heap;
  314. // Preallocate properly aligned SHM chunks (64MB)
  315. tmp_data.mem = shm_memalign(ZEND_MM_CHUNK_SIZE, ZEND_MM_CHUNK_SIZE * 32);
  316. // Initialize temporary storage data
  317. tmp_data.free_pages = 0;
  318. // Create heap
  319. apc_heap = zend_mm_startup_ex(&apc_handlers, &tmp_data, sizeof(tmp_data));
  320. // Allocate some data in the heap
  321. old_heap = zend_mm_set_heap(apc_heap);
  322. ALLOC_HASHTABLE(apc_ht);
  323. zend_hash_init(apc_ht, 64, NULL, ZVAL_PTR_DTOR, 0);
  324. zend_mm_set_heap(old_heap);
  325. }
  326. */
  327. #ifdef ZTS
  328. size_t zend_mm_globals_size(void);
  329. #endif
  330. END_EXTERN_C()
  331. #endif