Skip to content

Commit 07a4cce

Browse files
Apply code review suggestions
1 parent 818f6d5 commit 07a4cce

File tree

5 files changed

+29
-20
lines changed

5 files changed

+29
-20
lines changed

Zend/zend.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -735,16 +735,16 @@ static void compiler_globals_ctor(zend_compiler_globals *compiler_globals) /* {{
735735
compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(NULL);
736736
compiler_globals->map_ptr_size = 0;
737737
compiler_globals->map_ptr_last = global_map_ptr_last;
738-
compiler_globals->internal_run_time_cache = 0;
739-
if (compiler_globals->map_ptr_last) {
738+
compiler_globals->internal_run_time_cache = NULL;
739+
if (compiler_globals->map_ptr_last || zend_map_ptr_static_size) {
740740
/* Allocate map_ptr table */
741741
compiler_globals->map_ptr_size = ZEND_MM_ALIGNED_SIZE_EX(compiler_globals->map_ptr_last, 4096);
742-
void *base = pemalloc(compiler_globals->map_ptr_size * sizeof(void*), 1);
742+
void *base = pemalloc((zend_map_ptr_static_size + compiler_globals->map_ptr_size) * sizeof(void*), 1);
743743
compiler_globals->map_ptr_real_base = base;
744744
compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(base);
745-
memset(base, 0, compiler_globals->map_ptr_last * sizeof(void*));
746-
zend_init_internal_run_time_cache();
745+
memset(base, 0, (zend_map_ptr_static_size + compiler_globals->map_ptr_last) * sizeof(void*));
747746
}
747+
zend_init_internal_run_time_cache();
748748
}
749749
/* }}} */
750750

@@ -789,6 +789,7 @@ static void compiler_globals_dtor(zend_compiler_globals *compiler_globals) /* {{
789789
}
790790
if (compiler_globals->internal_run_time_cache) {
791791
pefree(compiler_globals->internal_run_time_cache, 1);
792+
compiler_globals->internal_run_time_cache = NULL;
792793
}
793794
}
794795
/* }}} */
@@ -1088,7 +1089,6 @@ zend_result zend_post_startup(void) /* {{{ */
10881089

10891090
zend_compiler_globals *compiler_globals = ts_resource(compiler_globals_id);
10901091
zend_executor_globals *executor_globals = ts_resource(executor_globals_id);
1091-
global_map_ptr_last = compiler_globals->map_ptr_last;
10921092
#endif
10931093

10941094
startup_done = true;
@@ -1119,11 +1119,12 @@ zend_result zend_post_startup(void) /* {{{ */
11191119
if (compiler_globals->map_ptr_real_base) {
11201120
free(compiler_globals->map_ptr_real_base);
11211121
}
1122+
compiler_globals->map_ptr_real_base = NULL;
1123+
compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(NULL);
11221124
if (compiler_globals->internal_run_time_cache) {
11231125
pefree(compiler_globals->internal_run_time_cache, 1);
11241126
}
1125-
compiler_globals->map_ptr_real_base = NULL;
1126-
compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(NULL);
1127+
compiler_globals->internal_run_time_cache = NULL;
11271128
if ((script_encoding_list = (zend_encoding **)compiler_globals->script_encoding_list)) {
11281129
compiler_globals_ctor(compiler_globals);
11291130
compiler_globals->script_encoding_list = (const zend_encoding **)script_encoding_list;
@@ -1309,7 +1310,7 @@ ZEND_API void zend_activate(void) /* {{{ */
13091310
init_executor();
13101311
startup_scanner();
13111312
if (CG(map_ptr_last)) {
1312-
memset((char *)CG(map_ptr_real_base) + zend_map_ptr_static_size * sizeof(void*), 0, (CG(map_ptr_last) - zend_map_ptr_static_size) * sizeof(void*));
1313+
memset((void **)CG(map_ptr_real_base) + zend_map_ptr_static_size, 0, CG(map_ptr_last) * sizeof(void*));
13131314
}
13141315
zend_reset_internal_run_time_cache();
13151316
zend_observer_activate();
@@ -2011,10 +2012,10 @@ ZEND_API void *zend_map_ptr_new(void)
20112012
if (CG(map_ptr_last) >= CG(map_ptr_size)) {
20122013
/* Grow map_ptr table */
20132014
CG(map_ptr_size) = ZEND_MM_ALIGNED_SIZE_EX(CG(map_ptr_last) + 1, 4096);
2014-
CG(map_ptr_real_base) = perealloc(CG(map_ptr_real_base), CG(map_ptr_size) * sizeof(void*), 1);
2015+
CG(map_ptr_real_base) = perealloc(CG(map_ptr_real_base), (zend_map_ptr_static_size + CG(map_ptr_size)) * sizeof(void*), 1);
20152016
CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(CG(map_ptr_real_base));
20162017
}
2017-
ptr = (void**)CG(map_ptr_real_base) + CG(map_ptr_last);
2018+
ptr = (void**)CG(map_ptr_real_base) + zend_map_ptr_static_size + CG(map_ptr_last);
20182019
*ptr = NULL;
20192020
CG(map_ptr_last)++;
20202021
return ZEND_MAP_PTR_PTR2OFFSET(ptr);
@@ -2025,13 +2026,13 @@ ZEND_API void *zend_map_ptr_new_static(void)
20252026
void **ptr;
20262027

20272028
if (zend_map_ptr_static_last >= zend_map_ptr_static_size) {
2028-
CG(map_ptr_last) += 4096;
20292029
zend_map_ptr_static_size += 4096;
20302030
/* Grow map_ptr table */
2031-
CG(map_ptr_size) = ZEND_MM_ALIGNED_SIZE_EX(CG(map_ptr_last), 4096);
2032-
CG(map_ptr_real_base) = perealloc(CG(map_ptr_real_base), CG(map_ptr_size) * sizeof(void*), 1);
2033-
memmove((char*)CG(map_ptr_real_base) + 4096 * sizeof(void *), CG(map_ptr_real_base), (CG(map_ptr_last) - 4096) * sizeof(void *));
2034-
CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(CG(map_ptr_real_base));
2031+
void *new_base = pemalloc((zend_map_ptr_static_size + CG(map_ptr_size)) * sizeof(void*), 1);
2032+
memcpy((char*)new_base + 4096 * sizeof(void *), CG(map_ptr_real_base), (CG(map_ptr_last) + zend_map_ptr_static_size - 4096) * sizeof(void *));
2033+
pefree(CG(map_ptr_real_base), 1);
2034+
CG(map_ptr_real_base) = new_base;
2035+
CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(new_base);
20352036
}
20362037
ptr = (void**)CG(map_ptr_real_base) + (zend_map_ptr_static_last & 4095);
20372038
*ptr = NULL;
@@ -2047,10 +2048,10 @@ ZEND_API void zend_map_ptr_extend(size_t last)
20472048
if (last >= CG(map_ptr_size)) {
20482049
/* Grow map_ptr table */
20492050
CG(map_ptr_size) = ZEND_MM_ALIGNED_SIZE_EX(last, 4096);
2050-
CG(map_ptr_real_base) = perealloc(CG(map_ptr_real_base), CG(map_ptr_size) * sizeof(void*), 1);
2051+
CG(map_ptr_real_base) = perealloc(CG(map_ptr_real_base), (zend_map_ptr_static_size + CG(map_ptr_size)) * sizeof(void*), 1);
20512052
CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(CG(map_ptr_real_base));
20522053
}
2053-
ptr = (void**)CG(map_ptr_real_base) + CG(map_ptr_last);
2054+
ptr = (void**)CG(map_ptr_real_base) + zend_map_ptr_static_size + CG(map_ptr_last);
20542055
memset(ptr, 0, (last - CG(map_ptr_last)) * sizeof(void*));
20552056
CG(map_ptr_last) = last;
20562057
}

