Skip to content

Commit e5d60dd

Browse files
deduplicate observer calling
1 parent 2256cf7 commit e5d60dd

File tree

1 file changed

+14
-27
lines changed

1 file changed

+14
-27
lines changed

Zend/zend_alloc.c

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,6 +2503,17 @@ ZEND_API bool is_zend_ptr(const void *ptr)
25032503

25042504
#if ZEND_MM_CUSTOM
25052505

2506+
#define HANDLE_OBSERVERS(observer_function, ...) \
2507+
if (use_custom_heap & ZEND_MM_CUSTOM_HEAP_OBSERVED) { \
2508+
zend_mm_observer *current = heap->observers; \
2509+
while (current != NULL) { \
2510+
if (current->observer_function != NULL) { \
2511+
current->observer_function(__VA_ARGS__ ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); \
2512+
} \
2513+
current = current->next; \
2514+
} \
2515+
}
2516+
25062517
static ZEND_COLD void* ZEND_FASTCALL _malloc_custom(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
25072518
{
25082519
void *ptr;
@@ -2516,15 +2527,7 @@ static ZEND_COLD void* ZEND_FASTCALL _malloc_custom(size_t size ZEND_FILE_LINE_D
25162527
// no custom memory manager, only observer present
25172528
ptr = zend_mm_alloc_heap(AG(mm_heap), size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
25182529
}
2519-
if (use_custom_heap & ZEND_MM_CUSTOM_HEAP_OBSERVED) {
2520-
zend_mm_observer *current = heap->observers;
2521-
while (current != NULL) {
2522-
if (current->malloc != NULL) {
2523-
current->malloc(size, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
2524-
}
2525-
current = current->next;
2526-
}
2527-
}
2530+
HANDLE_OBSERVERS(malloc, size, ptr)
25282531
return ptr;
25292532
}
25302533

@@ -2533,15 +2536,7 @@ static ZEND_COLD void ZEND_FASTCALL _efree_custom(void *ptr ZEND_FILE_LINE_DC ZE
25332536
zend_mm_heap *heap = AG(mm_heap);
25342537
int use_custom_heap = heap->use_custom_heap;
25352538

2536-
if (use_custom_heap & ZEND_MM_CUSTOM_HEAP_OBSERVED) {
2537-
zend_mm_observer *current = heap->observers;
2538-
while (current != NULL) {
2539-
if (current->free != NULL) {
2540-
current->free(ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
2541-
}
2542-
current = current->next;
2543-
}
2544-
}
2539+
HANDLE_OBSERVERS(free, ptr)
25452540

25462541
if (ZEND_DEBUG && use_custom_heap & ZEND_MM_CUSTOM_HEAP_DEBUG) {
25472542
heap->custom_heap.debug._free(ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
@@ -2566,15 +2561,7 @@ static ZEND_COLD void* ZEND_FASTCALL _realloc_custom(void *ptr, size_t size ZEND
25662561
// no custom memory manager, only observer present
25672562
new_ptr = zend_mm_realloc_heap(AG(mm_heap), ptr, size, 0, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
25682563
}
2569-
if (use_custom_heap & ZEND_MM_CUSTOM_HEAP_OBSERVED) {
2570-
zend_mm_observer *current = heap->observers;
2571-
while (current != NULL) {
2572-
if (current->realloc != NULL) {
2573-
current->realloc(ptr, size, new_ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
2574-
}
2575-
current = current->next;
2576-
}
2577-
}
2564+
HANDLE_OBSERVERS(realloc, ptr, size, new_ptr)
25782565
return new_ptr;
25792566
}
25802567
#endif

0 commit comments

Comments
 (0)