Search code examples
c++embeddedzlib

ZLIB with small memory usage


I'm working on embedded device (STM32) with < 5kb free FLASH memory left. I'm trying to compress string with zlib library. I created function HERE and it returns -2 (Z_STREAM_ERROR).

What I did:

  • On zconf.h, I changed value of MAX_MEM_LEVEL to 1 and MAX_WBITS to 5 to lower memory usage. But i still returns -2 (Z_STREAM_ERROR).

Then I found this On deflate.c which I cannot lower MAX_WBITS less than 8:

if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
        windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
        strategy < 0 || strategy > Z_FIXED || (windowBits == 8 && wrap != 1)) {
        return Z_STREAM_ERROR;
}
if (windowBits == 8) windowBits = 9;  /* until 256-byte window bug fixed */

Then I set it to MAX_MEM_LEVEL to 1 and MAX_WBITS to 8 but when I try to compile it always returns region FLASH overflowed by 15960 bytes. Looks like I run out of memory.

Then I tried another example:

  • Follow with zpipe.c sample with defined CHUNK to 1024.

Result still the same, region FLASH overflowed by 15960 bytes. Is there any other way to do it with FLASH memory only 5kb left?

Note:

  • RAM is also 5kb left.

Solution

  • No. The minimum amount of RAM required by deflate is 9K. You might try a different compressor, such as lz4.

    The deflate code itself compiles to 33K on my machine (x86_64 ISA), with speed optimization. I tried compiling with aggressive space optimization, which got it down to 25K.