]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
updated ZSTD_DCtx_reset()
authorYann Collet <cyan@fb.com>
Fri, 16 Nov 2018 00:12:39 +0000 (16:12 -0800)
committerYann Collet <cyan@fb.com>
Fri, 16 Nov 2018 00:12:39 +0000 (16:12 -0800)
signature and behavior is now the same as ZSTD_CCtx_reset()

doc/zstd_manual.html
lib/compress/zstd_compress.c
lib/decompress/zstd_decompress.c
lib/zstd.h
programs/benchzstd.c
tests/fuzzer.c
tests/paramgrill.c
tests/zstreamtest.c

index be9385fa893502bf7d06956177d0aa1c4054048f..cee8294c0cc60b4427f1b76fe1f688339b7aee2a 100644 (file)
@@ -609,12 +609,12 @@ size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict);
 </p></pre><BR>
 
 <pre><b>typedef enum {
-    ZSTD_CCtx_reset_session_only = 1,
-    ZSTD_CCtx_reset_parameters = 2,
-    ZSTD_CCtx_reset_session_and_parameters = 3
-} ZSTD_CCtx_reset_directive;
+    ZSTD_reset_session_only = 1,
+    ZSTD_reset_parameters = 2,
+    ZSTD_reset_session_and_parameters = 3
+} ZSTD_reset_directive;
 </b></pre><BR>
-<pre><b>size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_CCtx_reset_directive zcrd);
+<pre><b>size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_reset_directive reset);
 </b><p>  There are 2 different things that can be reset, independently or jointly :
   - The session : will stop compressing current frame, and make CCtx ready to start a new one.
                   Useful after an error, or to interrupt any ongoing compression.
@@ -734,12 +734,11 @@ size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict);
  
 </p></pre><BR>
 
-<pre><b>void ZSTD_DCtx_reset(ZSTD_DCtx* dctx);     </b>/* <==== There is a discrepancy with ZSTD_CCtx_reset(): here it necessarily resets everything (context and parameters) */<b>
+<pre><b>size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_reset_directive reset);
 </b><p>  Return a DCtx to clean state.
-  If a decompression was ongoing, any internal data not yet flushed is cancelled.
-  All parameters are back to default values, including sticky ones.
-  Dictionary (if any) is dropped.
-  Parameters can be modified again after a reset.
+  Session and parameters can be reset jointly or separately
+  Parameters can only be reset when no active frame is being decompressed.
+ @return : 0, or an error code, which can be tested with ZSTD_isError()
  
 </p></pre><BR>
 
index 5f7fdba396360f518ed9a21859ecb286e6d6539a..8fbbb44fd22e7ccaace3a8af58a4c7d3dbf6d85e 100644 (file)
@@ -61,7 +61,7 @@ static void ZSTD_initCCtx(ZSTD_CCtx* cctx, ZSTD_customMem memManager)
     memset(cctx, 0, sizeof(*cctx));
     cctx->customMem = memManager;
     cctx->bmi2 = ZSTD_cpuid_bmi2(ZSTD_cpuid());
