Skip to content

Free cached chunks when the requested memory limit is above real usage. #8053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Zend/zend_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2669,7 +2669,19 @@ ZEND_API char* ZEND_FASTCALL zend_strndup(const char *s, size_t length)
ZEND_API zend_result zend_set_memory_limit(size_t memory_limit)
{
#if ZEND_MM_LIMIT
if (UNEXPECTED(memory_limit < AG(mm_heap)->real_size)) {
zend_mm_heap *heap = AG(mm_heap);
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