From 3ddbad95896b6ace0e66716fc444ed359a3f1065 Mon Sep 17 00:00:00 2001 From: Arnaud Le Blanc Date: Fri, 1 Aug 2025 18:30:37 +0200 Subject: [PATCH] Allocate a fast thread-safe-resource id for opcache Closes GH-19347 --- ext/opcache/ZendAccelerator.c | 3 ++- ext/opcache/ZendAccelerator.h | 3 ++- ext/opcache/jit/zend_jit.c | 3 ++- ext/opcache/jit/zend_jit.h | 3 ++- main/main.c | 6 +++++- 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c index a4e5d5d526a..b789f836fc3 100644 --- a/ext/opcache/ZendAccelerator.c +++ b/ext/opcache/ZendAccelerator.c @@ -109,6 +109,7 @@ static zend_extension opcache_extension_entry; zend_accel_globals accel_globals; #else int accel_globals_id; +size_t accel_globals_offset; #endif /* Points to the structure shared across all PHP processes */ @@ -3162,7 +3163,7 @@ void start_accel_extension(void) static int accel_startup(zend_extension *extension) { #ifdef ZTS - accel_globals_id = ts_allocate_id(&accel_globals_id, sizeof(zend_accel_globals), (ts_allocate_ctor) accel_globals_ctor, (ts_allocate_dtor) accel_globals_dtor); + accel_globals_id = ts_allocate_fast_id(&accel_globals_id, &accel_globals_offset, sizeof(zend_accel_globals), (ts_allocate_ctor) accel_globals_ctor, (ts_allocate_dtor) accel_globals_dtor); #else accel_globals_ctor(&accel_globals); #endif diff --git a/ext/opcache/ZendAccelerator.h b/ext/opcache/ZendAccelerator.h index e103b7efee7..0766996cbb1 100644 --- a/ext/opcache/ZendAccelerator.h +++ b/ext/opcache/ZendAccelerator.h @@ -300,8 +300,9 @@ extern zend_accel_shared_globals *accel_shared_globals; #define ZCSG(element) (accel_shared_globals->element) #ifdef ZTS -# define ZCG(v) ZEND_TSRMG(accel_globals_id, zend_accel_globals *, v) +# define ZCG(v) ZEND_TSRMG_FAST(accel_globals_offset, zend_accel_globals *, v) extern int accel_globals_id; +extern size_t accel_globals_offset; #else # define ZCG(v) (accel_globals.v) extern zend_accel_globals accel_globals; diff --git a/ext/opcache/jit/zend_jit.c b/ext/opcache/jit/zend_jit.c index e743532a693..5e3909a1ce2 100644 --- a/ext/opcache/jit/zend_jit.c +++ b/ext/opcache/jit/zend_jit.c @@ -48,6 +48,7 @@ #ifdef ZTS int jit_globals_id; +size_t jit_globals_offset; #else zend_jit_globals jit_globals; #endif @@ -3700,7 +3701,7 @@ int zend_jit_debug_config(zend_long old_val, zend_long new_val, int stage) void zend_jit_init(void) { #ifdef ZTS - jit_globals_id = ts_allocate_id(&jit_globals_id, sizeof(zend_jit_globals), (ts_allocate_ctor) zend_jit_globals_ctor, (ts_allocate_dtor) zend_jit_globals_dtor); + jit_globals_id = ts_allocate_fast_id(&jit_globals_id, &jit_globals_offset, sizeof(zend_jit_globals), (ts_allocate_ctor) zend_jit_globals_ctor, (ts_allocate_dtor) zend_jit_globals_dtor); #else zend_jit_globals_ctor(&jit_globals); #endif diff --git a/ext/opcache/jit/zend_jit.h b/ext/opcache/jit/zend_jit.h index 9178d340a0e..5e6b225676a 100644 --- a/ext/opcache/jit/zend_jit.h +++ b/ext/opcache/jit/zend_jit.h @@ -145,8 +145,9 @@ typedef struct _zend_jit_globals { } zend_jit_globals; #ifdef ZTS -# define JIT_G(v) ZEND_TSRMG(jit_globals_id, zend_jit_globals *, v) +# define JIT_G(v) ZEND_TSRMG_FAST(jit_globals_offset, zend_jit_globals *, v) extern int jit_globals_id; +extern size_t jit_globals_offset; #else # define JIT_G(v) (jit_globals.v) extern zend_jit_globals jit_globals; diff --git a/main/main.c b/main/main.c index be03d9a70fd..0736c2d7360 100644 --- a/main/main.c +++ b/main/main.c @@ -51,6 +51,8 @@ #include "ext/date/php_date.h" #include "ext/random/php_random_csprng.h" #include "ext/random/php_random_zend_utils.h" +#include "ext/opcache/ZendAccelerator.h" +#include "ext/opcache/jit/zend_jit.h" #include "php_variables.h" #include "ext/standard/credits.h" #ifdef PHP_WIN32 @@ -2803,7 +2805,9 @@ PHPAPI void php_reserve_tsrm_memory(void) TSRM_ALIGNED_SIZE(zend_mm_globals_size()) + TSRM_ALIGNED_SIZE(zend_gc_globals_size()) + TSRM_ALIGNED_SIZE(sizeof(php_core_globals)) + - TSRM_ALIGNED_SIZE(sizeof(sapi_globals_struct)) + TSRM_ALIGNED_SIZE(sizeof(sapi_globals_struct)) + + TSRM_ALIGNED_SIZE(sizeof(zend_accel_globals)) + + TSRM_ALIGNED_SIZE(sizeof(zend_jit_globals)) ); } /* }}} */