-    {   size_t const err = ZSTD_CCtx_reset(cctx, ZSTD_CCtx_reset_parameters);
+    {   size_t const err = ZSTD_CCtx_reset(cctx, ZSTD_reset_parameters);
         assert(!ZSTD_isError(err));
         (void)err;
     }
@@ -672,15 +672,15 @@ size_t ZSTD_CCtx_refPrefix_advanced(
 
 /*! ZSTD_CCtx_reset() :
  *  Also dumps dictionary */
-size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_CCtx_reset_directive zcrd)
+size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_reset_directive reset)
 {
-    if ( (zcrd == ZSTD_CCtx_reset_session_only)
-      || (zcrd == ZSTD_CCtx_reset_session_and_parameters) ) {
+    if ( (reset == ZSTD_reset_session_only)
+      || (reset == ZSTD_reset_session_and_parameters) ) {
         cctx->streamStage = zcss_init;
         cctx->pledgedSrcSizePlusOne = 0;
     }
-    if ( (zcrd == ZSTD_CCtx_reset_parameters)
-      || (zcrd == ZSTD_CCtx_reset_session_and_parameters) ) {
+    if ( (reset == ZSTD_reset_parameters)
+      || (reset == ZSTD_reset_session_and_parameters) ) {
         if (cctx->streamStage != zcss_init) return ERROR(stage_wrong);
         cctx->cdict = NULL;
         return ZSTD_CCtxParams_reset(&cctx->requestedParams);
@@ -3703,7 +3703,7 @@ size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs,
                 ip = iend;
                 op += cSize;
                 zcs->frameEnded = 1;
-                ZSTD_CCtx_reset(zcs, ZSTD_CCtx_reset_session_only);
+                ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
                 someMoreWork = 0; break;
             }
             /* complete loading into inBuffer */
@@ -3756,7 +3756,7 @@ size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs,
                     if (zcs->frameEnded) {
                         DEBUGLOG(5, "Frame completed directly in outBuffer");
                         someMoreWork = 0;
-                        ZSTD_CCtx_reset(zcs, ZSTD_CCtx_reset_session_only);
+                        ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
                     }
                     break;
                 }
@@ -3784,7 +3784,7 @@ size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs,
                 if (zcs->frameEnded) {
                     DEBUGLOG(5, "Frame completed on flush");
                     someMoreWork = 0;
-                    ZSTD_CCtx_reset(zcs, ZSTD_CCtx_reset_session_only);
+                    ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
                     break;
                 }
                 zcs->streamStage = zcss_load;
@@ -3878,7 +3878,7 @@ size_t ZSTD_compress_generic (ZSTD_CCtx* cctx,
         {   size_t const flushMin = ZSTDMT_compressStream_generic(cctx->mtctx, output, input, endOp);
             if ( ZSTD_isError(flushMin)
               || (endOp == ZSTD_e_end && flushMin == 0) ) { /* compression completed */
-                ZSTD_CCtx_reset(cctx, ZSTD_CCtx_reset_session_only);
+                ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only);
             }
             DEBUGLOG(5, "completed ZSTD_compress_generic delegating to ZSTDMT_compressStream_generic");
             return flushMin;
index 76f81a37ca42583f556427ce205515c83a72dec7..ea54d6125e9dd82663f4385a56c476a9e524533d 100644 (file)
@@ -1585,9 +1585,18 @@ size_t ZSTD_decompress_generic_simpleArgs (
     return cErr;
 }
 
-void ZSTD_DCtx_reset(ZSTD_DCtx* dctx)
+size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_reset_directive reset)
 {
-    (void)ZSTD_initDStream(dctx);
-    dctx->format = ZSTD_f_zstd1;
-    dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT;
+    if ( (reset == ZSTD_reset_session_only)
+      || (reset == ZSTD_reset_session_and_parameters) ) {
+        (void)ZSTD_initDStream(dctx);
+    }
+    if ( (reset == ZSTD_reset_parameters)
+      || (reset == ZSTD_reset_session_and_parameters) ) {
+        if (dctx->streamStage != zdss_init)
+            return ERROR(stage_wrong);
+        dctx->format = ZSTD_f_zstd1;
+        dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT;
+    }
+    return 0;
 }
index 3338eae18f594ab5fa32e1bc824b1fe11bf4d00d..769cbf50cd03a7de490146a49068f2ca7afa7f04 100644 (file)
@@ -729,10 +729,10 @@ ZSTDLIB_API size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx,
 
 
 typedef enum {
-    ZSTD_CCtx_reset_session_only = 1,
-    ZSTD_CCtx_reset_parameters = 2,
-    ZSTD_CCtx_reset_session_and_parameters = 3
-} ZSTD_CCtx_reset_directive;
+    ZSTD_reset_session_only = 1,
+    ZSTD_reset_parameters = 2,
+    ZSTD_reset_session_and_parameters = 3
+} ZSTD_reset_directive;
 
 /*! ZSTD_CCtx_reset() :
  *  There are 2 different things that can be reset, independently or jointly :
@@ -748,7 +748,7 @@ typedef enum {
  *                  otherwise the reset fails, and function returns an error value (which can be tested using ZSTD_isError())
  *  - Both : similar to resetting the session, followed by resetting parameters.
  */
-ZSTDLIB_API size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_CCtx_reset_directive zcrd);
+ZSTDLIB_API size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_reset_directive reset);
 
 
 
