[PATCH v3 03/18] lib/lz4: add lz4hc_mem_compress()
From: Michal Wilczynski
Date: Wed Dec 02 2026 - 12:34:07 EST
LZ4HC states are LZ4HC_MEM_COMPRESS bytes at every level. That stops
being true when the vendored compressor comes in later in this series:
upstream's levels 10 and up use an optimal parser whose ~64K workspace
follows the state, so a state for those levels is larger.
Add lz4hc_mem_compress(), which gives the size of a state for a
compression level, so that zram and f2fs can size theirs by it before
the switch. It returns LZ4HC_MEM_COMPRESS at every level for now.
Signed-off-by: Michal Wilczynski <m.wilczynski@xxxxxxxxxxx>
---
include/linux/lz4.h | 18 ++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)
diff --git a/include/linux/lz4.h b/include/linux/lz4.h
index 0df69e2b1ad523c291e3a96ab5595df9239f7252..156f493c4a2ca5eb689d9beaf6d25611dd4e9c83 100644
--- a/include/linux/lz4.h
+++ b/include/linux/lz4.h
@@ -156,6 +156,19 @@ typedef union {
#define LZ4_MEM_COMPRESS LZ4_STREAMSIZE
#define LZ4HC_MEM_COMPRESS LZ4_STREAMHCSIZE
+/**
+ * lz4hc_mem_compress() - size of an LZ4HC state for a compression level
+ * @compressionLevel: the level the state is used at
+ *
+ * Return: the size to allocate for the @wrkmem of LZ4_compress_HC() and
+ * for a stream passed to LZ4_resetStreamHC() at @compressionLevel.
+ * It is LZ4HC_MEM_COMPRESS at every level for now.
+ */
+static inline size_t lz4hc_mem_compress(int compressionLevel)
+{
+ return LZ4HC_MEM_COMPRESS;
+}
+
/*-************************************************************************
* Compression Functions
**************************************************************************/
@@ -314,7 +327,7 @@ int LZ4_decompress_safe_partial(const char *source, char *dest,
* value between 1 and LZ4HC_MAX_CLEVEL will work.
* Values >LZ4HC_MAX_CLEVEL behave the same as 16.
* @wrkmem: address of the working memory.
- * This requires 'wrkmem' of size LZ4HC_MEM_COMPRESS.
+ * This requires 'wrkmem' of size lz4hc_mem_compress(compressionLevel).
*
* Compress data from 'src' into 'dst', using the more powerful
* but slower "HC" algorithm. Compression is guaranteed to succeed if
@@ -327,7 +340,8 @@ int LZ4_compress_HC(const char *src, char *dst, int srcSize, int dstCapacity,
/**
* LZ4_resetStreamHC() - Init an allocated 'LZ4_streamHC_t' structure
- * @streamHCPtr: pointer to the 'LZ4_streamHC_t' structure
+ * @streamHCPtr: pointer to the 'LZ4_streamHC_t' structure, of
+ * lz4hc_mem_compress(compressionLevel) bytes
* @compressionLevel: Recommended values are between 4 and 9, although any
* value between 1 and LZ4HC_MAX_CLEVEL will work.
* Values >LZ4HC_MAX_CLEVEL behave the same as 16.
--
2.34.1