Skip to content
Draft
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
15 changes: 8 additions & 7 deletions TSRM/TSRM.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

#include "TSRM.h"
#include "zend_alloc.h"

#ifdef ZTS

Expand Down Expand Up @@ -254,12 +255,12 @@ static void tsrm_update_active_threads(void)
if (p->count < id_count) {
int j;

p->storage = (void *) realloc(p->storage, sizeof(void *)*id_count);
p->storage = (void *) prealloc(p->storage, sizeof(void *)*id_count);
for (j=p->count; j<id_count; j++) {
if (resource_types_table[j].fast_offset) {
p->storage[j] = (void *) (((char*)p) + resource_types_table[j].fast_offset);
} else {
p->storage[j] = (void *) malloc(resource_types_table[j].size);
p->storage[j] = (void *) pmalloc(resource_types_table[j].size);
}
if (resource_types_table[j].ctor) {
resource_types_table[j].ctor(p->storage[j]);
Expand Down Expand Up @@ -378,10 +379,10 @@ static void set_thread_local_storage_resource_to(tsrm_tls_entry *thread_resource
static void allocate_new_resource(tsrm_tls_entry **thread_resources_ptr, THREAD_T thread_id)
{/*{{{*/
TSRM_ERROR((TSRM_ERROR_LEVEL_CORE, "Creating data structures for thread %x", thread_id));
(*thread_resources_ptr) = (tsrm_tls_entry *) malloc(TSRM_ALIGNED_SIZE(sizeof(tsrm_tls_entry)) + tsrm_reserved_size);
(*thread_resources_ptr) = (tsrm_tls_entry *) pmalloc(TSRM_ALIGNED_SIZE(sizeof(tsrm_tls_entry)) + tsrm_reserved_size);
(*thread_resources_ptr)->storage = NULL;
if (id_count > 0) {
(*thread_resources_ptr)->storage = (void **) malloc(sizeof(void *)*id_count);
(*thread_resources_ptr)->storage = (void **) pmalloc(sizeof(void *)*id_count);
}
(*thread_resources_ptr)->count = id_count;
(*thread_resources_ptr)->thread_id = thread_id;
Expand All @@ -400,7 +401,7 @@ static void allocate_new_resource(tsrm_tls_entry **thread_resources_ptr, THREAD_
if (resource_types_table[i].fast_offset) {
(*thread_resources_ptr)->storage[i] = (void *) (((char*)(*thread_resources_ptr)) + resource_types_table[i].fast_offset);
} else {
(*thread_resources_ptr)->storage[i] = (void *) malloc(resource_types_table[i].size);
(*thread_resources_ptr)->storage[i] = (void *) pmalloc(resource_types_table[i].size);
}
if (resource_types_table[i].ctor) {
resource_types_table[i].ctor((*thread_resources_ptr)->storage[i]);
Expand Down Expand Up @@ -618,10 +619,10 @@ TSRM_API MUTEX_T tsrm_mutex_alloc(void)
{/*{{{*/
MUTEX_T mutexp;
#ifdef TSRM_WIN32
mutexp = malloc(sizeof(CRITICAL_SECTION));
mutexp = pmalloc(sizeof(CRITICAL_SECTION));
InitializeCriticalSection(mutexp);
#else
mutexp = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
mutexp = (pthread_mutex_t *)pmalloc(sizeof(pthread_mutex_t));
pthread_mutex_init(mutexp,NULL);
#endif
#ifdef THR_DEBUG
Expand Down
22 changes: 11 additions & 11 deletions Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -715,18 +715,18 @@ static void compiler_globals_ctor(zend_compiler_globals *compiler_globals) /* {{
compiler_globals->compiled_filename = NULL;
compiler_globals->zend_lineno = 0;

compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
compiler_globals->function_table = (HashTable *) pmalloc(sizeof(HashTable));
zend_hash_init(compiler_globals->function_table, 1024, NULL, ZEND_FUNCTION_DTOR, 1);
zend_hash_copy(compiler_globals->function_table, global_function_table, NULL);
compiler_globals->copied_functions_count = zend_hash_num_elements(compiler_globals->function_table);

compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
compiler_globals->class_table = (HashTable *) pmalloc(sizeof(HashTable));
zend_hash_init(compiler_globals->class_table, 64, NULL, ZEND_CLASS_DTOR, 1);
zend_hash_copy(compiler_globals->class_table, global_class_table, zend_class_add_ref);

zend_set_default_compile_time_values();

compiler_globals->auto_globals = (HashTable *) malloc(sizeof(HashTable));
compiler_globals->auto_globals = (HashTable *) pmalloc(sizeof(HashTable));
zend_hash_init(compiler_globals->auto_globals, 8, NULL, auto_global_dtor, 1);
zend_hash_copy(compiler_globals->auto_globals, global_auto_globals_table, auto_global_copy_ctor);

Expand Down Expand Up @@ -1004,10 +1004,10 @@ void zend_startup(zend_utility_functions *utility_functions) /* {{{ */
zend_version_info = strdup(ZEND_CORE_VERSION_INFO);
zend_version_info_length = sizeof(ZEND_CORE_VERSION_INFO) - 1;

GLOBAL_FUNCTION_TABLE = (HashTable *) malloc(sizeof(HashTable));
GLOBAL_CLASS_TABLE = (HashTable *) malloc(sizeof(HashTable));
GLOBAL_AUTO_GLOBALS_TABLE = (HashTable *) malloc(sizeof(HashTable));
GLOBAL_CONSTANTS_TABLE = (HashTable *) malloc(sizeof(HashTable));
GLOBAL_FUNCTION_TABLE = (HashTable *) pmalloc(sizeof(HashTable));
GLOBAL_CLASS_TABLE = (HashTable *) pmalloc(sizeof(HashTable));
GLOBAL_AUTO_GLOBALS_TABLE = (HashTable *) pmalloc(sizeof(HashTable));
GLOBAL_CONSTANTS_TABLE = (HashTable *) pmalloc(sizeof(HashTable));

zend_hash_init(GLOBAL_FUNCTION_TABLE, 1024, NULL, ZEND_FUNCTION_DTOR, 1);
zend_hash_init(GLOBAL_CLASS_TABLE, 64, NULL, ZEND_CLASS_DTOR, 1);
Expand All @@ -1027,8 +1027,8 @@ void zend_startup(zend_utility_functions *utility_functions) /* {{{ */

compiler_globals_dtor(compiler_globals);
compiler_globals->in_compilation = 0;
compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
compiler_globals->function_table = (HashTable *) pmalloc(sizeof(HashTable));
compiler_globals->class_table = (HashTable *) pmalloc(sizeof(HashTable));

*compiler_globals->function_table = *GLOBAL_FUNCTION_TABLE;
*compiler_globals->class_table = *GLOBAL_CLASS_TABLE;
Expand Down Expand Up @@ -1296,11 +1296,11 @@ ZEND_API void zend_append_version_info(const zend_extension *extension) /* {{{ *
+ strlen(extension->copyright)
+ strlen(extension->author));

new_info = (char *) malloc(new_info_length + 1);
new_info = (char *) pmalloc(new_info_length + 1);

snprintf(new_info, new_info_length, " with %s v%s, %s, by %s\n", extension->name, extension->version, extension->copyright, extension->author);

zend_version_info = (char *) realloc(zend_version_info, zend_version_info_length+new_info_length + 1);
zend_version_info = (char *) prealloc(zend_version_info, zend_version_info_length+new_info_length + 1);
strncat(zend_version_info, new_info, new_info_length);
zend_version_info_length += new_info_length;
free(new_info);
Expand Down
18 changes: 9 additions & 9 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -2519,7 +2519,7 @@ ZEND_API void zend_collect_module_handlers(void) /* {{{ */
dl_loaded_count++;
}
} ZEND_HASH_FOREACH_END();
module_request_startup_handlers = (zend_module_entry**)realloc(
module_request_startup_handlers = (zend_module_entry**)prealloc(
module_request_startup_handlers,
sizeof(zend_module_entry*) *
(startup_count + 1 +
Expand All @@ -2531,7 +2531,7 @@ ZEND_API void zend_collect_module_handlers(void) /* {{{ */
module_post_deactivate_handlers = module_request_shutdown_handlers + shutdown_count + 1;
module_post_deactivate_handlers[post_deactivate_count] = NULL;
/* Cannot reuse module_request_startup_handlers because it is freed in zend_destroy_modules, which happens before zend_unload_modules. */
modules_dl_loaded = realloc(modules_dl_loaded, sizeof(zend_module_entry*) * (dl_loaded_count + 1));
modules_dl_loaded = prealloc(modules_dl_loaded, sizeof(zend_module_entry*) * (dl_loaded_count + 1));
modules_dl_loaded[dl_loaded_count] = NULL;
startup_count = 0;

Expand All @@ -2558,7 +2558,7 @@ ZEND_API void zend_collect_module_handlers(void) /* {{{ */
}
} ZEND_HASH_FOREACH_END();

class_cleanup_handlers = (zend_class_entry**)realloc(
class_cleanup_handlers = (zend_class_entry**)prealloc(
class_cleanup_handlers,
sizeof(zend_class_entry*) *
(class_count + 1));
Expand Down Expand Up @@ -3083,7 +3083,7 @@ ZEND_API zend_result zend_register_functions(zend_class_entry *scope, const zend
}
lowercase_name = zend_string_tolower_ex(internal_function->function_name, type == MODULE_PERSISTENT);
lowercase_name = zend_new_interned_string(lowercase_name);
reg_function = malloc(sizeof(zend_internal_function));
reg_function = pmalloc(sizeof(zend_internal_function));
memcpy(reg_function, &function, sizeof(zend_internal_function));
if (zend_hash_add_ptr(target_function_table, lowercase_name, reg_function) == NULL) {
unload=1;
Expand All @@ -3101,8 +3101,8 @@ ZEND_API zend_result zend_register_functions(zend_class_entry *scope, const zend
zend_flf_capacity *= 2;
}
/* +1 for NULL terminator */
zend_flf_handlers = realloc(zend_flf_handlers, (zend_flf_capacity + 1) * sizeof(void *));
zend_flf_functions = realloc(zend_flf_functions, (zend_flf_capacity + 1) * sizeof(zend_function *));
zend_flf_handlers = prealloc(zend_flf_handlers, (zend_flf_capacity + 1) * sizeof(void *));
zend_flf_functions = prealloc(zend_flf_functions, (zend_flf_capacity + 1) * sizeof(zend_function *));
}
zend_flf_handlers[zend_flf_count] = flf_info->handler;
zend_flf_functions[zend_flf_count] = (zend_function *)reg_function;
Expand Down Expand Up @@ -3150,7 +3150,7 @@ ZEND_API zend_result zend_register_functions(zend_class_entry *scope, const zend

/* Treat return type as an extra argument */
num_args++;
new_arg_info = malloc(sizeof(zend_internal_arg_info) * num_args);
new_arg_info = pmalloc(sizeof(zend_internal_arg_info) * num_args);
memcpy(new_arg_info, arg_info, sizeof(zend_internal_arg_info) * num_args);
reg_function->arg_info = new_arg_info + 1;
for (i = 0; i < num_args; i++) {
Expand All @@ -3176,7 +3176,7 @@ ZEND_API zend_result zend_register_functions(zend_class_entry *scope, const zend
new_arg_info[i].type.type_mask |= _ZEND_TYPE_NAME_BIT;
} else {
/* Union type */
zend_type_list *list = malloc(ZEND_TYPE_LIST_SIZE(num_types));
zend_type_list *list = pmalloc(ZEND_TYPE_LIST_SIZE(num_types));
list->num_types = num_types;
ZEND_TYPE_SET_LIST(new_arg_info[i].type, list);
ZEND_TYPE_FULL_MASK(new_arg_info[i].type) |= _ZEND_TYPE_UNION_BIT;
Expand Down Expand Up @@ -3487,7 +3487,7 @@ ZEND_API int zend_next_free_module(void) /* {{{ */

static zend_class_entry *do_register_internal_class(zend_class_entry *orig_class_entry, uint32_t ce_flags) /* {{{ */
{
zend_class_entry *class_entry = malloc(sizeof(zend_class_entry));
zend_class_entry *class_entry = pmalloc(sizeof(zend_class_entry));
zend_string *lowercase_name;
*class_entry = *orig_class_entry;

Expand Down
4 changes: 2 additions & 2 deletions Zend/zend_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3300,7 +3300,7 @@ static void alloc_globals_ctor(zend_alloc_globals *alloc_globals)
tmp = getenv("USE_ZEND_ALLOC");
if (tmp && !ZEND_ATOL(tmp)) {
bool tracked = (tmp = getenv("USE_TRACKED_ALLOC")) && ZEND_ATOL(tmp);
zend_mm_heap *mm_heap = alloc_globals->mm_heap = malloc(sizeof(zend_mm_heap));
zend_mm_heap *mm_heap = alloc_globals->mm_heap = pmalloc(sizeof(zend_mm_heap));
memset(mm_heap, 0, sizeof(zend_mm_heap));
mm_heap->use_custom_heap = ZEND_MM_CUSTOM_HEAP_STD;
mm_heap->limit = (size_t)Z_L(-1) >> 1;
Expand All @@ -3316,7 +3316,7 @@ static void alloc_globals_ctor(zend_alloc_globals *alloc_globals)
mm_heap->custom_heap._malloc = tracked_malloc;
mm_heap->custom_heap._free = tracked_free;
mm_heap->custom_heap._realloc = tracked_realloc;
mm_heap->tracked_allocs = malloc(sizeof(HashTable));
mm_heap->tracked_allocs = pmalloc(sizeof(HashTable));
zend_hash_init(mm_heap->tracked_allocs, 1024, NULL, NULL, 1);
}
return;
Expand Down
12 changes: 8 additions & 4 deletions Zend/zend_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,15 @@ ZEND_API void ZEND_FASTCALL _efree_huge(void *, size_t size);
#define estrndup_rel(s, length) _estrndup((s), (length) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
#define zend_mem_block_size_rel(ptr) _zend_mem_block_size((ptr) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)

ZEND_API ZEND_ATTRIBUTE_MALLOC void * __zend_malloc(size_t len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_ALLOC_SIZE(1);
ZEND_API ZEND_ATTRIBUTE_MALLOC void * __zend_calloc(size_t nmemb, size_t len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_ALLOC_SIZE2(1,2);
ZEND_API void * __zend_realloc(void *p, size_t len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_ALLOC_SIZE(2);
ZEND_API ZEND_ATTRIBUTE_MALLOC ZEND_RETURNS_NONNULL void * __zend_malloc(size_t len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_ALLOC_SIZE(1);
ZEND_API ZEND_ATTRIBUTE_MALLOC ZEND_RETURNS_NONNULL void * __zend_calloc(size_t nmemb, size_t len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_ALLOC_SIZE2(1,2);
ZEND_API ZEND_RETURNS_NONNULL void * __zend_realloc(void *p, size_t len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_ALLOC_SIZE(2);
ZEND_API void __zend_free(void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
ZEND_API ZEND_ATTRIBUTE_MALLOC char * __zend_strdup(const char *s);
ZEND_API ZEND_RETURNS_NONNULL ZEND_ATTRIBUTE_MALLOC char * __zend_strdup(const char *s);

#define pmalloc(size) (__zend_malloc(size ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC))
#define pcalloc(nmemb, size) (__zend_calloc((nmemb), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC))
#define prealloc(ptr, size) (__zend_realloc((ptr), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC))

/* Selective persistent/non persistent allocation macros */
#define pemalloc(size, persistent) ((persistent)?__zend_malloc(size ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC):emalloc(size))
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_call_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ static bool zend_call_stack_get_netbsd_vm(zend_call_stack *stack, void **ptr)

// kinfo_getvmmap uses the same formula, only we do not want to rely on libkvm
len = len * 4 / 3 ;
*ptr = malloc(len);
*ptr = pmalloc(len);

if (sysctl(mib, 5, *ptr, &len, NULL, 0) != 0) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void clean_module_constants(int module_number)

void zend_startup_constants(void)
{
EG(zend_constants) = (HashTable *) malloc(sizeof(HashTable));
EG(zend_constants) = (HashTable *) pmalloc(sizeof(HashTable));
zend_hash_init(EG(zend_constants), 128, NULL, ZEND_CONSTANT_DTOR, 1);
}

Expand Down
4 changes: 2 additions & 2 deletions Zend/zend_inheritance.c
Original file line number Diff line number Diff line change
Expand Up @@ -1600,7 +1600,7 @@ static void zend_do_inherit_interfaces(zend_class_entry *ce, const zend_class_en
ce_num = ce->num_interfaces;

if (ce->type == ZEND_INTERNAL_CLASS) {
ce->interfaces = (zend_class_entry **) realloc(ce->interfaces, sizeof(zend_class_entry *) * (ce_num + if_num));
ce->interfaces = (zend_class_entry **) prealloc(ce->interfaces, sizeof(zend_class_entry *) * (ce_num + if_num));
} else {
ce->interfaces = (zend_class_entry **) erealloc(ce->interfaces, sizeof(zend_class_entry *) * (ce_num + if_num));
}
Expand Down Expand Up @@ -2238,7 +2238,7 @@ ZEND_API void zend_do_implement_interface(zend_class_entry *ce, zend_class_entry
} else {
if (ce->num_interfaces >= current_iface_num) {
if (ce->type == ZEND_INTERNAL_CLASS) {
ce->interfaces = (zend_class_entry **) realloc(ce->interfaces, sizeof(zend_class_entry *) * (++current_iface_num));
ce->interfaces = (zend_class_entry **) prealloc(ce->interfaces, sizeof(zend_class_entry *) * (++current_iface_num));
} else {
ce->interfaces = (zend_class_entry **) erealloc(ce->interfaces, sizeof(zend_class_entry *) * (++current_iface_num));
}
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_ini.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static void free_ini_entry(zval *zv) /* {{{ */
*/
ZEND_API void zend_ini_startup(void) /* {{{ */
{
registered_zend_ini_directives = (HashTable *) malloc(sizeof(HashTable));
registered_zend_ini_directives = (HashTable *) pmalloc(sizeof(HashTable));

EG(ini_directives) = registered_zend_ini_directives;
EG(modified_ini_directives) = NULL;
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ ZEND_API int zend_register_list_destructors_ex(rsrc_dtor_func_t ld, rsrc_dtor_fu
zend_rsrc_list_dtors_entry *lde;
zval zv;

lde = malloc(sizeof(zend_rsrc_list_dtors_entry));
lde = pmalloc(sizeof(zend_rsrc_list_dtors_entry));
lde->list_dtor_ex = ld;
lde->plist_dtor_ex = pld;
lde->module_number = module_number;
Expand Down
6 changes: 6 additions & 0 deletions Zend/zend_portability.h
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,12 @@ extern "C++" {
# define ZEND_NONSTRING
#endif

#if __has_attribute(returns_nonnull)
# define ZEND_RETURNS_NONNULL __attribute__((returns_nonnull))
#else
# define ZEND_RETURNS_NONNULL
#endif

#define __ZEND_DO_PRAGMA(x) _Pragma(#x)
#define _ZEND_DO_PRAGMA(x) __ZEND_DO_PRAGMA(x)
#if defined(__clang__)
Expand Down
6 changes: 3 additions & 3 deletions Zend/zend_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ static zend_always_inline uint32_t zval_gc_info(uint32_t gc_type_info) {
#define ZVAL_NEW_PERSISTENT_ARR(z) do { \
zval *__z = (z); \
zend_array *_arr = \
(zend_array *) malloc(sizeof(zend_array)); \
(zend_array *) pmalloc(sizeof(zend_array)); \
Z_ARR_P(__z) = _arr; \
Z_TYPE_INFO_P(__z) = IS_ARRAY_EX; \
} while (0)
Expand Down Expand Up @@ -1198,7 +1198,7 @@ static zend_always_inline uint32_t zval_gc_info(uint32_t gc_type_info) {

#define ZVAL_NEW_PERSISTENT_RES(z, h, p, t) do { \
zend_resource *_res = \
(zend_resource *) malloc(sizeof(zend_resource)); \
(zend_resource *) pmalloc(sizeof(zend_resource)); \
zval *__z; \
GC_SET_REFCOUNT(_res, 1); \
GC_TYPE_INFO(_res) = GC_RESOURCE | \
Expand Down Expand Up @@ -1252,7 +1252,7 @@ static zend_always_inline uint32_t zval_gc_info(uint32_t gc_type_info) {

#define ZVAL_NEW_PERSISTENT_REF(z, r) do { \
zend_reference *_ref = \
(zend_reference *) malloc(sizeof(zend_reference)); \
(zend_reference *) pmalloc(sizeof(zend_reference)); \
GC_SET_REFCOUNT(_ref, 1); \
GC_TYPE_INFO(_ref) = GC_REFERENCE | \
(GC_PERSISTENT << GC_FLAGS_SHIFT); \
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_vm_execute.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Zend/zend_vm_execute.skl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static void init_opcode_serialiser(void)
int i;
zval tmp;

zend_handlers_table = malloc(sizeof(HashTable));
zend_handlers_table = pmalloc(sizeof(HashTable));
zend_hash_init(zend_handlers_table, zend_handlers_count, NULL, NULL, 1);
zend_hash_real_init(zend_handlers_table, 0);
Z_TYPE_INFO(tmp) = IS_LONG;
Expand Down
2 changes: 1 addition & 1 deletion build/gen_stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2491,7 +2491,7 @@ protected function getTypeCode(string $variableLikeName, string &$code): string
}

$classTypeCount = count($arginfoType->classTypes);
$code .= "\tzend_type_list *{$variableLikeType}_{$variableLikeName}_type_list = malloc(ZEND_TYPE_LIST_SIZE($classTypeCount));\n";
$code .= "\tzend_type_list *{$variableLikeType}_{$variableLikeName}_type_list = pmalloc(ZEND_TYPE_LIST_SIZE($classTypeCount));\n";
$code .= "\t{$variableLikeType}_{$variableLikeName}_type_list->num_types = $classTypeCount;\n";

foreach ($arginfoType->classTypes as $k => $classType) {
Expand Down
2 changes: 1 addition & 1 deletion ext/exif/exif.c
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,7 @@ static const maker_note_type maker_note_array[] = {

static HashTable *exif_make_tag_ht(tag_info_type *tag_table)
{
HashTable *ht = malloc(sizeof(HashTable));
HashTable *ht = pmalloc(sizeof(HashTable));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we take the opportunity of the churn to also avoid repeating the type unnecessarily?

Suggested change
HashTable *ht = pmalloc(sizeof(HashTable));
HashTable *ht = pmalloc(sizeof(*ht));

see: https://github.com/haproxy/haproxy/blob/master/dev/coccinelle/xalloc_size.cocci

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't usually use the sizeof(*ht) style, and I don't think most people do. I don't particularly care, but I think moving towards one style only makes sense if it becomes policy. Maybe suggest it on Slack first?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not going to change all such usage so I would prefer to stick with full types. I find it also more readable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not going to change all such usage

What do you mean by “we are not going to”?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if you meant doing just for pmalloc of changing every single sizeof.

If former, then I don't think there is much point in creating inconsistency that is less readable. If latter, you don't get full agreement as it would be a big churn so you would probably have to go through RFC which is not exactly meant for internal changes like this. It reminds me those header includes refactoring where we ended up not having any process to decide it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can also be used as some kind of static analyzer to find incorrect constructs (e.g. cases where the types already differ).

This is what I'm most interested in. It's hard to remember rules, and it's even harder for everyone to remember all rules. IMO, guidelines are most useful when they are enforced, even if only for the 95% cases. At least that helps you internalize the rules.

Regardless, I think such a decision would need discussion, though I fully acknowledge there's no good channel to do so. I think Slack (e.g. #php-src) would make the most sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of conflict is the easiest possible conflict to resolve: It affects a single line that doesn't require much thought.

But it's a still delay to check it, edit and commit for something that has no value and is not even an improvement.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends on the change. There's a non-trivial amount of time spent in PRs discussing the same nits (e.g. prefer /* */ over //, though I have no idea whether these kinds of things are solvable with Coccinelle).

Copy link
Member

@TimWolla TimWolla Sep 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a non-trivial amount of time spent in PRs discussing the same nits (e.g. prefer /* */ over //, though I have no idea whether these kinds of things are solvable with Coccinelle).

That example would probably rather be clang-format. I believe Coccinelle can also deal with comments, but the intended use case is making changes to the code based on type and control-flow information with a convenient syntax that resembles a patch file and thus is easy to grok even for less experienced users.

To use malloc() as an example:

@@
type T;
T *x;
@@

  x = malloc(
- sizeof(T)
+ sizeof(*x)
  )

This defines placeholders of an arbitrary type T and a variable x that is a pointer to T. It then searches for constructs x = malloc(sizeof(T)) and replaces the sizeof(T) by sizeof(*x).

For a C program:

#include <stdlib.h>

int
main(void) {
	int *foo = malloc(sizeof(int));
	int *bar = malloc(sizeof(double));
}

running that Coccinelle patch would make the following changes:

$ spatch -sp_file example.cocci test.c 
init_defs_builtins: /usr/lib/coccinelle/standard.h
HANDLING: test.c
diff = 
--- test.c
+++ /tmp/cocci-output-4310-cbc9b9-test.c
@@ -2,6 +2,6 @@
 
 int
 main(void) {
-	int *foo = malloc(sizeof(int));
+	int *foo = malloc(sizeof(*foo));
 	int *bar = malloc(sizeof(double));
 }

If we now wanted to find places where the types differ, we could use the following patch:

@@
type T;
type U;
T *x;
@@

  x = malloc(
(
  sizeof(T)
|
  sizeof(*x)
|
* sizeof(U)
)
  )

The * is an indicator to just “highlight” the match and not perform any replacement. We define an additional type U and then define multiple alternatives. For the correct variants sizeof(T) and sizeof(*x) nothing is reported. For sizeof(U) (where U != T, since T was already handled), a report is created:

$ spatch -sp_file example.cocci test.c 
init_defs_builtins: /usr/lib/coccinelle/standard.h
HANDLING: test.c
diff = 
--- test.c
+++ /tmp/cocci-output-4315-fa4dae-test.c
@@ -3,5 +3,4 @@
 int
 main(void) {
 	int *foo = malloc(sizeof(int));
-	int *bar = malloc(sizeof(double));
 }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coccinelle works very well and is easy to use. I've used it a few times and it's been battle tested against the Linux kernel, which isn't particularly known for it's consistent coding style and is known for it's complex macros etc.
I'm in favor to use the malloc sizeof pattern Tim proposes, and if we don't want to convert existing code (which I think we should), we should at least use that pattern for new code.

zend_hash_init(ht, 0, NULL, NULL, 1);
while (tag_table->Tag != TAG_END_OF_LIST) {
if (!zend_hash_index_add_ptr(ht, tag_table->Tag, tag_table->Desc)) {
Expand Down
4 changes: 2 additions & 2 deletions ext/ffi/ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -3531,12 +3531,12 @@ static zend_ffi *zend_ffi_load(const char *filename, bool preload) /* {{{ */
}

if (!scope) {
scope = malloc(sizeof(zend_ffi_scope));
scope = pmalloc(sizeof(zend_ffi_scope));
scope->symbols = FFI_G(symbols);
scope->tags = FFI_G(tags);

if (!FFI_G(scopes)) {
FFI_G(scopes) = malloc(sizeof(HashTable));
FFI_G(scopes) = pmalloc(sizeof(HashTable));
zend_hash_init(FFI_G(scopes), 0, NULL, zend_ffi_scope_hash_dtor, 1);
}

Expand Down
2 changes: 1 addition & 1 deletion ext/mysqli/mysqli_nonapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ void mysqli_common_connect(INTERNAL_FUNCTION_PARAMETERS, bool is_real_connect, b
} while (0);
}
} else {
plist = calloc(1, sizeof(mysqli_plist_entry));
plist = pcalloc(1, sizeof(mysqli_plist_entry));

zend_ptr_stack_init_ex(&plist->free_links, 1);
zend_register_persistent_resource(ZSTR_VAL(hash_key), ZSTR_LEN(hash_key), plist, php_le_pmysqli());
Expand Down
Loading