From 037ea175409d811f49017d0bba9a84af372b09a5 Mon Sep 17 00:00:00 2001 From: Arndt Kaiser <42686074+madmajestro@users.noreply.github.com> Date: Sat, 1 Mar 2025 23:17:39 +0100 Subject: [PATCH] Fix memory size passed to expunge() The function apc_sma_get_avail_size() now takes care of computing the real size to achieve better separation of layers. In addition, the computation of the real size has been fixed. --- apc_sma.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/apc_sma.c b/apc_sma.c index a4534012..8811b104 100644 --- a/apc_sma.c +++ b/apc_sma.c @@ -449,7 +449,7 @@ PHP_APCU_API void *apc_sma_malloc_ex(apc_sma_t *sma, size_t n, size_t *allocated /* Expunge cache in hope of freeing up memory, but only once */ if (!nuked) { - sma->expunge(*sma->data, n+fragment); + sma->expunge(*sma->data, n); nuked = 1; goto restart; } @@ -629,12 +629,13 @@ PHP_APCU_API size_t apc_sma_get_avail_mem(apc_sma_t* sma) { PHP_APCU_API zend_bool apc_sma_get_avail_size(apc_sma_t* sma, size_t size) { int32_t i; + size_t realsize = ALIGNWORD(size + ALIGNWORD(sizeof(struct block_t))); for (i = 0; i < sma->num; i++) { sma_header_t *shmaddr = SMA_HDR(sma, i); /* If total size of available memory is too small, we can skip the contiguous-block check */ - if (shmaddr->avail < size) { + if (shmaddr->avail < realsize) { continue; } @@ -645,7 +646,7 @@ PHP_APCU_API zend_bool apc_sma_get_avail_size(apc_sma_t* sma, size_t size) { while (cur->fnext) { cur = BLOCKAT(cur->fnext); - if (cur->size >= size) { + if (cur->size >= realsize) { SMA_UNLOCK(sma, i); return 1; }