@@ -873,12 +873,11 @@ ZSTDLIB_API size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx,
 
 /*! ZSTD_DCtx_reset() :
  *  Return a DCtx to clean state.
- *  If a decompression was ongoing, any internal data not yet flushed is cancelled.
- *  All parameters are back to default values, including sticky ones.
- *  Dictionary (if any) is dropped.
- *  Parameters can be modified again after a reset.
+ *  Session and parameters can be reset jointly or separately
+ *  Parameters can only be reset when no active frame is being decompressed.
+ * @return : 0, or an error code, which can be tested with ZSTD_isError()
  */
-ZSTDLIB_API void ZSTD_DCtx_reset(ZSTD_DCtx* dctx);     /* <==== There is a discrepancy with ZSTD_CCtx_reset(): here it necessarily resets everything (context and parameters) */
+ZSTDLIB_API size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_reset_directive reset);
 
 
 /*! ZSTD_decompress_generic() :
index 134f88289b04b100c7fb732968a0ed857f94ce09..99049ca3fe961c4464379c03f76e38c606447c34 100644 (file)
@@ -160,7 +160,7 @@ typedef struct {
 static void BMK_initCCtx(ZSTD_CCtx* ctx,
     const void* dictBuffer, size_t dictBufferSize, int cLevel,
     const ZSTD_compressionParameters* comprParams, const BMK_advancedParams_t* adv) {
-    ZSTD_CCtx_reset(ctx, ZSTD_CCtx_reset_session_and_parameters);
+    ZSTD_CCtx_reset(ctx, ZSTD_reset_session_and_parameters);
     if (adv->nbWorkers==1) {
         ZSTD_CCtx_setParameter(ctx, ZSTD_p_nbWorkers, 0);
     } else {
@@ -184,7 +184,7 @@ static void BMK_initCCtx(ZSTD_CCtx* ctx,
 
 static void BMK_initDCtx(ZSTD_DCtx* dctx,
     const void* dictBuffer, size_t dictBufferSize) {
-    ZSTD_DCtx_reset(dctx);
+    ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters);
     ZSTD_DCtx_loadDictionary(dctx, dictBuffer, dictBufferSize);
 }
 
index bbc3c2622141b7f4eda6e8a58f50a2058b5c1030..375a6c4458c7816c71090be11061d37f0bf18fa5 100644 (file)
@@ -527,13 +527,13 @@ static int basicUnitTests(U32 seed, double compressibility)
         CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_p_hashLog, &value));
         CHECK_EQ(value, ZSTD_HASHLOG_MIN);
         /* Reset the CCtx */
-        ZSTD_CCtx_reset(cctx, ZSTD_CCtx_reset_session_only);
+        ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only);
         CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_p_compressionLevel, &value));
         CHECK_EQ(value, 7);
         CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_p_hashLog, &value));
         CHECK_EQ(value, ZSTD_HASHLOG_MIN);
         /* Reset the parameters */
-        ZSTD_CCtx_reset(cctx, ZSTD_CCtx_reset_parameters);
+        ZSTD_CCtx_reset(cctx, ZSTD_reset_parameters);
         CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_p_compressionLevel, &value));
         CHECK_EQ(value, 3);
         CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_p_hashLog, &value));
