Skip to content

Commit cec0911

Browse files
committed
Replace zend_hash_apply... with ZEND_HASH_FOREACH...
1 parent f67f421 commit cec0911

File tree

16 files changed

+274
-357
lines changed

16 files changed

+274
-357
lines changed

Zend/zend_API.c

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2626,28 +2626,22 @@ ZEND_API void zend_activate_modules(void) /* {{{ */
26262626
}
26272627
/* }}} */
26282628

2629-
/* call request shutdown for all modules */
2630-
static int module_registry_cleanup(zval *zv) /* {{{ */
2631-
{
2632-
zend_module_entry *module = Z_PTR_P(zv);
2633-
2634-
if (module->request_shutdown_func) {
2635-
#if 0
2636-
zend_printf("%s: Request shutdown\n", module->name);
2637-
#endif
2638-
module->request_shutdown_func(module->type, module->module_number);
2639-
}
2640-
return 0;
2641-
}
2642-
/* }}} */
2643-
26442629
ZEND_API void zend_deactivate_modules(void) /* {{{ */
26452630
{
26462631
EG(current_execute_data) = NULL; /* we're no longer executing anything */
26472632

26482633
zend_try {
26492634
if (EG(full_tables_cleanup)) {
2650-
zend_hash_reverse_apply(&module_registry, module_registry_cleanup);
2635+
zend_module_entry *module;
2636+
2637+
ZEND_HASH_REVERSE_FOREACH_PTR(&module_registry, module) {
2638+
if (module->request_shutdown_func) {
2639+
#if 0
2640+
zend_printf("%s: Request shutdown\n", module->name);
2641+
#endif
2642+
module->request_shutdown_func(module->type, module->module_number);
2643+
}
2644+
} ZEND_HASH_FOREACH_END();
26512645
} else {
26522646
zend_module_entry **p = module_request_shutdown_handlers;
26532647

@@ -2673,34 +2667,21 @@ ZEND_API void zend_cleanup_internal_classes(void) /* {{{ */
26732667
}
26742668
/* }}} */
26752669

2676-
int module_registry_unload_temp(const zend_module_entry *module) /* {{{ */
2677-
{
2678-
return (module->type == MODULE_TEMPORARY) ? ZEND_HASH_APPLY_REMOVE : ZEND_HASH_APPLY_STOP;
2679-
}
2680-
/* }}} */
2681-
2682-
static int module_registry_unload_temp_wrapper(zval *el) /* {{{ */
2683-
{
2684-
zend_module_entry *module = (zend_module_entry *)Z_PTR_P(el);
2685-
return module_registry_unload_temp((const zend_module_entry *)module);
2686-
}
2687-
/* }}} */
2688-
2689-
static int exec_done_cb(zval *el) /* {{{ */
2690-
{
2691-
zend_module_entry *module = (zend_module_entry *)Z_PTR_P(el);
2692-
if (module->post_deactivate_func) {
2693-
module->post_deactivate_func();
2694-
}
2695-
return 0;
2696-
}
2697-
/* }}} */
2698-
26992670
ZEND_API void zend_post_deactivate_modules(void) /* {{{ */
27002671
{
27012672
if (EG(full_tables_cleanup)) {
2702-
zend_hash_apply(&module_registry, exec_done_cb);
2703-
zend_hash_reverse_apply(&module_registry, module_registry_unload_temp_wrapper);
2673+
zend_module_entry *module;
2674+
2675+
ZEND_HASH_FOREACH_PTR(&module_registry, module) {
2676+
if (module->post_deactivate_func) {
2677+
module->post_deactivate_func();
2678+
}
2679+
} ZEND_HASH_FOREACH_END();
2680+
ZEND_HASH_REVERSE_FOREACH_PTR(&module_registry, module) {
2681+
if (module->type != MODULE_TEMPORARY) {
2682+
break;
2683+
}
2684+
} ZEND_HASH_FOREACH_END_DEL();
27042685
} else {
27052686
zend_module_entry **p = module_post_deactivate_handlers;
27062687

Zend/zend_builtin_functions.c

Lines changed: 69 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,101 +1759,79 @@ ZEND_FUNCTION(restore_exception_handler)
17591759
}
17601760
/* }}} */
17611761

1762-
static int copy_class_or_interface_name(zval *el, int num_args, va_list args, zend_hash_key *hash_key) /* {{{ */
1763-
{
1764-
zend_class_entry *ce = (zend_class_entry *)Z_PTR_P(el);
1765-
zval *array = va_arg(args, zval *);
1766-
uint32_t mask = va_arg(args, uint32_t);
1767-
uint32_t comply = va_arg(args, uint32_t);
1768-
uint32_t comply_mask = (comply)? mask:0;
1769-
1770-
if ((hash_key->key && ZSTR_VAL(hash_key->key)[0] != 0)
1771-
&& (comply_mask == (ce->ce_flags & mask))) {
1772-
if ((ce->refcount > 1 || (ce->ce_flags & ZEND_ACC_IMMUTABLE)) &&
1773-
!same_name(hash_key->key, ce->name)) {
1774-
add_next_index_str(array, zend_string_copy(hash_key->key));
1775-
} else {
1776-
add_next_index_str(array, zend_string_copy(ce->name));
1777-
}
1762+
static void copy_class_or_interface_name(zval *array, zend_string *key, zend_class_entry *ce) /* {{{ */
1763+
{
1764+
if ((ce->refcount == 1 && !(ce->ce_flags & ZEND_ACC_IMMUTABLE)) ||
1765+
same_name(key, ce->name)) {
1766+
key = ce->name;
17781767
}
1779-
return ZEND_HASH_APPLY_KEEP;
1768+
add_next_index_str(array, zend_string_copy(key));
17801769
}
17811770
/* }}} */
17821771

17831772
/* {{{ proto array get_declared_traits()
17841773
Returns an array of all declared traits. */
17851774
ZEND_FUNCTION(get_declared_traits)
17861775
{
1787-
uint32_t mask = ZEND_ACC_TRAIT;
1788-
uint32_t comply = 1;
1776+
zend_string *key;
1777+
zend_class_entry *ce;
17891778

17901779
if (zend_parse_parameters_none() == FAILURE) {
17911780
return;
17921781
}
17931782

17941783
array_init(return_value);
1795-
zend_hash_apply_with_arguments(EG(class_table), copy_class_or_interface_name, 3, return_value, mask, comply);
1784+
ZEND_HASH_FOREACH_STR_KEY_PTR(EG(class_table), key, ce) {
1785+
if (key
1786+
&& ZSTR_VAL(key)[0] != 0
1787+
&& (ce->ce_flags & ZEND_ACC_TRAIT)) {
1788+
copy_class_or_interface_name(return_value, key, ce);
1789+
}
1790+
} ZEND_HASH_FOREACH_END();
17961791
}
17971792
/* }}} */
17981793

17991794
/* {{{ proto array get_declared_classes()
18001795
Returns an array of all declared classes. */
18011796
ZEND_FUNCTION(get_declared_classes)
18021797
{
1803-
uint32_t mask = ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT;
1804-
uint32_t comply = 0;
1798+
zend_string *key;
1799+
zend_class_entry *ce;
18051800

18061801
if (zend_parse_parameters_none() == FAILURE) {
18071802
return;
18081803
}
18091804

18101805
array_init(return_value);
1811-
zend_hash_apply_with_arguments(EG(class_table), copy_class_or_interface_name, 3, return_value, mask, comply);
1806+
ZEND_HASH_FOREACH_STR_KEY_PTR(EG(class_table), key, ce) {
1807+
if (key
1808+
&& ZSTR_VAL(key)[0] != 0
1809+
&& !(ce->ce_flags & (ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT))) {
1810+
copy_class_or_interface_name(return_value, key, ce);
1811+
}
1812+
} ZEND_HASH_FOREACH_END();
18121813
}
18131814
/* }}} */
18141815

18151816
/* {{{ proto array get_declared_interfaces()
18161817
Returns an array of all declared interfaces. */
18171818
ZEND_FUNCTION(get_declared_interfaces)
18181819
{
1819-
uint32_t mask = ZEND_ACC_INTERFACE;
1820-
uint32_t comply = 1;
1820+
zend_string *key;
1821+
zend_class_entry *ce;
18211822

18221823
if (zend_parse_parameters_none() == FAILURE) {
18231824
return;
18241825
}
18251826

18261827
array_init(return_value);
1827-
zend_hash_apply_with_arguments(EG(class_table), copy_class_or_interface_name, 3, return_value, mask, comply);
1828-
}
1829-
/* }}} */
1830-
1831-
static int copy_function_name(zval *zv, int num_args, va_list args, zend_hash_key *hash_key) /* {{{ */
1832-
{
1833-
zend_function *func = Z_PTR_P(zv);
1834-
zval *internal_ar = va_arg(args, zval *),
1835-
*user_ar = va_arg(args, zval *);
1836-
zend_bool *exclude_disabled = va_arg(args, zend_bool *);
1837-
1838-
if (hash_key->key == NULL || ZSTR_VAL(hash_key->key)[0] == 0) {
1839-
return 0;
1840-
}
1841-
1842-
if (func->type == ZEND_INTERNAL_FUNCTION) {
1843-
char *disable_functions = INI_STR("disable_functions");
1844-
1845-
if ((*exclude_disabled == 1) && (disable_functions != NULL)) {
1846-
if (strstr(disable_functions, func->common.function_name->val) == NULL) {
1847-
add_next_index_str(internal_ar, zend_string_copy(hash_key->key));
1848-
}
1849-
} else {
1850-
add_next_index_str(internal_ar, zend_string_copy(hash_key->key));
1828+
ZEND_HASH_FOREACH_STR_KEY_PTR(EG(class_table), key, ce) {
1829+
if (key
1830+
&& ZSTR_VAL(key)[0] != 0
1831+
&& (ce->ce_flags & ZEND_ACC_INTERFACE)) {
1832+
copy_class_or_interface_name(return_value, key, ce);
18511833
}
1852-
} else if (func->type == ZEND_USER_FUNCTION) {
1853-
add_next_index_str(user_ar, zend_string_copy(hash_key->key));
1854-
}
1855-
1856-
return 0;
1834+
} ZEND_HASH_FOREACH_END();
18571835
}
18581836
/* }}} */
18591837

@@ -1862,7 +1840,10 @@ static int copy_function_name(zval *zv, int num_args, va_list args, zend_hash_ke
18621840
ZEND_FUNCTION(get_defined_functions)
18631841
{
18641842
zval internal, user;
1843+
zend_string *key;
1844+
zend_function *func;
18651845
zend_bool exclude_disabled = 0;
1846+
char *disable_functions = NULL;
18661847

18671848
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &exclude_disabled) == FAILURE) {
18681849
return;
@@ -1872,7 +1853,24 @@ ZEND_FUNCTION(get_defined_functions)
18721853
array_init(&user);
18731854
array_init(return_value);
18741855

1875-
zend_hash_apply_with_arguments(EG(function_table), copy_function_name, 3, &internal, &user, &exclude_disabled);
1856+
if (exclude_disabled) {
1857+
disable_functions = INI_STR("disable_functions");
1858+
}
1859+
ZEND_HASH_FOREACH_STR_KEY_PTR(EG(function_table), key, func) {
1860+
if (key && ZSTR_VAL(key)[0] != 0) {
1861+
if (func->type == ZEND_INTERNAL_FUNCTION) {
1862+
if (disable_functions != NULL) {
1863+
if (strstr(disable_functions, func->common.function_name->val) == NULL) {
1864+
add_next_index_str(&internal, zend_string_copy(key));
1865+
}
1866+
} else {
1867+
add_next_index_str(&internal, zend_string_copy(key));
1868+
}
1869+
} else if (func->type == ZEND_USER_FUNCTION) {
1870+
add_next_index_str(&user, zend_string_copy(key));
1871+
}
1872+
}
1873+
} ZEND_HASH_FOREACH_END();
18761874

18771875
zend_hash_str_add_new(Z_ARRVAL_P(return_value), "internal", sizeof("internal")-1, &internal);
18781876
zend_hash_str_add_new(Z_ARRVAL_P(return_value), "user", sizeof("user")-1, &user);
@@ -2044,15 +2042,6 @@ ZEND_FUNCTION(get_resources)
20442042
}
20452043
/* }}} */
20462044

2047-
static int add_extension_info(zval *item, void *arg) /* {{{ */
2048-
{
2049-
zval *name_array = (zval *)arg;
2050-
zend_module_entry *module = (zend_module_entry*)Z_PTR_P(item);
2051-
add_next_index_string(name_array, module->name);
2052-
return 0;
2053-
}
2054-
/* }}} */
2055-
20562045
static int add_zendext_info(zend_extension *ext, void *arg) /* {{{ */
20572046
{
20582047
zval *name_array = (zval *)arg;
@@ -2061,23 +2050,6 @@ static int add_zendext_info(zend_extension *ext, void *arg) /* {{{ */
20612050
}
20622051
/* }}} */
20632052

2064-
static int add_constant_info(zval *item, void *arg) /* {{{ */
2065-
{
2066-
zval *name_array = (zval *)arg;
2067-
zend_constant *constant = (zend_constant*)Z_PTR_P(item);
2068-
zval const_val;
2069-
2070-
if (!constant->name) {
2071-
/* skip special constants */
2072-
return 0;
2073-
}
2074-
2075-
ZVAL_COPY_OR_DUP(&const_val, &constant->value);
2076-
zend_hash_add_new(Z_ARRVAL_P(name_array), constant->name, &const_val);
2077-
return 0;
2078-
}
2079-
/* }}} */
2080-
20812053
/* {{{ proto array get_loaded_extensions([bool zend_extensions])
20822054
Return an array containing names of loaded extensions */
20832055
ZEND_FUNCTION(get_loaded_extensions)
@@ -2093,7 +2065,11 @@ ZEND_FUNCTION(get_loaded_extensions)
20932065
if (zendext) {
20942066
zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t)add_zendext_info, return_value);
20952067
} else {
2096-
zend_hash_apply_with_argument(&module_registry, add_extension_info, return_value);
2068+
zend_module_entry *module;
2069+
2070+
ZEND_HASH_FOREACH_PTR(&module_registry, module) {
2071+
add_next_index_string(return_value, module->name);
2072+
} ZEND_HASH_FOREACH_END();
20972073
}
20982074
}
20992075
/* }}} */
@@ -2155,7 +2131,17 @@ ZEND_FUNCTION(get_defined_constants)
21552131
efree(module_names);
21562132
efree(modules);
21572133
} else {
2158-
zend_hash_apply_with_argument(EG(zend_constants), add_constant_info, return_value);
2134+
zend_constant *constant;
2135+
zval const_val;
2136+
2137+
ZEND_HASH_FOREACH_PTR(EG(zend_constants), constant) {
2138+
if (!constant->name) {
2139+
/* skip special constants */
2140+
continue;
2141+
}
2142+
ZVAL_COPY_OR_DUP(&const_val, &constant->value);
2143+
zend_hash_add_new(Z_ARRVAL_P(return_value), constant->name, &const_val);
2144+
} ZEND_HASH_FOREACH_END();
21592145
}
21602146
}
21612147
/* }}} */

Zend/zend_inheritance.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,7 @@ static void zend_fixup_trait_method(zend_function *fn, zend_class_entry *ce) /*
13331333
}
13341334
/* }}} */
13351335

1336-
static int zend_traits_copy_functions(zend_string *fnname, zend_function *fn, zend_class_entry *ce, HashTable **overridden, HashTable *exclude_table, zend_class_entry **aliases) /* {{{ */
1336+
static void zend_traits_copy_functions(zend_string *fnname, zend_function *fn, zend_class_entry *ce, HashTable **overridden, HashTable *exclude_table, zend_class_entry **aliases) /* {{{ */
13371337
{
13381338
zend_trait_alias *alias, **alias_ptr;
13391339
zend_string *lcname;
@@ -1413,8 +1413,6 @@ static int zend_traits_copy_functions(zend_string *fnname, zend_function *fn, ze
14131413

14141414
zend_add_trait_method(ce, ZSTR_VAL(fn->common.function_name), fnname, &fn_copy, overridden);
14151415
}
1416-
1417-
return ZEND_HASH_APPLY_KEEP;
14181416
}
14191417
/* }}} */
14201418

0 commit comments

Comments
 (0)