@@ -2705,16 +2705,11 @@ static zend_always_inline void tracked_add(zend_mm_heap *heap, void *ptr, size_t
2705
2705
zend_hash_index_add_new (heap -> tracked_allocs , h , & size_zv );
2706
2706
}
2707
2707
2708
- static zend_always_inline size_t tracked_del (zend_mm_heap * heap , void * ptr ) {
2709
- if (!ptr ) {
2710
- return 0 ;
2711
- }
2712
-
2708
+ static zend_always_inline zval * tracked_get_size_zv (zend_mm_heap * heap , void * ptr ) {
2713
2709
zend_ulong h = ((uintptr_t ) ptr ) >> ZEND_MM_ALIGNMENT_LOG2 ;
2714
2710
zval * size_zv = zend_hash_index_find (heap -> tracked_allocs , h );
2715
2711
ZEND_ASSERT (size_zv && "Trying to free pointer not allocated through ZendMM" );
2716
- zend_hash_del_bucket (heap -> tracked_allocs , (Bucket * ) size_zv );
2717
- return Z_LVAL_P (size_zv );
2712
+ return size_zv ;
2718
2713
}
2719
2714
2720
2715
static zend_always_inline void tracked_check_limit (zend_mm_heap * heap , size_t add_size ) {
@@ -2743,18 +2738,35 @@ static void *tracked_malloc(size_t size)
2743
2738
}
2744
2739
2745
2740
static void tracked_free (void * ptr ) {
2741
+ if (!ptr ) {
2742
+ return ;
2743
+ }
2744
+
2746
2745
zend_mm_heap * heap = AG (mm_heap );
2747
- heap -> size -= tracked_del (heap , ptr );
2746
+ zval * size_zv = tracked_get_size_zv (heap , ptr );
2747
+ heap -> size -= Z_LVAL_P (size_zv );
2748
+ zend_hash_del_bucket (heap -> tracked_allocs , (Bucket * ) size_zv );
2748
2749
free (ptr );
2749
2750
}
2750
2751
2751
2752
static void * tracked_realloc (void * ptr , size_t new_size ) {
2752
2753
zend_mm_heap * heap = AG (mm_heap );
2753
- size_t old_size = tracked_del (heap , ptr );
2754
+ zval * old_size_zv = NULL ;
2755
+ size_t old_size = 0 ;
2756
+ if (ptr ) {
2757
+ old_size_zv = tracked_get_size_zv (heap , ptr );
2758
+ old_size = Z_LVAL_P (old_size_zv );
2759
+ }
2760
+
2754
2761
if (new_size > old_size ) {
2755
2762
tracked_check_limit (heap , new_size - old_size );
2756
2763
}
2757
2764
2765
+ /* Delete information about old allocation only after checking the memory limit. */
2766
+ if (old_size_zv ) {
2767
+ zend_hash_del_bucket (heap -> tracked_allocs , (Bucket * ) old_size_zv );
2768
+ }
2769
+
2758
2770
ptr = __zend_realloc (ptr , new_size );
2759
2771
tracked_add (heap , ptr , new_size );
2760
2772
heap -> size += new_size - old_size ;
0 commit comments