Skip to content

Commit c7558e2

Browse files
committed
Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0: Free cached chunks when the requested memory limit is above real usage
2 parents 4a630e6 + c035298 commit c7558e2

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

Zend/zend_alloc.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2661,7 +2661,20 @@ ZEND_API char* ZEND_FASTCALL zend_strndup(const char *s, size_t length)
26612661
ZEND_API zend_result zend_set_memory_limit(size_t memory_limit)
26622662
{
26632663
#if ZEND_MM_LIMIT
2664-
if (UNEXPECTED(memory_limit < AG(mm_heap)->real_size)) {
2664+
zend_mm_heap *heap = AG(mm_heap);
2665+
2666+
if (UNEXPECTED(memory_limit < heap->real_size)) {
2667+
if (memory_limit >= heap->real_size - heap->cached_chunks_count * ZEND_MM_CHUNK_SIZE) {
2668+
/* free some cached chunks to fit into new memory limit */
2669+
do {
2670+
zend_mm_chunk *p = heap->cached_chunks;
2671+
heap->cached_chunks = p->next;
2672+
zend_mm_chunk_free(heap, p, ZEND_MM_CHUNK_SIZE);
2673+
heap->cached_chunks_count--;
2674+
heap->real_size -= ZEND_MM_CHUNK_SIZE;
2675+
} while (memory_limit < heap->real_size);
2676+
return SUCCESS;
2677+
}
26652678
return FAILURE;
26662679
}
26672680
AG(mm_heap)->limit = memory_limit;

0 commit comments

Comments
 (0)