]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Enforce nbThreads<=1 for estimateCCtxSize
authorStella Lau <laus@fb.com>
Thu, 24 Aug 2017 23:28:49 +0000 (16:28 -0700)
committerStella Lau <laus@fb.com>
Thu, 24 Aug 2017 23:28:49 +0000 (16:28 -0700)
lib/compress/zstd_compress.c
lib/zstd.h
tests/Makefile

index f3f6022301493807755f5d37814cb0481220c023..4513d98318e505ad916a00118901aa4a25c2c525 100644 (file)
@@ -678,29 +678,34 @@ ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, u
 
 size_t ZSTD_estimateCCtxSize_advanced_opaque(const ZSTD_CCtx_params* params)
 {
-    ZSTD_compressionParameters const cParams = params->cParams;
-    size_t const blockSize = MIN(ZSTD_BLOCKSIZE_MAX, (size_t)1 << cParams.windowLog);
-    U32    const divider = (cParams.searchLength==3) ? 3 : 4;
-    size_t const maxNbSeq = blockSize / divider;
-    size_t const tokenSpace = blockSize + 11*maxNbSeq;
-    size_t const chainSize =
-            (cParams.strategy == ZSTD_fast) ? 0 : (1 << cParams.chainLog);
-    size_t const hSize = ((size_t)1) << cParams.hashLog;
-    U32    const hashLog3 = (cParams.searchLength>3) ?
-                            0 : MIN(ZSTD_HASHLOG3_MAX, cParams.windowLog);
-    size_t const h3Size = ((size_t)1) << hashLog3;
-    size_t const entropySpace = sizeof(ZSTD_entropyCTables_t);
-    size_t const tableSpace = (chainSize + hSize + h3Size) * sizeof(U32);
-
-    size_t const optBudget =
-            ((MaxML+1) + (MaxLL+1) + (MaxOff+1) + (1<<Litbits))*sizeof(U32)
-            + (ZSTD_OPT_NUM+1)*(sizeof(ZSTD_match_t) + sizeof(ZSTD_optimal_t));
-    size_t const optSpace = ((cParams.strategy == ZSTD_btopt) || (cParams.strategy == ZSTD_btultra)) ? optBudget : 0;
-    size_t const neededSpace = entropySpace + tableSpace + tokenSpace + optSpace;
-
-    DEBUGLOG(5, "sizeof(ZSTD_CCtx) : %u", (U32)sizeof(ZSTD_CCtx));
-    DEBUGLOG(5, "estimate workSpace : %u", (U32)neededSpace);
-    return sizeof(ZSTD_CCtx) + neededSpace;
+    /* Estimate CCtx size is supported for single-threaded compression only. */
+    if (params->nbThreads > 1) {
+      return 0;
+    }
+    {   ZSTD_compressionParameters const cParams = params->cParams;
+        size_t const blockSize = MIN(ZSTD_BLOCKSIZE_MAX, (size_t)1 << cParams.windowLog);
+        U32    const divider = (cParams.searchLength==3) ? 3 : 4;
+        size_t const maxNbSeq = blockSize / divider;
+        size_t const tokenSpace = blockSize + 11*maxNbSeq;
+        size_t const chainSize =
+                (cParams.strategy == ZSTD_fast) ? 0 : (1 << cParams.chainLog);
+        size_t const hSize = ((size_t)1) << cParams.hashLog;
+        U32    const hashLog3 = (cParams.searchLength>3) ?
+                                0 : MIN(ZSTD_HASHLOG3_MAX, cParams.windowLog);
+        size_t const h3Size = ((size_t)1) << hashLog3;
+        size_t const entropySpace = sizeof(ZSTD_entropyCTables_t);
+        size_t const tableSpace = (chainSize + hSize + h3Size) * sizeof(U32);
+
+        size_t const optBudget =
+                ((MaxML+1) + (MaxLL+1) + (MaxOff+1) + (1<<Litbits))*sizeof(U32)
+                + (ZSTD_OPT_NUM+1)*(sizeof(ZSTD_match_t) + sizeof(ZSTD_optimal_t));
+        size_t const optSpace = ((cParams.strategy == ZSTD_btopt) || (cParams.strategy == ZSTD_btultra)) ? optBudget : 0;
+        size_t const neededSpace = entropySpace + tableSpace + tokenSpace + optSpace;
+
+        DEBUGLOG(5, "sizeof(ZSTD_CCtx) : %u", (U32)sizeof(ZSTD_CCtx));
+        DEBUGLOG(5, "estimate workSpace : %u", (U32)neededSpace);
+        return sizeof(ZSTD_CCtx) + neededSpace;
+    }
 }
 
 size_t ZSTD_estimateCCtxSize_advanced(ZSTD_compressionParameters cParams)
