xref: /php-src/Zend/zend_alloc.h (revision f4557b48)
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 
21 #ifndef ZEND_ALLOC_H
22 #define ZEND_ALLOC_H
23 
24 #include <stdio.h>
25 
26 #include "zend_types.h"
27 #include "../TSRM/TSRM.h"
28 
29 #ifndef ZEND_MM_ALIGNMENT
30 # error "ZEND_MM_ALIGNMENT was not defined during configure"
31 #endif
32 
33 #define ZEND_MM_ALIGNMENT_MASK ~(ZEND_MM_ALIGNMENT - 1)
34 
35 #define ZEND_MM_ALIGNED_SIZE(size)	(((size) + ZEND_MM_ALIGNMENT - 1) & ZEND_MM_ALIGNMENT_MASK)
36 
37 #define ZEND_MM_ALIGNED_SIZE_EX(size, alignment) \
38 	(((size) + ((alignment) - 1)) & ~((alignment) - 1))
39 
40 typedef struct _zend_leak_info {
41 	void *addr;
42 	size_t size;
43 	const char *filename;
44 	const char *orig_filename;
45 	uint32_t lineno;
46 	uint32_t orig_lineno;
47 } zend_leak_info;
48 
49 #if ZEND_DEBUG
50 typedef struct _zend_mm_debug_info {
51 	size_t             size;
52 	const char        *filename;
53 	const char        *orig_filename;
54 	uint32_t               lineno;
55 	uint32_t               orig_lineno;
56 } zend_mm_debug_info;
57 
58 # define ZEND_MM_OVERHEAD ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))
59 #else
60 # define ZEND_MM_OVERHEAD 0
61 #endif
62 
63 BEGIN_EXTERN_C()
64 
65 ZEND_API ZEND_ATTRIBUTE_MALLOC char*  ZEND_FASTCALL zend_strndup(const char *s, size_t length);
66 
67 ZEND_API ZEND_ATTRIBUTE_MALLOC void*  ZEND_FASTCALL _emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_ALLOC_SIZE(1);
68 ZEND_API ZEND_ATTRIBUTE_MALLOC void*  ZEND_FASTCALL _safe_emalloc(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
69 ZEND_API ZEND_ATTRIBUTE_MALLOC void*  ZEND_FASTCALL _safe_malloc(size_t nmemb, size_t size, size_t offset);
70 ZEND_API void   ZEND_FASTCALL _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
71 ZEND_API ZEND_ATTRIBUTE_MALLOC void*  ZEND_FASTCALL _ecalloc(size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_ALLOC_SIZE2(1,2);
72 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);
73 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);
74 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);
75 ZEND_API void*  ZEND_FASTCALL _safe_realloc(void *ptr, size_t nmemb, size_t size, size_t offset);
76 ZEND_API ZEND_ATTRIBUTE_MALLOC char*  ZEND_FASTCALL _estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
77 ZEND_API ZEND_ATTRIBUTE_MALLOC char*  ZEND_FASTCALL _estrndup(const char *s, size_t length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
78 ZEND_API size_t ZEND_FASTCALL _zend_mem_block_size(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
79 
80 #include "zend_alloc_sizes.h"
81 
82 /* _emalloc() & _efree() specialization */
83 #if !ZEND_DEBUG && defined(HAVE_BUILTIN_CONSTANT_P)
84 
85 # define _ZEND_BIN_ALLOCATOR_DEF(_num, _size, _elements, _pages, x, y) \
86 	ZEND_API ZEND_ATTRIBUTE_MALLOC void* ZEND_FASTCALL _emalloc_  ## _size(void);
87 
88 ZEND_MM_BINS_INFO(_ZEND_BIN_ALLOCATOR_DEF, x, y)
89 
90 ZEND_API ZEND_ATTRIBUTE_MALLOC void* ZEND_FASTCALL _emalloc_large(size_t size) ZEND_ATTRIBUTE_ALLOC_SIZE(1);
91 ZEND_API ZEND_ATTRIBUTE_MALLOC void* ZEND_FASTCALL _emalloc_huge(size_t size) ZEND_ATTRIBUTE_ALLOC_SIZE(1);
92 
93 # define _ZEND_BIN_ALLOCATOR_SELECTOR_START(_num, _size, _elements, _pages, size, y) \
94 	((size <= _size) ? _emalloc_ ## _size() :
95 # define _ZEND_BIN_ALLOCATOR_SELECTOR_END(_num, _size, _elements, _pages, size, y) \
96 	)
97 
98 # define ZEND_ALLOCATOR(size) \
99 	ZEND_MM_BINS_INFO(_ZEND_BIN_ALLOCATOR_SELECTOR_START, size, y) \
100 	((size <= ZEND_MM_MAX_LARGE_SIZE) ? _emalloc_large(size) : _emalloc_huge(size)) \
101 	ZEND_MM_BINS_INFO(_ZEND_BIN_ALLOCATOR_SELECTOR_END, size, y)
102 
103 # define _emalloc(size) \
104 	(__builtin_constant_p(size) ? \
105 		ZEND_ALLOCATOR(size) \
106 	: \
107 		_emalloc(size) \
108 	)
109 
110 # define _ZEND_BIN_DEALLOCATOR_DEF(_num, _size, _elements, _pages, x, y) \
111 	ZEND_API void ZEND_FASTCALL _efree_ ## _size(void *);
112 
113 ZEND_MM_BINS_INFO(_ZEND_BIN_DEALLOCATOR_DEF, x, y)
114 
115 ZEND_API void ZEND_FASTCALL _efree_large(void *, size_t size);
116 ZEND_API void ZEND_FASTCALL _efree_huge(void *, size_t size);
117 
118 # define _ZEND_BIN_DEALLOCATOR_SELECTOR_START(_num, _size, _elements, _pages, ptr, size) \
119 	if (size <= _size) { _efree_ ## _size(ptr); } else
120 
121 # define ZEND_DEALLOCATOR(ptr, size) \
122 	ZEND_MM_BINS_INFO(_ZEND_BIN_DEALLOCATOR_SELECTOR_START, ptr, size) \
123 	if (size <= ZEND_MM_MAX_LARGE_SIZE) { _efree_large(ptr, size); } \
124 	else { _efree_huge(ptr, size); }
125 
126 # define efree_size(ptr, size) do { \
127 		if (__builtin_constant_p(size)) { \
128 			ZEND_DEALLOCATOR(ptr, size) \
129 		} else { \
130 			_efree(ptr); \
131 		} \
132 	} while (0)
133 # define efree_size_rel(ptr, size) \
134 	efree_size(ptr, size)
135 
136 #else
137 
138 # define efree_size(ptr, size) \
139 	efree(ptr)
140 # define efree_size_rel(ptr, size) \
141 	efree_rel(ptr)
142 
143 #define _emalloc_large _emalloc
144 #define _emalloc_huge  _emalloc
145 #define _efree_large   _efree
146 #define _efree_huge    _efree
147 
148 #endif
149 
150 /* Standard wrapper macros */
151 #define emalloc(size)						_emalloc((size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
152 #define emalloc_large(size)					_emalloc_large((size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
153 #define emalloc_huge(size)					_emalloc_huge((size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
154 #define safe_emalloc(nmemb, size, offset)	_safe_emalloc((nmemb), (size), (offset) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
155 #define efree(ptr)							_efree((ptr) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
156 #define efree_large(ptr)					_efree_large((ptr) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
157 #define efree_huge(ptr)						_efree_huge((ptr) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
158 #define ecalloc(nmemb, size)				_ecalloc((nmemb), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
159 #define erealloc(ptr, size)					_erealloc((ptr), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
160 #define erealloc2(ptr, size, copy_size)		_erealloc2((ptr), (size), (copy_size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
161 #define safe_erealloc(ptr, nmemb, size, offset)	_safe_erealloc((ptr), (nmemb), (size), (offset) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
162 #define erealloc_recoverable(ptr, size)		_erealloc((ptr), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
163 #define erealloc2_recoverable(ptr, size, copy_size) _erealloc2((ptr), (size), (copy_size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
164 #define estrdup(s)							_estrdup((s) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
165 #define estrndup(s, length)					_estrndup((s), (length) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
166 #define zend_mem_block_size(ptr)			_zend_mem_block_size((ptr) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
167 
168 /* Relay wrapper macros */
169 #define emalloc_rel(size)						_emalloc((size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
170 #define safe_emalloc_rel(nmemb, size, offset)	_safe_emalloc((nmemb), (size), (offset) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
171 #define efree_rel(ptr)							_efree((ptr) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
172 #define ecalloc_rel(nmemb, size)				_ecalloc((nmemb), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
173 #define erealloc_rel(ptr, size)					_erealloc((ptr), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
174 #define erealloc2_rel(ptr, size, copy_size)		_erealloc2((ptr), (size), (copy_size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
175 #define erealloc_recoverable_rel(ptr, size)		_erealloc((ptr), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
176 #define erealloc2_recoverable_rel(ptr, size, copy_size) _erealloc2((ptr), (size), (copy_size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
177 #define safe_erealloc_rel(ptr, nmemb, size, offset)	_safe_erealloc((ptr), (nmemb), (size), (offset) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
178 #define estrdup_rel(s)							_estrdup((s) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
179 #define estrndup_rel(s, length)					_estrndup((s), (length) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
180 #define zend_mem_block_size_rel(ptr)			_zend_mem_block_size((ptr) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
181 
182 ZEND_API ZEND_ATTRIBUTE_MALLOC void * __zend_malloc(size_t len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_ALLOC_SIZE(1);
183 ZEND_API ZEND_ATTRIBUTE_MALLOC void * __zend_calloc(size_t nmemb, size_t len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_ALLOC_SIZE2(1,2);
184 ZEND_API void * __zend_realloc(void *p, size_t len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_ALLOC_SIZE(2);
185 ZEND_API void __zend_free(void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
186 ZEND_API ZEND_ATTRIBUTE_MALLOC char * __zend_strdup(const char *s);
187 
188 /* Selective persistent/non persistent allocation macros */
189 #define pemalloc(size, persistent) ((persistent)?__zend_malloc(size ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC):emalloc(size))
190 #define safe_pemalloc(nmemb, size, offset, persistent)	((persistent)?_safe_malloc(nmemb, size, offset):safe_emalloc(nmemb, size, offset))
191 #define pefree(ptr, persistent)  ((persistent)?free(ptr):efree(ptr))
192 #define pefree_size(ptr, size, persistent)  do { \
193 		if (persistent) { \
194 			free(ptr); \
195 		} else { \
196 			efree_size(ptr, size);\
197 		} \
198 	} while (0)
199 
200 #define pecalloc(nmemb, size, persistent) ((persistent)?__zend_calloc((nmemb), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC):ecalloc((nmemb), (size)))
201 #define perealloc(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC):erealloc((ptr), (size)))
202 #define perealloc2(ptr, size, copy_size, persistent) ((persistent)?__zend_realloc((ptr), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC):erealloc2((ptr), (size), (copy_size)))
203 #define safe_perealloc(ptr, nmemb, size, offset, persistent)	((persistent)?_safe_realloc((ptr), (nmemb), (size), (offset)):safe_erealloc((ptr), (nmemb), (size), (offset)))
204 #define perealloc_recoverable(ptr, size, persistent) ((persistent)?realloc((ptr), (size)):erealloc_recoverable((ptr), (size)))
205 #define perealloc2_recoverable(ptr, size, persistent) ((persistent)?realloc((ptr), (size)):erealloc2_recoverable((ptr), (size), (copy_size)))
206 #define pestrdup(s, persistent) ((persistent)?__zend_strdup(s):estrdup(s))
207 #define pestrndup(s, length, persistent) ((persistent)?zend_strndup((s),(length)):estrndup((s),(length)))
208 
209 #define pemalloc_rel(size, persistent) ((persistent)?__zend_malloc(size ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC):emalloc_rel(size))
210 #define pefree_rel(ptr, persistent)	((persistent)?free(ptr):efree_rel(ptr))
211 #define pecalloc_rel(nmemb, size, persistent) ((persistent)?__zend_calloc((nmemb), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC):ecalloc_rel((nmemb), (size)))
212 #define perealloc_rel(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC):erealloc_rel((ptr), (size)))
213 #define perealloc2_rel(ptr, size, copy_size, persistent) ((persistent)?__zend_realloc((ptr), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC):erealloc2_rel((ptr), (size), (copy_size)))
214 #define perealloc_recoverable_rel(ptr, size, persistent) ((persistent)?realloc((ptr), (size)):erealloc_recoverable_rel((ptr), (size)))
215 #define perealloc2_recoverable_rel(ptr, size, copy_size, persistent) ((persistent)?realloc((ptr), (size)):erealloc2_recoverable_rel((ptr), (size), (copy_size)))
216 #define pestrdup_rel(s, persistent) ((persistent)?strdup(s):estrdup_rel(s))
217 
218 ZEND_API zend_result zend_set_memory_limit(size_t memory_limit);
219 ZEND_API bool zend_alloc_in_memory_limit_error_reporting(void);
220 
221 ZEND_API void start_memory_manager(void);
222 ZEND_API void shutdown_memory_manager(bool silent, bool full_shutdown);
223 ZEND_API bool is_zend_mm(void);
224 ZEND_API bool is_zend_ptr(const void *ptr);
225 
226 ZEND_API size_t zend_memory_usage(bool real_usage);
227 ZEND_API size_t zend_memory_peak_usage(bool real_usage);
228 ZEND_API void zend_memory_reset_peak_usage(void);
229 
230 /* fast cache for HashTables */
231 #define ALLOC_HASHTABLE(ht)	\
232 	(ht) = (HashTable *) emalloc(sizeof(HashTable))
233 
234 #define FREE_HASHTABLE(ht)	\
235 	efree_size(ht, sizeof(HashTable))
236 
237 #define ALLOC_HASHTABLE_REL(ht)	\
238 	(ht) = (HashTable *) emalloc_rel(sizeof(HashTable))
239 
240 #define FREE_HASHTABLE_REL(ht)	\
241 	efree_size_rel(ht, sizeof(HashTable))
242 
243 /* Heap functions */
244 typedef struct _zend_mm_heap zend_mm_heap;
245 
246 ZEND_API zend_mm_heap *zend_mm_startup(void);
247 ZEND_API void zend_mm_shutdown(zend_mm_heap *heap, bool full_shutdown, bool silent);
248 ZEND_API ZEND_ATTRIBUTE_MALLOC void*  ZEND_FASTCALL _zend_mm_alloc(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_ALLOC_SIZE(2);
249 ZEND_API void   ZEND_FASTCALL _zend_mm_free(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
250 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);
251 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);
252 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);
253 
254 #define zend_mm_alloc(heap, size)			_zend_mm_alloc((heap), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
255 #define zend_mm_free(heap, p)				_zend_mm_free((heap), (p) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
256 #define zend_mm_realloc(heap, p, size)		_zend_mm_realloc((heap), (p), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
257 #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)
258 #define zend_mm_block_size(heap, p)			_zend_mm_block_size((heap), (p) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
259 
260 #define zend_mm_alloc_rel(heap, size)		_zend_mm_alloc((heap), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
261 #define zend_mm_free_rel(heap, p)			_zend_mm_free((heap), (p) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
262 #define zend_mm_realloc_rel(heap, p, size)	_zend_mm_realloc((heap), (p), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
263 #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)
264 #define zend_mm_block_size_rel(heap, p)		_zend_mm_block_size((heap), (p) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
265 
266 ZEND_API zend_mm_heap *zend_mm_set_heap(zend_mm_heap *new_heap);
267 ZEND_API zend_mm_heap *zend_mm_get_heap(void);
268 
269 ZEND_API size_t zend_mm_gc(zend_mm_heap *heap);
270 
271 #define ZEND_MM_CUSTOM_HEAP_NONE  0
272 #define ZEND_MM_CUSTOM_HEAP_STD   1
273 #define ZEND_MM_CUSTOM_HEAP_DEBUG 2
274 
275 ZEND_API bool zend_mm_is_custom_heap(zend_mm_heap *new_heap);
276 ZEND_API void zend_mm_set_custom_handlers(zend_mm_heap *heap,
277                                           void*  (*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
278                                           void   (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
279                                           void*  (*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC));
280 ZEND_API void zend_mm_set_custom_handlers_ex(zend_mm_heap *heap,
281 	                                         void*  (*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
282 		                                     void   (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
283 			                                 void*  (*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
284 				                             size_t (*_gc)(void),
285 					                         void   (*_shutdown)(bool, bool));
286 ZEND_API void zend_mm_get_custom_handlers(zend_mm_heap *heap,
287                                           void*  (**_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
288                                           void   (**_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
289                                           void*  (**_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC));
290 ZEND_API void zend_mm_get_custom_handlers_ex(zend_mm_heap *heap,
291                                           void*  (**_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
292                                           void   (**_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
293                                           void*  (**_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
294                                           size_t (**_gc)(void),
295                                           void   (**_shutdown)(bool, bool));
296 
297 typedef struct _zend_mm_storage zend_mm_storage;
298 
299 typedef	void* (*zend_mm_chunk_alloc_t)(zend_mm_storage *storage, size_t size, size_t alignment);
300 typedef void  (*zend_mm_chunk_free_t)(zend_mm_storage *storage, void *chunk, size_t size);
301 typedef bool   (*zend_mm_chunk_truncate_t)(zend_mm_storage *storage, void *chunk, size_t old_size, size_t new_size);
302 typedef bool   (*zend_mm_chunk_extend_t)(zend_mm_storage *storage, void *chunk, size_t old_size, size_t new_size);
303 
304 typedef struct _zend_mm_handlers {
305 	zend_mm_chunk_alloc_t       chunk_alloc;
306 	zend_mm_chunk_free_t        chunk_free;
307 	zend_mm_chunk_truncate_t    chunk_truncate;
308 	zend_mm_chunk_extend_t      chunk_extend;
309 } zend_mm_handlers;
310 
311 struct _zend_mm_storage {
312 	const zend_mm_handlers handlers;
313 	void *data;
314 };
315 
316 ZEND_API zend_mm_storage *zend_mm_get_storage(zend_mm_heap *heap);
317 ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_handlers *handlers, void *data, size_t data_size);
318 
319 /*
320 
321 // The following example shows how to use zend_mm_heap API with custom storage
322 
323 static zend_mm_heap *apc_heap = NULL;
324 static HashTable    *apc_ht = NULL;
325 
326 typedef struct _apc_data {
327 	void     *mem;
328 	uint32_t  free_pages;
329 } apc_data;
330 
331 static void *apc_chunk_alloc(zend_mm_storage *storage, size_t size, size_t alignment)
332 {
333 	apc_data *data = (apc_data*)(storage->data);
334 	size_t real_size = ((size + (ZEND_MM_CHUNK_SIZE-1)) & ~(ZEND_MM_CHUNK_SIZE-1));
335 	uint32_t count = real_size / ZEND_MM_CHUNK_SIZE;
336 	uint32_t first, last, i;
337 
338 	ZEND_ASSERT(alignment == ZEND_MM_CHUNK_SIZE);
339 
340 	for (first = 0; first < 32; first++) {
341 		if (!(data->free_pages & (1 << first))) {
342 			last = first;
343 			do {
344 				if (last - first == count - 1) {
345 					for (i = first; i <= last; i++) {
346 						data->free_pages |= (1 << i);
347 					}
348 					return (void *)(((char*)(data->mem)) + ZEND_MM_CHUNK_SIZE * (1 << first));
349 				}
350 				last++;
351 			} while (last < 32 && !(data->free_pages & (1 << last)));
352 			first = last;
353 		}
354 	}
355 	return NULL;
356 }
357 
358 static void apc_chunk_free(zend_mm_storage *storage, void *chunk, size_t size)
359 {
360 	apc_data *data = (apc_data*)(storage->data);
361 	uint32_t i;
362 
363 	ZEND_ASSERT(((uintptr_t)chunk & (ZEND_MM_CHUNK_SIZE - 1)) == 0);
364 
365 	i = ((uintptr_t)chunk - (uintptr_t)(data->mem)) / ZEND_MM_CHUNK_SIZE;
366 	while (1) {
367 		data->free_pages &= ~(1 << i);
368 		if (size <= ZEND_MM_CHUNK_SIZE) {
369 			break;
370 		}
371 		size -= ZEND_MM_CHUNK_SIZE;
372 	}
373 }
374 
375 static void apc_init_heap(void)
376 {
377 	zend_mm_handlers apc_handlers = {
378 		apc_chunk_alloc,
379 		apc_chunk_free,
380 		NULL,
381 		NULL,
382 	};
383 	apc_data tmp_data;
384 	zend_mm_heap *old_heap;
385 
386 	// Preallocate properly aligned SHM chunks (64MB)
387 	tmp_data.mem = shm_memalign(ZEND_MM_CHUNK_SIZE, ZEND_MM_CHUNK_SIZE * 32);
388 
389 	// Initialize temporary storage data
390 	tmp_data.free_pages = 0;
391 
392 	// Create heap
393 	apc_heap = zend_mm_startup_ex(&apc_handlers, &tmp_data, sizeof(tmp_data));
394 
395 	// Allocate some data in the heap
396 	old_heap = zend_mm_set_heap(apc_heap);
397 	ALLOC_HASHTABLE(apc_ht);
398 	zend_hash_init(apc_ht, 64, NULL, ZVAL_PTR_DTOR, 0);
399 	zend_mm_set_heap(old_heap);
400 }
401 
402 */
403 
404 #ifdef ZTS
405 size_t zend_mm_globals_size(void);
406 #endif
407 
408 END_EXTERN_C()
409 
410 #endif
411