Skip to content

Commit

Permalink
Free cached chunks when the requested memory limit is above real usage
Browse files Browse the repository at this point in the history
  • Loading branch information
dstogov committed Feb 8, 2022
1 parent 1d48da6 commit c035298
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Zend/zend_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2660,10 +2660,23 @@ ZEND_API char* ZEND_FASTCALL zend_strndup(const char *s, size_t length)
ZEND_API zend_result zend_set_memory_limit_ex(size_t memory_limit)
{
#if ZEND_MM_LIMIT
zend_mm_heap *heap = AG(mm_heap);

if (memory_limit < ZEND_MM_CHUNK_SIZE) {
memory_limit = ZEND_MM_CHUNK_SIZE;
}
if (UNEXPECTED(memory_limit < AG(mm_heap)->real_size)) {
if (UNEXPECTED(memory_limit < heap->real_size)) {
if (memory_limit >= heap->real_size - heap->cached_chunks_count * ZEND_MM_CHUNK_SIZE) {
/* free some cached chunks to fit into new memory limit */
do {
zend_mm_chunk *p = heap->cached_chunks;
heap->cached_chunks = p->next;
zend_mm_chunk_free(heap, p, ZEND_MM_CHUNK_SIZE);
heap->cached_chunks_count--;
heap->real_size -= ZEND_MM_CHUNK_SIZE;
} while (memory_limit < heap->real_size);
return SUCCESS;
}
return FAILURE;
}
AG(mm_heap)->limit = memory_limit;
Expand Down

0 comments on commit c035298

Please sign in to comment.