@@ -717,13 +722,17 @@ size_t ZSTD_estimateCCtxSize(int compressionLevel)
 
 size_t ZSTD_estimateCStreamSize_advanced_opaque(const ZSTD_CCtx_params* params)
 {
-    size_t const CCtxSize = ZSTD_estimateCCtxSize_advanced_opaque(params);
-    size_t const blockSize = MIN(ZSTD_BLOCKSIZE_MAX, (size_t)1 << params->cParams.windowLog);
-    size_t const inBuffSize = ((size_t)1 << params->cParams.windowLog) + blockSize;
-    size_t const outBuffSize = ZSTD_compressBound(blockSize) + 1;
-    size_t const streamingSize = inBuffSize + outBuffSize;
+    if (params->nbThreads > 1) {
+        return 0;
+    }
+    {   size_t const CCtxSize = ZSTD_estimateCCtxSize_advanced_opaque(params);
+        size_t const blockSize = MIN(ZSTD_BLOCKSIZE_MAX, (size_t)1 << params->cParams.windowLog);
+        size_t const inBuffSize = ((size_t)1 << params->cParams.windowLog) + blockSize;
+        size_t const outBuffSize = ZSTD_compressBound(blockSize) + 1;
+        size_t const streamingSize = inBuffSize + outBuffSize;
 
-    return CCtxSize + streamingSize;
+        return CCtxSize + streamingSize;
+    }
 }
 
 size_t ZSTD_estimateCStreamSize_advanced(ZSTD_compressionParameters cParams)
index 4cff6974bcceecdce6a091e9f95132b0ee42f97b..03994f9a8ba95edd3164d5bcc783cca297525595 100644 (file)
@@ -498,7 +498,7 @@ ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict);
  *  It will also consider src size to be arbitrarily "large", which is worst case.
  *  If srcSize is known to always be small, ZSTD_estimateCCtxSize_advanced() can provide a tighter estimation.
  *  ZSTD_estimateCCtxSize_advanced() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel.
- *  ZSTD_estimateCCtxSize_advanced_opaque() can be used in tandem with ZSTD_CCtxParam_setParameter().
+ *  ZSTD_estimateCCtxSize_advanced_opaque() can be used in tandem with ZSTD_CCtxParam_setParameter(). Only single-threaded compression is supported. This function will return 0 if ZSTD_p_nbThreads is set to a value > 1.
  *  Note : CCtx estimation is only correct for single-threaded compression */
 ZSTDLIB_API size_t ZSTD_estimateCCtxSize(int compressionLevel);
 ZSTDLIB_API size_t ZSTD_estimateCCtxSize_advanced(ZSTD_compressionParameters cParams);
@@ -510,7 +510,7 @@ ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void);
  *  It will also consider src size to be arbitrarily "large", which is worst case.
  *  If srcSize is known to always be small, ZSTD_estimateCStreamSize_advanced() can provide a tighter estimation.
  *  ZSTD_estimateCStreamSize_advanced() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel.
- *  ZSTD_estimateCStreamSize_advanced_opaque() can be used in tandem with ZSTD_CCtxParam_setParameter().
+ *  ZSTD_estimateCStreamSize_advanced_opaque() can be used in tandem with ZSTD_CCtxParam_setParameter(). Only single-threaded compression is supported. This function will return 0 if ZSTD_p_nbThreads is set to a value > 1.
  *  Note : CStream estimation is only correct for single-threaded compression.
  *  ZSTD_DStream memory budget depends on window Size.
  *  This information can be passed manually, using ZSTD_estimateDStreamSize,
@@ -1113,7 +1113,8 @@ size_t ZSTD_compress_generic_simpleArgs (
  *  - ZSTD_compress_generic() : Do compression using the CCtx.
  *  - ZSTD_freeCCtxParams() : Free the memory.
  *
- *  This can be used with ZSTD_estimateCCtxSize_opaque() for static allocation. */
+ *  This can be used with ZSTD_estimateCCtxSize_opaque() for static allocation
+ *  for single-threaded compression. */
 
 ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void);
 
index 3734f7737e9562be37672a3a8b3744a746a04cc1..006059b72d31e51380c75f586d6c678f67a92a93 100644 (file)
@@ -323,6 +323,7 @@ test-zstream: zstreamtest
        $(QEMU_SYS) ./zstreamtest $(ZSTREAM_TESTTIME) $(FUZZER_FLAGS)
        $(QEMU_SYS) ./zstreamtest --mt $(ZSTREAM_TESTTIME) $(FUZZER_FLAGS)
        $(QEMU_SYS) ./zstreamtest --newapi $(ZSTREAM_TESTTIME) $(FUZZER_FLAGS)
+       $(QEMU_SYS) ./zstreamtest --opaqueapi $(ZSTREAM_TESTTIME) $(FUZZER_FLAGS)
 
 test-zstream32: zstreamtest32
        $(QEMU_SYS) ./zstreamtest32 $(ZSTREAM_TESTTIME) $(FUZZER_FLAGS)