Skip to content

Commit 14873dd

Browse files
Drop zend_mm_set_custom_debug_handlers() (#13457)
Simplifies zend_mm_set_custom_debug_handlers to just use zend_mm_set_custom_handlers(), saving some conditionals when the Zend allocator is not used.
1 parent ba27fab commit 14873dd

File tree

8 files changed

+114
-137
lines changed

8 files changed

+114
-137
lines changed

UPGRADING.INTERNALS

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,44 @@ PHP 8.4 INTERNALS UPGRADE NOTES
4040
* The inet_aton() and win32/inet.h on Windows have been removed. Use Windows
4141
native inet_pton() from ws2tcpip.h.
4242

43+
* zend_mm_set_custom_debug_handlers() has been removed from ZendMM, use
44+
zend_mm_set_custom_handlers() instead which now supports DEBUG builds
45+
46+
* zend_mm_set_custom_handlers() has changed its signature from
47+
void()(zend_mm_heap *heap,
48+
void* (*_malloc)(size_t),
49+
void (*_free)(void*),
50+
void* (*_realloc)(void*, size_t))
51+
to
52+
void()(zend_mm_heap *heap,
53+
void* (*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
54+
void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
55+
void* (*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC))
56+
57+
* zend_mm_get_custom_handlers() has changed its signature from
58+
void()(zend_mm_heap *heap,
59+
void* (**_malloc)(size_t),
60+
void (**_free)(void*),
61+
void* (**_realloc)(void*, size_t))
62+
to
63+
void()(zend_mm_heap *heap,
64+
void* (**_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
65+
void (**_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
66+
void* (**_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC))
67+
68+
* __zend_malloc() has changed their signature from
69+
void(*)(size_t) to
70+
void(*)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
71+
72+
* __zend_calloc() has changed their signature from
73+
void(*)(size_t, size_t) to
74+
void(*)(size_t, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
75+
76+
* __zend_realloc() has changed their signature from
77+
void(*)(void *, size_t) to
78+
void(*)(void *, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
79+
80+
4381
========================
4482
2. Build system changes
4583
========================

Zend/zend_alloc.c

Lines changed: 48 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -268,17 +268,10 @@ struct _zend_mm_heap {
268268
int last_chunks_delete_boundary; /* number of chunks after last deletion */
269269
int last_chunks_delete_count; /* number of deletion over the last boundary */
270270
#if ZEND_MM_CUSTOM
271-
union {
272-
struct {
273-
void *(*_malloc)(size_t);
274-
void (*_free)(void*);
275-
void *(*_realloc)(void*, size_t);
276-
} std;
277-
struct {
278-
void *(*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
279-
void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
280-
void *(*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
281-
} debug;
271+
struct {
272+
void *(*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
273+
void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
274+
void *(*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
282275
} custom_heap;
283276
HashTable *tracked_allocs;
284277
#endif
@@ -2261,7 +2254,7 @@ static void zend_mm_check_leaks(zend_mm_heap *heap)
22612254
#endif
22622255

22632256
#if ZEND_MM_CUSTOM
2264-
static void *tracked_malloc(size_t size);
2257+
static void *tracked_malloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
22652258
static void tracked_free_all(void);
22662259
#endif
22672260

@@ -2272,7 +2265,7 @@ void zend_mm_shutdown(zend_mm_heap *heap, bool full, bool silent)
22722265

22732266
#if ZEND_MM_CUSTOM
22742267
if (heap->use_custom_heap) {
2275-
if (heap->custom_heap.std._malloc == tracked_malloc) {
2268+
if (heap->custom_heap._malloc == tracked_malloc) {
22762269
if (silent) {
22772270
tracked_free_all();
22782271
}
@@ -2281,17 +2274,13 @@ void zend_mm_shutdown(zend_mm_heap *heap, bool full, bool silent)
22812274
zend_hash_destroy(heap->tracked_allocs);
22822275
free(heap->tracked_allocs);
22832276
/* Make sure the heap free below does not use tracked_free(). */
2284-
heap->custom_heap.std._free = free;
2277+
heap->custom_heap._free = __zend_free;
22852278
}
22862279
heap->size = 0;
22872280
}
22882281

22892282
if (full) {
2290-
if (ZEND_DEBUG && heap->use_custom_heap == ZEND_MM_CUSTOM_HEAP_DEBUG) {
2291-
heap->custom_heap.debug._free(heap ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
2292-
} else {
2293-
heap->custom_heap.std._free(heap);
2294-
}
2283+
heap->custom_heap._free(heap ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
22952284
}
22962285
return;
22972286
}
@@ -2412,7 +2401,7 @@ ZEND_API size_t ZEND_FASTCALL _zend_mm_block_size(zend_mm_heap *heap, void *ptr
24122401
{
24132402
#if ZEND_MM_CUSTOM
24142403
if (UNEXPECTED(heap->use_custom_heap)) {
2415-
if (heap->custom_heap.std._malloc == tracked_malloc) {
2404+
if (heap->custom_heap._malloc == tracked_malloc) {
24162405
zend_ulong h = ((uintptr_t) ptr) >> ZEND_MM_ALIGNMENT_LOG2;
24172406
zval *size_zv = zend_hash_index_find(heap->tracked_allocs, h);
24182407
if (size_zv) {
@@ -2455,7 +2444,7 @@ ZEND_API bool is_zend_ptr(const void *ptr)
24552444
{
24562445
#if ZEND_MM_CUSTOM
24572446
if (AG(mm_heap)->use_custom_heap) {
2458-
if (AG(mm_heap)->custom_heap.std._malloc == tracked_malloc) {
2447+
if (AG(mm_heap)->custom_heap._malloc == tracked_malloc) {
24592448
zend_ulong h = ((uintptr_t) ptr) >> ZEND_MM_ALIGNMENT_LOG2;
24602449
zval *size_zv = zend_hash_index_find(AG(mm_heap)->tracked_allocs, h);
24612450
if (size_zv) {
@@ -2492,48 +2481,18 @@ ZEND_API bool is_zend_ptr(const void *ptr)
24922481
return 0;
24932482
}
24942483

2495-
#if ZEND_MM_CUSTOM
2496-
2497-
static ZEND_COLD void* ZEND_FASTCALL _malloc_custom(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
2498-
{
2499-
if (ZEND_DEBUG && AG(mm_heap)->use_custom_heap == ZEND_MM_CUSTOM_HEAP_DEBUG) {
2500-
return AG(mm_heap)->custom_heap.debug._malloc(size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
2501-
} else {
2502-
return AG(mm_heap)->custom_heap.std._malloc(size);
2503-
}
2504-
}
2505-
2506-
static ZEND_COLD void ZEND_FASTCALL _efree_custom(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
2507-
{
2508-
if (ZEND_DEBUG && AG(mm_heap)->use_custom_heap == ZEND_MM_CUSTOM_HEAP_DEBUG) {
2509-
AG(mm_heap)->custom_heap.debug._free(ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
2510-
} else {
2511-
AG(mm_heap)->custom_heap.std._free(ptr);
2512-
}
2513-
}
2514-
2515-
static ZEND_COLD void* ZEND_FASTCALL _realloc_custom(void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
2516-
{
2517-
if (ZEND_DEBUG && AG(mm_heap)->use_custom_heap == ZEND_MM_CUSTOM_HEAP_DEBUG) {
2518-
return AG(mm_heap)->custom_heap.debug._realloc(ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
2519-
} else {
2520-
return AG(mm_heap)->custom_heap.std._realloc(ptr, size);
2521-
}
2522-
}
2523-
#endif
2524-
25252484
#if !ZEND_DEBUG && defined(HAVE_BUILTIN_CONSTANT_P)
25262485
#undef _emalloc
25272486

25282487
#if ZEND_MM_CUSTOM
25292488
# define ZEND_MM_CUSTOM_ALLOCATOR(size) do { \
25302489
if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { \
2531-
return _malloc_custom(size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); \
2490+
return AG(mm_heap)->custom_heap._malloc(size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); \
25322491
} \
25332492
} while (0)
25342493
# define ZEND_MM_CUSTOM_DEALLOCATOR(ptr) do { \
25352494
if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { \
2536-
_efree_custom(ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); \
2495+
AG(mm_heap)->custom_heap._free(ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); \
25372496
return; \
25382497
} \
25392498
} while (0)
@@ -2618,7 +2577,7 @@ ZEND_API void* ZEND_FASTCALL _emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LI
26182577
{
26192578
#if ZEND_MM_CUSTOM
26202579
if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) {
2621-
return _malloc_custom(size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
2580+
return AG(mm_heap)->custom_heap._malloc(size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); \
26222581
}
26232582
#endif
26242583
return zend_mm_alloc_heap(AG(mm_heap), size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
@@ -2628,7 +2587,7 @@ ZEND_API void ZEND_FASTCALL _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_OR
26282587
{
26292588
#if ZEND_MM_CUSTOM
26302589
if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) {
2631-
_efree_custom(ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
2590+
AG(mm_heap)->custom_heap._free(ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
26322591
return;
26332592
}
26342593
#endif
@@ -2639,7 +2598,7 @@ ZEND_API void* ZEND_FASTCALL _erealloc(void *ptr, size_t size ZEND_FILE_LINE_DC
26392598
{
26402599
#if ZEND_MM_CUSTOM
26412600
if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) {
2642-
return _realloc_custom(ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
2601+
return AG(mm_heap)->custom_heap._realloc(ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
26432602
}
26442603
#endif
26452604
return zend_mm_realloc_heap(AG(mm_heap), ptr, size, 0, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
@@ -2649,7 +2608,7 @@ ZEND_API void* ZEND_FASTCALL _erealloc2(void *ptr, size_t size, size_t copy_size
26492608
{
26502609
#if ZEND_MM_CUSTOM
26512610
if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) {
2652-
return _realloc_custom(ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
2611+
return AG(mm_heap)->custom_heap._realloc(ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
26532612
}
26542613
#endif
26552614
return zend_mm_realloc_heap(AG(mm_heap), ptr, size, 1, copy_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
@@ -2844,7 +2803,7 @@ static zend_always_inline void tracked_check_limit(zend_mm_heap *heap, size_t ad
28442803
}
28452804
}
28462805

2847-
static void *tracked_malloc(size_t size)
2806+
static void *tracked_malloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
28482807
{
28492808
zend_mm_heap *heap = AG(mm_heap);
28502809
tracked_check_limit(heap, size);
@@ -2859,7 +2818,7 @@ static void *tracked_malloc(size_t size)
28592818
return ptr;
28602819
}
28612820

2862-
static void tracked_free(void *ptr) {
2821+
static void tracked_free(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) {
28632822
if (!ptr) {
28642823
return;
28652824
}
@@ -2871,7 +2830,7 @@ static void tracked_free(void *ptr) {
28712830
free(ptr);
28722831
}
28732832

2874-
static void *tracked_realloc(void *ptr, size_t new_size) {
2833+
static void *tracked_realloc(void *ptr, size_t new_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) {
28752834
zend_mm_heap *heap = AG(mm_heap);
28762835
zval *old_size_zv = NULL;
28772836
size_t old_size = 0;
@@ -2889,7 +2848,7 @@ static void *tracked_realloc(void *ptr, size_t new_size) {
28892848
zend_hash_del_bucket(heap->tracked_allocs, (Bucket *) old_size_zv);
28902849
}
28912850

2892-
ptr = __zend_realloc(ptr, new_size);
2851+
ptr = __zend_realloc(ptr, new_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
28932852
tracked_add(heap, ptr, new_size);
28942853
heap->size += new_size - old_size;
28952854
return ptr;
@@ -2921,14 +2880,14 @@ static void alloc_globals_ctor(zend_alloc_globals *alloc_globals)
29212880

29222881
if (!tracked) {
29232882
/* Use system allocator. */
2924-
mm_heap->custom_heap.std._malloc = __zend_malloc;
2925-
mm_heap->custom_heap.std._free = free;
2926-
mm_heap->custom_heap.std._realloc = __zend_realloc;
2883+
mm_heap->custom_heap._malloc = __zend_malloc;
2884+
mm_heap->custom_heap._free = __zend_free;
2885+
mm_heap->custom_heap._realloc = __zend_realloc;
29272886
} else {
29282887
/* Use system allocator and track allocations for auto-free. */
2929-
mm_heap->custom_heap.std._malloc = tracked_malloc;
2930-
mm_heap->custom_heap.std._free = tracked_free;
2931-
mm_heap->custom_heap.std._realloc = tracked_realloc;
2888+
mm_heap->custom_heap._malloc = tracked_malloc;
2889+
mm_heap->custom_heap._free = tracked_free;
2890+
mm_heap->custom_heap._realloc = tracked_realloc;
29322891
mm_heap->tracked_allocs = malloc(sizeof(HashTable));
29332892
zend_hash_init(mm_heap->tracked_allocs, 1024, NULL, NULL, 1);
29342893
}
@@ -2990,9 +2949,9 @@ ZEND_API bool zend_mm_is_custom_heap(zend_mm_heap *new_heap)
29902949
}
29912950

29922951
ZEND_API void zend_mm_set_custom_handlers(zend_mm_heap *heap,
2993-
void* (*_malloc)(size_t),
2994-
void (*_free)(void*),
2995-
void* (*_realloc)(void*, size_t))
2952+
void* (*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
2953+
void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
2954+
void* (*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC))
29962955
{
29972956
#if ZEND_MM_CUSTOM
29982957
zend_mm_heap *_heap = (zend_mm_heap*)heap;
@@ -3001,25 +2960,25 @@ ZEND_API void zend_mm_set_custom_handlers(zend_mm_heap *heap,
30012960
_heap->use_custom_heap = ZEND_MM_CUSTOM_HEAP_NONE;
30022961
} else {
30032962
_heap->use_custom_heap = ZEND_MM_CUSTOM_HEAP_STD;
3004-
_heap->custom_heap.std._malloc = _malloc;
3005-
_heap->custom_heap.std._free = _free;
3006-
_heap->custom_heap.std._realloc = _realloc;
2963+
_heap->custom_heap._malloc = _malloc;
2964+
_heap->custom_heap._free = _free;
2965+
_heap->custom_heap._realloc = _realloc;
30072966
}
30082967
#endif
30092968
}
30102969

30112970
ZEND_API void zend_mm_get_custom_handlers(zend_mm_heap *heap,
3012-
void* (**_malloc)(size_t),
3013-
void (**_free)(void*),
3014-
void* (**_realloc)(void*, size_t))
2971+
void* (**_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
2972+
void (**_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
2973+
void* (**_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC))
30152974
{
30162975
#if ZEND_MM_CUSTOM
30172976
zend_mm_heap *_heap = (zend_mm_heap*)heap;
30182977

30192978
if (heap->use_custom_heap) {
3020-
*_malloc = _heap->custom_heap.std._malloc;
3021-
*_free = _heap->custom_heap.std._free;
3022-
*_realloc = _heap->custom_heap.std._realloc;
2979+
*_malloc = _heap->custom_heap._malloc;
2980+
*_free = _heap->custom_heap._free;
2981+
*_realloc = _heap->custom_heap._realloc;
30232982
} else {
30242983
*_malloc = NULL;
30252984
*_free = NULL;
@@ -3032,23 +2991,6 @@ ZEND_API void zend_mm_get_custom_handlers(zend_mm_heap *heap,
30322991
#endif
30332992
}
30342993

3035-
#if ZEND_DEBUG
3036-
ZEND_API void zend_mm_set_custom_debug_handlers(zend_mm_heap *heap,
3037-
void* (*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
3038-
void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
3039-
void* (*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC))
3040-
{
3041-
#if ZEND_MM_CUSTOM
3042-
zend_mm_heap *_heap = (zend_mm_heap*)heap;
3043-
3044-
_heap->use_custom_heap = ZEND_MM_CUSTOM_HEAP_DEBUG;
3045-
_heap->custom_heap.debug._malloc = _malloc;
3046-
_heap->custom_heap.debug._free = _free;
3047-
_heap->custom_heap.debug._realloc = _realloc;
3048-
#endif
3049-
}
3050-
#endif
3051-
30522994
ZEND_API zend_mm_storage *zend_mm_get_storage(zend_mm_heap *heap)
30532995
{
30542996
#if ZEND_MM_STORAGE
@@ -3134,7 +3076,7 @@ ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_handlers *handlers, void
31343076
#endif
31353077
}
31363078

3137-
ZEND_API void * __zend_malloc(size_t len)
3079+
ZEND_API void * __zend_malloc(size_t len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
31383080
{
31393081
void *tmp = malloc(len);
31403082
if (EXPECTED(tmp || !len)) {
@@ -3143,17 +3085,17 @@ ZEND_API void * __zend_malloc(size_t len)
31433085
zend_out_of_memory();
31443086
}
31453087

3146-
ZEND_API void * __zend_calloc(size_t nmemb, size_t len)
3088+
ZEND_API void * __zend_calloc(size_t nmemb, size_t len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
31473089
{
31483090
void *tmp;
31493091

31503092
len = zend_safe_address_guarded(nmemb, len, 0);
3151-
tmp = __zend_malloc(len);
3093+
tmp = __zend_malloc(len ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
31523094
memset(tmp, 0, len);
31533095
return tmp;
31543096
}
31553097

3156-
ZEND_API void * __zend_realloc(void *p, size_t len)
3098+
ZEND_API void * __zend_realloc(void *p, size_t len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
31573099
{
31583100
p = realloc(p, len);
31593101
if (EXPECTED(p || !len)) {
@@ -3162,6 +3104,12 @@ ZEND_API void * __zend_realloc(void *p, size_t len)
31623104
zend_out_of_memory();
31633105
}
31643106

3107+
ZEND_API void __zend_free(void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
3108+
{
3109+
free(p);
3110+
return;
3111+
}
3112+
31653113
ZEND_API char * __zend_strdup(const char *s)
31663114
{
31673115
char *tmp = strdup(s);

0 commit comments

Comments
 (0)