zend_persist_calc.c 12 KB

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