Skip to content

random: Embed the Mt19937 and CombinedLCG state within the module globals #13579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ext/random/php_random.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ PHP_MSHUTDOWN_FUNCTION(random);
PHP_RINIT_FUNCTION(random);

ZEND_BEGIN_MODULE_GLOBALS(random)
php_random_status_state_combinedlcg *combined_lcg;
int random_fd;
bool combined_lcg_seeded;
php_random_status_state_mt19937 *mt19937;
bool mt19937_seeded;
int random_fd;
php_random_status_state_combinedlcg combined_lcg;
php_random_status_state_mt19937 mt19937;
ZEND_END_MODULE_GLOBALS(random)

PHPAPI ZEND_EXTERN_MODULE_GLOBALS(random)
Expand Down
18 changes: 3 additions & 15 deletions ext/random/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ PHPAPI const php_random_algo *php_random_default_algo(void)
/* {{{ php_random_default_status */
PHPAPI void *php_random_default_status(void)
{
php_random_status_state_mt19937 *state = RANDOM_G(mt19937);
php_random_status_state_mt19937 *state = &RANDOM_G(mt19937);

if (!RANDOM_G(mt19937_seeded)) {
php_random_mt19937_seed_default(state);
Expand Down Expand Up @@ -390,7 +390,7 @@ PHPAPI bool php_random_hex2bin_le(zend_string *hexstr, void *dest)
/* {{{ php_combined_lcg */
PHPAPI double php_combined_lcg(void)
{
php_random_status_state_combinedlcg *state = RANDOM_G(combined_lcg);
php_random_status_state_combinedlcg *state = &RANDOM_G(combined_lcg);

if (!RANDOM_G(combined_lcg_seeded)) {
php_random_combinedlcg_seed_default(state);
Expand Down Expand Up @@ -472,7 +472,7 @@ PHP_FUNCTION(mt_srand)
zend_long seed = 0;
bool seed_is_null = true;
zend_long mode = MT_RAND_MT19937;
php_random_status_state_mt19937 *state = RANDOM_G(mt19937);
php_random_status_state_mt19937 *state = &RANDOM_G(mt19937);

ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Expand Down Expand Up @@ -615,12 +615,6 @@ PHP_FUNCTION(random_int)
static PHP_GINIT_FUNCTION(random)
{
random_globals->random_fd = -1;

random_globals->combined_lcg = php_random_status_alloc(&php_random_algo_combinedlcg, true);
random_globals->combined_lcg_seeded = false;

random_globals->mt19937 = php_random_status_alloc(&php_random_algo_mt19937, true);
random_globals->mt19937_seeded = false;
}
/* }}} */

Expand All @@ -631,12 +625,6 @@ static PHP_GSHUTDOWN_FUNCTION(random)
close(random_globals->random_fd);
random_globals->random_fd = -1;
}

php_random_status_free(random_globals->combined_lcg, true);
random_globals->combined_lcg = NULL;

php_random_status_free(random_globals->mt19937, true);
random_globals->mt19937 = NULL;
}
/* }}} */

Expand Down