xref: /PHP-5.5/ext/opcache/zend_persist_calc.c (revision 73c1be26)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend OPcache                                                         |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2015 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Andi Gutmans <andi@zend.com>                                |
16    |          Zeev Suraski <zeev@zend.com>                                |
17    |          Stanislav Malyshev <stas@zend.com>                          |
18    |          Dmitry Stogov <dmitry@zend.com>                             |
19    +----------------------------------------------------------------------+
20 */
21 
22 #include "zend.h"
23 #include "ZendAccelerator.h"
24 #include "zend_persist.h"
25 #include "zend_extensions.h"
26 #include "zend_shared_alloc.h"
27 #include "zend_operators.h"
28 
29 #define START_SIZE()       uint memory_used = 0
30 #define ADD_DUP_SIZE(m,s)  memory_used += zend_shared_memdup_size((void*)m, s)
31 #define ADD_SIZE(m)        memory_used += ZEND_ALIGNED_SIZE(m)
32 #define RETURN_SIZE()      return memory_used
33 
34 #if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO
35 # define ADD_INTERNED_STRING(str, len) do { \
36 		if (!IS_INTERNED(str)) { \
37 			const char *tmp = accel_new_interned_string((str), (len), 1 TSRMLS_CC); \
38 			if (tmp != (str)) { \
39 				(str) = (char*)tmp; \
40 			} else { \
41 				ADD_DUP_SIZE((str), (len)); \
42 			} \
43 		} \
44 	} while (0)
45 #else
46 # define ADD_INTERNED_STRING(str, len) ADD_DUP_SIZE((str), (len))
47 #endif
48 
49 static uint zend_persist_zval_ptr_calc(zval **zp TSRMLS_DC);
50 
zend_hash_persist_calc(HashTable * ht,int (* pPersistElement)(void * pElement TSRMLS_DC),size_t el_size TSRMLS_DC)51 static uint zend_hash_persist_calc(HashTable *ht, int (*pPersistElement)(void *pElement TSRMLS_DC), size_t el_size TSRMLS_DC)
52 {
53 	Bucket *p = ht->pListHead;
54 	START_SIZE();
55 
56 	while (p) {
57 		/* persist bucket and key */
58 #if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO
59 		ADD_DUP_SIZE(p, sizeof(Bucket));
60 		if (p->nKeyLength) {
61 			const char *tmp = accel_new_interned_string(p->arKey, p->nKeyLength, 0 TSRMLS_CC);
62 			if (tmp != p->arKey) {
63 				p->arKey = tmp;
64 			} else {
65 				ADD_DUP_SIZE(p->arKey, p->nKeyLength);
66 			}
67 		}
68 #else
69 		ADD_DUP_SIZE(p, sizeof(Bucket) - 1 + p->nKeyLength);
70 #endif
71 
72 		/* persist data pointer in bucket */
73 		if (!p->pDataPtr) {
74 			ADD_DUP_SIZE(p->pData, el_size);
75 		}
76 
77 		/* persist the data itself */
78 		if (pPersistElement) {
79 			ADD_SIZE(pPersistElement(p->pData TSRMLS_CC));
80 		}
81 
82 		p = p->pListNext;
83 	}
84 
85 #if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO
86 	if (ht->nTableMask) {
87 		ADD_DUP_SIZE(ht->arBuckets, sizeof(Bucket*) * ht->nTableSize);
88 	}
89 #else
90 	ADD_DUP_SIZE(ht->arBuckets, sizeof(Bucket*) * ht->nTableSize);
91 #endif
92 
93 	RETURN_SIZE();
94 }
95 
zend_persist_zval_calc(zval * z TSRMLS_DC)96 static uint zend_persist_zval_calc(zval *z TSRMLS_DC)
97 {
98 	START_SIZE();
99 
100 #if ZEND_EXTENSION_API_NO >= PHP_5_3_X_API_NO
101 	switch (z->type & IS_CONSTANT_TYPE_MASK) {
102 #else
103 	switch (z->type & ~IS_CONSTANT_INDEX) {
104 #endif
105 		case IS_STRING:
106 		case IS_CONSTANT:
107 			ADD_INTERNED_STRING(Z_STRVAL_P(z), Z_STRLEN_P(z) + 1);
108 			break;
109 		case IS_ARRAY:
110 		case IS_CONSTANT_ARRAY:
111 			ADD_DUP_SIZE(z->value.ht, sizeof(HashTable));
112 			ADD_SIZE(zend_hash_persist_calc(z->value.ht, (int (*)(void* TSRMLS_DC)) zend_persist_zval_ptr_calc, sizeof(zval**) TSRMLS_CC));
113 			break;
114 	}
115 	RETURN_SIZE();
116 }
117 
118 static uint zend_persist_zval_ptr_calc(zval **zp TSRMLS_DC)
119 {
120 	START_SIZE();
121 	zval *new_ptr = zend_shared_alloc_get_xlat_entry(*zp);
122 
123 	if (!new_ptr) {
124 		ADD_DUP_SIZE(*zp, sizeof(zval));
125 		ADD_SIZE(zend_persist_zval_calc(*zp TSRMLS_CC));
126 	}
127 	RETURN_SIZE();
128 }
129 
130 static uint zend_persist_op_array_calc(zend_op_array *op_array TSRMLS_DC)
131 {
132 	START_SIZE();
133 
134 	if (op_array->type != ZEND_USER_FUNCTION) {
135 		return 0;
136 	}
137 
138 	if (op_array->filename) {
139 		ADD_DUP_SIZE(op_array->filename, strlen(op_array->filename) + 1);
140 	}
141 
142 #if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO
143 	if (op_array->literals && !zend_shared_alloc_get_xlat_entry(op_array->literals)) {
144 		zend_literal *p = op_array->literals;
145 		zend_literal *end = p + op_array->last_literal;
146 		ADD_DUP_SIZE(op_array->literals, sizeof(zend_literal) * op_array->last_literal);
147 		while (p < end) {
148 			ADD_SIZE(zend_persist_zval_calc(&p->constant TSRMLS_CC));
149 			p++;
150 		}
151 	}
152 #endif
153 
154 	if (!zend_shared_alloc_get_xlat_entry(op_array->opcodes)) {
155 #if ZEND_EXTENSION_API_NO <= PHP_5_3_X_API_NO
156 		zend_op *opline = op_array->opcodes;
157 		zend_op *end = op_array->opcodes + op_array->last;
158 
159 		ADD_DUP_SIZE(op_array->opcodes, sizeof(zend_op) * op_array->last);
160 		while (opline<end) {
161 			if (opline->op1.op_type == IS_CONST) {
162 				ADD_SIZE(zend_persist_zval_calc(&opline->op1.u.constant TSRMLS_CC));
163 			}
164 			if (opline->op2.op_type == IS_CONST) {
165 				ADD_SIZE(zend_persist_zval_calc(&opline->op2.u.constant TSRMLS_CC));
166 			}
167 			opline++;
168 		}
169 #else
170 		ADD_DUP_SIZE(op_array->opcodes, sizeof(zend_op) * op_array->last);
171 #endif
172 	}
173 
174 	if (op_array->function_name) {
175 		ADD_DUP_SIZE(op_array->function_name, strlen(op_array->function_name) + 1);
176     }
177 
178 	if (op_array->arg_info &&
179 		!zend_shared_alloc_get_xlat_entry(op_array->arg_info)) {
180 		zend_uint i;
181 
182 		ADD_DUP_SIZE(op_array->arg_info, sizeof(zend_arg_info) * op_array->num_args);
183 		for (i = 0; i < op_array->num_args; i++) {
184 			if (op_array->arg_info[i].name) {
185 				ADD_INTERNED_STRING(op_array->arg_info[i].name, op_array->arg_info[i].name_len + 1);
186 			}
187 			if (op_array->arg_info[i].class_name) {
188 				ADD_INTERNED_STRING(op_array->arg_info[i].class_name, op_array->arg_info[i].class_name_len + 1);
189 			}
190 
191 		}
192 	}
193 
194 	if (op_array->brk_cont_array) {
195 		ADD_DUP_SIZE(op_array->brk_cont_array, sizeof(zend_brk_cont_element) * op_array->last_brk_cont);
196 	}
197 
198 	if (op_array->static_variables) {
199 		ADD_DUP_SIZE(op_array->static_variables, sizeof(HashTable));
200 		ADD_SIZE(zend_hash_persist_calc(op_array->static_variables, (int (*)(void* TSRMLS_DC)) zend_persist_zval_ptr_calc, sizeof(zval**) TSRMLS_CC));
201 	}
202 
203 	if (ZCG(accel_directives).save_comments && op_array->doc_comment) {
204 		ADD_DUP_SIZE(op_array->doc_comment, op_array->doc_comment_len + 1);
205 	}
206 
207 	if (op_array->try_catch_array) {
208 		ADD_DUP_SIZE(op_array->try_catch_array, sizeof(zend_try_catch_element) * op_array->last_try_catch);
209 	}
210 
211 	if (op_array->vars && !zend_shared_alloc_get_xlat_entry(op_array->vars)) {
212 		int i;
213 
214 		ADD_DUP_SIZE(op_array->vars, sizeof(zend_compiled_variable) * op_array->last_var);
215 		for (i = 0; i < op_array->last_var; i++) {
216 			ADD_INTERNED_STRING(op_array->vars[i].name, op_array->vars[i].name_len + 1);
217 		}
218 	}
219 
220 	RETURN_SIZE();
221 }
222 
223 static uint zend_persist_property_info_calc(zend_property_info *prop TSRMLS_DC)
224 {
225 	START_SIZE();
226 	ADD_INTERNED_STRING(prop->name, prop->name_length + 1);
227 	if (ZCG(accel_directives).save_comments && prop->doc_comment) {
228 		ADD_DUP_SIZE(prop->doc_comment, prop->doc_comment_len + 1);
229 	}
230 	RETURN_SIZE();
231 }
232 
233 static uint zend_persist_class_entry_calc(zend_class_entry **pce TSRMLS_DC)
234 {
235 	zend_class_entry *ce = *pce;
236 	START_SIZE();
237 
238 	if (ce->type == ZEND_USER_CLASS) {
239 		ADD_DUP_SIZE(ce, sizeof(zend_class_entry));
240 		ADD_INTERNED_STRING(ce->name, ce->name_length + 1);
241 		ADD_SIZE(zend_hash_persist_calc(&ce->function_table, (int (*)(void* TSRMLS_DC)) zend_persist_op_array_calc, sizeof(zend_op_array) TSRMLS_CC));
242 #if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO
243 		if (ce->default_properties_table) {
244 		    int i;
245 
246 			ADD_SIZE(sizeof(zval*) * ce->default_properties_count);
247 			for (i = 0; i < ce->default_properties_count; i++) {
248 				if (ce->default_properties_table[i]) {
249 					ADD_SIZE(zend_persist_zval_ptr_calc(&ce->default_properties_table[i] TSRMLS_CC));
250 				}
251 			}
252 		}
253 		if (ce->default_static_members_table) {
254 		    int i;
255 
256 			ADD_SIZE(sizeof(zval*) * ce->default_static_members_count);
257 			for (i = 0; i < ce->default_static_members_count; i++) {
258 				if (ce->default_static_members_table[i]) {
259 					ADD_SIZE(zend_persist_zval_ptr_calc(&ce->default_static_members_table[i] TSRMLS_CC));
260 				}
261 			}
262 		}
263 #else
264 		ADD_SIZE(zend_hash_persist_calc(&ce->default_properties, (int (*)(void* TSRMLS_DC)) zend_persist_zval_ptr_calc, sizeof(zval**) TSRMLS_CC));
265 		ADD_SIZE(zend_hash_persist_calc(&ce->default_static_members, (int (*)(void* TSRMLS_DC)) zend_persist_zval_ptr_calc, sizeof(zval**) TSRMLS_CC));
266 #endif
267 		ADD_SIZE(zend_hash_persist_calc(&ce->constants_table, (int (*)(void* TSRMLS_DC)) zend_persist_zval_ptr_calc, sizeof(zval**) TSRMLS_CC));
268 
269 		if (ZEND_CE_FILENAME(ce)) {
270 			ADD_DUP_SIZE(ZEND_CE_FILENAME(ce), strlen(ZEND_CE_FILENAME(ce)) + 1);
271 		}
272 		if (ZCG(accel_directives).save_comments && ZEND_CE_DOC_COMMENT(ce)) {
273 			ADD_DUP_SIZE(ZEND_CE_DOC_COMMENT(ce), ZEND_CE_DOC_COMMENT_LEN(ce) + 1);
274 		}
275 
276 		ADD_SIZE(zend_hash_persist_calc(&ce->properties_info, (int (*)(void* TSRMLS_DC)) zend_persist_property_info_calc, sizeof(zend_property_info) TSRMLS_CC));
277 
278 #if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO
279 		if (ce->trait_aliases) {
280 			int i = 0;
281 			while (ce->trait_aliases[i]) {
282 				if (ce->trait_aliases[i]->trait_method) {
283 					if (ce->trait_aliases[i]->trait_method->method_name) {
284 						ADD_SIZE(ce->trait_aliases[i]->trait_method->mname_len + 1);
285 					}
286 					if (ce->trait_aliases[i]->trait_method->class_name) {
287 						ADD_SIZE(ce->trait_aliases[i]->trait_method->cname_len + 1);
288 					}
289 					ADD_SIZE(sizeof(zend_trait_method_reference));
290 				}
291 
292 				if (ce->trait_aliases[i]->alias) {
293 					ADD_SIZE(ce->trait_aliases[i]->alias_len + 1);
294 				}
295 				ADD_SIZE(sizeof(zend_trait_alias));
296 				i++;
297 			}
298 			ADD_SIZE(sizeof(zend_trait_alias*) * (i + 1));
299 		}
300 
301 		if (ce->trait_precedences) {
302 			int i = 0;
303 
304 			while (ce->trait_precedences[i]) {
305 				ADD_SIZE(ce->trait_precedences[i]->trait_method->mname_len + 1);
306 				ADD_SIZE(ce->trait_precedences[i]->trait_method->cname_len + 1);
307 				ADD_SIZE(sizeof(zend_trait_method_reference));
308 
309 				if (ce->trait_precedences[i]->exclude_from_classes) {
310 					int j = 0;
311 
312 					while (ce->trait_precedences[i]->exclude_from_classes[j]) {
313 						ADD_SIZE(strlen((char*)ce->trait_precedences[i]->exclude_from_classes[j]) + 1);
314 						j++;
315 					}
316 					ADD_SIZE(sizeof(zend_class_entry*) * (j + 1));
317 				}
318 				ADD_SIZE(sizeof(zend_trait_precedence));
319 				i++;
320 			}
321 			ADD_SIZE(sizeof(zend_trait_precedence*) * (i + 1));
322 		}
323 #endif
324 	}
325 	RETURN_SIZE();
326 }
327 
328 static uint zend_accel_persist_class_table_calc(HashTable *class_table TSRMLS_DC)
329 {
330 	return zend_hash_persist_calc(class_table, (int (*)(void* TSRMLS_DC)) zend_persist_class_entry_calc, sizeof(zend_class_entry*) TSRMLS_CC);
331 }
332 
333 uint zend_accel_script_persist_calc(zend_persistent_script *new_persistent_script, char *key, unsigned int key_length TSRMLS_DC)
334 {
335 	START_SIZE();
336 
337 	ADD_SIZE(zend_hash_persist_calc(&new_persistent_script->function_table, (int (*)(void* TSRMLS_DC)) zend_persist_op_array_calc, sizeof(zend_op_array) TSRMLS_CC));
338 	ADD_SIZE(zend_accel_persist_class_table_calc(&new_persistent_script->class_table TSRMLS_CC));
339 	ADD_SIZE(zend_persist_op_array_calc(&new_persistent_script->main_op_array TSRMLS_CC));
340 	ADD_DUP_SIZE(key, key_length + 1);
341 	ADD_DUP_SIZE(new_persistent_script->full_path, new_persistent_script->full_path_len + 1);
342 	ADD_DUP_SIZE(new_persistent_script, sizeof(zend_persistent_script));
343 
344 	RETURN_SIZE();
345 }
346