From 2099836c2b05dbd0432ff7182b599809523651c2 Mon Sep 17 00:00:00 2001 From: notaz Date: Sun, 29 Sep 2024 03:58:24 +0300 Subject: [PATCH] release lzma init buffers The allocator keeps buffers for reuse but only if the size matches. However no buffers of that size are ever needed again after init so that memory is held by the allocator and effectively wasted hurting low-ram devices. --- src/libchdr_chd.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/libchdr_chd.c b/src/libchdr_chd.c index a1dcff7..69d8a4f 100644 --- a/src/libchdr_chd.c +++ b/src/libchdr_chd.c @@ -459,6 +459,28 @@ static void lzma_allocator_free(void* p ) } } +/*------------------------------------------------- + * lzma_allocator_free_unused + * free unused buffers only + *------------------------------------------------- + */ + +static void lzma_allocator_free_unused(lzma_allocator *codec) +{ + int i; + + for (i = 0; i < MAX_LZMA_ALLOCS; i++) + { + uint32_t *ptr = codec->allocptr[i]; + if (ptr && (*ptr & 1) == 0) + { + free(codec->allocptr[i]); + codec->allocptr[i] = NULL; + codec->allocptr2[i] = NULL; + } + } +} + /*------------------------------------------------- * lzma_fast_alloc - fast malloc for lzma, which * allocates and frees memory frequently @@ -600,6 +622,7 @@ static chd_error lzma_codec_init(void* codec, uint32_t hunkbytes) return CHDERR_DECOMPRESSION_ERROR; } LzmaEnc_Destroy(enc, (ISzAlloc*)alloc, (ISzAlloc*)alloc); + lzma_allocator_free_unused(alloc); /* do memory allocations */ if (LzmaDec_Allocate(&lzma_codec->decoder, decoder_props, LZMA_PROPS_SIZE, (ISzAlloc*)alloc) != SZ_OK)