Skip to content

Commit 9426c6e

Browse files
committed
Don't use global for array_walk_fci
There's really no good reason for this to be a global, we can easily pass it down to php_array_walk().
1 parent d1845ac commit 9426c6e

File tree

3 files changed

+26
-51
lines changed

3 files changed

+26
-51
lines changed

ext/standard/array.c

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,13 @@ PHP_FUNCTION(max)
13211321
}
13221322
/* }}} */
13231323

1324-
static int php_array_walk(zval *array, zval *userdata, int recursive) /* {{{ */
1324+
typedef struct {
1325+
zend_fcall_info fci;
1326+
zend_fcall_info_cache fci_cache;
1327+
} php_array_walk_context;
1328+
1329+
static int php_array_walk(
1330+
php_array_walk_context *context, zval *array, zval *userdata, int recursive)
13251331
{
13261332
zval args[3], /* Arguments to userland function */
13271333
retval, /* Return value - unused */
@@ -1331,15 +1337,19 @@ static int php_array_walk(zval *array, zval *userdata, int recursive) /* {{{ */
13311337
uint32_t ht_iter;
13321338
int result = SUCCESS;
13331339

1340+
/* Create a local copy of fci, as we want to use different arguments at different
1341+
* levels of recursion. */
1342+
zend_fcall_info fci = context->fci;
1343+
13341344
/* Set up known arguments */
13351345
ZVAL_UNDEF(&args[1]);
13361346
if (userdata) {
13371347
ZVAL_COPY(&args[2], userdata);
13381348
}
13391349

1340-
BG(array_walk_fci).retval = &retval;
1341-
BG(array_walk_fci).param_count = userdata ? 3 : 2;
1342-
BG(array_walk_fci).params = args;
1350+
fci.retval = &retval;
1351+
fci.param_count = userdata ? 3 : 2;
1352+
fci.params = args;
13431353

13441354
zend_hash_internal_pointer_reset_ex(target_hash, &pos);
13451355
ht_iter = zend_hash_iterator_add(target_hash, pos);
@@ -1386,8 +1396,6 @@ static int php_array_walk(zval *array, zval *userdata, int recursive) /* {{{ */
13861396

13871397
if (recursive && Z_TYPE_P(Z_REFVAL_P(zv)) == IS_ARRAY) {
13881398
HashTable *thash;
1389-
zend_fcall_info orig_array_walk_fci;
1390-
zend_fcall_info_cache orig_array_walk_fci_cache;
13911399
zval ref;
13921400
ZVAL_COPY_VALUE(&ref, zv);
13931401

@@ -1400,28 +1408,20 @@ static int php_array_walk(zval *array, zval *userdata, int recursive) /* {{{ */
14001408
break;
14011409
}
14021410

1403-
/* backup the fcall info and cache */
1404-
orig_array_walk_fci = BG(array_walk_fci);
1405-
orig_array_walk_fci_cache = BG(array_walk_fci_cache);
1406-
14071411
Z_ADDREF(ref);
14081412
GC_PROTECT_RECURSION(thash);
1409-
result = php_array_walk(zv, userdata, recursive);
1413+
result = php_array_walk(context, zv, userdata, recursive);
14101414
if (Z_TYPE_P(Z_REFVAL(ref)) == IS_ARRAY && thash == Z_ARRVAL_P(Z_REFVAL(ref))) {
14111415
/* If the hashtable changed in the meantime, we'll "leak" this apply count
14121416
* increment -- our reference to thash is no longer valid. */
14131417
GC_UNPROTECT_RECURSION(thash);
14141418
}
14151419
zval_ptr_dtor(&ref);
1416-
1417-
/* restore the fcall info and cache */
1418-
BG(array_walk_fci) = orig_array_walk_fci;
1419-
BG(array_walk_fci_cache) = orig_array_walk_fci_cache;
14201420
} else {
14211421
ZVAL_COPY(&args[0], zv);
14221422

14231423
/* Call the userland function */
1424-
result = zend_call_function(&BG(array_walk_fci), &BG(array_walk_fci_cache));
1424+
result = zend_call_function(&fci, &context->fci_cache);
14251425
if (result == SUCCESS) {
14261426
zval_ptr_dtor(&retval);
14271427
}
@@ -1458,33 +1458,22 @@ static int php_array_walk(zval *array, zval *userdata, int recursive) /* {{{ */
14581458
zend_hash_iterator_del(ht_iter);
14591459
return result;
14601460
}
1461-
/* }}} */
14621461

