Merge pull request #1107 from terrelln/reset-params

Add ZSTD_CCtx_resetParameters() function
This commit is contained in:
Yann Collet
2018-04-13 13:23:47 -07:00
committed by GitHub
3 changed files with 30 additions and 14 deletions
+14 -11
View File
@@ -72,9 +72,11 @@ ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem)
{ ZSTD_CCtx* const cctx = (ZSTD_CCtx*)ZSTD_calloc(sizeof(ZSTD_CCtx), customMem); { ZSTD_CCtx* const cctx = (ZSTD_CCtx*)ZSTD_calloc(sizeof(ZSTD_CCtx), customMem);
if (!cctx) return NULL; if (!cctx) return NULL;
cctx->customMem = customMem; cctx->customMem = customMem;
cctx->requestedParams.compressionLevel = ZSTD_CLEVEL_DEFAULT;
cctx->requestedParams.fParams.contentSizeFlag = 1;
cctx->bmi2 = ZSTD_cpuid_bmi2(ZSTD_cpuid()); cctx->bmi2 = ZSTD_cpuid_bmi2(ZSTD_cpuid());
{ size_t const err = ZSTD_CCtx_resetParameters(cctx);
assert(!ZSTD_isError(err));
(void)err;
}
return cctx; return cctx;
} }
} }
@@ -657,18 +659,19 @@ size_t ZSTD_CCtx_refPrefix_advanced(
return 0; return 0;
} }
static void ZSTD_startNewCompression(ZSTD_CCtx* cctx) /*! ZSTD_CCtx_reset() :
* Also dumps dictionary */
void ZSTD_CCtx_reset(ZSTD_CCtx* cctx)
{ {
cctx->streamStage = zcss_init; cctx->streamStage = zcss_init;
cctx->pledgedSrcSizePlusOne = 0; cctx->pledgedSrcSizePlusOne = 0;
} }
/*! ZSTD_CCtx_reset() : size_t ZSTD_CCtx_resetParameters(ZSTD_CCtx* cctx)
* Also dumps dictionary */
void ZSTD_CCtx_reset(ZSTD_CCtx* cctx)
{ {
ZSTD_startNewCompression(cctx); if (cctx->streamStage != zcss_init) return ERROR(stage_wrong);
cctx->cdict = NULL; cctx->cdict = NULL;
return ZSTD_CCtxParams_reset(&cctx->requestedParams);
} }
/** ZSTD_checkCParams() : /** ZSTD_checkCParams() :
@@ -3181,7 +3184,7 @@ size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs,
ip = iend; ip = iend;
op += cSize; op += cSize;
zcs->frameEnded = 1; zcs->frameEnded = 1;
ZSTD_startNewCompression(zcs); ZSTD_CCtx_reset(zcs);
someMoreWork = 0; break; someMoreWork = 0; break;
} }
/* complete loading into inBuffer */ /* complete loading into inBuffer */
@@ -3234,7 +3237,7 @@ size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs,
if (zcs->frameEnded) { if (zcs->frameEnded) {
DEBUGLOG(5, "Frame completed directly in outBuffer"); DEBUGLOG(5, "Frame completed directly in outBuffer");
someMoreWork = 0; someMoreWork = 0;
ZSTD_startNewCompression(zcs); ZSTD_CCtx_reset(zcs);
} }
break; break;
} }
@@ -3262,7 +3265,7 @@ size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs,
if (zcs->frameEnded) { if (zcs->frameEnded) {
DEBUGLOG(5, "Frame completed on flush"); DEBUGLOG(5, "Frame completed on flush");
someMoreWork = 0; someMoreWork = 0;
ZSTD_startNewCompression(zcs); ZSTD_CCtx_reset(zcs);
break; break;
} }
zcs->streamStage = zcss_load; zcs->streamStage = zcss_load;
@@ -3359,7 +3362,7 @@ size_t ZSTD_compress_generic (ZSTD_CCtx* cctx,
{ size_t const flushMin = ZSTDMT_compressStream_generic(cctx->mtctx, output, input, endOp); { size_t const flushMin = ZSTDMT_compressStream_generic(cctx->mtctx, output, input, endOp);
if ( ZSTD_isError(flushMin) if ( ZSTD_isError(flushMin)
|| (endOp == ZSTD_e_end && flushMin == 0) ) { /* compression completed */ || (endOp == ZSTD_e_end && flushMin == 0) ) { /* compression completed */
ZSTD_startNewCompression(cctx); ZSTD_CCtx_reset(cctx);
} }
return flushMin; return flushMin;
} } } }
+10 -3
View File
@@ -1141,12 +1141,19 @@ ZSTDLIB_API size_t ZSTD_CCtx_refPrefix_advanced(ZSTD_CCtx* cctx, const void* pre
* Return a CCtx to clean state. * Return a CCtx to clean state.
* Useful after an error, or to interrupt an ongoing compression job and start a new one. * Useful after an error, or to interrupt an ongoing compression job and start a new one.
* Any internal data not yet flushed is cancelled. * Any internal data not yet flushed is cancelled.
* Dictionary (if any) is dropped. * The parameters and dictionary are kept unchanged, to reset them use ZSTD_CCtx_resetParameters().
* All parameters are back to default values (compression level is ZSTD_CLEVEL_DEFAULT).
* After a reset, all compression parameters can be modified again.
*/ */
ZSTDLIB_API void ZSTD_CCtx_reset(ZSTD_CCtx* cctx); ZSTDLIB_API void ZSTD_CCtx_reset(ZSTD_CCtx* cctx);
/*! ZSTD_CCtx_resetParameters() :
* All parameters are back to default values (compression level is ZSTD_CLEVEL_DEFAULT).
* Dictionary (if any) is dropped.
* Resetting parameters is only possible during frame initialization (before starting compression).
* To reset the context use ZSTD_CCtx_reset().
* @return 0 or an error code (which can be checked with ZSTD_isError()).
*/
ZSTDLIB_API size_t ZSTD_CCtx_resetParameters(ZSTD_CCtx* cctx);
typedef enum { typedef enum {
+6
View File
@@ -433,6 +433,12 @@ static int basicUnitTests(U32 seed, double compressibility)
CHECK_EQ(value, 7); CHECK_EQ(value, 7);
CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_p_hashLog, &value)); CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_p_hashLog, &value));
CHECK_EQ(value, ZSTD_HASHLOG_MIN); CHECK_EQ(value, ZSTD_HASHLOG_MIN);
/* Reset the parameters */
ZSTD_CCtx_resetParameters(cctx);
CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_p_compressionLevel, &value));
CHECK_EQ(value, 3);
CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_p_hashLog, &value));
CHECK_EQ(value, 0);
ZSTD_freeCCtx(cctx); ZSTD_freeCCtx(cctx);
} }