From e34debd07ffb65bc80d2a75d11ef209f29de371f Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Tue, 7 Dec 2021 15:26:44 +0100 Subject: [PATCH] Fix #81585: Reused cached_chunks are not counted to heap size Whenever we reuse or delete a `cached_chunk`, we need to adjust the memory stats like we do when allocating or deleting a non cached chunk. --- Zend/zend_alloc.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c index 0d3e41776d732..2baf07710982f 100644 --- a/Zend/zend_alloc.c +++ b/Zend/zend_alloc.c @@ -978,18 +978,18 @@ static void *zend_mm_alloc_pages(zend_mm_heap *heap, uint32_t pages_count ZEND_F return NULL; } } + } #if ZEND_MM_STAT - do { - size_t size = heap->real_size + ZEND_MM_CHUNK_SIZE; - size_t peak = MAX(heap->real_peak, size); - heap->real_size = size; - heap->real_peak = peak; - } while (0); + do { + size_t size = heap->real_size + ZEND_MM_CHUNK_SIZE; + size_t peak = MAX(heap->real_peak, size); + heap->real_size = size; + heap->real_peak = peak; + } while (0); #elif ZEND_MM_LIMIT heap->real_size += ZEND_MM_CHUNK_SIZE; #endif - } heap->chunks_count++; if (heap->chunks_count > heap->peak_chunks_count) { heap->peak_chunks_count = heap->chunks_count; @@ -1060,6 +1060,9 @@ static zend_always_inline void zend_mm_delete_chunk(zend_mm_heap *heap, zend_mm_ chunk->next->prev = chunk->prev; chunk->prev->next = chunk->next; heap->chunks_count--; +#if ZEND_MM_STAT || ZEND_MM_LIMIT + heap->real_size -= ZEND_MM_CHUNK_SIZE; +#endif if (heap->chunks_count + heap->cached_chunks_count < heap->avg_chunks_count + 0.1 || (heap->chunks_count == heap->last_chunks_delete_boundary && heap->last_chunks_delete_count >= 4)) { @@ -1068,9 +1071,6 @@ static zend_always_inline void zend_mm_delete_chunk(zend_mm_heap *heap, zend_mm_ chunk->next = heap->cached_chunks; heap->cached_chunks = chunk; } else { -#if ZEND_MM_STAT || ZEND_MM_LIMIT - heap->real_size -= ZEND_MM_CHUNK_SIZE; -#endif if (!heap->cached_chunks) { if (heap->chunks_count != heap->last_chunks_delete_boundary) { heap->last_chunks_delete_boundary = heap->chunks_count;