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