zend_persist_calc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend OPcache |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 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. | https://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@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. | Stanislav Malyshev <stas@zend.com> |
  18. | Dmitry Stogov <dmitry@php.net> |
  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. #include "zend_attributes.h"
  28. #define ADD_DUP_SIZE(m,s) ZCG(current_persistent_script)->size += zend_shared_memdup_size((void*)m, s)
  29. #define ADD_SIZE(m) ZCG(current_persistent_script)->size += ZEND_ALIGNED_SIZE(m)
  30. # define ADD_STRING(str) ADD_DUP_SIZE((str), _ZSTR_STRUCT_SIZE(ZSTR_LEN(str)))
  31. # define ADD_INTERNED_STRING(str) do { \
  32. if (ZCG(current_persistent_script)->corrupted) { \
  33. ADD_STRING(str); \
  34. } else if (!IS_ACCEL_INTERNED(str)) { \
  35. zend_string *tmp = accel_new_interned_string(str); \
  36. if (tmp != (str)) { \
  37. (str) = tmp; \
  38. } else { \
  39. ADD_STRING(str); \
  40. } \
  41. } \
  42. } while (0)
  43. static void zend_persist_zval_calc(zval *z);
  44. static void zend_persist_op_array_calc(zval *zv);
  45. static void zend_hash_persist_calc(HashTable *ht)
  46. {
  47. if ((HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED) || ht->nNumUsed == 0) {
  48. return;
  49. }
  50. if (!(HT_FLAGS(ht) & HASH_FLAG_PACKED) && ht->nNumUsed > HT_MIN_SIZE && ht->nNumUsed < (uint32_t)(-(int32_t)ht->nTableMask) / 4) {
  51. /* compact table */
  52. uint32_t hash_size;
  53. hash_size = (uint32_t)(-(int32_t)ht->nTableMask);
  54. while (hash_size >> 2 > ht->nNumUsed) {
  55. hash_size >>= 1;
  56. }
  57. ADD_SIZE(hash_size * sizeof(uint32_t) + ht->nNumUsed * sizeof(Bucket));
  58. } else {
  59. ADD_SIZE(HT_USED_SIZE(ht));
  60. }
  61. }
  62. static void zend_persist_ast_calc(zend_ast *ast)
  63. {
  64. uint32_t i;
  65. if (ast->kind == ZEND_AST_ZVAL || ast->kind == ZEND_AST_CONSTANT) {
  66. ADD_SIZE(sizeof(zend_ast_zval));
  67. zend_persist_zval_calc(&((zend_ast_zval*)(ast))->val);
  68. } else if (zend_ast_is_list(ast)) {
  69. zend_ast_list *list = zend_ast_get_list(ast);
  70. ADD_SIZE(sizeof(zend_ast_list) - sizeof(zend_ast *) + sizeof(zend_ast *) * list->children);
  71. for (i = 0; i < list->children; i++) {
  72. if (list->child[i]) {
  73. zend_persist_ast_calc(list->child[i]);
  74. }
  75. }
  76. } else {
  77. uint32_t children = zend_ast_get_num_children(ast);
  78. ADD_SIZE(sizeof(zend_ast) - sizeof(zend_ast *) + sizeof(zend_ast *) * children);
  79. for (i = 0; i < children; i++) {
  80. if (ast->child[i]) {
  81. zend_persist_ast_calc(ast->child[i]);
  82. }
  83. }
  84. }
  85. }
  86. static void zend_persist_zval_calc(zval *z)
  87. {
  88. uint32_t size;
  89. switch (Z_TYPE_P(z)) {
  90. case IS_STRING:
  91. ADD_INTERNED_STRING(Z_STR_P(z));
  92. if (ZSTR_IS_INTERNED(Z_STR_P(z))) {
  93. Z_TYPE_FLAGS_P(z) = 0;
  94. }
  95. break;
  96. case IS_ARRAY:
  97. if (!ZCG(current_persistent_script)->corrupted
  98. && zend_accel_in_shm(Z_ARR_P(z))) {
  99. return;
  100. }
  101. size = zend_shared_memdup_size(Z_ARR_P(z), sizeof(zend_array));
  102. if (size) {
  103. Bucket *p;
  104. ADD_SIZE(size);
  105. zend_hash_persist_calc(Z_ARRVAL_P(z));
  106. ZEND_HASH_FOREACH_BUCKET(Z_ARRVAL_P(z), p) {
  107. if (p->key) {
  108. ADD_INTERNED_STRING(p->key);
  109. }
  110. zend_persist_zval_calc(&p->val);
  111. } ZEND_HASH_FOREACH_END();
  112. }
  113. break;
  114. case IS_CONSTANT_AST:
  115. if (ZCG(current_persistent_script)->corrupted
  116. || !zend_accel_in_shm(Z_AST_P(z))) {
  117. size = zend_shared_memdup_size(Z_AST_P(z), sizeof(zend_ast_ref));
  118. if (size) {
  119. ADD_SIZE(size);
  120. zend_persist_ast_calc(Z_ASTVAL_P(z));
  121. }
  122. }
  123. break;
  124. default:
  125. ZEND_ASSERT(Z_TYPE_P(z) < IS_STRING);
  126. break;
  127. }
  128. }
  129. static void zend_persist_attributes_calc(HashTable *attributes)
  130. {
  131. if (!zend_shared_alloc_get_xlat_entry(attributes)
  132. && (ZCG(current_persistent_script)->corrupted
  133. || !zend_accel_in_shm(attributes))) {
  134. zend_attribute *attr;
  135. uint32_t i;
  136. zend_shared_alloc_register_xlat_entry(attributes, attributes);
  137. ADD_SIZE(sizeof(HashTable));
  138. zend_hash_persist_calc(attributes);
  139. ZEND_HASH_FOREACH_PTR(attributes, attr) {
  140. ADD_SIZE(ZEND_ATTRIBUTE_SIZE(attr->argc));
  141. ADD_INTERNED_STRING(attr->name);
  142. ADD_INTERNED_STRING(attr->lcname);
  143. for (i = 0; i < attr->argc; i++) {
  144. if (attr->args[i].name) {
  145. ADD_INTERNED_STRING(attr->args[i].name);
  146. }
  147. zend_persist_zval_calc(&attr->args[i].value);
  148. }
  149. } ZEND_HASH_FOREACH_END();
  150. }
  151. }
  152. static void zend_persist_type_calc(zend_type *type)
  153. {
  154. if (ZEND_TYPE_HAS_LIST(*type)) {
  155. ADD_SIZE(ZEND_TYPE_LIST_SIZE(ZEND_TYPE_LIST(*type)->num_types));
  156. }
  157. zend_type *single_type;
  158. ZEND_TYPE_FOREACH(*type, single_type) {
  159. if (ZEND_TYPE_HAS_NAME(*single_type)) {
  160. zend_string *type_name = ZEND_TYPE_NAME(*single_type);
  161. ADD_INTERNED_STRING(type_name);
  162. ZEND_TYPE_SET_PTR(*single_type, type_name);
  163. }
  164. } ZEND_TYPE_FOREACH_END();
  165. }
  166. static void zend_persist_op_array_calc_ex(zend_op_array *op_array)
  167. {
  168. if (op_array->function_name) {
  169. zend_string *old_name = op_array->function_name;
  170. ADD_INTERNED_STRING(op_array->function_name);
  171. /* Remember old function name, so it can be released multiple times if shared. */
  172. if (op_array->function_name != old_name
  173. && !zend_shared_alloc_get_xlat_entry(&op_array->function_name)) {
  174. zend_shared_alloc_register_xlat_entry(&op_array->function_name, old_name);
  175. }
  176. }
  177. if (op_array->scope) {
  178. if (zend_shared_alloc_get_xlat_entry(op_array->opcodes)) {
  179. /* already stored */
  180. ADD_SIZE(ZEND_ALIGNED_SIZE(zend_extensions_op_array_persist_calc(op_array)));
  181. return;
  182. }
  183. }
  184. if (op_array->scope
  185. && !(op_array->fn_flags & ZEND_ACC_CLOSURE)
  186. && (op_array->scope->ce_flags & ZEND_ACC_CACHED)) {
  187. return;
  188. }
  189. if (op_array->static_variables && !zend_accel_in_shm(op_array->static_variables)) {
  190. if (!zend_shared_alloc_get_xlat_entry(op_array->static_variables)) {
  191. Bucket *p;
  192. zend_shared_alloc_register_xlat_entry(op_array->static_variables, op_array->static_variables);
  193. ADD_SIZE(sizeof(HashTable));
  194. zend_hash_persist_calc(op_array->static_variables);
  195. ZEND_HASH_FOREACH_BUCKET(op_array->static_variables, p) {
  196. ZEND_ASSERT(p->key != NULL);
  197. ADD_INTERNED_STRING(p->key);
  198. zend_persist_zval_calc(&p->val);
  199. } ZEND_HASH_FOREACH_END();
  200. }
  201. }
  202. if (op_array->literals) {
  203. zval *p = op_array->literals;
  204. zval *end = p + op_array->last_literal;
  205. ADD_SIZE(sizeof(zval) * op_array->last_literal);
  206. while (p < end) {
  207. zend_persist_zval_calc(p);
  208. p++;
  209. }
  210. }
  211. zend_shared_alloc_register_xlat_entry(op_array->opcodes, op_array->opcodes);
  212. ADD_SIZE(sizeof(zend_op) * op_array->last);
  213. if (op_array->filename) {
  214. ADD_STRING(op_array->filename);
  215. }
  216. if (op_array->arg_info) {
  217. zend_arg_info *arg_info = op_array->arg_info;
  218. uint32_t num_args = op_array->num_args;
  219. uint32_t i;
  220. if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
  221. num_args++;
  222. }
  223. if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
  224. arg_info--;
  225. num_args++;
  226. }
  227. ADD_SIZE(sizeof(zend_arg_info) * num_args);
  228. for (i = 0; i < num_args; i++) {
  229. if (arg_info[i].name) {
  230. ADD_INTERNED_STRING(arg_info[i].name);
  231. }
  232. zend_persist_type_calc(&arg_info[i].type);
  233. }
  234. }
  235. if (op_array->live_range) {
  236. ADD_SIZE(sizeof(zend_live_range) * op_array->last_live_range);
  237. }
  238. if (ZCG(accel_directives).save_comments && op_array->doc_comment) {
  239. ADD_STRING(op_array->doc_comment);
  240. }
  241. if (op_array->attributes) {
  242. zend_persist_attributes_calc(op_array->attributes);
  243. }
  244. if (op_array->try_catch_array) {
  245. ADD_SIZE(sizeof(zend_try_catch_element) * op_array->last_try_catch);
  246. }
  247. if (op_array->vars) {
  248. int i;
  249. ADD_SIZE(sizeof(zend_string*) * op_array->last_var);
  250. for (i = 0; i < op_array->last_var; i++) {
  251. ADD_INTERNED_STRING(op_array->vars[i]);
  252. }
  253. }
  254. if (op_array->num_dynamic_func_defs) {
  255. ADD_SIZE(sizeof(void *) * op_array->num_dynamic_func_defs);
  256. for (uint32_t i = 0; i < op_array->num_dynamic_func_defs; i++) {
  257. zval tmp;
  258. ZVAL_PTR(&tmp, op_array->dynamic_func_defs[i]);
  259. zend_persist_op_array_calc(&tmp);
  260. }
  261. }
  262. ADD_SIZE(ZEND_ALIGNED_SIZE(zend_extensions_op_array_persist_calc(op_array)));
  263. }
  264. static void zend_persist_op_array_calc(zval *zv)
  265. {
  266. zend_op_array *op_array = Z_PTR_P(zv);
  267. ZEND_ASSERT(op_array->type == ZEND_USER_FUNCTION);
  268. if (!zend_shared_alloc_get_xlat_entry(op_array)) {
  269. zend_shared_alloc_register_xlat_entry(op_array, op_array);
  270. ADD_SIZE(sizeof(zend_op_array));
  271. zend_persist_op_array_calc_ex(op_array);
  272. } else {
  273. /* This can happen during preloading, if a dynamic function definition is declared. */
  274. }
  275. }
  276. static void zend_persist_class_method_calc(zval *zv)
  277. {
  278. zend_op_array *op_array = Z_PTR_P(zv);
  279. zend_op_array *old_op_array;
  280. if (op_array->type != ZEND_USER_FUNCTION) {
  281. ZEND_ASSERT(op_array->type == ZEND_INTERNAL_FUNCTION);
  282. if (op_array->fn_flags & ZEND_ACC_ARENA_ALLOCATED) {
  283. old_op_array = zend_shared_alloc_get_xlat_entry(op_array);
  284. if (!old_op_array) {
  285. ADD_SIZE(sizeof(zend_internal_function));
  286. zend_shared_alloc_register_xlat_entry(op_array, Z_PTR_P(zv));
  287. }
  288. }
  289. return;
  290. }
  291. if ((op_array->fn_flags & ZEND_ACC_IMMUTABLE)
  292. && !ZCG(current_persistent_script)->corrupted
  293. && zend_accel_in_shm(op_array)) {
  294. zend_shared_alloc_register_xlat_entry(op_array, op_array);
  295. return;
  296. }
  297. old_op_array = zend_shared_alloc_get_xlat_entry(op_array);
  298. if (!old_op_array) {
  299. ADD_SIZE(sizeof(zend_op_array));
  300. zend_persist_op_array_calc_ex(Z_PTR_P(zv));
  301. zend_shared_alloc_register_xlat_entry(op_array, Z_PTR_P(zv));
  302. } else {
  303. /* If op_array is shared, the function name refcount is still incremented for each use,
  304. * so we need to release it here. We remembered the original function name in xlat. */
  305. zend_string *old_function_name =
  306. zend_shared_alloc_get_xlat_entry(&old_op_array->function_name);
  307. if (old_function_name) {
  308. zend_string_release_ex(old_function_name, 0);
  309. }
  310. }
  311. }
  312. static void zend_persist_property_info_calc(zend_property_info *prop)
  313. {
  314. ADD_SIZE(sizeof(zend_property_info));
  315. ADD_INTERNED_STRING(prop->name);
  316. zend_persist_type_calc(&prop->type);
  317. if (ZCG(accel_directives).save_comments && prop->doc_comment) {
  318. ADD_STRING(prop->doc_comment);
  319. }
  320. if (prop->attributes) {
  321. zend_persist_attributes_calc(prop->attributes);
  322. }
  323. }
  324. static void zend_persist_class_constant_calc(zval *zv)
  325. {
  326. zend_class_constant *c = Z_PTR_P(zv);
  327. if (!zend_shared_alloc_get_xlat_entry(c)) {
  328. if (!ZCG(current_persistent_script)->corrupted
  329. && zend_accel_in_shm(Z_PTR_P(zv))) {
  330. return;
  331. }
  332. zend_shared_alloc_register_xlat_entry(c, c);
  333. ADD_SIZE(sizeof(zend_class_constant));
  334. zend_persist_zval_calc(&c->value);
  335. if (ZCG(accel_directives).save_comments && c->doc_comment) {
  336. ADD_STRING(c->doc_comment);
  337. }
  338. if (c->attributes) {
  339. zend_persist_attributes_calc(c->attributes);
  340. }
  341. }
  342. }
  343. void zend_persist_class_entry_calc(zend_class_entry *ce)
  344. {
  345. Bucket *p;
  346. if (ce->type == ZEND_USER_CLASS) {
  347. /* The same zend_class_entry may be reused by class_alias */
  348. if (zend_shared_alloc_get_xlat_entry(ce)) {
  349. return;
  350. }
  351. zend_shared_alloc_register_xlat_entry(ce, ce);
  352. ADD_SIZE(sizeof(zend_class_entry));
  353. if (!(ce->ce_flags & ZEND_ACC_CACHED)) {
  354. ADD_INTERNED_STRING(ce->name);
  355. if (ce->parent_name && !(ce->ce_flags & ZEND_ACC_LINKED)) {
  356. ADD_INTERNED_STRING(ce->parent_name);
  357. }
  358. }
  359. zend_hash_persist_calc(&ce->function_table);
  360. ZEND_HASH_FOREACH_BUCKET(&ce->function_table, p) {
  361. ZEND_ASSERT(p->key != NULL);
  362. ADD_INTERNED_STRING(p->key);
  363. zend_persist_class_method_calc(&p->val);
  364. } ZEND_HASH_FOREACH_END();
  365. if (ce->default_properties_table) {
  366. int i;
  367. ADD_SIZE(sizeof(zval) * ce->default_properties_count);
  368. for (i = 0; i < ce->default_properties_count; i++) {
  369. zend_persist_zval_calc(&ce->default_properties_table[i]);
  370. }
  371. }
  372. if (ce->default_static_members_table) {
  373. int i;
  374. ADD_SIZE(sizeof(zval) * ce->default_static_members_count);
  375. for (i = 0; i < ce->default_static_members_count; i++) {
  376. if (Z_TYPE(ce->default_static_members_table[i]) != IS_INDIRECT) {
  377. zend_persist_zval_calc(&ce->default_static_members_table[i]);
  378. }
  379. }
  380. }
  381. zend_hash_persist_calc(&ce->constants_table);
  382. ZEND_HASH_FOREACH_BUCKET(&ce->constants_table, p) {
  383. ZEND_ASSERT(p->key != NULL);
  384. ADD_INTERNED_STRING(p->key);
  385. zend_persist_class_constant_calc(&p->val);
  386. } ZEND_HASH_FOREACH_END();
  387. zend_hash_persist_calc(&ce->properties_info);
  388. ZEND_HASH_FOREACH_BUCKET(&ce->properties_info, p) {
  389. zend_property_info *prop = Z_PTR(p->val);
  390. ZEND_ASSERT(p->key != NULL);
  391. ADD_INTERNED_STRING(p->key);
  392. if (prop->ce == ce) {
  393. zend_persist_property_info_calc(prop);
  394. }
  395. } ZEND_HASH_FOREACH_END();
  396. if (ce->properties_info_table) {
  397. ADD_SIZE(sizeof(zend_property_info *) * ce->default_properties_count);
  398. }
  399. if (ce->num_interfaces && (ce->ce_flags & ZEND_ACC_LINKED)) {
  400. ADD_SIZE(sizeof(zend_class_entry*) * ce->num_interfaces);
  401. }
  402. if (ce->iterator_funcs_ptr) {
  403. ADD_SIZE(sizeof(zend_class_iterator_funcs));
  404. }
  405. if (ce->ce_flags & ZEND_ACC_CACHED) {
  406. return;
  407. }
  408. if (ce->info.user.filename) {
  409. ADD_STRING(ce->info.user.filename);
  410. }
  411. if (ZCG(accel_directives).save_comments && ce->info.user.doc_comment) {
  412. ADD_STRING(ce->info.user.doc_comment);
  413. }
  414. if (ce->attributes) {
  415. zend_persist_attributes_calc(ce->attributes);
  416. }
  417. if (ce->num_interfaces) {
  418. uint32_t i;
  419. if (!(ce->ce_flags & ZEND_ACC_LINKED)) {
  420. for (i = 0; i < ce->num_interfaces; i++) {
  421. ADD_INTERNED_STRING(ce->interface_names[i].name);
  422. ADD_INTERNED_STRING(ce->interface_names[i].lc_name);
  423. }
  424. ADD_SIZE(sizeof(zend_class_name) * ce->num_interfaces);
  425. }
  426. }
  427. if (ce->num_traits) {
  428. uint32_t i;
  429. for (i = 0; i < ce->num_traits; i++) {
  430. ADD_INTERNED_STRING(ce->trait_names[i].name);
  431. ADD_INTERNED_STRING(ce->trait_names[i].lc_name);
  432. }
  433. ADD_SIZE(sizeof(zend_class_name) * ce->num_traits);
  434. if (ce->trait_aliases) {
  435. i = 0;
  436. while (ce->trait_aliases[i]) {
  437. if (ce->trait_aliases[i]->trait_method.method_name) {
  438. ADD_INTERNED_STRING(ce->trait_aliases[i]->trait_method.method_name);
  439. }
  440. if (ce->trait_aliases[i]->trait_method.class_name) {
  441. ADD_INTERNED_STRING(ce->trait_aliases[i]->trait_method.class_name);
  442. }
  443. if (ce->trait_aliases[i]->alias) {
  444. ADD_INTERNED_STRING(ce->trait_aliases[i]->alias);
  445. }
  446. ADD_SIZE(sizeof(zend_trait_alias));
  447. i++;
  448. }
  449. ADD_SIZE(sizeof(zend_trait_alias*) * (i + 1));
  450. }
  451. if (ce->trait_precedences) {
  452. int j;
  453. i = 0;
  454. while (ce->trait_precedences[i]) {
  455. ADD_INTERNED_STRING(ce->trait_precedences[i]->trait_method.method_name);
  456. ADD_INTERNED_STRING(ce->trait_precedences[i]->trait_method.class_name);
  457. for (j = 0; j < ce->trait_precedences[i]->num_excludes; j++) {
  458. ADD_INTERNED_STRING(ce->trait_precedences[i]->exclude_class_names[j]);
  459. }
  460. ADD_SIZE(sizeof(zend_trait_precedence) + (ce->trait_precedences[i]->num_excludes - 1) * sizeof(zend_string*));
  461. i++;
  462. }
  463. ADD_SIZE(sizeof(zend_trait_precedence*) * (i + 1));
  464. }
  465. }
  466. if (ce->backed_enum_table) {
  467. Bucket *p;
  468. ADD_SIZE(sizeof(HashTable));
  469. zend_hash_persist_calc(ce->backed_enum_table);
  470. ZEND_HASH_FOREACH_BUCKET(ce->backed_enum_table, p) {
  471. if (p->key != NULL) {
  472. ADD_INTERNED_STRING(p->key);
  473. }
  474. zend_persist_zval_calc(&p->val);
  475. } ZEND_HASH_FOREACH_END();
  476. }
  477. }
  478. }
  479. static void zend_accel_persist_class_table_calc(HashTable *class_table)
  480. {
  481. Bucket *p;
  482. zend_hash_persist_calc(class_table);
  483. ZEND_HASH_FOREACH_BUCKET(class_table, p) {
  484. ZEND_ASSERT(p->key != NULL);
  485. ADD_INTERNED_STRING(p->key);
  486. zend_persist_class_entry_calc(Z_CE(p->val));
  487. } ZEND_HASH_FOREACH_END();
  488. }
  489. void zend_persist_warnings_calc(uint32_t num_warnings, zend_error_info **warnings) {
  490. ADD_SIZE(num_warnings * sizeof(zend_error_info *));
  491. for (uint32_t i = 0; i < num_warnings; i++) {
  492. ADD_SIZE(sizeof(zend_error_info));
  493. ADD_STRING(warnings[i]->filename);
  494. ADD_STRING(warnings[i]->message);
  495. }
  496. }
  497. uint32_t zend_accel_script_persist_calc(zend_persistent_script *new_persistent_script, int for_shm)
  498. {
  499. Bucket *p;
  500. new_persistent_script->mem = NULL;
  501. new_persistent_script->size = 0;
  502. new_persistent_script->corrupted = 0;
  503. ZCG(current_persistent_script) = new_persistent_script;
  504. if (!for_shm) {
  505. /* script is not going to be saved in SHM */
  506. new_persistent_script->corrupted = 1;
  507. }
  508. ADD_SIZE(sizeof(zend_persistent_script));
  509. ADD_INTERNED_STRING(new_persistent_script->script.filename);
  510. #if defined(__AVX__) || defined(__SSE2__)
  511. /* Align size to 64-byte boundary */
  512. new_persistent_script->size = (new_persistent_script->size + 63) & ~63;
  513. #endif
  514. if (new_persistent_script->script.class_table.nNumUsed != new_persistent_script->script.class_table.nNumOfElements) {
  515. zend_hash_rehash(&new_persistent_script->script.class_table);
  516. }
  517. zend_accel_persist_class_table_calc(&new_persistent_script->script.class_table);
  518. if (new_persistent_script->script.function_table.nNumUsed != new_persistent_script->script.function_table.nNumOfElements) {
  519. zend_hash_rehash(&new_persistent_script->script.function_table);
  520. }
  521. zend_hash_persist_calc(&new_persistent_script->script.function_table);
  522. ZEND_HASH_FOREACH_BUCKET(&new_persistent_script->script.function_table, p) {
  523. ZEND_ASSERT(p->key != NULL);
  524. ADD_INTERNED_STRING(p->key);
  525. zend_persist_op_array_calc(&p->val);
  526. } ZEND_HASH_FOREACH_END();
  527. zend_persist_op_array_calc_ex(&new_persistent_script->script.main_op_array);
  528. zend_persist_warnings_calc(
  529. new_persistent_script->num_warnings, new_persistent_script->warnings);
  530. new_persistent_script->corrupted = 0;
  531. ZCG(current_persistent_script) = NULL;
  532. return new_persistent_script->size;
  533. }