Skip to content

Commit 68a51e3

Browse files
committed
Merge branch 'PHP-8.1'
* PHP-8.1: Free cached chunks when the requested memory limit is above real usage
2 parents 0052af2 + c7558e2 commit 68a51e3

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
@@ -2669,7 +2669,20 @@ ZEND_API char* ZEND_FASTCALL zend_strndup(const char *s, size_t length)
26692669
ZEND_API zend_result zend_set_memory_limit(size_t memory_limit)
26702670
{
26712671
#if ZEND_MM_LIMIT
2672-
if (UNEXPECTED(memory_limit < AG(mm_heap)->real_size)) {
2672+
zend_mm_heap *heap = AG(mm_heap);
2673+
2674+
if (UNEXPECTED(memory_limit < heap->real_size)) {
2675+
if (memory_limit >= heap->real_size - heap->cached_chunks_count * ZEND_MM_CHUNK_SIZE) {
2676+
/* free some cached chunks to fit into new memory limit */
2677+
do {
2678+
zend_mm_chunk *p = heap->cached_chunks;
2679+
heap->cached_chunks = p->next;
2680+
zend_mm_chunk_free(heap, p, ZEND_MM_CHUNK_SIZE);
2681+
heap->cached_chunks_count--;
2682+
heap->real_size -= ZEND_MM_CHUNK_SIZE;
2683+
} while (memory_limit < heap->real_size);
2684+
return SUCCESS;
2685+
}
26732686
return FAILURE;
26742687
}
26752688
AG(mm_heap)->limit = memory_limit;

0 commit comments

Comments
 (0)