@@ -1327,7 +1327,7 @@ static int basicUnitTests(U32 seed, double compressibility)
         }
 
         DISPLAYLEVEL(3, "test%3i : decompress of magic-less frame : ", testNb++);
-        ZSTD_DCtx_reset(dctx);
+        ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters);
         CHECK( ZSTD_DCtx_setFormat(dctx, ZSTD_f_zstd1_magicless) );
         {   ZSTD_frameHeader zfh;
             size_t const zfhrt = ZSTD_getFrameHeader_advanced(&zfh, compressedBuffer, cSize, ZSTD_f_zstd1_magicless);
index 30b91720e62cffc7cd5bd95167cad4b22aca1c0d..0fac0d244340c6f5fa70ba7710383e1888b74d30 100644 (file)
@@ -893,7 +893,7 @@ typedef struct {
 static size_t local_initCCtx(void* payload) {
     const BMK_initCCtxArgs* ag = (const BMK_initCCtxArgs*)payload;
     varInds_t i;
-    ZSTD_CCtx_reset(ag->cctx, ZSTD_CCtx_reset_session_and_parameters);
+    ZSTD_CCtx_reset(ag->cctx, ZSTD_reset_session_and_parameters);
     ZSTD_CCtx_setParameter(ag->cctx, ZSTD_p_compressionLevel, ag->cLevel);
 
     for(i = 0; i < NUM_PARAMS; i++) {
@@ -913,7 +913,7 @@ typedef struct {
 
 static size_t local_initDCtx(void* payload) {
     const BMK_initDCtxArgs* ag = (const BMK_initDCtxArgs*)payload;
-    ZSTD_DCtx_reset(ag->dctx);
+    ZSTD_DCtx_reset(ag->dctx, ZSTD_reset_session_and_parameters);
     ZSTD_DCtx_loadDictionary(ag->dctx, ag->dictBuffer, ag->dictBufferSize);
     return 0;
 }
index 9ab66eeea1ceac3ecdd3099231aa3729a2f953ec..1fb5f31c538ab4bb5332e49390bf531a761cf3d0 100644 (file)
@@ -662,7 +662,7 @@ static int basicUnitTests(U32 seed, double compressibility)
     { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
       if (!ZSTD_isError(r)) goto _output_error;  /* must fail : frame requires > 100 bytes */
       DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r)); }
-    ZSTD_DCtx_reset(zd);   /* leave zd in good shape for next tests */
+    ZSTD_DCtx_reset(zd, ZSTD_reset_session_and_parameters);   /* leave zd in good shape for next tests */
 
     DISPLAYLEVEL(3, "test%3i : dictionary source size and level : ", testNb++);
     {   ZSTD_DCtx* const dctx = ZSTD_createDCtx();
@@ -930,7 +930,7 @@ static int basicUnitTests(U32 seed, double compressibility)
 
         if (!cdict || !ddict) goto _output_error;
 
-        ZSTD_CCtx_reset(zc, ZSTD_CCtx_reset_session_only);
+        ZSTD_CCtx_reset(zc, ZSTD_reset_session_only);
         ZSTD_resetDStream(zd);
         CHECK_Z(ZSTD_CCtx_refCDict(zc, cdict));
         CHECK_Z(ZSTD_initDStream_usingDDict(zd, ddict));
@@ -1077,7 +1077,7 @@ static int basicUnitTests(U32 seed, double compressibility)
         int remainingInput = 256 * 1024;
         int offset;
 
-        CHECK_Z(ZSTD_CCtx_reset(zc, ZSTD_CCtx_reset_session_and_parameters));
+        CHECK_Z(ZSTD_CCtx_reset(zc, ZSTD_reset_session_and_parameters));
         CHECK_Z(ZSTD_CCtx_refCDict(zc, cdict));
         CHECK_Z(ZSTD_CCtx_setParameter(zc, ZSTD_p_checksumFlag, 1));
         /* Write a bunch of 6 byte blocks */