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