14631462
/* {{{ Apply a user function to every member of an array */
14641463
PHP_FUNCTION(array_walk)
14651464
{
14661465
zval *array;
14671466
zval *userdata = NULL;
1468-
zend_fcall_info orig_array_walk_fci;
1469-
zend_fcall_info_cache orig_array_walk_fci_cache;
1470-
1471-
orig_array_walk_fci = BG(array_walk_fci);
1472-
orig_array_walk_fci_cache = BG(array_walk_fci_cache);
1467+
php_array_walk_context context;
14731468

14741469
ZEND_PARSE_PARAMETERS_START(2, 3)
14751470
Z_PARAM_ARRAY_OR_OBJECT_EX(array, 0, 1)
1476-
Z_PARAM_FUNC(BG(array_walk_fci), BG(array_walk_fci_cache))
1471+
Z_PARAM_FUNC(context.fci, context.fci_cache)
14771472
Z_PARAM_OPTIONAL
14781473
Z_PARAM_ZVAL(userdata)
1479-
ZEND_PARSE_PARAMETERS_END_EX(
1480-
BG(array_walk_fci) = orig_array_walk_fci;
1481-
BG(array_walk_fci_cache) = orig_array_walk_fci_cache;
1482-
return
1483-
);
1484-
1485-
php_array_walk(array, userdata, 0);
1486-
BG(array_walk_fci) = orig_array_walk_fci;
1487-
BG(array_walk_fci_cache) = orig_array_walk_fci_cache;
1474+
ZEND_PARSE_PARAMETERS_END();
1475+
1476+
php_array_walk(&context, array, userdata, 0);
14881477
RETURN_TRUE;
14891478
}
14901479
/* }}} */
@@ -1494,26 +1483,16 @@ PHP_FUNCTION(array_walk_recursive)
14941483
{
14951484
zval *array;
14961485
zval *userdata = NULL;
1497-
zend_fcall_info orig_array_walk_fci;
1498-
zend_fcall_info_cache orig_array_walk_fci_cache;
1499-
1500-
orig_array_walk_fci = BG(array_walk_fci);
1501-
orig_array_walk_fci_cache = BG(array_walk_fci_cache);
1486+
php_array_walk_context context;
15021487

15031488
ZEND_PARSE_PARAMETERS_START(2, 3)
15041489
Z_PARAM_ARRAY_OR_OBJECT_EX(array, 0, 1)
1505-
Z_PARAM_FUNC(BG(array_walk_fci), BG(array_walk_fci_cache))
1490+
Z_PARAM_FUNC(context.fci, context.fci_cache)
15061491
Z_PARAM_OPTIONAL
15071492
Z_PARAM_ZVAL(userdata)
1508-
ZEND_PARSE_PARAMETERS_END_EX(
1509-
BG(array_walk_fci) = orig_array_walk_fci;
1510-
BG(array_walk_fci_cache) = orig_array_walk_fci_cache;
1511-
return
1512-
);
1513-
1514-
php_array_walk(array, userdata, 1);
1515-
BG(array_walk_fci) = orig_array_walk_fci;
1516-
BG(array_walk_fci_cache) = orig_array_walk_fci_cache;
1493+
ZEND_PARSE_PARAMETERS_END();
1494+
1495+
php_array_walk(&context, array, userdata, 1);
15171496
RETURN_TRUE;
15181497
}
15191498
/* }}} */

ext/standard/basic_functions.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,6 @@ PHP_RINIT_FUNCTION(basic) /* {{{ */
463463
BG(strtok_last) = NULL;
464464
BG(ctype_string) = NULL;
465465
BG(locale_changed) = 0;
466-
BG(array_walk_fci) = empty_fcall_info;
467-
BG(array_walk_fci_cache) = empty_fcall_info_cache;
468466
BG(user_compare_fci) = empty_fcall_info;
469467
BG(user_compare_fci_cache) = empty_fcall_info_cache;
470468
BG(page_uid) = -1;

ext/standard/basic_functions.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ typedef struct _php_basic_globals {
6666
char *strtok_last;
6767
char strtok_table[256];
6868
size_t strtok_len;
69-
zend_fcall_info array_walk_fci;
70-
zend_fcall_info_cache array_walk_fci_cache;
7169
zend_fcall_info user_compare_fci;
7270
zend_fcall_info_cache user_compare_fci_cache;
7371
zend_llist *user_tick_functions;

0 commit comments

Comments
 (0)