Zend/zend_API.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2952,7 +2952,11 @@ ZEND_API zend_result zend_register_functions(zend_class_entry *scope, const zend
29522952
if (EG(active)) { // at run-time: this ought to only happen if registered with dl() or somehow temporarily at runtime
29532953
ZEND_MAP_PTR_INIT(internal_function->run_time_cache, zend_arena_calloc(&CG(arena), 1, zend_internal_run_time_cache_reserved_size()));
29542954
} else {
2955+
#if ZTS
29552956
ZEND_MAP_PTR_NEW_STATIC(internal_function->run_time_cache);
2957+
#else
2958+
ZEND_MAP_PTR_INIT(internal_function->run_time_cache, NULL);
2959+
#endif
29562960
}
29572961
if (ptr->flags) {
29582962
if (!(ptr->flags & ZEND_ACC_PPP_MASK)) {

Zend/zend_enum.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,11 @@ static void zend_enum_register_func(zend_class_entry *ce, zend_known_string_id n
421421
if (EG(active)) { // at run-time
422422
ZEND_MAP_PTR_INIT(zif->run_time_cache, zend_arena_calloc(&CG(arena), 1, zend_internal_run_time_cache_reserved_size()));
423423
} else {
424+
#if ZTS
424425
ZEND_MAP_PTR_NEW_STATIC(zif->run_time_cache);
426+
#else
427+
ZEND_MAP_PTR_INIT(zif->run_time_cache, NULL);
428+
#endif
425429
}
426430

427431
if (!zend_hash_add_ptr(&ce->function_table, name, zif)) {

Zend/zend_map_ptr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ typedef struct _zend_string zend_string;
4848

4949
#if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR_OR_OFFSET
5050
# define ZEND_MAP_PTR_NEW_OFFSET() \
51-
((uint32_t)(intptr_t)zend_map_ptr_new())
51+
((uint32_t)(uintptr_t)zend_map_ptr_new())
5252
# define ZEND_MAP_PTR_IS_OFFSET(ptr) \
5353
(((uintptr_t)ZEND_MAP_PTR(ptr)) & 1L)
5454
# define ZEND_MAP_PTR_GET(ptr) \

Zend/zend_types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ static zend_always_inline uint32_t zval_gc_info(uint32_t gc_type_info) {
839839
#define ZSTR_GET_CE_CACHE(s) ZSTR_GET_CE_CACHE_EX(s, 1)
840840
#define ZSTR_SET_CE_CACHE(s, ce) ZSTR_SET_CE_CACHE_EX(s, ce, 1)
841841

842-
#define ZSTR_VALID_CE_CACHE(s) EXPECTED((GC_REFCOUNT(s)-1)/sizeof(void *) < CG(map_ptr_last) - zend_map_ptr_static_size)
842+
#define ZSTR_VALID_CE_CACHE(s) EXPECTED((GC_REFCOUNT(s)-1)/sizeof(void *) < CG(map_ptr_last))
843843

844844
#define ZSTR_GET_CE_CACHE_EX(s, validate) \
845845
((!(validate) || ZSTR_VALID_CE_CACHE(s)) ? GET_CE_CACHE(GC_REFCOUNT(s)) : NULL)

0 commit comments

Comments
 (0)