Use context-less zstd decompression API

Currently we pre-allocate around 128K of zstd state ahead of time,
keeping if for the whole daemon lifetime... Even if we don't need to
decompress any files. As mentioned by Dmitry:

    Granted that tqftpserv handles only few files during the whole
    lifetime and most of devices don't have swap, I think it's fine to
    use non-context versions of the functions. Let's free the memory for
    other software.

Swap ZSTD_decompressDCtx() for ZSTD_decompress() which will allocate and
free the state when needed.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
This commit is contained in:
Emil Velikov 2024-09-19 12:56:21 +01:00
parent 95d0c67322
commit 0f7fd27524
3 changed files with 1 additions and 25 deletions

View File

@ -15,7 +15,6 @@
#include "list.h"
#include "translate.h"
#include "zstd-decompress.h"
#define MAX(x, y) ((x) > (y) ? (x) : (y))
@ -571,8 +570,6 @@ int main(int argc, char **argv)
exit(1);
}
zstd_init();
for (;;) {
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
@ -674,7 +671,6 @@ int main(int argc, char **argv)
}
close(fd);
zstd_free();
return 0;
}

View File

@ -18,24 +18,6 @@
#include "zstd-decompress.h"
static ZSTD_DCtx *zstd_context = NULL;
/**
* zstd_init() - set up state for decompression. Needs to be called before zstd_decompress_file()
*/
void zstd_init()
{
zstd_context = ZSTD_createDCtx();
}
/**
* zstd_free() - free state used for decompression. zstd_decompress_file() may not be called after this
*/
void zstd_free()
{
ZSTD_freeDCtx(zstd_context);
}
/**
* zstd_decompress_file() - decompress a zstd-compressed file
* @filename: path to a file to decompress
@ -86,7 +68,7 @@ int zstd_decompress_file(const char *filename)
return -1;
}
const size_t return_size = ZSTD_decompressDCtx(zstd_context, decompressed_buffer, decompressed_size, compressed_buffer, file_size);
const size_t return_size = ZSTD_decompress(decompressed_buffer, decompressed_size, compressed_buffer, file_size);
if (ZSTD_isError(return_size)) {
fprintf(stderr, "ZSTD_decompress failed: %s\n", ZSTD_getErrorName(return_size));
free(decompressed_buffer);

View File

@ -8,8 +8,6 @@
#include <stdbool.h>
void zstd_init();
void zstd_free();
int zstd_decompress_file(const char *filename);
#endif