xref: /PHP-5.6/Zend/zend_alloc.h (revision 19866fb7)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2016 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@zend.com>                                |
16    |          Zeev Suraski <zeev@zend.com>                                |
17    +----------------------------------------------------------------------+
18 */
19 
20 /* $Id$ */
21 
22 #ifndef ZEND_ALLOC_H
23 #define ZEND_ALLOC_H
24 
25 #include <stdio.h>
26 
27 #include "../TSRM/TSRM.h"
28 #include "zend.h"
29 
30 #ifndef ZEND_MM_ALIGNMENT
31 # define ZEND_MM_ALIGNMENT 8
32 # define ZEND_MM_ALIGNMENT_LOG2 3
33 #elif ZEND_MM_ALIGNMENT < 4
34 # undef ZEND_MM_ALIGNMENT
35 # undef ZEND_MM_ALIGNMENT_LOG2
36 # define ZEND_MM_ALIGNMENT 4
37 # define ZEND_MM_ALIGNMENT_LOG2 2
38 #endif
39 
40 #define ZEND_MM_ALIGNMENT_MASK ~(ZEND_MM_ALIGNMENT-1)
41 
42 #define ZEND_MM_ALIGNED_SIZE(size)	(((size) + ZEND_MM_ALIGNMENT - 1) & ZEND_MM_ALIGNMENT_MASK)
43 
44 typedef struct _zend_leak_info {
45 	void *addr;
46 	size_t size;
47 	const char *filename;
48 	uint lineno;
49 	const char *orig_filename;
50 	uint orig_lineno;
51 } zend_leak_info;
52 
53 BEGIN_EXTERN_C()
54 
55 ZEND_API char *zend_strndup(const char *s, unsigned int length) ZEND_ATTRIBUTE_MALLOC;
56 
57 ZEND_API void *_emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE(1);
58 ZEND_API void *_safe_emalloc(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
59 ZEND_API void *_safe_emalloc_string(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
60 ZEND_API void *_safe_malloc(size_t nmemb, size_t size, size_t offset) ZEND_ATTRIBUTE_MALLOC;
61 ZEND_API void _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
62 ZEND_API void *_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);
63 ZEND_API void *_erealloc(void *ptr, size_t size, int allow_failure ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_ALLOC_SIZE(2);
64 ZEND_API void *_safe_erealloc(void *ptr, size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
65 ZEND_API void *_safe_realloc(void *ptr, size_t nmemb, size_t size, size_t offset);
66 ZEND_API char *_estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
67 ZEND_API char *_estrndup(const char *s, unsigned int length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
68 ZEND_API size_t _zend_mem_block_size(void *ptr TSRMLS_DC ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
69 
70 /* Standard wrapper macros */
71 #define emalloc(size)						_emalloc((size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
72 #define safe_emalloc(nmemb, size, offset)	_safe_emalloc((nmemb), (size), (offset) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
73 #define safe_emalloc_string(nmemb, size, offset)	_safe_emalloc_string((nmemb), (size), (offset) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
74 #define efree(ptr)							_efree((ptr) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
75 #define ecalloc(nmemb, size)				_ecalloc((nmemb), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
76 #define erealloc(ptr, size)					_erealloc((ptr), (size), 0 ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
77 #define safe_erealloc(ptr, nmemb, size, offset)	_safe_erealloc((ptr), (nmemb), (size), (offset) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
78 #define erealloc_recoverable(ptr, size)		_erealloc((ptr), (size), 1 ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
79 #define estrdup(s)							_estrdup((s) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
80 #define estrndup(s, length)					_estrndup((s), (length) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
81 #define zend_mem_block_size(ptr)			_zend_mem_block_size((ptr) TSRMLS_CC ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
82 
83 /* Relay wrapper macros */
84 #define emalloc_rel(size)						_emalloc((size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
85 #define safe_emalloc_rel(nmemb, size, offset)	_safe_emalloc((nmemb), (size), (offset) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
86 #define efree_rel(ptr)							_efree((ptr) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
87 #define ecalloc_rel(nmemb, size)				_ecalloc((nmemb), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
88 #define erealloc_rel(ptr, size)					_erealloc((ptr), (size), 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
89 #define erealloc_recoverable_rel(ptr, size)		_erealloc((ptr), (size), 1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
90 #define safe_erealloc_rel(ptr, nmemb, size, offset)	_safe_erealloc((ptr), (nmemb), (size), (offset) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
91 #define estrdup_rel(s)							_estrdup((s) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
92 #define estrndup_rel(s, length)					_estrndup((s), (length) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
93 #define zend_mem_block_size_rel(ptr)			_zend_mem_block_size((ptr) TSRMLS_CC ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
94 
__zend_malloc(size_t len)95 inline static void * __zend_malloc(size_t len)
96 {
97 	void *tmp = malloc(len);
98 	if (tmp) {
99 		return tmp;
100 	}
101 	fprintf(stderr, "Out of memory\n");
102 	exit(1);
103 }
104 
__zend_calloc(size_t nmemb,size_t len)105 inline static void * __zend_calloc(size_t nmemb, size_t len)
106 {
107 	void *tmp = _safe_malloc(nmemb, len, 0);
108 	memset(tmp, 0, nmemb * len);
109 	return tmp;
110 }
111 
__zend_realloc(void * p,size_t len)112 inline static void * __zend_realloc(void *p, size_t len)
113 {
114 	p = realloc(p, len);
115 	if (p) {
116 		return p;
117 	}
118 	fprintf(stderr, "Out of memory\n");
119 	exit(1);
120 }
121 
122 
123 /* Selective persistent/non persistent allocation macros */
124 #define pemalloc(size, persistent) ((persistent)?__zend_malloc(size):emalloc(size))
125 #define safe_pemalloc(nmemb, size, offset, persistent)	((persistent)?_safe_malloc(nmemb, size, offset):safe_emalloc(nmemb, size, offset))
126 #define pefree(ptr, persistent)  ((persistent)?free(ptr):efree(ptr))
127 #define pecalloc(nmemb, size, persistent) ((persistent)?__zend_calloc((nmemb), (size)):ecalloc((nmemb), (size)))
128 #define perealloc(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc((ptr), (size)))
129 #define safe_perealloc(ptr, nmemb, size, offset, persistent)	((persistent)?_safe_realloc((ptr), (nmemb), (size), (offset)):safe_erealloc((ptr), (nmemb), (size), (offset)))
130 #define perealloc_recoverable(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc_recoverable((ptr), (size)))
131 #define pestrdup(s, persistent) ((persistent)?strdup(s):estrdup(s))
132 #define pestrndup(s, length, persistent) ((persistent)?zend_strndup((s),(length)):estrndup((s),(length)))
133 
134 #define pemalloc_rel(size, persistent) ((persistent)?__zend_malloc(size):emalloc_rel(size))
135 #define pefree_rel(ptr, persistent)	((persistent)?free(ptr):efree_rel(ptr))
136 #define pecalloc_rel(nmemb, size, persistent) ((persistent)?__zend_calloc((nmemb), (size)):ecalloc_rel((nmemb), (size)))
137 #define perealloc_rel(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc_rel((ptr), (size)))
138 #define perealloc_recoverable_rel(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc_recoverable_rel((ptr), (size)))
139 #define pestrdup_rel(s, persistent) ((persistent)?strdup(s):estrdup_rel(s))
140 
141 #define safe_estrdup(ptr)  ((ptr)?(estrdup(ptr)):STR_EMPTY_ALLOC())
142 #define safe_estrndup(ptr, len) ((ptr)?(estrndup((ptr), (len))):STR_EMPTY_ALLOC())
143 
144 ZEND_API int zend_set_memory_limit(size_t memory_limit);
145 
146 ZEND_API void start_memory_manager(TSRMLS_D);
147 ZEND_API void shutdown_memory_manager(int silent, int full_shutdown TSRMLS_DC);
148 ZEND_API int is_zend_mm(TSRMLS_D);
149 
150 #if ZEND_DEBUG
151 ZEND_API int _mem_block_check(void *ptr, int silent ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
152 ZEND_API void _full_mem_check(int silent ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
153 void zend_debug_alloc_output(char *format, ...);
154 #define mem_block_check(ptr, silent) _mem_block_check(ptr, silent ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
155 #define full_mem_check(silent) _full_mem_check(silent ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
156 #else
157 #define mem_block_check(type, ptr, silent)
158 #define full_mem_check(silent)
159 #endif
160 
161 ZEND_API size_t zend_memory_usage(int real_usage TSRMLS_DC);
162 ZEND_API size_t zend_memory_peak_usage(int real_usage TSRMLS_DC);
163 
164 END_EXTERN_C()
165 
166 /* fast cache for zval's */
167 #define ALLOC_ZVAL(z)	\
168 	(z) = (zval *) emalloc(sizeof(zval))
169 
170 #define FREE_ZVAL(z)	\
171 	efree_rel(z)
172 
173 #define ALLOC_ZVAL_REL(z)	\
174 	(z) = (zval *) emalloc_rel(sizeof(zval))
175 
176 #define FREE_ZVAL_REL(z)	\
177 	efree_rel(z)
178 
179 /* fast cache for HashTables */
180 #define ALLOC_HASHTABLE(ht)	\
181 	(ht) = (HashTable *) emalloc(sizeof(HashTable))
182 
183 #define FREE_HASHTABLE(ht)	\
184 	efree(ht)
185 
186 #define ALLOC_HASHTABLE_REL(ht)	\
187 	(ht) = (HashTable *) emalloc_rel(sizeof(HashTable))
188 
189 #define FREE_HASHTABLE_REL(ht)	\
190 	efree_rel(ht)
191 
192 /* Heap functions */
193 typedef struct _zend_mm_heap zend_mm_heap;
194 
195 ZEND_API zend_mm_heap *zend_mm_startup(void);
196 ZEND_API void zend_mm_shutdown(zend_mm_heap *heap, int full_shutdown, int silent TSRMLS_DC);
197 ZEND_API void *_zend_mm_alloc(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
198 ZEND_API void _zend_mm_free(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
199 ZEND_API void *_zend_mm_realloc(zend_mm_heap *heap, void *p, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
200 ZEND_API size_t _zend_mm_block_size(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
201 
202 #define zend_mm_alloc(heap, size)			_zend_mm_alloc((heap), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
203 #define zend_mm_free(heap, p)				_zend_mm_free((heap), (p) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
204 #define zend_mm_realloc(heap, p, size)		_zend_mm_realloc((heap), (p), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
205 #define zend_mm_block_size(heap, p)			_zend_mm_block_size((heap), (p) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
206 
207 #define zend_mm_alloc_rel(heap, size)		_zend_mm_alloc((heap), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
208 #define zend_mm_free_rel(heap, p)			_zend_mm_free((heap), (p) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
209 #define zend_mm_realloc_rel(heap, p, size)	_zend_mm_realloc((heap), (p), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
210 #define zend_mm_block_size_rel(heap, p)		_zend_mm_block_size((heap), (p) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
211 
212 /* Heaps with user defined storage */
213 typedef struct _zend_mm_storage zend_mm_storage;
214 
215 typedef struct _zend_mm_segment {
216 	size_t	size;
217 	struct _zend_mm_segment *next_segment;
218 } zend_mm_segment;
219 
220 typedef struct _zend_mm_mem_handlers {
221 	const char *name;
222 	zend_mm_storage* (*init)(void *params);
223 	void (*dtor)(zend_mm_storage *storage);
224 	void (*compact)(zend_mm_storage *storage);
225 	zend_mm_segment* (*_alloc)(zend_mm_storage *storage, size_t size);
226 	zend_mm_segment* (*_realloc)(zend_mm_storage *storage, zend_mm_segment *ptr, size_t size);
227 	void (*_free)(zend_mm_storage *storage, zend_mm_segment *ptr);
228 } zend_mm_mem_handlers;
229 
230 struct _zend_mm_storage {
231 	const zend_mm_mem_handlers *handlers;
232 	void *data;
233 };
234 
235 ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_mem_handlers *handlers, size_t block_size, size_t reserve_size, int internal, void *params);
236 ZEND_API zend_mm_heap *zend_mm_set_heap(zend_mm_heap *new_heap TSRMLS_DC);
237 ZEND_API zend_mm_storage *zend_mm_get_storage(zend_mm_heap *heap);
238 
239 ZEND_API void zend_mm_set_custom_handlers(zend_mm_heap *heap,
240                                           void* (*_malloc)(size_t),
241                                           void  (*_free)(void*),
242                                           void* (*_realloc)(void*, size_t));
243 
244 #endif
245 
246 /*
247  * Local variables:
248  * tab-width: 4
249  * c-basic-offset: 4
250  * indent-tabs-mode: t
251  * End:
252  */
253