From 6a1e526ea7af6f10dc61e59a2187a8423df9b244 Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Thu, 24 Sep 2020 17:52:16 -0700 Subject: [PATCH] [lib] Add ZSTD_COMPRESS_HEAPMODE tuning parameter --- contrib/linux-kernel/Makefile | 1 + lib/compress/zstd_compress.c | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/contrib/linux-kernel/Makefile b/contrib/linux-kernel/Makefile index c04c3633b..1d6fd96d2 100644 --- a/contrib/linux-kernel/Makefile +++ b/contrib/linux-kernel/Makefile @@ -36,6 +36,7 @@ libzstd: -DSTATIC_BMI2=0 \ -DZSTD_ADDRESS_SANITIZER=0 \ -DZSTD_MEMORY_SANITIZER=0 \ + -DZSTD_COMPRESS_HEAPMODE=1 \ -UZSTD_NO_INLINE \ -UNO_PREFETCH \ -U__cplusplus \ diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 7fe539c3d..b6edba207 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -29,6 +29,19 @@ #include "zstd_ldm.h" #include "zstd_compress_superblock.h" +/* *************************************************************** +* Tuning parameters +*****************************************************************/ +/*! + * COMPRESS_HEAPMODE : + * Select how default decompression function ZSTD_compress() allocates its context, + * on stack (0, default), or into heap (1). + * Note that functions with explicit context such as ZSTD_compressCCtx() are unaffected. + */ +#ifndef ZSTD_COMPRESS_HEAPMODE +# define ZSTD_COMPRESS_HEAPMODE 0 +#endif + /*-************************************* * Helper functions @@ -3370,10 +3383,17 @@ size_t ZSTD_compress(void* dst, size_t dstCapacity, int compressionLevel) { size_t result; +#if ZSTD_COMPRESS_HEAPMODE + ZSTD_CCtx* cctx = ZSTD_createCCtx(); + RETURN_ERROR_IF(!cctx, memory_allocation, "ZSTD_createCCtx failed"); + result = ZSTD_compressCCtx(cctx, dst, dstCapacity, src, srcSize, compressionLevel); + ZSTD_freeCCtx(cctx);; +#else ZSTD_CCtx ctxBody; ZSTD_initCCtx(&ctxBody, ZSTD_defaultCMem); result = ZSTD_compressCCtx(&ctxBody, dst, dstCapacity, src, srcSize, compressionLevel); ZSTD_freeCCtxContent(&ctxBody); /* can't free ctxBody itself, as it's on stack; free only heap content */ +#endif return result; }