From 699f11b4f726c258e4589a446eb16428550e7e8c Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Thu, 17 Aug 2017 17:33:46 -0700 Subject: [PATCH 01/52] Create opaque parameter structure --- lib/common/zstd_internal.h | 4 +++ lib/compress/zstd_compress.c | 52 ++++++++++++++++++++++++++---------- lib/zstd.h | 2 ++ 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index 1621bca61..ddafc7874 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -221,6 +221,10 @@ typedef struct seqDef_s { U16 matchLength; } seqDef; +typedef struct ZSTD_CCtx_params_s { + ZSTD_compressionParameters cParams; + ZSTD_frameParameters fParams; +} ZSTD_CCtx_params; typedef struct { seqDef* sequencesStart; diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 998fb3d49..3de173a62 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -84,8 +84,8 @@ struct ZSTD_CCtx_s { ZSTD_compressionStage_e stage; U32 dictID; int compressionLevel; - ZSTD_parameters requestedParams; - ZSTD_parameters appliedParams; + ZSTD_CCtx_params requestedParams; + ZSTD_CCtx_params appliedParams; void* workSpace; size_t workSpaceSize; size_t blockSize; @@ -202,7 +202,28 @@ size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs) /* private API call, for dictBuilder only */ const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx) { return &(ctx->seqStore); } -static ZSTD_parameters ZSTD_getParamsFromCCtx(const ZSTD_CCtx* cctx) { return cctx->appliedParams; } +// TODO: get rid of this function +static ZSTD_parameters ZSTD_getParamsFromCCtxParams(const ZSTD_CCtx_params cctxParams) +{ + ZSTD_parameters params; + params.cParams = cctxParams.cParams; + params.fParams = cctxParams.fParams; + return params; +} + +// TODO: get rid of this function too +static ZSTD_CCtx_params ZSTD_makeCCtxParamsFromParams(ZSTD_parameters params) { + ZSTD_CCtx_params cctxParams; + memset(&cctxParams, 0, sizeof(ZSTD_CCtx_params)); + cctxParams.cParams = params.cParams; + cctxParams.fParams = params.fParams; + return cctxParams; +} + + +static ZSTD_parameters ZSTD_getParamsFromCCtx(const ZSTD_CCtx* cctx) { + return ZSTD_getParamsFromCCtxParams(cctx->appliedParams); +} /* older variant; will be deprecated */ size_t ZSTD_setCCtxParameter(ZSTD_CCtx* cctx, ZSTD_CCtxParameter param, unsigned value) @@ -582,7 +603,7 @@ static size_t ZSTD_continueCCtx(ZSTD_CCtx* cctx, ZSTD_parameters params, U64 ple { U32 const end = (U32)(cctx->nextSrc - cctx->base); DEBUGLOG(5, "continue mode"); - cctx->appliedParams = params; + cctx->appliedParams = ZSTD_makeCCtxParamsFromParams(params); cctx->pledgedSrcSizePlusOne = pledgedSrcSize+1; cctx->consumedSrcSize = 0; if (pledgedSrcSize == ZSTD_CONTENTSIZE_UNKNOWN) @@ -670,7 +691,7 @@ static size_t ZSTD_resetCCtx_internal(ZSTD_CCtx* zc, } } /* init params */ - zc->appliedParams = params; + zc->appliedParams = ZSTD_makeCCtxParamsFromParams(params); zc->pledgedSrcSizePlusOne = pledgedSrcSize+1; zc->consumedSrcSize = 0; if (pledgedSrcSize == ZSTD_CONTENTSIZE_UNKNOWN) @@ -766,7 +787,7 @@ static size_t ZSTD_copyCCtx_internal(ZSTD_CCtx* dstCCtx, if (srcCCtx->stage!=ZSTDcs_init) return ERROR(stage_wrong); memcpy(&dstCCtx->customMem, &srcCCtx->customMem, sizeof(ZSTD_customMem)); - { ZSTD_parameters params = srcCCtx->appliedParams; + { ZSTD_parameters params = ZSTD_getParamsFromCCtxParams(srcCCtx->appliedParams); params.fParams = fParams; ZSTD_resetCCtx_internal(dstCCtx, params, pledgedSrcSize, ZSTDcrp_noMemset, zbuff); @@ -2952,8 +2973,10 @@ static size_t ZSTD_compressContinue_internal (ZSTD_CCtx* cctx, if (cctx->stage==ZSTDcs_created) return ERROR(stage_wrong); /* missing init (ZSTD_compressBegin) */ if (frame && (cctx->stage==ZSTDcs_init)) { - fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, cctx->appliedParams, - cctx->pledgedSrcSizePlusOne-1, cctx->dictID); + fhSize = ZSTD_writeFrameHeader( + dst, dstCapacity, + ZSTD_getParamsFromCCtxParams(cctx->appliedParams), + cctx->pledgedSrcSizePlusOne-1, cctx->dictID); if (ZSTD_isError(fhSize)) return fhSize; dstCapacity -= fhSize; dst = (char*)dst + fhSize; @@ -3269,7 +3292,7 @@ static size_t ZSTD_writeEpilogue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity) /* special case : empty frame */ if (cctx->stage == ZSTDcs_init) { - fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, cctx->appliedParams, 0, 0); + fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, ZSTD_getParamsFromCCtxParams(cctx->appliedParams), 0, 0); if (ZSTD_isError(fhSize)) return fhSize; dstCapacity -= fhSize; op += fhSize; @@ -3545,7 +3568,8 @@ size_t ZSTD_compressBegin_usingCDict_advanced( ZSTD_frameParameters const fParams, unsigned long long const pledgedSrcSize) { if (cdict==NULL) return ERROR(dictionary_wrong); - { ZSTD_parameters params = cdict->refContext->appliedParams; + { ZSTD_parameters params = + ZSTD_getParamsFromCCtxParams(cdict->refContext->appliedParams); params.fParams = fParams; DEBUGLOG(5, "ZSTD_compressBegin_usingCDict_advanced"); return ZSTD_compressBegin_internal(cctx, @@ -3653,7 +3677,7 @@ static size_t ZSTD_resetCStream_internal(ZSTD_CStream* zcs, size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize) { - ZSTD_parameters params = zcs->requestedParams; + ZSTD_parameters params = ZSTD_getParamsFromCCtxParams(zcs->requestedParams); params.fParams.contentSizeFlag = (pledgedSrcSize > 0); DEBUGLOG(5, "ZSTD_resetCStream"); if (zcs->compressionLevel != ZSTD_CLEVEL_CUSTOM) { @@ -3696,7 +3720,7 @@ size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, zcs->cdict = cdict; } - zcs->requestedParams = params; + zcs->requestedParams = ZSTD_makeCCtxParamsFromParams(params); zcs->compressionLevel = ZSTD_CLEVEL_CUSTOM; return ZSTD_resetCStream_internal(zcs, NULL, 0, zcs->dictMode, zcs->cdict, params, pledgedSrcSize); } @@ -3729,7 +3753,7 @@ size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, ZSTD_parameters params, unsigned long long pledgedSrcSize) { CHECK_F( ZSTD_checkCParams(params.cParams) ); - zcs->requestedParams = params; + zcs->requestedParams = ZSTD_makeCCtxParamsFromParams(params); zcs->compressionLevel = ZSTD_CLEVEL_CUSTOM; return ZSTD_initCStream_internal(zcs, dict, dictSize, NULL, params, pledgedSrcSize); } @@ -3943,7 +3967,7 @@ size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, if (cctx->streamStage == zcss_init) { const void* const prefix = cctx->prefix; size_t const prefixSize = cctx->prefixSize; - ZSTD_parameters params = cctx->requestedParams; + ZSTD_parameters params = ZSTD_getParamsFromCCtxParams(cctx->requestedParams); if (cctx->compressionLevel != ZSTD_CLEVEL_CUSTOM) params.cParams = ZSTD_getCParams(cctx->compressionLevel, cctx->pledgedSrcSizePlusOne-1, 0 /*dictSize*/); diff --git a/lib/zstd.h b/lib/zstd.h index a2a756dfc..4b2f4a3b3 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -425,6 +425,8 @@ typedef struct { ZSTD_frameParameters fParams; } ZSTD_parameters; +typedef struct ZSTD_CCtx_params_s ZSTD_CCtx_params; + /*= Custom memory allocation functions */ typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size); typedef void (*ZSTD_freeFunction) (void* opaque, void* address); From ade95b8bed67c48b8bd91f0503904ea93af8dcd0 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Thu, 17 Aug 2017 18:13:08 -0700 Subject: [PATCH 02/52] Add opaque interfaces for static initialization --- lib/compress/zstd_compress.c | 150 ++++++++++++++++++++++++----------- lib/zstd.h | 9 +++ 2 files changed, 111 insertions(+), 48 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 3de173a62..b5bdf680e 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -220,6 +220,16 @@ static ZSTD_CCtx_params ZSTD_makeCCtxParamsFromParams(ZSTD_parameters params) { return cctxParams; } +// TODO: get rid of this function too +static ZSTD_CCtx_params ZSTD_makeCCtxParamsFromCParams( + ZSTD_compressionParameters cParams) +{ + ZSTD_CCtx_params cctxParams; + memset(&cctxParams, 0, sizeof(ZSTD_CCtx_params)); + cctxParams.cParams = cParams; + return cctxParams; +} + static ZSTD_parameters ZSTD_getParamsFromCCtx(const ZSTD_CCtx* cctx) { return ZSTD_getParamsFromCCtxParams(cctx->appliedParams); @@ -537,29 +547,40 @@ ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, u return ZSTD_adjustCParams_internal(cPar, srcSize, dictSize); } +size_t ZSTD_estimateCCtxSize_advanced_opaque(ZSTD_CCtx_params* params) +{ + if (params == NULL) { return 0; } + { ZSTD_compressionParameters 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<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<cParams; + size_t const CCtxSize = ZSTD_estimateCCtxSize_advanced(cParams); + size_t const blockSize = MIN(ZSTD_BLOCKSIZE_MAX, (size_t)1 << cParams.windowLog); + size_t const inBuffSize = ((size_t)1 << cParams.windowLog) + blockSize; + size_t const outBuffSize = ZSTD_compressBound(blockSize) + 1; + size_t const streamingSize = inBuffSize + outBuffSize; + + return CCtxSize + streamingSize; + } +} + size_t ZSTD_estimateCStreamSize_advanced(ZSTD_compressionParameters cParams) { - size_t const CCtxSize = ZSTD_estimateCCtxSize_advanced(cParams); - size_t const blockSize = MIN(ZSTD_BLOCKSIZE_MAX, (size_t)1 << cParams.windowLog); - size_t const inBuffSize = ((size_t)1 << cParams.windowLog) + blockSize; - size_t const outBuffSize = ZSTD_compressBound(blockSize) + 1; - size_t const streamingSize = inBuffSize + outBuffSize; - - return CCtxSize + streamingSize; + ZSTD_CCtx_params params = ZSTD_makeCCtxParamsFromCParams(cParams); + return ZSTD_estimateCStreamSize_advanced_opaque(¶ms); } size_t ZSTD_estimateCStreamSize(int compressionLevel) { @@ -3390,14 +3421,24 @@ size_t ZSTD_compress(void* dst, size_t dstCapacity, const void* src, size_t srcS /* ===== Dictionary API ===== */ +size_t ZSTD_estimateCDictSize_advanced_opaque( + size_t dictSize, ZSTD_CCtx_params* params, unsigned byReference) +{ + if (params == NULL) { return 0; } + DEBUGLOG(5, "sizeof(ZSTD_CDict) : %u", (U32)sizeof(ZSTD_CDict)); + DEBUGLOG(5, "CCtx estimate : %u", + (U32)ZSTD_estimateCCtxSize_advanced_opaque(params)); + return sizeof(ZSTD_CDict) + ZSTD_estimateCCtxSize_advanced_opaque(params) + + (byReference ? 0 : dictSize); + +} + /*! ZSTD_estimateCDictSize_advanced() : * Estimate amount of memory that will be needed to create a dictionary with following arguments */ size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, unsigned byReference) { - DEBUGLOG(5, "sizeof(ZSTD_CDict) : %u", (U32)sizeof(ZSTD_CDict)); - DEBUGLOG(5, "CCtx estimate : %u", (U32)ZSTD_estimateCCtxSize_advanced(cParams)); - return sizeof(ZSTD_CDict) + ZSTD_estimateCCtxSize_advanced(cParams) - + (byReference ? 0 : dictSize); + ZSTD_CCtx_params params = ZSTD_makeCCtxParamsFromCParams(cParams); + return ZSTD_estimateCDictSize_advanced_opaque(dictSize, ¶ms, byReference); } size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel) @@ -3510,24 +3551,12 @@ size_t ZSTD_freeCDict(ZSTD_CDict* cdict) } } -/*! ZSTD_initStaticCDict_advanced() : - * Generate a digested dictionary in provided memory area. - * workspace: The memory area to emplace the dictionary into. - * Provided pointer must 8-bytes aligned. - * It must outlive dictionary usage. - * workspaceSize: Use ZSTD_estimateCDictSize() - * to determine how large workspace must be. - * cParams : use ZSTD_getCParams() to transform a compression level - * into its relevants cParams. - * @return : pointer to ZSTD_CDict*, or NULL if error (size too small) - * Note : there is no corresponding "free" function. - * Since workspace was allocated externally, it must be freed externally. - */ -ZSTD_CDict* ZSTD_initStaticCDict(void* workspace, size_t workspaceSize, - const void* dict, size_t dictSize, - unsigned byReference, ZSTD_dictMode_e dictMode, - ZSTD_compressionParameters cParams) +ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( + void *workspace, size_t workspaceSize, const void* dict, + size_t dictSize, unsigned byReference, ZSTD_dictMode_e dictMode, + ZSTD_CCtx_params* params) { + ZSTD_compressionParameters cParams = params->cParams; size_t const cctxSize = ZSTD_estimateCCtxSize_advanced(cParams); size_t const neededSize = sizeof(ZSTD_CDict) + (byReference ? 0 : dictSize) + cctxSize; @@ -3557,6 +3586,31 @@ ZSTD_CDict* ZSTD_initStaticCDict(void* workspace, size_t workspaceSize, return cdict; } +/*! ZSTD_initStaticCDict_advanced() : + * Generate a digested dictionary in provided memory area. + * workspace: The memory area to emplace the dictionary into. + * Provided pointer must 8-bytes aligned. + * It must outlive dictionary usage. + * workspaceSize: Use ZSTD_estimateCDictSize() + * to determine how large workspace must be. + * cParams : use ZSTD_getCParams() to transform a compression level + * into its relevants cParams. + * @return : pointer to ZSTD_CDict*, or NULL if error (size too small) + * Note : there is no corresponding "free" function. + * Since workspace was allocated externally, it must be freed externally. + */ +ZSTD_CDict* ZSTD_initStaticCDict(void* workspace, size_t workspaceSize, + const void* dict, size_t dictSize, + unsigned byReference, ZSTD_dictMode_e dictMode, + ZSTD_compressionParameters cParams) +{ + ZSTD_CCtx_params params = ZSTD_makeCCtxParamsFromCParams(cParams); + return ZSTD_initStaticCDict_advanced_opaque( + workspace, workspaceSize, dict, dictSize, + byReference, dictMode, ¶ms); +} + + ZSTD_parameters ZSTD_getParamsFromCDict(const ZSTD_CDict* cdict) { return ZSTD_getParamsFromCCtx(cdict->refContext); } diff --git a/lib/zstd.h b/lib/zstd.h index 4b2f4a3b3..2111c9de0 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -501,6 +501,7 @@ ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict); * 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); +ZSTDLIB_API size_t ZSTD_estimateCCtxSize_advanced_opaque(ZSTD_CCtx_params* params); ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void); /*! ZSTD_estimate?StreamSize() : @@ -517,6 +518,7 @@ ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void); * In this case, get total size by adding ZSTD_estimate?DictSize */ ZSTDLIB_API size_t ZSTD_estimateCStreamSize(int compressionLevel); ZSTDLIB_API size_t ZSTD_estimateCStreamSize_advanced(ZSTD_compressionParameters cParams); +ZSTDLIB_API size_t ZSTD_estimateCStreamSize_advanced_opaque(ZSTD_CCtx_params* params); ZSTDLIB_API size_t ZSTD_estimateDStreamSize(size_t windowSize); ZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize); @@ -526,6 +528,7 @@ ZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t sr * Note : dictionary created "byReference" are smaller */ ZSTDLIB_API size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel); ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, unsigned byReference); +ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced_opaque(size_t dictSize, ZSTD_CCtx_params* params, unsigned byReference); ZSTDLIB_API size_t ZSTD_estimateDDictSize(size_t dictSize, unsigned byReference); @@ -602,6 +605,12 @@ ZSTDLIB_API ZSTD_CDict* ZSTD_initStaticCDict( unsigned byReference, ZSTD_dictMode_e dictMode, ZSTD_compressionParameters cParams); +ZSTDLIB_API ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( + void* workspace, size_t workspaceSize, + const void* dict, size_t dictSize, + unsigned byReference, ZSTD_dictMode_e dictMode, + ZSTD_CCtx_params* params); + /*! ZSTD_getCParams() : * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize. * `estimatedSrcSize` value is optional, select 0 if not known */ From 4169f49171a6fa98baf5e62c5ff6aeb74d675fd9 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Thu, 17 Aug 2017 18:45:04 -0700 Subject: [PATCH 03/52] Add initialization/allocation functions for opaque params --- lib/compress/zstd_compress.c | 38 ++++++++++++++++++++++++++++++++++++ lib/zstd.h | 6 ++++++ 2 files changed, 44 insertions(+) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index b5bdf680e..509f8bedc 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -230,6 +230,44 @@ static ZSTD_CCtx_params ZSTD_makeCCtxParamsFromCParams( return cctxParams; } +ZSTD_CCtx_params* ZSTD_createCCtxParams(void) +{ + ZSTD_CCtx_params* params = + (ZSTD_CCtx_params*)ZSTD_calloc(sizeof(ZSTD_CCtx_params), + ZSTD_defaultCMem); + if (!params) { return NULL; } + // TODO +// params->compressionLevel = ZSTD_CLEVEL_DEFAULT; + return params; +} + +size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* params, + ZSTD_compressionParameters cParams) +{ + CHECK_F( params == NULL ); + memset(params, 0, sizeof(ZSTD_CCtx_params)); + params->cParams = cParams; + return 0; +} + +ZSTD_CCtx_params* ZSTD_createAndInitCCtxParams( + int compressionLevel, unsigned long long estimatedSrcSize, + size_t dictSize) +{ + ZSTD_CCtx_params* params = ZSTD_createCCtxParams(); + if (params == NULL) { return NULL; } + ZSTD_initCCtxParams(params, ZSTD_getCParams( + compressionLevel, estimatedSrcSize, dictSize)); + return params; +} + +size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params) +{ + ZSTD_free(params, ZSTD_defaultCMem); + return 0; +} + + static ZSTD_parameters ZSTD_getParamsFromCCtx(const ZSTD_CCtx* cctx) { return ZSTD_getParamsFromCCtxParams(cctx->appliedParams); diff --git a/lib/zstd.h b/lib/zstd.h index 2111c9de0..b847d01f2 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -611,6 +611,12 @@ ZSTDLIB_API ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( unsigned byReference, ZSTD_dictMode_e dictMode, ZSTD_CCtx_params* params); +ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void); +ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createAndInitCCtxParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); +ZSTDLIB_API size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* params, ZSTD_compressionParameters cParams); +ZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params); + + /*! ZSTD_getCParams() : * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize. * `estimatedSrcSize` value is optional, select 0 if not known */ From c0221124d5344bcfe727c23fae22cdd506915da6 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Thu, 17 Aug 2017 19:30:22 -0700 Subject: [PATCH 04/52] Add function to set opaque parameters --- lib/common/zstd_internal.h | 2 + lib/compress/zstd_compress.c | 189 +++++++++++++++++++++++++++-------- lib/zstd.h | 4 + 3 files changed, 156 insertions(+), 39 deletions(-) diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index ddafc7874..7c806b87c 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -224,6 +224,8 @@ typedef struct seqDef_s { typedef struct ZSTD_CCtx_params_s { ZSTD_compressionParameters cParams; ZSTD_frameParameters fParams; + int compressionLevel; + } ZSTD_CCtx_params; typedef struct { diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 509f8bedc..a48184153 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -230,45 +230,6 @@ static ZSTD_CCtx_params ZSTD_makeCCtxParamsFromCParams( return cctxParams; } -ZSTD_CCtx_params* ZSTD_createCCtxParams(void) -{ - ZSTD_CCtx_params* params = - (ZSTD_CCtx_params*)ZSTD_calloc(sizeof(ZSTD_CCtx_params), - ZSTD_defaultCMem); - if (!params) { return NULL; } - // TODO -// params->compressionLevel = ZSTD_CLEVEL_DEFAULT; - return params; -} - -size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* params, - ZSTD_compressionParameters cParams) -{ - CHECK_F( params == NULL ); - memset(params, 0, sizeof(ZSTD_CCtx_params)); - params->cParams = cParams; - return 0; -} - -ZSTD_CCtx_params* ZSTD_createAndInitCCtxParams( - int compressionLevel, unsigned long long estimatedSrcSize, - size_t dictSize) -{ - ZSTD_CCtx_params* params = ZSTD_createCCtxParams(); - if (params == NULL) { return NULL; } - ZSTD_initCCtxParams(params, ZSTD_getCParams( - compressionLevel, estimatedSrcSize, dictSize)); - return params; -} - -size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params) -{ - ZSTD_free(params, ZSTD_defaultCMem); - return 0; -} - - - static ZSTD_parameters ZSTD_getParamsFromCCtx(const ZSTD_CCtx* cctx) { return ZSTD_getParamsFromCCtxParams(cctx->appliedParams); } @@ -296,6 +257,55 @@ static void ZSTD_cLevelToCParams(ZSTD_CCtx* cctx) cctx->compressionLevel = ZSTD_CLEVEL_CUSTOM; } +ZSTD_CCtx_params* ZSTD_createCCtxParams(void) +{ + ZSTD_CCtx_params* params = + (ZSTD_CCtx_params*)ZSTD_calloc(sizeof(ZSTD_CCtx_params), + ZSTD_defaultCMem); + if (!params) { return NULL; } + params->compressionLevel = ZSTD_CLEVEL_DEFAULT; + return params; +} + +size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* params, + ZSTD_compressionParameters cParams) +{ + memset(params, 0, sizeof(ZSTD_CCtx_params)); + params->cParams = cParams; + params->compressionLevel = ZSTD_CLEVEL_CUSTOM; + return 0; +} + +ZSTD_CCtx_params* ZSTD_createAndInitCCtxParams( + int compressionLevel, unsigned long long estimatedSrcSize, + size_t dictSize) +{ + ZSTD_CCtx_params* params = ZSTD_createCCtxParams(); + if (params == NULL) { return NULL; } + ZSTD_initCCtxParams(params, ZSTD_getCParams( + compressionLevel, estimatedSrcSize, dictSize)); + params->compressionLevel = compressionLevel; + return params; +} + +size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params) +{ + if (params == NULL) { return 0; } + ZSTD_free(params, ZSTD_defaultCMem); + return 0; +} + + + +static void ZSTD_cLevelToCCtxParams(ZSTD_CCtx_params* params) +{ + if (params->compressionLevel == ZSTD_CLEVEL_CUSTOM) return; + // TODO: src size, code duplication + params->cParams = ZSTD_getCParams(params->compressionLevel, 0, 0); + params->compressionLevel = ZSTD_CLEVEL_CUSTOM; + +} + #define CLAMPCHECK(val,min,max) { \ if (((val)<(min)) | ((val)>(max))) { \ return ERROR(parameter_outOfBound); \ @@ -443,6 +453,107 @@ size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned v } } +size_t ZSTD_CCtxParam_setParameter( + ZSTD_CCtx_params* params, ZSTD_cParameter param, unsigned value) +{ + switch(param) + { + case ZSTD_p_compressionLevel : + if ((int)value > ZSTD_maxCLevel()) value = ZSTD_maxCLevel(); + if (value == 0) return 0; + params->compressionLevel = value; + return 0; + + case ZSTD_p_windowLog : + if (value == 0) return 0; + CLAMPCHECK(value, ZSTD_WINDOWLOG_MIN, ZSTD_WINDOWLOG_MAX); + ZSTD_cLevelToCCtxParams(params); + params->cParams.windowLog = value; + return 0; + + case ZSTD_p_hashLog : + if (value == 0) return 0; + CLAMPCHECK(value, ZSTD_HASHLOG_MIN, ZSTD_HASHLOG_MAX); + ZSTD_cLevelToCCtxParams(params); + params->cParams.hashLog = value; + return 0; + + case ZSTD_p_chainLog : + if (value == 0) return 0; + CLAMPCHECK(value, ZSTD_CHAINLOG_MIN, ZSTD_CHAINLOG_MAX); + ZSTD_cLevelToCCtxParams(params); + params->cParams.chainLog = value; + return 0; + + case ZSTD_p_searchLog : + if (value == 0) return 0; + CLAMPCHECK(value, ZSTD_SEARCHLOG_MIN, ZSTD_SEARCHLOG_MAX); + ZSTD_cLevelToCCtxParams(params); + params->cParams.searchLog = value; + return 0; + + case ZSTD_p_minMatch : + if (value == 0) return 0; + CLAMPCHECK(value, ZSTD_SEARCHLENGTH_MIN, ZSTD_SEARCHLENGTH_MAX); + ZSTD_cLevelToCCtxParams(params); + params->cParams.searchLength = value; + return 0; + + case ZSTD_p_targetLength : + if (value == 0) return 0; + CLAMPCHECK(value, ZSTD_TARGETLENGTH_MIN, ZSTD_TARGETLENGTH_MAX); + ZSTD_cLevelToCCtxParams(params); + params->cParams.targetLength = value; + return 0; + + case ZSTD_p_compressionStrategy : + if (value == 0) return 0; + CLAMPCHECK(value, (unsigned)ZSTD_fast, (unsigned)ZSTD_btultra); + ZSTD_cLevelToCCtxParams(params); + params->cParams.strategy = (ZSTD_strategy)value; + return 0; + + case ZSTD_p_contentSizeFlag : + params->fParams.contentSizeFlag = value > 0; + return 0; + + case ZSTD_p_checksumFlag : + params->fParams.checksumFlag = value > 0; + return 0; + + case ZSTD_p_dictIDFlag : + params->fParams.noDictIDFlag = (value == 0); + return 0; + + case ZSTD_p_dictMode : + ZSTD_STATIC_ASSERT((U32)ZSTD_dm_fullDict > (U32)ZSTD_dm_rawContent); + if (value > (unsigned)ZSTD_dm_fullDict) { + return ERROR(parameter_outOfBound); + } +// cctx->dictMode = (ZSTD_dictMode_e)value; + return 0; + + case ZSTD_p_refDictContent : +// cctx->dictContentByRef = value > 0; + return 0; + + case ZSTD_p_forceMaxWindow : +// cctx->forceWindow = value > 0; + return 0; + + case ZSTD_p_nbThreads : + return 0; + + case ZSTD_p_jobSize : + return 0; + + case ZSTD_p_overlapSizeLog : + return 0; + + default: return ERROR(parameter_unsupported); + } +} + ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize) { DEBUGLOG(5, " setting pledgedSrcSize to %u", (U32)pledgedSrcSize); diff --git a/lib/zstd.h b/lib/zstd.h index b847d01f2..e428319d4 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -616,6 +616,8 @@ ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createAndInitCCtxParams(int compressionLevel, ZSTDLIB_API size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* params, ZSTD_compressionParameters cParams); ZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params); +ZSTDLIB_API + /*! ZSTD_getCParams() : * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize. @@ -1009,6 +1011,8 @@ typedef enum { * @result : 0, or an error code (which can be tested with ZSTD_isError()). */ ZSTDLIB_API size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned value); +ZSTDLIB_API size_t ZSTD_CCtxParam_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, unsigned value); + /*! ZSTD_CCtx_setPledgedSrcSize() : * Total input data size to be compressed as a single frame. * This value will be controlled at the end, and result in error if not respected. From 97e27affcbe26f89b52f16b1a1a0b5e8f924419a Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Fri, 18 Aug 2017 11:20:08 -0700 Subject: [PATCH 05/52] Move compression level to cctx params --- lib/compress/zstd_compress.c | 97 +++++++++++++++++++++++------------- lib/zstd.h | 2 + 2 files changed, 64 insertions(+), 35 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index a48184153..1ffeb5565 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -83,7 +83,7 @@ struct ZSTD_CCtx_s { U32 forceWindow; /* force back-references to respect limit of 1<customMem = customMem; - cctx->compressionLevel = ZSTD_CLEVEL_DEFAULT; + cctx->requestedParams.compressionLevel = ZSTD_CLEVEL_DEFAULT; ZSTD_STATIC_ASSERT(zcss_init==0); ZSTD_STATIC_ASSERT(ZSTD_CONTENTSIZE_UNKNOWN==(0ULL - 1)); return cctx; @@ -230,6 +230,7 @@ static ZSTD_CCtx_params ZSTD_makeCCtxParamsFromCParams( return cctxParams; } +// TODO: get rid of this function static ZSTD_parameters ZSTD_getParamsFromCCtx(const ZSTD_CCtx* cctx) { return ZSTD_getParamsFromCCtxParams(cctx->appliedParams); } @@ -251,10 +252,11 @@ size_t ZSTD_setCCtxParameter(ZSTD_CCtx* cctx, ZSTD_CCtxParameter param, unsigned #define ZSTD_CLEVEL_CUSTOM 999 static void ZSTD_cLevelToCParams(ZSTD_CCtx* cctx) { - if (cctx->compressionLevel==ZSTD_CLEVEL_CUSTOM) return; - cctx->requestedParams.cParams = ZSTD_getCParams(cctx->compressionLevel, - cctx->pledgedSrcSizePlusOne-1, 0); - cctx->compressionLevel = ZSTD_CLEVEL_CUSTOM; + if (cctx->requestedParams.compressionLevel==ZSTD_CLEVEL_CUSTOM) return; + cctx->requestedParams.cParams = + ZSTD_getCParams(cctx->requestedParams.compressionLevel, + cctx->pledgedSrcSizePlusOne-1, 0); + cctx->requestedParams.compressionLevel = ZSTD_CLEVEL_CUSTOM; } ZSTD_CCtx_params* ZSTD_createCCtxParams(void) @@ -321,7 +323,7 @@ size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned v if ((int)value > ZSTD_maxCLevel()) value = ZSTD_maxCLevel(); /* cap max compression level */ if (value == 0) return 0; /* special value : 0 means "don't change anything" */ if (cctx->cdict) return ERROR(stage_wrong); - cctx->compressionLevel = value; + cctx->requestedParams.compressionLevel = value; return 0; case ZSTD_p_windowLog : @@ -554,6 +556,13 @@ size_t ZSTD_CCtxParam_setParameter( } } +ZSTDLIB_API size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, ZSTD_CCtx_params* params) +{ + (void)cctx; + (void)params; + return 0; +} + ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize) { DEBUGLOG(5, " setting pledgedSrcSize to %u", (U32)pledgedSrcSize); @@ -573,9 +582,9 @@ ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, s cctx->cdict = NULL; } else { ZSTD_compressionParameters const cParams = - cctx->compressionLevel == ZSTD_CLEVEL_CUSTOM ? + cctx->requestedParams.compressionLevel == ZSTD_CLEVEL_CUSTOM ? cctx->requestedParams.cParams : - ZSTD_getCParams(cctx->compressionLevel, 0, dictSize); + ZSTD_getCParams(cctx->requestedParams.compressionLevel, 0, dictSize); cctx->cdictLocal = ZSTD_createCDict_advanced( dict, dictSize, cctx->dictContentByRef, cctx->dictMode, @@ -779,11 +788,11 @@ static U32 ZSTD_equivalentParams(ZSTD_compressionParameters cParams1, /*! ZSTD_continueCCtx() : * reuse CCtx without reset (note : requires no dictionary) */ -static size_t ZSTD_continueCCtx(ZSTD_CCtx* cctx, ZSTD_parameters params, U64 pledgedSrcSize) +static size_t ZSTD_continueCCtx(ZSTD_CCtx* cctx, ZSTD_CCtx_params params, U64 pledgedSrcSize) { U32 const end = (U32)(cctx->nextSrc - cctx->base); DEBUGLOG(5, "continue mode"); - cctx->appliedParams = ZSTD_makeCCtxParamsFromParams(params); + cctx->appliedParams = params; cctx->pledgedSrcSizePlusOne = pledgedSrcSize+1; cctx->consumedSrcSize = 0; if (pledgedSrcSize == ZSTD_CONTENTSIZE_UNKNOWN) @@ -808,7 +817,7 @@ typedef enum { ZSTDb_not_buffered, ZSTDb_buffered } ZSTD_buffered_policy_e; /*! ZSTD_resetCCtx_internal() : note : `params` are assumed fully validated at this stage */ static size_t ZSTD_resetCCtx_internal(ZSTD_CCtx* zc, - ZSTD_parameters params, U64 pledgedSrcSize, + ZSTD_CCtx_params params, U64 pledgedSrcSize, ZSTD_compResetPolicy_e const crp, ZSTD_buffered_policy_e const zbuff) { @@ -871,7 +880,7 @@ static size_t ZSTD_resetCCtx_internal(ZSTD_CCtx* zc, } } /* init params */ - zc->appliedParams = ZSTD_makeCCtxParamsFromParams(params); + zc->appliedParams = params; zc->pledgedSrcSizePlusOne = pledgedSrcSize+1; zc->consumedSrcSize = 0; if (pledgedSrcSize == ZSTD_CONTENTSIZE_UNKNOWN) @@ -967,7 +976,7 @@ static size_t ZSTD_copyCCtx_internal(ZSTD_CCtx* dstCCtx, if (srcCCtx->stage!=ZSTDcs_init) return ERROR(stage_wrong); memcpy(&dstCCtx->customMem, &srcCCtx->customMem, sizeof(ZSTD_customMem)); - { ZSTD_parameters params = ZSTD_getParamsFromCCtxParams(srcCCtx->appliedParams); + { ZSTD_CCtx_params params = srcCCtx->appliedParams; params.fParams = fParams; ZSTD_resetCCtx_internal(dstCCtx, params, pledgedSrcSize, ZSTDcrp_noMemset, zbuff); @@ -3100,7 +3109,7 @@ static size_t ZSTD_compress_frameChunk (ZSTD_CCtx* cctx, static size_t ZSTD_writeFrameHeader(void* dst, size_t dstCapacity, - ZSTD_parameters params, U64 pledgedSrcSize, U32 dictID) + ZSTD_CCtx_params params, U64 pledgedSrcSize, U32 dictID) { BYTE* const op = (BYTE*)dst; U32 const dictIDSizeCodeLength = (dictID>0) + (dictID>=256) + (dictID>=65536); /* 0-3 */ U32 const dictIDSizeCode = params.fParams.noDictIDFlag ? 0 : dictIDSizeCodeLength; /* 0-3 */ @@ -3155,7 +3164,7 @@ static size_t ZSTD_compressContinue_internal (ZSTD_CCtx* cctx, if (frame && (cctx->stage==ZSTDcs_init)) { fhSize = ZSTD_writeFrameHeader( dst, dstCapacity, - ZSTD_getParamsFromCCtxParams(cctx->appliedParams), + cctx->appliedParams, cctx->pledgedSrcSizePlusOne-1, cctx->dictID); if (ZSTD_isError(fhSize)) return fhSize; dstCapacity -= fhSize; @@ -3206,7 +3215,8 @@ size_t ZSTD_compressContinue (ZSTD_CCtx* cctx, size_t ZSTD_getBlockSize(const ZSTD_CCtx* cctx) { - U32 const cLevel = cctx->compressionLevel; + // TODO: Applied params compression level okay? Gets overwritten + U32 const cLevel = cctx->appliedParams.compressionLevel; ZSTD_compressionParameters cParams = (cLevel == ZSTD_CLEVEL_CUSTOM) ? cctx->appliedParams.cParams : ZSTD_getCParams(cLevel, 0, 0); @@ -3409,7 +3419,7 @@ static size_t ZSTD_compressBegin_internal(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_dictMode_e dictMode, const ZSTD_CDict* cdict, - ZSTD_parameters params, U64 pledgedSrcSize, + ZSTD_CCtx_params params, U64 pledgedSrcSize, ZSTD_buffered_policy_e zbuff) { DEBUGLOG(4, "ZSTD_compressBegin_internal"); @@ -3437,18 +3447,21 @@ size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize) { + + ZSTD_CCtx_params cctxParams = cctx->requestedParams; + cctxParams.cParams = params.cParams; + cctxParams.fParams = params.fParams; /* compression parameters verification and optimization */ CHECK_F(ZSTD_checkCParams(params.cParams)); return ZSTD_compressBegin_internal(cctx, dict, dictSize, ZSTD_dm_auto, NULL, - params, pledgedSrcSize, ZSTDb_not_buffered); + cctxParams, pledgedSrcSize, ZSTDb_not_buffered); } size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel) { ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, dictSize); - return ZSTD_compressBegin_internal(cctx, dict, dictSize, ZSTD_dm_auto, NULL, - params, 0, ZSTDb_not_buffered); + return ZSTD_compressBegin_advanced(cctx, dict, dictSize, params, 0); } @@ -3472,7 +3485,7 @@ static size_t ZSTD_writeEpilogue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity) /* special case : empty frame */ if (cctx->stage == ZSTDcs_init) { - fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, ZSTD_getParamsFromCCtxParams(cctx->appliedParams), 0, 0); + fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, cctx->appliedParams, 0, 0); if (ZSTD_isError(fhSize)) return fhSize; dstCapacity -= fhSize; op += fhSize; @@ -3528,8 +3541,12 @@ static size_t ZSTD_compress_internal (ZSTD_CCtx* cctx, const void* dict,size_t dictSize, ZSTD_parameters params) { + ZSTD_CCtx_params cctxParams = cctx->requestedParams; + cctxParams.cParams = params.cParams; + cctxParams.fParams = params.fParams; + CHECK_F( ZSTD_compressBegin_internal(cctx, dict, dictSize, ZSTD_dm_auto, NULL, - params, srcSize, ZSTDb_not_buffered) ); + cctxParams, srcSize, ZSTDb_not_buffered) ); return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize); } @@ -3634,10 +3651,14 @@ static size_t ZSTD_initCDict_internal( { ZSTD_frameParameters const fParams = { 0 /* contentSizeFlag */, 0 /* checksumFlag */, 0 /* noDictIDFlag */ }; /* dummy */ ZSTD_parameters const params = ZSTD_makeParams(cParams, fParams); + ZSTD_CCtx_params cctxParams = + ZSTD_makeCCtxParamsFromParams(params); + cctxParams.compressionLevel = + cdict->refContext->requestedParams.compressionLevel; CHECK_F( ZSTD_compressBegin_internal(cdict->refContext, cdict->dictContent, dictSize, dictMode, NULL, - params, ZSTD_CONTENTSIZE_UNKNOWN, + cctxParams, ZSTD_CONTENTSIZE_UNKNOWN, ZSTDb_not_buffered) ); } @@ -3764,6 +3785,10 @@ ZSTD_parameters ZSTD_getParamsFromCDict(const ZSTD_CDict* cdict) { return ZSTD_getParamsFromCCtx(cdict->refContext); } +static ZSTD_CCtx_params ZSTD_getCCtxParamsFromCDict(const ZSTD_CDict* cdict) { + return cdict->refContext->appliedParams; +} + /* ZSTD_compressBegin_usingCDict_advanced() : * cdict must be != NULL */ size_t ZSTD_compressBegin_usingCDict_advanced( @@ -3771,8 +3796,8 @@ size_t ZSTD_compressBegin_usingCDict_advanced( ZSTD_frameParameters const fParams, unsigned long long const pledgedSrcSize) { if (cdict==NULL) return ERROR(dictionary_wrong); - { ZSTD_parameters params = - ZSTD_getParamsFromCCtxParams(cdict->refContext->appliedParams); + { + ZSTD_CCtx_params params = cdict->refContext->appliedParams; params.fParams = fParams; DEBUGLOG(5, "ZSTD_compressBegin_usingCDict_advanced"); return ZSTD_compressBegin_internal(cctx, @@ -3858,6 +3883,8 @@ static size_t ZSTD_resetCStream_internal(ZSTD_CStream* zcs, const ZSTD_CDict* cdict, ZSTD_parameters params, unsigned long long pledgedSrcSize) { + ZSTD_CCtx_params cctxParams = ZSTD_makeCCtxParamsFromParams(params); + cctxParams.compressionLevel = zcs->requestedParams.compressionLevel; DEBUGLOG(4, "ZSTD_resetCStream_internal"); /* params are supposed to be fully validated at this point */ assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); @@ -3866,7 +3893,7 @@ static size_t ZSTD_resetCStream_internal(ZSTD_CStream* zcs, CHECK_F( ZSTD_compressBegin_internal(zcs, dict, dictSize, dictMode, cdict, - params, pledgedSrcSize, + cctxParams, pledgedSrcSize, ZSTDb_buffered) ); zcs->inToCompress = 0; @@ -3883,8 +3910,8 @@ size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize) ZSTD_parameters params = ZSTD_getParamsFromCCtxParams(zcs->requestedParams); params.fParams.contentSizeFlag = (pledgedSrcSize > 0); DEBUGLOG(5, "ZSTD_resetCStream"); - if (zcs->compressionLevel != ZSTD_CLEVEL_CUSTOM) { - params.cParams = ZSTD_getCParams(zcs->compressionLevel, pledgedSrcSize, 0 /* dictSize */); + if (zcs->requestedParams.compressionLevel != ZSTD_CLEVEL_CUSTOM) { + params.cParams = ZSTD_getCParams(zcs->requestedParams.compressionLevel, pledgedSrcSize, 0 /* dictSize */); } return ZSTD_resetCStream_internal(zcs, NULL, 0, zcs->dictMode, zcs->cdict, params, pledgedSrcSize); } @@ -3915,7 +3942,7 @@ size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, if (zcs->cdictLocal == NULL) return ERROR(memory_allocation); } else { if (cdict) { - ZSTD_parameters const cdictParams = ZSTD_getParamsFromCDict(cdict); + ZSTD_CCtx_params const cdictParams = ZSTD_getCCtxParamsFromCDict(cdict); params.cParams = cdictParams.cParams; /* cParams are enforced from cdict */ } ZSTD_freeCDict(zcs->cdictLocal); @@ -3924,7 +3951,7 @@ size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, } zcs->requestedParams = ZSTD_makeCCtxParamsFromParams(params); - zcs->compressionLevel = ZSTD_CLEVEL_CUSTOM; + zcs->requestedParams.compressionLevel = ZSTD_CLEVEL_CUSTOM; return ZSTD_resetCStream_internal(zcs, NULL, 0, zcs->dictMode, zcs->cdict, params, pledgedSrcSize); } @@ -3957,14 +3984,14 @@ size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, { CHECK_F( ZSTD_checkCParams(params.cParams) ); zcs->requestedParams = ZSTD_makeCCtxParamsFromParams(params); - zcs->compressionLevel = ZSTD_CLEVEL_CUSTOM; + zcs->requestedParams.compressionLevel = ZSTD_CLEVEL_CUSTOM; return ZSTD_initCStream_internal(zcs, dict, dictSize, NULL, params, pledgedSrcSize); } size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel) { ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, dictSize); - zcs->compressionLevel = compressionLevel; + zcs->requestedParams.compressionLevel = compressionLevel; return ZSTD_initCStream_internal(zcs, dict, dictSize, NULL, params, 0); } @@ -4171,8 +4198,8 @@ size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, const void* const prefix = cctx->prefix; size_t const prefixSize = cctx->prefixSize; ZSTD_parameters params = ZSTD_getParamsFromCCtxParams(cctx->requestedParams); - if (cctx->compressionLevel != ZSTD_CLEVEL_CUSTOM) - params.cParams = ZSTD_getCParams(cctx->compressionLevel, + if (cctx->requestedParams.compressionLevel != ZSTD_CLEVEL_CUSTOM) + params.cParams = ZSTD_getCParams(cctx->requestedParams.compressionLevel, cctx->pledgedSrcSizePlusOne-1, 0 /*dictSize*/); cctx->prefix = NULL; cctx->prefixSize = 0; /* single usage */ assert(prefix==NULL || cctx->cdict==NULL); /* only one can be set */ diff --git a/lib/zstd.h b/lib/zstd.h index e428319d4..e5ff0a38e 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -1013,6 +1013,8 @@ ZSTDLIB_API size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param ZSTDLIB_API size_t ZSTD_CCtxParam_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, unsigned value); +ZSTDLIB_API size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, ZSTD_CCtx_params* params); + /*! ZSTD_CCtx_setPledgedSrcSize() : * Total input data size to be compressed as a single frame. * This value will be controlled at the end, and result in error if not respected. From b6cb2ed8cb6eaf57f7f8a1c967e6f8590a998205 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Fri, 18 Aug 2017 11:43:31 -0700 Subject: [PATCH 06/52] Move dictMode to cctxParams --- lib/common/zstd_internal.h | 2 + lib/compress/zstd_compress.c | 76 +++++++++++++++++++++--------------- lib/zstd.h | 2 +- 3 files changed, 47 insertions(+), 33 deletions(-) diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index 7c806b87c..b1cbd05a7 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -225,6 +225,8 @@ typedef struct ZSTD_CCtx_params_s { ZSTD_compressionParameters cParams; ZSTD_frameParameters fParams; int compressionLevel; + U32 forceWindow; + ZSTD_dictMode_e dictMode; } ZSTD_CCtx_params; diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 1ffeb5565..ada9d4f57 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -80,7 +80,7 @@ struct ZSTD_CCtx_s { U32 nextToUpdate3; /* index from which to continue dictionary update */ U32 hashLog3; /* dispatch table : larger == faster, more memory */ U32 loadedDictEnd; /* index of end of dictionary */ - U32 forceWindow; /* force back-references to respect limit of 1<forceWindow = value>0; cctx->loadedDictEnd = 0; return 0; + case ZSTD_p_forceWindow : + cctx->requestedParams.forceWindow = value>0; + cctx->loadedDictEnd = 0; + return 0; ZSTD_STATIC_ASSERT(ZSTD_dm_auto==0); ZSTD_STATIC_ASSERT(ZSTD_dm_rawContent==1); - case ZSTD_p_forceRawDict : cctx->dictMode = (ZSTD_dictMode_e)(value>0); return 0; + case ZSTD_p_forceRawDict : + cctx->requestedParams.dictMode = (ZSTD_dictMode_e)(value>0); + return 0; default: return ERROR(parameter_unsupported); } } @@ -407,7 +412,7 @@ size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned v ZSTD_STATIC_ASSERT((U32)ZSTD_dm_fullDict > (U32)ZSTD_dm_rawContent); if (value > (unsigned)ZSTD_dm_fullDict) return ERROR(parameter_outOfBound); - cctx->dictMode = (ZSTD_dictMode_e)value; + cctx->requestedParams.dictMode = (ZSTD_dictMode_e)value; return 0; case ZSTD_p_refDictContent : @@ -419,7 +424,7 @@ size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned v case ZSTD_p_forceMaxWindow : /* Force back-references to remain < windowSize, * even when referencing into Dictionary content * default : 0 when using a CDict, 1 when using a Prefix */ - cctx->forceWindow = value>0; + cctx->requestedParams.forceWindow = value>0; cctx->loadedDictEnd = 0; return 0; @@ -532,7 +537,7 @@ size_t ZSTD_CCtxParam_setParameter( if (value > (unsigned)ZSTD_dm_fullDict) { return ERROR(parameter_outOfBound); } -// cctx->dictMode = (ZSTD_dictMode_e)value; + params->dictMode = (ZSTD_dictMode_e)value; return 0; case ZSTD_p_refDictContent : @@ -540,7 +545,7 @@ size_t ZSTD_CCtxParam_setParameter( return 0; case ZSTD_p_forceMaxWindow : -// cctx->forceWindow = value > 0; + params->forceWindow = value > 0; return 0; case ZSTD_p_nbThreads : @@ -587,7 +592,7 @@ ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, s ZSTD_getCParams(cctx->requestedParams.compressionLevel, 0, dictSize); cctx->cdictLocal = ZSTD_createCDict_advanced( dict, dictSize, - cctx->dictContentByRef, cctx->dictMode, + cctx->dictContentByRef, cctx->requestedParams.dictMode, cParams, cctx->customMem); cctx->cdict = cctx->cdictLocal; if (cctx->cdictLocal == NULL) @@ -3244,7 +3249,7 @@ static size_t ZSTD_loadDictionaryContent(ZSTD_CCtx* zc, const void* src, size_t zc->dictBase = zc->base; zc->base += ip - zc->nextSrc; zc->nextToUpdate = zc->dictLimit; - zc->loadedDictEnd = zc->forceWindow ? 0 : (U32)(iend - zc->base); + zc->loadedDictEnd = zc->appliedParams.forceWindow ? 0 : (U32)(iend - zc->base); zc->nextSrc = iend; if (srcSize <= HASH_READ_SIZE) return 0; @@ -3417,14 +3422,13 @@ static size_t ZSTD_compress_insertDictionary(ZSTD_CCtx* cctx, * @return : 0, or an error code */ static size_t ZSTD_compressBegin_internal(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, - ZSTD_dictMode_e dictMode, const ZSTD_CDict* cdict, ZSTD_CCtx_params params, U64 pledgedSrcSize, ZSTD_buffered_policy_e zbuff) { DEBUGLOG(4, "ZSTD_compressBegin_internal"); DEBUGLOG(4, "dict ? %s", dict ? "dict" : (cdict ? "cdict" : "none")); - DEBUGLOG(4, "dictMode : %u", (U32)dictMode); + DEBUGLOG(4, "dictMode : %u", (U32)(params.dictMode)); /* params are supposed to be fully validated at this point */ assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); assert(!((dict) && (cdict))); /* either dict or cdict, not both */ @@ -3437,7 +3441,7 @@ static size_t ZSTD_compressBegin_internal(ZSTD_CCtx* cctx, CHECK_F( ZSTD_resetCCtx_internal(cctx, params, pledgedSrcSize, ZSTDcrp_continue, zbuff) ); - return ZSTD_compress_insertDictionary(cctx, dict, dictSize, dictMode); + return ZSTD_compress_insertDictionary(cctx, dict, dictSize, params.dictMode); } @@ -3451,9 +3455,10 @@ size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, ZSTD_CCtx_params cctxParams = cctx->requestedParams; cctxParams.cParams = params.cParams; cctxParams.fParams = params.fParams; + cctxParams.dictMode = ZSTD_dm_auto; /* compression parameters verification and optimization */ CHECK_F(ZSTD_checkCParams(params.cParams)); - return ZSTD_compressBegin_internal(cctx, dict, dictSize, ZSTD_dm_auto, NULL, + return ZSTD_compressBegin_internal(cctx, dict, dictSize, NULL, cctxParams, pledgedSrcSize, ZSTDb_not_buffered); } @@ -3544,8 +3549,9 @@ static size_t ZSTD_compress_internal (ZSTD_CCtx* cctx, ZSTD_CCtx_params cctxParams = cctx->requestedParams; cctxParams.cParams = params.cParams; cctxParams.fParams = params.fParams; + cctxParams.dictMode = ZSTD_dm_auto; - CHECK_F( ZSTD_compressBegin_internal(cctx, dict, dictSize, ZSTD_dm_auto, NULL, + CHECK_F( ZSTD_compressBegin_internal(cctx, dict, dictSize, NULL, cctxParams, srcSize, ZSTDb_not_buffered) ); return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize); } @@ -3621,6 +3627,7 @@ size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict) return ZSTD_sizeof_CCtx(cdict->refContext) + (cdict->dictBuffer ? cdict->dictContentSize : 0) + sizeof(*cdict); } +#if 0 static ZSTD_parameters ZSTD_makeParams(ZSTD_compressionParameters cParams, ZSTD_frameParameters fParams) { ZSTD_parameters params; @@ -3628,6 +3635,7 @@ static ZSTD_parameters ZSTD_makeParams(ZSTD_compressionParameters cParams, ZSTD_ params.fParams = fParams; return params; } +#endif static size_t ZSTD_initCDict_internal( ZSTD_CDict* cdict, @@ -3650,13 +3658,12 @@ static size_t ZSTD_initCDict_internal( { ZSTD_frameParameters const fParams = { 0 /* contentSizeFlag */, 0 /* checksumFlag */, 0 /* noDictIDFlag */ }; /* dummy */ - ZSTD_parameters const params = ZSTD_makeParams(cParams, fParams); - ZSTD_CCtx_params cctxParams = - ZSTD_makeCCtxParamsFromParams(params); - cctxParams.compressionLevel = - cdict->refContext->requestedParams.compressionLevel; + ZSTD_CCtx_params cctxParams = cdict->refContext->requestedParams; + cctxParams.cParams = cParams; + cctxParams.fParams = fParams; + cctxParams.dictMode = dictMode; CHECK_F( ZSTD_compressBegin_internal(cdict->refContext, - cdict->dictContent, dictSize, dictMode, + cdict->dictContent, dictSize, NULL, cctxParams, ZSTD_CONTENTSIZE_UNKNOWN, ZSTDb_not_buffered) ); @@ -3723,7 +3730,7 @@ size_t ZSTD_freeCDict(ZSTD_CDict* cdict) ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( void *workspace, size_t workspaceSize, const void* dict, - size_t dictSize, unsigned byReference, ZSTD_dictMode_e dictMode, + size_t dictSize, unsigned byReference, ZSTD_CCtx_params* params) { ZSTD_compressionParameters cParams = params->cParams; @@ -3749,7 +3756,7 @@ ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( if (ZSTD_isError( ZSTD_initCDict_internal(cdict, dict, dictSize, - 1 /* byReference */, dictMode, + 1 /* byReference */, params->dictMode, cParams) )) return NULL; @@ -3775,9 +3782,10 @@ ZSTD_CDict* ZSTD_initStaticCDict(void* workspace, size_t workspaceSize, ZSTD_compressionParameters cParams) { ZSTD_CCtx_params params = ZSTD_makeCCtxParamsFromCParams(cParams); + params.dictMode = dictMode; return ZSTD_initStaticCDict_advanced_opaque( workspace, workspaceSize, dict, dictSize, - byReference, dictMode, ¶ms); + byReference, ¶ms); } @@ -3799,9 +3807,10 @@ size_t ZSTD_compressBegin_usingCDict_advanced( { ZSTD_CCtx_params params = cdict->refContext->appliedParams; params.fParams = fParams; + params.dictMode = ZSTD_dm_auto; DEBUGLOG(5, "ZSTD_compressBegin_usingCDict_advanced"); return ZSTD_compressBegin_internal(cctx, - NULL, 0, ZSTD_dm_auto, + NULL, 0, cdict, params, pledgedSrcSize, ZSTDb_not_buffered); @@ -3885,13 +3894,14 @@ static size_t ZSTD_resetCStream_internal(ZSTD_CStream* zcs, { ZSTD_CCtx_params cctxParams = ZSTD_makeCCtxParamsFromParams(params); cctxParams.compressionLevel = zcs->requestedParams.compressionLevel; + cctxParams.dictMode = dictMode; DEBUGLOG(4, "ZSTD_resetCStream_internal"); /* params are supposed to be fully validated at this point */ assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); assert(!((dict) && (cdict))); /* either dict or cdict, not both */ CHECK_F( ZSTD_compressBegin_internal(zcs, - dict, dictSize, dictMode, + dict, dictSize, cdict, cctxParams, pledgedSrcSize, ZSTDb_buffered) ); @@ -3913,7 +3923,7 @@ size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize) if (zcs->requestedParams.compressionLevel != ZSTD_CLEVEL_CUSTOM) { params.cParams = ZSTD_getCParams(zcs->requestedParams.compressionLevel, pledgedSrcSize, 0 /* dictSize */); } - return ZSTD_resetCStream_internal(zcs, NULL, 0, zcs->dictMode, zcs->cdict, params, pledgedSrcSize); + return ZSTD_resetCStream_internal(zcs, NULL, 0, zcs->requestedParams.dictMode, zcs->cdict, params, pledgedSrcSize); } /*! ZSTD_initCStream_internal() : @@ -3936,7 +3946,8 @@ size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, } ZSTD_freeCDict(zcs->cdictLocal); zcs->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, - zcs->dictContentByRef, zcs->dictMode, + zcs->dictContentByRef, + zcs->requestedParams.dictMode, params.cParams, zcs->customMem); zcs->cdict = zcs->cdictLocal; if (zcs->cdictLocal == NULL) return ERROR(memory_allocation); @@ -3949,10 +3960,11 @@ size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, zcs->cdictLocal = NULL; zcs->cdict = cdict; } - - zcs->requestedParams = ZSTD_makeCCtxParamsFromParams(params); + zcs->requestedParams.cParams = params.cParams; + zcs->requestedParams.fParams = params.fParams; zcs->requestedParams.compressionLevel = ZSTD_CLEVEL_CUSTOM; - return ZSTD_resetCStream_internal(zcs, NULL, 0, zcs->dictMode, zcs->cdict, params, pledgedSrcSize); + + return ZSTD_resetCStream_internal(zcs, NULL, 0, zcs->requestedParams.dictMode, zcs->cdict, params, pledgedSrcSize); } /* ZSTD_initCStream_usingCDict_advanced() : @@ -4212,7 +4224,7 @@ size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, } else #endif { - CHECK_F( ZSTD_resetCStream_internal(cctx, prefix, prefixSize, cctx->dictMode, cctx->cdict, params, cctx->pledgedSrcSizePlusOne-1) ); + CHECK_F( ZSTD_resetCStream_internal(cctx, prefix, prefixSize, cctx->requestedParams.dictMode, cctx->cdict, params, cctx->pledgedSrcSizePlusOne-1) ); } } /* compression stage */ diff --git a/lib/zstd.h b/lib/zstd.h index e5ff0a38e..be3d7c4cc 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -608,7 +608,7 @@ ZSTDLIB_API ZSTD_CDict* ZSTD_initStaticCDict( ZSTDLIB_API ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( void* workspace, size_t workspaceSize, const void* dict, size_t dictSize, - unsigned byReference, ZSTD_dictMode_e dictMode, + unsigned byReference, ZSTD_CCtx_params* params); ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void); From 2300c58a6f2fd98b9fbf602cfae4d868b38ab87a Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Fri, 18 Aug 2017 12:03:16 -0700 Subject: [PATCH 07/52] Move dictContentByRef to cctx params --- lib/common/zstd_internal.h | 5 +++++ lib/compress/zstd_compress.c | 33 ++++++++++++++++++++------------- lib/zstd.h | 5 +++-- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index b1cbd05a7..f7359646e 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -224,9 +224,14 @@ typedef struct seqDef_s { typedef struct ZSTD_CCtx_params_s { ZSTD_compressionParameters cParams; ZSTD_frameParameters fParams; + int compressionLevel; U32 forceWindow; + + /* Dictionary */ ZSTD_dictMode_e dictMode; + U32 dictContentByRef; + } ZSTD_CCtx_params; diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index ada9d4f57..7802df507 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -117,7 +117,7 @@ struct ZSTD_CCtx_s { /* Dictionary */ // ZSTD_dictMode_e dictMode; /* select restricting dictionary to "rawContent" or "fullDict" only */ - U32 dictContentByRef; +// U32 dictContentByRef; ZSTD_CDict* cdictLocal; const ZSTD_CDict* cdict; const void* prefix; @@ -418,7 +418,7 @@ size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned v case ZSTD_p_refDictContent : if (cctx->cdict) return ERROR(stage_wrong); /* must be set before loading */ /* dictionary content will be referenced, instead of copied */ - cctx->dictContentByRef = value>0; + cctx->requestedParams.dictContentByRef = value>0; return 0; case ZSTD_p_forceMaxWindow : /* Force back-references to remain < windowSize, @@ -541,7 +541,7 @@ size_t ZSTD_CCtxParam_setParameter( return 0; case ZSTD_p_refDictContent : -// cctx->dictContentByRef = value > 0; + params->dictContentByRef = value > 0; return 0; case ZSTD_p_forceMaxWindow : @@ -549,12 +549,15 @@ size_t ZSTD_CCtxParam_setParameter( return 0; case ZSTD_p_nbThreads : + // TODO return 0; case ZSTD_p_jobSize : + // TODO return 0; case ZSTD_p_overlapSizeLog : + // TODO return 0; default: return ERROR(parameter_unsupported); @@ -592,7 +595,8 @@ ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, s ZSTD_getCParams(cctx->requestedParams.compressionLevel, 0, dictSize); cctx->cdictLocal = ZSTD_createCDict_advanced( dict, dictSize, - cctx->dictContentByRef, cctx->requestedParams.dictMode, + cctx->requestedParams.dictContentByRef, + cctx->requestedParams.dictMode, cParams, cctx->customMem); cctx->cdict = cctx->cdictLocal; if (cctx->cdictLocal == NULL) @@ -3594,14 +3598,14 @@ size_t ZSTD_compress(void* dst, size_t dstCapacity, const void* src, size_t srcS /* ===== Dictionary API ===== */ size_t ZSTD_estimateCDictSize_advanced_opaque( - size_t dictSize, ZSTD_CCtx_params* params, unsigned byReference) + size_t dictSize, ZSTD_CCtx_params* params) { if (params == NULL) { return 0; } DEBUGLOG(5, "sizeof(ZSTD_CDict) : %u", (U32)sizeof(ZSTD_CDict)); DEBUGLOG(5, "CCtx estimate : %u", (U32)ZSTD_estimateCCtxSize_advanced_opaque(params)); return sizeof(ZSTD_CDict) + ZSTD_estimateCCtxSize_advanced_opaque(params) - + (byReference ? 0 : dictSize); + + (params->dictContentByRef ? 0 : dictSize); } @@ -3610,7 +3614,8 @@ size_t ZSTD_estimateCDictSize_advanced_opaque( size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, unsigned byReference) { ZSTD_CCtx_params params = ZSTD_makeCCtxParamsFromCParams(cParams); - return ZSTD_estimateCDictSize_advanced_opaque(dictSize, ¶ms, byReference); + params.dictContentByRef = byReference; + return ZSTD_estimateCDictSize_advanced_opaque(dictSize, ¶ms); } size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel) @@ -3662,6 +3667,8 @@ static size_t ZSTD_initCDict_internal( cctxParams.cParams = cParams; cctxParams.fParams = fParams; cctxParams.dictMode = dictMode; + cctxParams.dictContentByRef = byReference; + CHECK_F( ZSTD_compressBegin_internal(cdict->refContext, cdict->dictContent, dictSize, NULL, @@ -3730,12 +3737,12 @@ size_t ZSTD_freeCDict(ZSTD_CDict* cdict) ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( void *workspace, size_t workspaceSize, const void* dict, - size_t dictSize, unsigned byReference, + size_t dictSize, ZSTD_CCtx_params* params) { ZSTD_compressionParameters cParams = params->cParams; size_t const cctxSize = ZSTD_estimateCCtxSize_advanced(cParams); - size_t const neededSize = sizeof(ZSTD_CDict) + (byReference ? 0 : dictSize) + size_t const neededSize = sizeof(ZSTD_CDict) + (params->dictContentByRef ? 0 : dictSize) + cctxSize; ZSTD_CDict* const cdict = (ZSTD_CDict*) workspace; void* ptr; @@ -3745,7 +3752,7 @@ ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( (U32)workspaceSize, (U32)neededSize, (U32)(workspaceSize < neededSize)); if (workspaceSize < neededSize) return NULL; - if (!byReference) { + if (!params->dictContentByRef) { memcpy(cdict+1, dict, dictSize); dict = cdict+1; ptr = (char*)workspace + sizeof(ZSTD_CDict) + dictSize; @@ -3783,12 +3790,12 @@ ZSTD_CDict* ZSTD_initStaticCDict(void* workspace, size_t workspaceSize, { ZSTD_CCtx_params params = ZSTD_makeCCtxParamsFromCParams(cParams); params.dictMode = dictMode; + params.dictContentByRef = byReference; return ZSTD_initStaticCDict_advanced_opaque( workspace, workspaceSize, dict, dictSize, - byReference, ¶ms); + ¶ms); } - ZSTD_parameters ZSTD_getParamsFromCDict(const ZSTD_CDict* cdict) { return ZSTD_getParamsFromCCtx(cdict->refContext); } @@ -3946,7 +3953,7 @@ size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, } ZSTD_freeCDict(zcs->cdictLocal); zcs->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, - zcs->dictContentByRef, + zcs->requestedParams.dictContentByRef, zcs->requestedParams.dictMode, params.cParams, zcs->customMem); zcs->cdict = zcs->cdictLocal; diff --git a/lib/zstd.h b/lib/zstd.h index be3d7c4cc..56316160a 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -528,7 +528,9 @@ ZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t sr * Note : dictionary created "byReference" are smaller */ ZSTDLIB_API size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel); ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, unsigned byReference); -ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced_opaque(size_t dictSize, ZSTD_CCtx_params* params, unsigned byReference); + +// By reference +ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced_opaque(size_t dictSize, ZSTD_CCtx_params* params); ZSTDLIB_API size_t ZSTD_estimateDDictSize(size_t dictSize, unsigned byReference); @@ -608,7 +610,6 @@ ZSTDLIB_API ZSTD_CDict* ZSTD_initStaticCDict( ZSTDLIB_API ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( void* workspace, size_t workspaceSize, const void* dict, size_t dictSize, - unsigned byReference, ZSTD_CCtx_params* params); ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void); From 81d89d82a69681567934d7f48c2dfb8a9ca409d8 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Fri, 18 Aug 2017 12:08:57 -0700 Subject: [PATCH 08/52] Move nbThreads to cctx params --- lib/common/zstd_internal.h | 2 ++ lib/compress/zstd_compress.c | 27 ++++++++++++++++----------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index f7359646e..7ea2ecc47 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -232,6 +232,8 @@ typedef struct ZSTD_CCtx_params_s { ZSTD_dictMode_e dictMode; U32 dictContentByRef; + /* Multithreading */ + U32 nbThreads; } ZSTD_CCtx_params; diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 7802df507..e47ae9a1e 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -124,7 +124,7 @@ struct ZSTD_CCtx_s { size_t prefixSize; /* Multi-threading */ - U32 nbThreads; +// U32 nbThreads; ZSTDMT_CCtx* mtctx; }; @@ -434,25 +434,25 @@ size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned v #ifndef ZSTD_MULTITHREAD if (value > 1) return ERROR(parameter_unsupported); #endif - if ((value>1) && (cctx->nbThreads != value)) { + if ((value>1) && (cctx->requestedParams.nbThreads != value)) { if (cctx->staticSize) /* MT not compatible with static alloc */ return ERROR(parameter_unsupported); ZSTDMT_freeCCtx(cctx->mtctx); - cctx->nbThreads = 1; + cctx->requestedParams.nbThreads = 1; cctx->mtctx = ZSTDMT_createCCtx_advanced(value, cctx->customMem); if (cctx->mtctx == NULL) return ERROR(memory_allocation); } - cctx->nbThreads = value; + cctx->requestedParams.nbThreads = value; return 0; case ZSTD_p_jobSize: - if (cctx->nbThreads <= 1) return ERROR(parameter_unsupported); + if (cctx->requestedParams.nbThreads <= 1) return ERROR(parameter_unsupported); assert(cctx->mtctx != NULL); return ZSTDMT_setMTCtxParameter(cctx->mtctx, ZSTDMT_p_sectionSize, value); case ZSTD_p_overlapSizeLog: - DEBUGLOG(5, " setting overlap with nbThreads == %u", cctx->nbThreads); - if (cctx->nbThreads <= 1) return ERROR(parameter_unsupported); + DEBUGLOG(5, " setting overlap with nbThreads == %u", cctx->requestedParams.nbThreads); + if (cctx->requestedParams.nbThreads <= 1) return ERROR(parameter_unsupported); assert(cctx->mtctx != NULL); return ZSTDMT_setMTCtxParameter(cctx->mtctx, ZSTDMT_p_overlapSectionLog, value); @@ -549,7 +549,12 @@ size_t ZSTD_CCtxParam_setParameter( return 0; case ZSTD_p_nbThreads : - // TODO + if (value == 0) { return 0; } +#ifndef ZSTD_MULTITHREAD + if (value > 1) return ERROR(parameter_unsupported); +#endif + // Do checks when applying parameters to cctx. + params->nbThreads = value; return 0; case ZSTD_p_jobSize : @@ -4224,8 +4229,8 @@ size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, assert(prefix==NULL || cctx->cdict==NULL); /* only one can be set */ #ifdef ZSTD_MULTITHREAD - if (cctx->nbThreads > 1) { - DEBUGLOG(4, "call ZSTDMT_initCStream_internal as nbThreads=%u", cctx->nbThreads); + if (cctx->requestedParams.nbThreads > 1) { + DEBUGLOG(4, "call ZSTDMT_initCStream_internal as nbThreads=%u", cctx->requestedParams.nbThreads); CHECK_F( ZSTDMT_initCStream_internal(cctx->mtctx, prefix, prefixSize, cctx->cdict, params, cctx->pledgedSrcSizePlusOne-1) ); cctx->streamStage = zcss_load; } else @@ -4236,7 +4241,7 @@ size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, /* compression stage */ #ifdef ZSTD_MULTITHREAD - if (cctx->nbThreads > 1) { + if (cctx->requestedParams.nbThreads > 1) { size_t const flushMin = ZSTDMT_compressStream_generic(cctx->mtctx, output, input, endOp); DEBUGLOG(5, "ZSTDMT_compressStream_generic : %u", (U32)flushMin); if ( ZSTD_isError(flushMin) From 399ae013d4028bb197b14b183cb91b53742884f6 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Fri, 18 Aug 2017 13:01:55 -0700 Subject: [PATCH 09/52] Add function to apply cctx params --- lib/common/zstd_internal.h | 34 ++++++++++++++++++---------------- lib/compress/zstd_compress.c | 29 +++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index 7ea2ecc47..fa9f0c49c 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -221,22 +221,6 @@ typedef struct seqDef_s { U16 matchLength; } seqDef; -typedef struct ZSTD_CCtx_params_s { - ZSTD_compressionParameters cParams; - ZSTD_frameParameters fParams; - - int compressionLevel; - U32 forceWindow; - - /* Dictionary */ - ZSTD_dictMode_e dictMode; - U32 dictContentByRef; - - /* Multithreading */ - U32 nbThreads; - -} ZSTD_CCtx_params; - typedef struct { seqDef* sequencesStart; seqDef* sequences; @@ -251,6 +235,24 @@ typedef struct { U32 repToConfirm[ZSTD_REP_NUM]; } seqStore_t; +struct ZSTD_CCtx_params_s { + ZSTD_compressionParameters cParams; + ZSTD_frameParameters fParams; + + int compressionLevel; + + U32 forceWindow; /* force back-references to respect limit of 1<nbThreads <= 1) { return ERROR(parameter_unsupported); } + params->jobSize = value; return 0; case ZSTD_p_overlapSizeLog : - // TODO + params->overlapSizeLog = value; return 0; default: return ERROR(parameter_unsupported); } } +// This function should probably be updated whenever ZSTD_CCtx_params is updated. ZSTDLIB_API size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, ZSTD_CCtx_params* params) { - (void)cctx; - (void)params; + if (cctx->cdict) { return ERROR(stage_wrong); } + + /* Assume the compression and frame parameters are validated */ + cctx->requestedParams.cParams = params->cParams; + cctx->requestedParams.fParams = params->fParams; + cctx->requestedParams.compressionLevel = params->compressionLevel; + + /* Assume dictionary parameters are validated */ + cctx->requestedParams.dictMode = params->dictMode; + cctx->requestedParams.dictContentByRef = params->dictContentByRef; + + /* Set force window explicitly since it sets cctx->loadedDictEnd */ + CHECK_F( ZSTD_CCtx_setParameter( + cctx, ZSTD_p_forceMaxWindow, params->forceWindow) ); + + /* Set multithreading parameters explicitly */ + CHECK_F( ZSTD_CCtx_setParameter(cctx, ZSTD_p_nbThreads, params->nbThreads) ); + CHECK_F( ZSTD_CCtx_setParameter(cctx, ZSTD_p_jobSize, params->jobSize) ); + CHECK_F( ZSTD_CCtx_setParameter( + cctx, ZSTD_p_overlapSizeLog, params->overlapSizeLog) ); return 0; } From 63b8c985317b38e69b69fa69f4727e8d55ce2cb4 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Fri, 18 Aug 2017 16:17:24 -0700 Subject: [PATCH 10/52] Pass cctx parameters to MTCtx --- lib/compress/zstd_compress.c | 108 ++++++++++++++++++++++++--------- lib/compress/zstdmt_compress.c | 107 +++++++++++++++++++++++--------- lib/compress/zstdmt_compress.h | 9 +++ 3 files changed, 165 insertions(+), 59 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 426636cfc..b63519d9a 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -213,7 +213,7 @@ static ZSTD_parameters ZSTD_getParamsFromCCtxParams(const ZSTD_CCtx_params cctxP } // TODO: get rid of this function too -static ZSTD_CCtx_params ZSTD_makeCCtxParamsFromParams(ZSTD_parameters params) { +ZSTD_CCtx_params ZSTD_makeCCtxParamsFromParams(ZSTD_parameters params) { ZSTD_CCtx_params cctxParams; memset(&cctxParams, 0, sizeof(ZSTD_CCtx_params)); cctxParams.cParams = params.cParams; @@ -3474,6 +3474,17 @@ static size_t ZSTD_compressBegin_internal(ZSTD_CCtx* cctx, return ZSTD_compress_insertDictionary(cctx, dict, dictSize, params.dictMode); } +size_t ZSTD_compressBegin_advanced_opaque(ZSTD_CCtx* cctx, + const void* dict, size_t dictSize, + ZSTD_CCtx_params params, + unsigned long long pledgedSrcSize) +{ + /* compression parameters verification and optimization */ + CHECK_F( ZSTD_checkCParams(params.cParams) ); + return ZSTD_compressBegin_internal(cctx, dict, dictSize, NULL, + params, pledgedSrcSize, + ZSTDb_not_buffered); +} /*! ZSTD_compressBegin_advanced() : * @return : 0, or an error code */ @@ -3481,15 +3492,13 @@ size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize) { - ZSTD_CCtx_params cctxParams = cctx->requestedParams; cctxParams.cParams = params.cParams; cctxParams.fParams = params.fParams; cctxParams.dictMode = ZSTD_dm_auto; - /* compression parameters verification and optimization */ - CHECK_F(ZSTD_checkCParams(params.cParams)); - return ZSTD_compressBegin_internal(cctx, dict, dictSize, NULL, - cctxParams, pledgedSrcSize, ZSTDb_not_buffered); + + return ZSTD_compressBegin_advanced_opaque(cctx, dict, dictSize, cctxParams, + pledgedSrcSize); } @@ -3580,10 +3589,11 @@ static size_t ZSTD_compress_internal (ZSTD_CCtx* cctx, cctxParams.cParams = params.cParams; cctxParams.fParams = params.fParams; cctxParams.dictMode = ZSTD_dm_auto; - - CHECK_F( ZSTD_compressBegin_internal(cctx, dict, dictSize, NULL, - cctxParams, srcSize, ZSTDb_not_buffered) ); - return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize); + return ZSTD_compress_advanced_opaque(cctx, + dst, dstCapacity, + src, srcSize, + dict, dictSize, + cctxParams); } size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx, @@ -3596,6 +3606,18 @@ size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx, return ZSTD_compress_internal(ctx, dst, dstCapacity, src, srcSize, dict, dictSize, params); } +/* Internal */ +size_t ZSTD_compress_advanced_opaque(ZSTD_CCtx* cctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const void* dict,size_t dictSize, + ZSTD_CCtx_params params) +{ + CHECK_F( ZSTD_compressBegin_internal(cctx, dict, dictSize, NULL, + params, srcSize, ZSTDb_not_buffered) ); + return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize); +} + size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize, const void* dict, size_t dictSize, int compressionLevel) { @@ -3920,14 +3942,13 @@ size_t ZSTD_CStreamOutSize(void) return ZSTD_compressBound(ZSTD_BLOCKSIZE_MAX) + ZSTD_blockHeaderSize + 4 /* 32-bits hash */ ; } -static size_t ZSTD_resetCStream_internal(ZSTD_CStream* zcs, - const void* dict, size_t dictSize, ZSTD_dictMode_e dictMode, - const ZSTD_CDict* cdict, - ZSTD_parameters params, unsigned long long pledgedSrcSize) +static size_t ZSTD_resetCStream_internal_opaque( + ZSTD_CStream* zcs, + const void* dict, size_t dictSize, ZSTD_dictMode_e dictMode, + const ZSTD_CDict* cdict, + ZSTD_CCtx_params params, unsigned long long pledgedSrcSize) { - ZSTD_CCtx_params cctxParams = ZSTD_makeCCtxParamsFromParams(params); - cctxParams.compressionLevel = zcs->requestedParams.compressionLevel; - cctxParams.dictMode = dictMode; + params.dictMode = dictMode; DEBUGLOG(4, "ZSTD_resetCStream_internal"); /* params are supposed to be fully validated at this point */ assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); @@ -3936,7 +3957,7 @@ static size_t ZSTD_resetCStream_internal(ZSTD_CStream* zcs, CHECK_F( ZSTD_compressBegin_internal(zcs, dict, dictSize, cdict, - cctxParams, pledgedSrcSize, + params, pledgedSrcSize, ZSTDb_buffered) ); zcs->inToCompress = 0; @@ -3948,6 +3969,19 @@ static size_t ZSTD_resetCStream_internal(ZSTD_CStream* zcs, return 0; /* ready to go */ } +static size_t ZSTD_resetCStream_internal(ZSTD_CStream* zcs, + const void* dict, size_t dictSize, ZSTD_dictMode_e dictMode, + const ZSTD_CDict* cdict, + ZSTD_parameters params, unsigned long long pledgedSrcSize) +{ + ZSTD_CCtx_params cctxParams = zcs->requestedParams; + cctxParams.cParams = params.cParams; + cctxParams.fParams = params.fParams; + cctxParams.dictMode = dictMode; + return ZSTD_resetCStream_internal_opaque(zcs, dict, dictSize, dictMode, + cdict, cctxParams, pledgedSrcSize); +} + size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize) { ZSTD_parameters params = ZSTD_getParamsFromCCtxParams(zcs->requestedParams); @@ -3959,13 +3993,11 @@ size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize) return ZSTD_resetCStream_internal(zcs, NULL, 0, zcs->requestedParams.dictMode, zcs->cdict, params, pledgedSrcSize); } -/*! ZSTD_initCStream_internal() : - * Note : not static, but hidden (not exposed). Used by zstdmt_compress.c - * Assumption 1 : params are valid - * Assumption 2 : either dict, or cdict, is defined, not both */ -size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, - const void* dict, size_t dictSize, const ZSTD_CDict* cdict, - ZSTD_parameters params, unsigned long long pledgedSrcSize) +size_t ZSTD_initCStream_internal_opaque(ZSTD_CStream* zcs, + const void* dict, size_t dictSize, + const ZSTD_CDict* cdict, + ZSTD_CCtx_params params, + unsigned long long pledgedSrcSize) { DEBUGLOG(5, "ZSTD_initCStream_internal"); assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); @@ -3993,11 +4025,28 @@ size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, zcs->cdictLocal = NULL; zcs->cdict = cdict; } - zcs->requestedParams.cParams = params.cParams; - zcs->requestedParams.fParams = params.fParams; - zcs->requestedParams.compressionLevel = ZSTD_CLEVEL_CUSTOM; + zcs->requestedParams = params; - return ZSTD_resetCStream_internal(zcs, NULL, 0, zcs->requestedParams.dictMode, zcs->cdict, params, pledgedSrcSize); + return ZSTD_resetCStream_internal_opaque( + zcs, NULL, 0, zcs->requestedParams.dictMode, zcs->cdict, + params, pledgedSrcSize); +} + + +/*! ZSTD_initCStream_internal() : + * Note : not static, but hidden (not exposed). Used by zstdmt_compress.c + * Assumption 1 : params are valid + * Assumption 2 : either dict, or cdict, is defined, not both */ +size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, + const void* dict, size_t dictSize, const ZSTD_CDict* cdict, + ZSTD_parameters params, unsigned long long pledgedSrcSize) +{ + ZSTD_CCtx_params cctxParams = zcs->requestedParams; + cctxParams.cParams = params.cParams; + cctxParams.fParams = params.fParams; + cctxParams.compressionLevel = ZSTD_CLEVEL_CUSTOM; + return ZSTD_initCStream_internal_opaque(zcs, dict, dictSize, cdict, + cctxParams, pledgedSrcSize); } /* ZSTD_initCStream_usingCDict_advanced() : @@ -4227,7 +4276,6 @@ size_t ZSTDMT_initCStream_internal(ZSTDMT_CCtx* zcs, const void* dict, size_t dictSize, const ZSTD_CDict* cdict, ZSTD_parameters params, unsigned long long pledgedSrcSize); - size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, ZSTD_outBuffer* output, ZSTD_inBuffer* input, diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index 234ced9de..2eb714e60 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -186,6 +186,14 @@ static void ZSTDMT_releaseBuffer(ZSTDMT_bufferPool* bufPool, buffer_t buf) ZSTD_free(buf.start, bufPool->cMem); } +static void ZSTDMT_zeroCCtxParams(ZSTD_CCtx_params* params) +{ + params->forceWindow = 0; + params->dictMode = (ZSTD_dictMode_e)(0); + params->nbThreads = 0; + params->jobSize = 0; + params->overlapSizeLog = 0; +} /* ===== CCtx Pool ===== */ /* a single CCtx Pool can be invoked from multiple threads in parallel */ @@ -292,7 +300,7 @@ typedef struct { unsigned jobScanned; pthread_mutex_t* jobCompleted_mutex; pthread_cond_t* jobCompleted_cond; - ZSTD_parameters params; + ZSTD_CCtx_params params; const ZSTD_CDict* cdict; ZSTDMT_CCtxPool* cctxPool; ZSTDMT_bufferPool* bufPool; @@ -330,7 +338,7 @@ void ZSTDMT_compressChunk(void* jobDescription) } else { /* srcStart points at reloaded section */ if (!job->firstChunk) job->params.fParams.contentSizeFlag = 0; /* ensure no srcSize control */ { size_t const dictModeError = ZSTD_setCCtxParameter(cctx, ZSTD_p_forceRawDict, 1); /* Force loading dictionary in "content-only" mode (no header analysis) */ - size_t const initError = ZSTD_compressBegin_advanced(cctx, job->srcStart, job->dictSize, job->params, job->fullFrameSize); + size_t const initError = ZSTD_compressBegin_advanced_opaque(cctx, job->srcStart, job->dictSize, job->params, job->fullFrameSize); if (ZSTD_isError(initError) || ZSTD_isError(dictModeError)) { job->cSize = initError; goto _endJob; } ZSTD_setCCtxParameter(cctx, ZSTD_p_forceWindow, 1); } } @@ -382,7 +390,7 @@ struct ZSTDMT_CCtx_s { size_t dictSize; size_t targetDictSize; inBuff_t inBuff; - ZSTD_parameters params; + ZSTD_CCtx_params params; XXH64_state_t xxhState; unsigned nbThreads; unsigned jobIDMask; @@ -528,17 +536,17 @@ static unsigned computeNbChunks(size_t srcSize, unsigned windowLog, unsigned nbT return (multiplier>1) ? nbChunksLarge : nbChunksSmall; } - -size_t ZSTDMT_compress_advanced(ZSTDMT_CCtx* mtctx, - void* dst, size_t dstCapacity, - const void* src, size_t srcSize, - const ZSTD_CDict* cdict, - ZSTD_parameters const params, - unsigned overlapLog) +static size_t ZSTDMT_compress_advanced_opaque( + ZSTDMT_CCtx* mtctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const ZSTD_CDict* cdict, + ZSTD_CCtx_params const cctxParams, + unsigned overlapLog) { unsigned const overlapRLog = (overlapLog>9) ? 0 : 9-overlapLog; - size_t const overlapSize = (overlapRLog>=9) ? 0 : (size_t)1 << (params.cParams.windowLog - overlapRLog); - unsigned nbChunks = computeNbChunks(srcSize, params.cParams.windowLog, mtctx->nbThreads); + size_t const overlapSize = (overlapRLog>=9) ? 0 : (size_t)1 << (cctxParams.cParams.windowLog - overlapRLog); + unsigned nbChunks = computeNbChunks(srcSize, cctxParams.cParams.windowLog, mtctx->nbThreads); size_t const proposedChunkSize = (srcSize + (nbChunks-1)) / nbChunks; size_t const avgChunkSize = ((proposedChunkSize & 0x1FFFF) < 0x7FFF) ? proposedChunkSize + 0xFFFF : proposedChunkSize; /* avoid too small last block */ const char* const srcStart = (const char*)src; @@ -546,12 +554,15 @@ size_t ZSTDMT_compress_advanced(ZSTDMT_CCtx* mtctx, unsigned const compressWithinDst = (dstCapacity >= ZSTD_compressBound(srcSize)) ? nbChunks : (unsigned)(dstCapacity / ZSTD_compressBound(avgChunkSize)); /* presumes avgChunkSize >= 256 KB, which should be the case */ size_t frameStartPos = 0, dstBufferPos = 0; XXH64_state_t xxh64; + ZSTD_CCtx_params requestedParams = cctxParams; + ZSTDMT_zeroCCtxParams(&requestedParams); DEBUGLOG(4, "nbChunks : %2u (chunkSize : %u bytes) ", nbChunks, (U32)avgChunkSize); if (nbChunks==1) { /* fallback to single-thread mode */ ZSTD_CCtx* const cctx = mtctx->cctxPool->cctx[0]; - if (cdict) return ZSTD_compress_usingCDict_advanced(cctx, dst, dstCapacity, src, srcSize, cdict, params.fParams); - return ZSTD_compress_advanced(cctx, dst, dstCapacity, src, srcSize, NULL, 0, params); + + if (cdict) return ZSTD_compress_usingCDict_advanced(cctx, dst, dstCapacity, src, srcSize, cdict, cctxParams.fParams); + return ZSTD_compress_advanced_opaque(cctx, dst, dstCapacity, src, srcSize, NULL, 0, requestedParams); } assert(avgChunkSize >= 256 KB); /* condition for ZSTD_compressBound(A) + ZSTD_compressBound(B) <= ZSTD_compressBound(A+B), which is required for compressWithinDst */ ZSTDMT_setBufferSize(mtctx->bufPool, ZSTD_compressBound(avgChunkSize) ); @@ -580,7 +591,7 @@ size_t ZSTDMT_compress_advanced(ZSTDMT_CCtx* mtctx, mtctx->jobs[u].srcSize = chunkSize; mtctx->jobs[u].cdict = mtctx->nextJobID==0 ? cdict : NULL; mtctx->jobs[u].fullFrameSize = srcSize; - mtctx->jobs[u].params = params; + mtctx->jobs[u].params = requestedParams; /* do not calculate checksum within sections, but write it in header for first section */ if (u!=0) mtctx->jobs[u].params.fParams.checksumFlag = 0; mtctx->jobs[u].dstBuff = dstBuffer; @@ -592,7 +603,7 @@ size_t ZSTDMT_compress_advanced(ZSTDMT_CCtx* mtctx, mtctx->jobs[u].jobCompleted_mutex = &mtctx->jobCompleted_mutex; mtctx->jobs[u].jobCompleted_cond = &mtctx->jobCompleted_cond; - if (params.fParams.checksumFlag) { + if (cctxParams.fParams.checksumFlag) { XXH64_update(&xxh64, srcStart + frameStartPos, chunkSize); } @@ -636,7 +647,7 @@ size_t ZSTDMT_compress_advanced(ZSTDMT_CCtx* mtctx, } /* for (chunkID=0; chunkID dstCapacity) { error = ERROR(dstSize_tooSmall); @@ -649,6 +660,23 @@ size_t ZSTDMT_compress_advanced(ZSTDMT_CCtx* mtctx, if (!error) DEBUGLOG(4, "compressed size : %u ", (U32)dstPos); return error ? error : dstPos; } + +} + +size_t ZSTDMT_compress_advanced(ZSTDMT_CCtx* mtctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const ZSTD_CDict* cdict, + ZSTD_parameters const params, + unsigned overlapLog) +{ + ZSTD_CCtx_params cctxParams = mtctx->params; + cctxParams.cParams = params.cParams; + cctxParams.fParams = params.fParams; + return ZSTDMT_compress_advanced_opaque(mtctx, + dst, dstCapacity, + src, srcSize, + cdict, cctxParams, overlapLog); } @@ -683,23 +711,28 @@ static void ZSTDMT_waitForAllJobsCompleted(ZSTDMT_CCtx* zcs) } } - -/** ZSTDMT_initCStream_internal() : - * internal usage only */ -size_t ZSTDMT_initCStream_internal(ZSTDMT_CCtx* zcs, - const void* dict, size_t dictSize, const ZSTD_CDict* cdict, - ZSTD_parameters params, unsigned long long pledgedSrcSize) +size_t ZSTDMT_initCStream_internal_opaque( + ZSTDMT_CCtx* zcs, const void* dict, size_t dictSize, + const ZSTD_CDict* cdict, ZSTD_CCtx_params cctxParams, + unsigned long long pledgedSrcSize) { + ZSTD_parameters params; + params.cParams = cctxParams.cParams; + params.fParams = cctxParams.fParams; + DEBUGLOG(4, "ZSTDMT_initCStream_internal"); /* params are supposed to be fully validated at this point */ assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); assert(!((dict) && (cdict))); /* either dict or cdict, not both */ + /* TODO: Set stuff to 0 to preserve old semantics. */ + ZSTDMT_zeroCCtxParams(&cctxParams); + if (zcs->nbThreads==1) { DEBUGLOG(4, "single thread mode"); - return ZSTD_initCStream_internal(zcs->cctxPool->cctx[0], - dict, dictSize, cdict, - params, pledgedSrcSize); + return ZSTD_initCStream_internal_opaque(zcs->cctxPool->cctx[0], + dict, dictSize, cdict, + cctxParams, pledgedSrcSize); } if (zcs->allJobsCompleted == 0) { /* previous compression not correctly finished */ @@ -708,7 +741,7 @@ size_t ZSTDMT_initCStream_internal(ZSTDMT_CCtx* zcs, zcs->allJobsCompleted = 1; } - zcs->params = params; + zcs->params = cctxParams; zcs->frameContentSize = pledgedSrcSize; if (dict) { DEBUGLOG(4,"cdictLocal: %08X", (U32)(size_t)zcs->cdictLocal); @@ -742,6 +775,21 @@ size_t ZSTDMT_initCStream_internal(ZSTDMT_CCtx* zcs, zcs->allJobsCompleted = 0; if (params.fParams.checksumFlag) XXH64_reset(&zcs->xxhState, 0); return 0; + +} + + +/** ZSTDMT_initCStream_internal() : + * internal usage only */ +size_t ZSTDMT_initCStream_internal(ZSTDMT_CCtx* zcs, + const void* dict, size_t dictSize, const ZSTD_CDict* cdict, + ZSTD_parameters params, unsigned long long pledgedSrcSize) +{ + ZSTD_CCtx_params cctxParams = zcs->params; + cctxParams.cParams = params.cParams; + cctxParams.fParams = params.fParams; + return ZSTDMT_initCStream_internal_opaque(zcs, dict, dictSize, cdict, + cctxParams, pledgedSrcSize); } size_t ZSTDMT_initCStream_advanced(ZSTDMT_CCtx* mtctx, @@ -772,7 +820,8 @@ size_t ZSTDMT_resetCStream(ZSTDMT_CCtx* zcs, unsigned long long pledgedSrcSize) { if (zcs->nbThreads==1) return ZSTD_resetCStream(zcs->cctxPool->cctx[0], pledgedSrcSize); - return ZSTDMT_initCStream_internal(zcs, NULL, 0, 0, zcs->params, pledgedSrcSize); + return ZSTDMT_initCStream_internal_opaque(zcs, NULL, 0, 0, zcs->params, + pledgedSrcSize); } size_t ZSTDMT_initCStream(ZSTDMT_CCtx* zcs, int compressionLevel) { @@ -930,7 +979,7 @@ size_t ZSTDMT_compressStream_generic(ZSTDMT_CCtx* mtctx, && (mtctx->inBuff.filled==0) /* nothing buffered */ && (endOp==ZSTD_e_end) /* end order */ && (output->size - output->pos >= ZSTD_compressBound(input->size - input->pos)) ) { /* enough room */ - size_t const cSize = ZSTDMT_compress_advanced(mtctx, + size_t const cSize = ZSTDMT_compress_advanced_opaque(mtctx, (char*)output->dst + output->pos, output->size - output->pos, (const char*)input->src + input->pos, input->size - input->pos, mtctx->cdict, mtctx->params, mtctx->overlapLog); diff --git a/lib/compress/zstdmt_compress.h b/lib/compress/zstdmt_compress.h index 843a240aa..0b478b735 100644 --- a/lib/compress/zstdmt_compress.h +++ b/lib/compress/zstdmt_compress.h @@ -69,6 +69,15 @@ ZSTDLIB_API size_t ZSTDMT_compress_advanced(ZSTDMT_CCtx* mtctx, const ZSTD_CDict* cdict, ZSTD_parameters const params, unsigned overlapLog); +#if 0 +ZSTDLIB_API size_t ZSTDMT_compress_advanced_opaque( + ZSTDMT_CCtx* mtctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const ZSTD_CDict* cdict, + ZSTD_CCtx_params* const params, + unsigned overlapLog); +#endif ZSTDLIB_API size_t ZSTDMT_initCStream_advanced(ZSTDMT_CCtx* mtctx, const void* dict, size_t dictSize, /* dict can be released after init, a local copy is preserved within zcs */ From d77551929654d35bf46ebad1f0ebd6f0e31b6ccf Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Fri, 18 Aug 2017 17:37:58 -0700 Subject: [PATCH 11/52] Add cctxParam versions of internal functions --- lib/common/zstd_internal.h | 23 ++++- lib/compress/zstd_compress.c | 169 +++++++++++++++++++-------------- lib/compress/zstdmt_compress.c | 8 +- 3 files changed, 122 insertions(+), 78 deletions(-) diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index fa9f0c49c..0da950787 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -351,10 +351,11 @@ void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx); * expects params to be valid. * must receive dict, or cdict, or none, but not both. * @return : 0, or an error code */ -size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, - const void* dict, size_t dictSize, - const ZSTD_CDict* cdict, - ZSTD_parameters params, unsigned long long pledgedSrcSize); +size_t ZSTD_initCStream_internal_opaque(ZSTD_CStream* zcs, + const void* dict, size_t dictSize, + const ZSTD_CDict* cdict, + ZSTD_CCtx_params params, + unsigned long long pledgedSrcSize); /*! ZSTD_compressStream_generic() : * Private use only. To be called from zstdmt_compress.c in single-thread mode. */ @@ -365,8 +366,20 @@ size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs, /*! ZSTD_getParamsFromCDict() : * as the name implies */ -ZSTD_parameters ZSTD_getParamsFromCDict(const ZSTD_CDict* cdict); +ZSTD_CCtx_params ZSTD_getCCtxParamsFromCDict(const ZSTD_CDict* cdict); +/* INTERNAL */ +size_t ZSTD_compressBegin_advanced_opaque(ZSTD_CCtx* cctx, + const void* dict, size_t dictSize, + ZSTD_CCtx_params params, + unsigned long long pledgedSrcSize); + +/* INTERNAL */ +size_t ZSTD_compress_advanced_opaque(ZSTD_CCtx* cctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const void* dict,size_t dictSize, + ZSTD_CCtx_params params); typedef struct { blockType_e blockType; diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index b63519d9a..434f40562 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -203,6 +203,7 @@ size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs) /* private API call, for dictBuilder only */ const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx) { return &(ctx->seqStore); } +#if 0 // TODO: get rid of this function static ZSTD_parameters ZSTD_getParamsFromCCtxParams(const ZSTD_CCtx_params cctxParams) { @@ -211,15 +212,18 @@ static ZSTD_parameters ZSTD_getParamsFromCCtxParams(const ZSTD_CCtx_params cctxP params.fParams = cctxParams.fParams; return params; } +#endif +#if 0 // TODO: get rid of this function too -ZSTD_CCtx_params ZSTD_makeCCtxParamsFromParams(ZSTD_parameters params) { +static ZSTD_CCtx_params ZSTD_makeCCtxParamsFromParams(ZSTD_parameters params) { ZSTD_CCtx_params cctxParams; memset(&cctxParams, 0, sizeof(ZSTD_CCtx_params)); cctxParams.cParams = params.cParams; cctxParams.fParams = params.fParams; return cctxParams; } +#endif // TODO: get rid of this function too static ZSTD_CCtx_params ZSTD_makeCCtxParamsFromCParams( @@ -230,11 +234,12 @@ static ZSTD_CCtx_params ZSTD_makeCCtxParamsFromCParams( cctxParams.cParams = cParams; return cctxParams; } - +#if 0 // TODO: get rid of this function static ZSTD_parameters ZSTD_getParamsFromCCtx(const ZSTD_CCtx* cctx) { return ZSTD_getParamsFromCCtxParams(cctx->appliedParams); } +#endif /* older variant; will be deprecated */ size_t ZSTD_setCCtxParameter(ZSTD_CCtx* cctx, ZSTD_CCtxParameter param, unsigned value) @@ -292,7 +297,7 @@ ZSTD_CCtx_params* ZSTD_createAndInitCCtxParams( if (params == NULL) { return NULL; } ZSTD_initCCtxParams(params, ZSTD_getCParams( compressionLevel, estimatedSrcSize, dictSize)); - params->compressionLevel = compressionLevel; + params->compressionLevel = ZSTD_CLEVEL_CUSTOM; return params; } @@ -786,10 +791,9 @@ size_t ZSTD_estimateCStreamSize_advanced_opaque(ZSTD_CCtx_params* params) { if (params == NULL) { return 0; } { - ZSTD_compressionParameters cParams = params->cParams; - size_t const CCtxSize = ZSTD_estimateCCtxSize_advanced(cParams); - size_t const blockSize = MIN(ZSTD_BLOCKSIZE_MAX, (size_t)1 << cParams.windowLog); - size_t const inBuffSize = ((size_t)1 << cParams.windowLog) + blockSize; + 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; @@ -808,9 +812,8 @@ size_t ZSTD_estimateCStreamSize(int compressionLevel) { return ZSTD_estimateCStreamSize_advanced(cParams); } - -static U32 ZSTD_equivalentParams(ZSTD_compressionParameters cParams1, - ZSTD_compressionParameters cParams2) +static U32 ZSTD_equivalentCParams(ZSTD_compressionParameters cParams1, + ZSTD_compressionParameters cParams2) { U32 bslog1 = MIN(cParams1.windowLog, ZSTD_BLOCKSIZELOG_MAX); U32 bslog2 = MIN(cParams2.windowLog, ZSTD_BLOCKSIZELOG_MAX); @@ -821,6 +824,13 @@ static U32 ZSTD_equivalentParams(ZSTD_compressionParameters cParams1, & ((cParams1.searchLength==3) == (cParams2.searchLength==3)); /* hashlog3 space */ } +/** Equivalence for resetCCtx purposes */ +static U32 ZSTD_equivalentParams(ZSTD_CCtx_params params1, + ZSTD_CCtx_params params2) +{ + return ZSTD_equivalentCParams(params1.cParams, params2.cParams); +} + /*! ZSTD_continueCCtx() : * reuse CCtx without reset (note : requires no dictionary) */ static size_t ZSTD_continueCCtx(ZSTD_CCtx* cctx, ZSTD_CCtx_params params, U64 pledgedSrcSize) @@ -859,8 +869,8 @@ static size_t ZSTD_resetCCtx_internal(ZSTD_CCtx* zc, assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); if (crp == ZSTDcrp_continue) { - if (ZSTD_equivalentParams(params.cParams, zc->appliedParams.cParams)) { - DEBUGLOG(5, "ZSTD_equivalentParams()==1"); + if (ZSTD_equivalentParams(params, zc->appliedParams)) { + DEBUGLOG(5, "ZSTD_equivalentCParams()==1"); zc->entropy->hufCTable_repeatMode = HUF_repeat_none; zc->entropy->offcode_repeatMode = FSE_repeat_none; zc->entropy->matchlength_repeatMode = FSE_repeat_none; @@ -3255,6 +3265,7 @@ size_t ZSTD_getBlockSize(const ZSTD_CCtx* cctx) ZSTD_compressionParameters cParams = (cLevel == ZSTD_CLEVEL_CUSTOM) ? cctx->appliedParams.cParams : ZSTD_getCParams(cLevel, 0, 0); + DEBUGLOG(2, "ZSTD_getBlockSize: cLevel %u\n", cLevel); return MIN (ZSTD_BLOCKSIZE_MAX, 1 << cParams.windowLog); } @@ -3490,7 +3501,8 @@ size_t ZSTD_compressBegin_advanced_opaque(ZSTD_CCtx* cctx, * @return : 0, or an error code */ size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, - ZSTD_parameters params, unsigned long long pledgedSrcSize) + ZSTD_parameters params, + unsigned long long pledgedSrcSize) { ZSTD_CCtx_params cctxParams = cctx->requestedParams; cctxParams.cParams = params.cParams; @@ -3618,8 +3630,11 @@ size_t ZSTD_compress_advanced_opaque(ZSTD_CCtx* cctx, return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize); } -size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize, - const void* dict, size_t dictSize, int compressionLevel) +size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, void* dst, + size_t dstCapacity, + const void* src, size_t srcSize, + const void* dict, size_t dictSize, + int compressionLevel) { ZSTD_parameters params = ZSTD_getParams(compressionLevel, srcSize, dict ? dictSize : 0); params.fParams.contentSizeFlag = 1; @@ -3690,14 +3705,12 @@ static ZSTD_parameters ZSTD_makeParams(ZSTD_compressionParameters cParams, ZSTD_ } #endif -static size_t ZSTD_initCDict_internal( - ZSTD_CDict* cdict, - const void* dictBuffer, size_t dictSize, - unsigned byReference, ZSTD_dictMode_e dictMode, - ZSTD_compressionParameters cParams) +static size_t ZSTD_initCDict_internal_opaque(ZSTD_CDict* cdict, + const void* dictBuffer, size_t dictSize, + ZSTD_CCtx_params params) { - DEBUGLOG(5, "ZSTD_initCDict_internal, mode %u", (U32)dictMode); - if ((byReference) || (!dictBuffer) || (!dictSize)) { + DEBUGLOG(5, "ZSTD_initCDict_internal_opaque, mode %u", (U32)params.dictMode); + if ((params.dictContentByRef) || (!dictBuffer) || (!dictSize)) { cdict->dictBuffer = NULL; cdict->dictContent = dictBuffer; } else { @@ -3709,21 +3722,30 @@ static size_t ZSTD_initCDict_internal( } cdict->dictContentSize = dictSize; - { ZSTD_frameParameters const fParams = { 0 /* contentSizeFlag */, + /* Frame parameters should be zero? */ + CHECK_F( ZSTD_compressBegin_internal(cdict->refContext, + cdict->dictContent, dictSize, + NULL, + params, ZSTD_CONTENTSIZE_UNKNOWN, + ZSTDb_not_buffered) ); + return 0; +} + +static size_t ZSTD_initCDict_internal( + ZSTD_CDict* cdict, + const void* dictBuffer, size_t dictSize, + unsigned byReference, ZSTD_dictMode_e dictMode, + ZSTD_compressionParameters cParams) +{ + ZSTD_CCtx_params cctxParams = cdict->refContext->requestedParams; + ZSTD_frameParameters const fParams = { 0 /* contentSizeFlag */, 0 /* checksumFlag */, 0 /* noDictIDFlag */ }; /* dummy */ - ZSTD_CCtx_params cctxParams = cdict->refContext->requestedParams; - cctxParams.cParams = cParams; - cctxParams.fParams = fParams; - cctxParams.dictMode = dictMode; - cctxParams.dictContentByRef = byReference; - - CHECK_F( ZSTD_compressBegin_internal(cdict->refContext, - cdict->dictContent, dictSize, - NULL, - cctxParams, ZSTD_CONTENTSIZE_UNKNOWN, - ZSTDb_not_buffered) ); - } - + cctxParams.cParams = cParams; + cctxParams.fParams = fParams; + cctxParams.dictMode = dictMode; + cctxParams.dictContentByRef = byReference; + CHECK_F (ZSTD_initCDict_internal_opaque( + cdict, dictBuffer, dictSize, cctxParams) ); return 0; } @@ -3788,8 +3810,7 @@ ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( size_t dictSize, ZSTD_CCtx_params* params) { - ZSTD_compressionParameters cParams = params->cParams; - size_t const cctxSize = ZSTD_estimateCCtxSize_advanced(cParams); + size_t const cctxSize = ZSTD_estimateCCtxSize_advanced_opaque(params); size_t const neededSize = sizeof(ZSTD_CDict) + (params->dictContentByRef ? 0 : dictSize) + cctxSize; ZSTD_CDict* const cdict = (ZSTD_CDict*) workspace; @@ -3808,11 +3829,9 @@ ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( ptr = cdict+1; } cdict->refContext = ZSTD_initStaticCCtx(ptr, cctxSize); + params->dictContentByRef = 1; - if (ZSTD_isError( ZSTD_initCDict_internal(cdict, - dict, dictSize, - 1 /* byReference */, params->dictMode, - cParams) )) + if (ZSTD_isError( ZSTD_initCDict_internal_opaque(cdict, dict, dictSize, *params) )) return NULL; return cdict; @@ -3843,12 +3862,13 @@ ZSTD_CDict* ZSTD_initStaticCDict(void* workspace, size_t workspaceSize, workspace, workspaceSize, dict, dictSize, ¶ms); } - -ZSTD_parameters ZSTD_getParamsFromCDict(const ZSTD_CDict* cdict) { +#if 0 +static ZSTD_parameters ZSTD_getParamsFromCDict(const ZSTD_CDict* cdict) { return ZSTD_getParamsFromCCtx(cdict->refContext); -} +}a +#endif -static ZSTD_CCtx_params ZSTD_getCCtxParamsFromCDict(const ZSTD_CDict* cdict) { +ZSTD_CCtx_params ZSTD_getCCtxParamsFromCDict(const ZSTD_CDict* cdict) { return cdict->refContext->appliedParams; } @@ -3969,6 +3989,7 @@ static size_t ZSTD_resetCStream_internal_opaque( return 0; /* ready to go */ } +#if 0 static size_t ZSTD_resetCStream_internal(ZSTD_CStream* zcs, const void* dict, size_t dictSize, ZSTD_dictMode_e dictMode, const ZSTD_CDict* cdict, @@ -3981,16 +4002,17 @@ static size_t ZSTD_resetCStream_internal(ZSTD_CStream* zcs, return ZSTD_resetCStream_internal_opaque(zcs, dict, dictSize, dictMode, cdict, cctxParams, pledgedSrcSize); } +#endif size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize) { - ZSTD_parameters params = ZSTD_getParamsFromCCtxParams(zcs->requestedParams); + ZSTD_CCtx_params params = zcs->requestedParams; params.fParams.contentSizeFlag = (pledgedSrcSize > 0); DEBUGLOG(5, "ZSTD_resetCStream"); - if (zcs->requestedParams.compressionLevel != ZSTD_CLEVEL_CUSTOM) { - params.cParams = ZSTD_getCParams(zcs->requestedParams.compressionLevel, pledgedSrcSize, 0 /* dictSize */); + if (params.compressionLevel != ZSTD_CLEVEL_CUSTOM) { + params.cParams = ZSTD_getCParams(params.compressionLevel, pledgedSrcSize, 0 /* dictSize */); } - return ZSTD_resetCStream_internal(zcs, NULL, 0, zcs->requestedParams.dictMode, zcs->cdict, params, pledgedSrcSize); + return ZSTD_resetCStream_internal_opaque(zcs, NULL, 0, params.dictMode, zcs->cdict, params, pledgedSrcSize); } size_t ZSTD_initCStream_internal_opaque(ZSTD_CStream* zcs, @@ -4032,12 +4054,12 @@ size_t ZSTD_initCStream_internal_opaque(ZSTD_CStream* zcs, params, pledgedSrcSize); } - +#if 0 /*! ZSTD_initCStream_internal() : * Note : not static, but hidden (not exposed). Used by zstdmt_compress.c * Assumption 1 : params are valid * Assumption 2 : either dict, or cdict, is defined, not both */ -size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, +static size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, const void* dict, size_t dictSize, const ZSTD_CDict* cdict, ZSTD_parameters params, unsigned long long pledgedSrcSize) { @@ -4048,6 +4070,7 @@ size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, return ZSTD_initCStream_internal_opaque(zcs, dict, dictSize, cdict, cctxParams, pledgedSrcSize); } +#endif /* ZSTD_initCStream_usingCDict_advanced() : * same as ZSTD_initCStream_usingCDict(), with control over frame parameters */ @@ -4057,9 +4080,10 @@ size_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize) { /* cannot handle NULL cdict (does not know what to do) */ if (!cdict) return ERROR(dictionary_wrong); - { ZSTD_parameters params = ZSTD_getParamsFromCDict(cdict); + { ZSTD_CCtx_params params = ZSTD_getCCtxParamsFromCDict(cdict); params.fParams = fParams; - return ZSTD_initCStream_internal(zcs, + params.compressionLevel = ZSTD_CLEVEL_CUSTOM; + return ZSTD_initCStream_internal_opaque(zcs, NULL, 0, cdict, params, pledgedSrcSize); } @@ -4076,30 +4100,34 @@ size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize) { + ZSTD_CCtx_params cctxParams = zcs->requestedParams; CHECK_F( ZSTD_checkCParams(params.cParams) ); - zcs->requestedParams = ZSTD_makeCCtxParamsFromParams(params); + zcs->requestedParams.cParams = params.cParams; + zcs->requestedParams.fParams = params.fParams; zcs->requestedParams.compressionLevel = ZSTD_CLEVEL_CUSTOM; - return ZSTD_initCStream_internal(zcs, dict, dictSize, NULL, params, pledgedSrcSize); + return ZSTD_initCStream_internal_opaque(zcs, dict, dictSize, NULL, cctxParams, pledgedSrcSize); } size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel) { ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, dictSize); - zcs->requestedParams.compressionLevel = compressionLevel; - return ZSTD_initCStream_internal(zcs, dict, dictSize, NULL, params, 0); + ZSTD_CCtx_params cctxParams = zcs->requestedParams; + cctxParams.cParams = params.cParams; + cctxParams.fParams = params.fParams; + cctxParams.compressionLevel = compressionLevel; + return ZSTD_initCStream_internal_opaque(zcs, dict, dictSize, NULL, cctxParams, 0); } size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize) { ZSTD_parameters params = ZSTD_getParams(compressionLevel, pledgedSrcSize, 0); params.fParams.contentSizeFlag = (pledgedSrcSize>0); - return ZSTD_initCStream_internal(zcs, NULL, 0, NULL, params, pledgedSrcSize); + return ZSTD_initCStream_advanced(zcs, NULL, 0, params, pledgedSrcSize); } size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel) { - ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, 0); - return ZSTD_initCStream_internal(zcs, NULL, 0, NULL, params, 0); + return ZSTD_initCStream_srcSize(zcs, compressionLevel, 0); } /*====== Compression ======*/ @@ -4272,9 +4300,9 @@ size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuf * expects params to be valid. * must receive dict, or cdict, or none, but not both. * @return : 0, or an error code */ -size_t ZSTDMT_initCStream_internal(ZSTDMT_CCtx* zcs, +size_t ZSTDMT_initCStream_internal_opaque(ZSTDMT_CCtx* zcs, const void* dict, size_t dictSize, const ZSTD_CDict* cdict, - ZSTD_parameters params, unsigned long long pledgedSrcSize); + ZSTD_CCtx_params params, unsigned long long pledgedSrcSize); size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, ZSTD_outBuffer* output, @@ -4290,22 +4318,23 @@ size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, if (cctx->streamStage == zcss_init) { const void* const prefix = cctx->prefix; size_t const prefixSize = cctx->prefixSize; - ZSTD_parameters params = ZSTD_getParamsFromCCtxParams(cctx->requestedParams); - if (cctx->requestedParams.compressionLevel != ZSTD_CLEVEL_CUSTOM) - params.cParams = ZSTD_getCParams(cctx->requestedParams.compressionLevel, + ZSTD_CCtx_params params = cctx->requestedParams; + + if (params.compressionLevel != ZSTD_CLEVEL_CUSTOM) + params.cParams = ZSTD_getCParams(params.compressionLevel, cctx->pledgedSrcSizePlusOne-1, 0 /*dictSize*/); cctx->prefix = NULL; cctx->prefixSize = 0; /* single usage */ assert(prefix==NULL || cctx->cdict==NULL); /* only one can be set */ #ifdef ZSTD_MULTITHREAD - if (cctx->requestedParams.nbThreads > 1) { - DEBUGLOG(4, "call ZSTDMT_initCStream_internal as nbThreads=%u", cctx->requestedParams.nbThreads); - CHECK_F( ZSTDMT_initCStream_internal(cctx->mtctx, prefix, prefixSize, cctx->cdict, params, cctx->pledgedSrcSizePlusOne-1) ); + if (params.nbThreads > 1) { + DEBUGLOG(4, "call ZSTDMT_initCStream_internal as nbThreads=%u", params.nbThreads); + CHECK_F( ZSTDMT_initCStream_internal_opaque(cctx->mtctx, prefix, prefixSize, cctx->cdict, params, cctx->pledgedSrcSizePlusOne-1) ); cctx->streamStage = zcss_load; } else #endif { - CHECK_F( ZSTD_resetCStream_internal(cctx, prefix, prefixSize, cctx->requestedParams.dictMode, cctx->cdict, params, cctx->pledgedSrcSizePlusOne-1) ); + CHECK_F( ZSTD_resetCStream_internal_opaque(cctx, prefix, prefixSize, params.dictMode, cctx->cdict, params, cctx->pledgedSrcSizePlusOne-1) ); } } /* compression stage */ diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index 2eb714e60..ea5828f35 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -806,11 +806,13 @@ size_t ZSTDMT_initCStream_usingCDict(ZSTDMT_CCtx* mtctx, ZSTD_frameParameters fParams, unsigned long long pledgedSrcSize) { - ZSTD_parameters params = ZSTD_getParamsFromCDict(cdict); + ZSTD_CCtx_params params = ZSTD_getCCtxParamsFromCDict(cdict); if (cdict==NULL) return ERROR(dictionary_wrong); /* method incompatible with NULL cdict */ + ZSTDMT_zeroCCtxParams(¶ms); params.fParams = fParams; - return ZSTDMT_initCStream_internal(mtctx, NULL, 0 /*dictSize*/, cdict, - params, pledgedSrcSize); + + return ZSTDMT_initCStream_internal_opaque(mtctx, NULL, 0 /*dictSize*/, cdict, + params, pledgedSrcSize); } From 6cee6e07e5a7e049527b9a32d5a1386d7debb66f Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Fri, 18 Aug 2017 22:48:31 -0700 Subject: [PATCH 12/52] Add internal createCDict function --- lib/compress/zstd_compress.c | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 434f40562..4f10aedf4 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -3731,6 +3731,7 @@ static size_t ZSTD_initCDict_internal_opaque(ZSTD_CDict* cdict, return 0; } +#if 0 static size_t ZSTD_initCDict_internal( ZSTD_CDict* cdict, const void* dictBuffer, size_t dictSize, @@ -3748,11 +3749,52 @@ static size_t ZSTD_initCDict_internal( cdict, dictBuffer, dictSize, cctxParams) ); return 0; } +#endif + +ZSTD_CDict* ZSTD_createCDict_advanced_opaque( + const void* dictBuffer, size_t dictSize, + ZSTD_CCtx_params params, ZSTD_customMem customMem) +{ + DEBUGLOG(5, "ZSTD_createCDict_advanced, mode %u", (U32)dictMode); + if (!customMem.customAlloc ^ !customMem.customFree) return NULL; + + { ZSTD_CDict* const cdict = (ZSTD_CDict*)ZSTD_malloc(sizeof(ZSTD_CDict), customMem); + ZSTD_CCtx* const cctx = ZSTD_createCCtx_advanced(customMem); + /* Initialize to 0 to preserve semantics */ + ZSTD_frameParameters const fParams = { 0, 0, 0 }; + params.fParams = fParams; + + if (!cdict || !cctx) { + ZSTD_free(cdict, customMem); + ZSTD_freeCCtx(cctx); + return NULL; + } + cdict->refContext = cctx; + + + if (ZSTD_isError( ZSTD_initCDict_internal_opaque( + cdict, + dictBuffer, dictSize, + params) )) { + ZSTD_freeCDict(cdict); + return NULL; + } + return cdict; + } +} + ZSTD_CDict* ZSTD_createCDict_advanced(const void* dictBuffer, size_t dictSize, unsigned byReference, ZSTD_dictMode_e dictMode, ZSTD_compressionParameters cParams, ZSTD_customMem customMem) { + ZSTD_CCtx_params cctxParams = ZSTD_makeCCtxParamsFromCParams(cParams); + ZSTD_frameParameters const fParams = { 0, 0, 0 }; + cctxParams.fParams = fParams; + cctxParams.dictMode = dictMode; + cctxParams.dictContentByRef = byReference; + return ZSTD_createCDict_advanced_opaque(dictBuffer, dictSize, cctxParams, customMem); +#if 0 DEBUGLOG(5, "ZSTD_createCDict_advanced, mode %u", (U32)dictMode); if (!customMem.customAlloc ^ !customMem.customFree) return NULL; @@ -3776,6 +3818,7 @@ ZSTD_CDict* ZSTD_createCDict_advanced(const void* dictBuffer, size_t dictSize, return cdict; } +#endif } ZSTD_CDict* ZSTD_createCDict(const void* dict, size_t dictSize, int compressionLevel) From 023b24e6d4491d9aa8ed6aba4ed43cff2ea61b34 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Sun, 20 Aug 2017 22:55:07 -0700 Subject: [PATCH 13/52] Add cctx param tests --- lib/common/zstd_internal.h | 5 +- lib/compress/zstd_compress.c | 230 ++++++++++-------------- lib/compress/zstdmt_compress.c | 4 +- lib/zstd.h | 10 +- programs/Makefile | 3 +- programs/fileio.c | 5 + programs/zstdcli.c | 1 + tests/Makefile | 8 +- tests/fullbench.c | 5 + tests/roundTripCrashOpaque.c | 203 ++++++++++++++++++++++ tests/zstreamtest.c | 309 ++++++++++++++++++++++++++++++++- 11 files changed, 639 insertions(+), 144 deletions(-) create mode 100644 tests/roundTripCrashOpaque.c diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index 0da950787..1cccca286 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -246,11 +246,14 @@ struct ZSTD_CCtx_params_s { /* Dictionary */ ZSTD_dictMode_e dictMode; /* select restricting dictionary to "rawContent" or "fullDict" only */ U32 dictContentByRef; - U32 nbThreads; /* Multithreading: used only to set mtctx parameters */ + U32 nbThreads; unsigned jobSize; unsigned overlapSizeLog; + + /* Test parameter */ + U32 testParam; }; typedef struct { diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 4f10aedf4..051a848a5 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -261,6 +261,7 @@ size_t ZSTD_setCCtxParameter(ZSTD_CCtx* cctx, ZSTD_CCtxParameter param, unsigned #define ZSTD_CLEVEL_CUSTOM 999 +#if 0 static void ZSTD_cLevelToCParams(ZSTD_CCtx* cctx) { if (cctx->requestedParams.compressionLevel==ZSTD_CLEVEL_CUSTOM) return; @@ -269,6 +270,7 @@ static void ZSTD_cLevelToCParams(ZSTD_CCtx* cctx) cctx->pledgedSrcSizePlusOne-1, 0); cctx->requestedParams.compressionLevel = ZSTD_CLEVEL_CUSTOM; } +#endif ZSTD_CCtx_params* ZSTD_createCCtxParams(void) { @@ -280,6 +282,14 @@ ZSTD_CCtx_params* ZSTD_createCCtxParams(void) return params; } +size_t ZSTD_resetCCtxParams(ZSTD_CCtx_params* params) +{ + if (!params) { return ERROR(GENERIC); } + memset(params, 0, sizeof(ZSTD_CCtx_params)); + params->compressionLevel = ZSTD_CLEVEL_DEFAULT; + return 0; +} + size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* params, ZSTD_compressionParameters cParams) { @@ -330,102 +340,27 @@ size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned v switch(param) { - case ZSTD_p_compressionLevel : - if ((int)value > ZSTD_maxCLevel()) value = ZSTD_maxCLevel(); /* cap max compression level */ + case ZSTD_p_compressionLevel: + case ZSTD_p_windowLog: + case ZSTD_p_hashLog: + case ZSTD_p_chainLog: + case ZSTD_p_searchLog: + case ZSTD_p_minMatch: + case ZSTD_p_targetLength: + case ZSTD_p_compressionStrategy: if (value == 0) return 0; /* special value : 0 means "don't change anything" */ if (cctx->cdict) return ERROR(stage_wrong); - cctx->requestedParams.compressionLevel = value; - return 0; + return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); - case ZSTD_p_windowLog : - DEBUGLOG(5, "setting ZSTD_p_windowLog = %u (cdict:%u)", - value, (cctx->cdict!=NULL)); - if (value == 0) return 0; /* special value : 0 means "don't change anything" */ - if (cctx->cdict) return ERROR(stage_wrong); - CLAMPCHECK(value, ZSTD_WINDOWLOG_MIN, ZSTD_WINDOWLOG_MAX); - ZSTD_cLevelToCParams(cctx); - cctx->requestedParams.cParams.windowLog = value; - return 0; + case ZSTD_p_contentSizeFlag: + case ZSTD_p_checksumFlag: + case ZSTD_p_dictIDFlag: + return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); - case ZSTD_p_hashLog : - if (value == 0) return 0; /* special value : 0 means "don't change anything" */ - if (cctx->cdict) return ERROR(stage_wrong); - CLAMPCHECK(value, ZSTD_HASHLOG_MIN, ZSTD_HASHLOG_MAX); - ZSTD_cLevelToCParams(cctx); - cctx->requestedParams.cParams.hashLog = value; - return 0; - - case ZSTD_p_chainLog : - if (value == 0) return 0; /* special value : 0 means "don't change anything" */ - if (cctx->cdict) return ERROR(stage_wrong); - CLAMPCHECK(value, ZSTD_CHAINLOG_MIN, ZSTD_CHAINLOG_MAX); - ZSTD_cLevelToCParams(cctx); - cctx->requestedParams.cParams.chainLog = value; - return 0; - - case ZSTD_p_searchLog : - if (value == 0) return 0; /* special value : 0 means "don't change anything" */ - if (cctx->cdict) return ERROR(stage_wrong); - CLAMPCHECK(value, ZSTD_SEARCHLOG_MIN, ZSTD_SEARCHLOG_MAX); - ZSTD_cLevelToCParams(cctx); - cctx->requestedParams.cParams.searchLog = value; - return 0; - - case ZSTD_p_minMatch : - if (value == 0) return 0; /* special value : 0 means "don't change anything" */ - if (cctx->cdict) return ERROR(stage_wrong); - CLAMPCHECK(value, ZSTD_SEARCHLENGTH_MIN, ZSTD_SEARCHLENGTH_MAX); - ZSTD_cLevelToCParams(cctx); - cctx->requestedParams.cParams.searchLength = value; - return 0; - - case ZSTD_p_targetLength : - if (value == 0) return 0; /* special value : 0 means "don't change anything" */ - if (cctx->cdict) return ERROR(stage_wrong); - CLAMPCHECK(value, ZSTD_TARGETLENGTH_MIN, ZSTD_TARGETLENGTH_MAX); - ZSTD_cLevelToCParams(cctx); - cctx->requestedParams.cParams.targetLength = value; - return 0; - - case ZSTD_p_compressionStrategy : - if (value == 0) return 0; /* special value : 0 means "don't change anything" */ - if (cctx->cdict) return ERROR(stage_wrong); - CLAMPCHECK(value, (unsigned)ZSTD_fast, (unsigned)ZSTD_btultra); - ZSTD_cLevelToCParams(cctx); - cctx->requestedParams.cParams.strategy = (ZSTD_strategy)value; - return 0; - - case ZSTD_p_contentSizeFlag : - DEBUGLOG(5, "set content size flag = %u", (value>0)); - /* Content size written in frame header _when known_ (default:1) */ - cctx->requestedParams.fParams.contentSizeFlag = value>0; - return 0; - - case ZSTD_p_checksumFlag : - /* A 32-bits content checksum will be calculated and written at end of frame (default:0) */ - cctx->requestedParams.fParams.checksumFlag = value>0; - return 0; - - case ZSTD_p_dictIDFlag : /* When applicable, dictionary's dictID is provided in frame header (default:1) */ - DEBUGLOG(5, "set dictIDFlag = %u", (value>0)); - cctx->requestedParams.fParams.noDictIDFlag = (value==0); - return 0; - - /* Dictionary parameters */ - case ZSTD_p_dictMode : + case ZSTD_p_dictMode: + case ZSTD_p_refDictContent: if (cctx->cdict) return ERROR(stage_wrong); /* must be set before loading */ - /* restrict dictionary mode, to "rawContent" or "fullDict" only */ - ZSTD_STATIC_ASSERT((U32)ZSTD_dm_fullDict > (U32)ZSTD_dm_rawContent); - if (value > (unsigned)ZSTD_dm_fullDict) - return ERROR(parameter_outOfBound); - cctx->requestedParams.dictMode = (ZSTD_dictMode_e)value; - return 0; - - case ZSTD_p_refDictContent : - if (cctx->cdict) return ERROR(stage_wrong); /* must be set before loading */ - /* dictionary content will be referenced, instead of copied */ - cctx->requestedParams.dictContentByRef = value>0; - return 0; + return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); case ZSTD_p_forceMaxWindow : /* Force back-references to remain < windowSize, * even when referencing into Dictionary content @@ -462,6 +397,11 @@ size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned v assert(cctx->mtctx != NULL); return ZSTDMT_setMTCtxParameter(cctx->mtctx, ZSTDMT_p_overlapSectionLog, value); + case ZSTD_p_test : + DEBUGLOG(2, "Setting test parameter = %u", value); + cctx->requestedParams.testParam = (value > 0); + return 0; + default: return ERROR(parameter_unsupported); } } @@ -527,14 +467,18 @@ size_t ZSTD_CCtxParam_setParameter( return 0; case ZSTD_p_contentSizeFlag : + /* Content size written in frame header _when known_ (default:1) */ + DEBUGLOG(5, "set content size flag = %u", (value>0)); params->fParams.contentSizeFlag = value > 0; return 0; case ZSTD_p_checksumFlag : + /* A 32-bits content checksum will be calculated and written at end of frame (default:0) */ params->fParams.checksumFlag = value > 0; return 0; case ZSTD_p_dictIDFlag : + DEBUGLOG(5, "set dictIDFlag = %u", (value>0)); params->fParams.noDictIDFlag = (value == 0); return 0; @@ -559,7 +503,7 @@ size_t ZSTD_CCtxParam_setParameter( #ifndef ZSTD_MULTITHREAD if (value > 1) return ERROR(parameter_unsupported); #endif - // Do checks when applying parameters to cctx. + /* Do checks when applying params to cctx */ params->nbThreads = value; return 0; @@ -569,18 +513,57 @@ size_t ZSTD_CCtxParam_setParameter( return 0; case ZSTD_p_overlapSizeLog : + if (params->nbThreads <= 1) { return ERROR(parameter_unsupported); } params->overlapSizeLog = value; return 0; + case ZSTD_p_test : + DEBUGLOG(2, "setting opaque: ZSTD_p_test: %u", value); + params->testParam = (value > 0); + return 0; + default: return ERROR(parameter_unsupported); } } +static void ZSTD_debugPrintCCtxParams(ZSTD_CCtx_params* params) +{ + DEBUGLOG(2, "======CCtxParams======"); + DEBUGLOG(2, "cParams: %u %u %u %u %u %u %u", + params->cParams.windowLog, + params->cParams.chainLog, + params->cParams.hashLog, + params->cParams.searchLog, + params->cParams.searchLength, + params->cParams.targetLength, + params->cParams.strategy); + DEBUGLOG(2, "fParams: %u %u %u", + params->fParams.contentSizeFlag, + params->fParams.checksumFlag, + params->fParams.noDictIDFlag); + DEBUGLOG(2, "cLevel, forceWindow: %u %u", + params->compressionLevel, + params->forceWindow); + DEBUGLOG(2, "dictionary: %u %u", + params->dictMode, + params->dictContentByRef); + DEBUGLOG(2, "multithreading: %u %u %u", + params->nbThreads, + params->jobSize, + params->overlapSizeLog); + DEBUGLOG(2, "testParam: %u", + params->testParam); +} + // This function should probably be updated whenever ZSTD_CCtx_params is updated. ZSTDLIB_API size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, ZSTD_CCtx_params* params) { + if (params == NULL) { return ERROR(GENERIC); } if (cctx->cdict) { return ERROR(stage_wrong); } + DEBUGLOG(2, "Applying cctx params\n"); + ZSTD_debugPrintCCtxParams(params); + /* Assume the compression and frame parameters are validated */ cctx->requestedParams.cParams = params->cParams; cctx->requestedParams.fParams = params->fParams; @@ -596,9 +579,14 @@ ZSTDLIB_API size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, ZSTD_CCtx_params* /* Set multithreading parameters explicitly */ CHECK_F( ZSTD_CCtx_setParameter(cctx, ZSTD_p_nbThreads, params->nbThreads) ); - CHECK_F( ZSTD_CCtx_setParameter(cctx, ZSTD_p_jobSize, params->jobSize) ); - CHECK_F( ZSTD_CCtx_setParameter( + if (params->nbThreads > 1) { + CHECK_F( ZSTD_CCtx_setParameter(cctx, ZSTD_p_jobSize, params->jobSize) ); + CHECK_F( ZSTD_CCtx_setParameter( cctx, ZSTD_p_overlapSizeLog, params->overlapSizeLog) ); + + } + /* Copy test parameter */ + cctx->requestedParams.testParam = params->testParam; return 0; } @@ -790,8 +778,7 @@ size_t ZSTD_estimateCCtxSize(int compressionLevel) size_t ZSTD_estimateCStreamSize_advanced_opaque(ZSTD_CCtx_params* params) { if (params == NULL) { return 0; } - { - size_t const CCtxSize = ZSTD_estimateCCtxSize_advanced_opaque(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; @@ -3265,7 +3252,6 @@ size_t ZSTD_getBlockSize(const ZSTD_CCtx* cctx) ZSTD_compressionParameters cParams = (cLevel == ZSTD_CLEVEL_CUSTOM) ? cctx->appliedParams.cParams : ZSTD_getCParams(cLevel, 0, 0); - DEBUGLOG(2, "ZSTD_getBlockSize: cLevel %u\n", cLevel); return MIN (ZSTD_BLOCKSIZE_MAX, 1 << cParams.windowLog); } @@ -3485,7 +3471,8 @@ static size_t ZSTD_compressBegin_internal(ZSTD_CCtx* cctx, return ZSTD_compress_insertDictionary(cctx, dict, dictSize, params.dictMode); } -size_t ZSTD_compressBegin_advanced_opaque(ZSTD_CCtx* cctx, +size_t ZSTD_compressBegin_advanced_opaque( + ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_CCtx_params params, unsigned long long pledgedSrcSize) @@ -3619,11 +3606,12 @@ size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx, } /* Internal */ -size_t ZSTD_compress_advanced_opaque(ZSTD_CCtx* cctx, - void* dst, size_t dstCapacity, - const void* src, size_t srcSize, - const void* dict,size_t dictSize, - ZSTD_CCtx_params params) +size_t ZSTD_compress_advanced_opaque( + ZSTD_CCtx* cctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const void* dict,size_t dictSize, + ZSTD_CCtx_params params) { CHECK_F( ZSTD_compressBegin_internal(cctx, dict, dictSize, NULL, params, srcSize, ZSTDb_not_buffered) ); @@ -3751,11 +3739,11 @@ static size_t ZSTD_initCDict_internal( } #endif -ZSTD_CDict* ZSTD_createCDict_advanced_opaque( +static ZSTD_CDict* ZSTD_createCDict_advanced_opaque( const void* dictBuffer, size_t dictSize, ZSTD_CCtx_params params, ZSTD_customMem customMem) { - DEBUGLOG(5, "ZSTD_createCDict_advanced, mode %u", (U32)dictMode); + DEBUGLOG(5, "ZSTD_createCDict_advanced_opaque, mode %u", (U32)params.dictMode); if (!customMem.customAlloc ^ !customMem.customFree) return NULL; { ZSTD_CDict* const cdict = (ZSTD_CDict*)ZSTD_malloc(sizeof(ZSTD_CDict), customMem); @@ -3794,31 +3782,6 @@ ZSTD_CDict* ZSTD_createCDict_advanced(const void* dictBuffer, size_t dictSize, cctxParams.dictMode = dictMode; cctxParams.dictContentByRef = byReference; return ZSTD_createCDict_advanced_opaque(dictBuffer, dictSize, cctxParams, customMem); -#if 0 - DEBUGLOG(5, "ZSTD_createCDict_advanced, mode %u", (U32)dictMode); - if (!customMem.customAlloc ^ !customMem.customFree) return NULL; - - { ZSTD_CDict* const cdict = (ZSTD_CDict*)ZSTD_malloc(sizeof(ZSTD_CDict), customMem); - ZSTD_CCtx* const cctx = ZSTD_createCCtx_advanced(customMem); - - if (!cdict || !cctx) { - ZSTD_free(cdict, customMem); - ZSTD_freeCCtx(cctx); - return NULL; - } - cdict->refContext = cctx; - - if (ZSTD_isError( ZSTD_initCDict_internal(cdict, - dictBuffer, dictSize, - byReference, dictMode, - cParams) )) { - ZSTD_freeCDict(cdict); - return NULL; - } - - return cdict; - } -#endif } ZSTD_CDict* ZSTD_createCDict(const void* dict, size_t dictSize, int compressionLevel) @@ -4064,7 +4027,6 @@ size_t ZSTD_initCStream_internal_opaque(ZSTD_CStream* zcs, ZSTD_CCtx_params params, unsigned long long pledgedSrcSize) { - DEBUGLOG(5, "ZSTD_initCStream_internal"); assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); assert(!((dict) && (cdict))); /* either dict or cdict, not both */ @@ -4145,9 +4107,9 @@ size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, { ZSTD_CCtx_params cctxParams = zcs->requestedParams; CHECK_F( ZSTD_checkCParams(params.cParams) ); - zcs->requestedParams.cParams = params.cParams; - zcs->requestedParams.fParams = params.fParams; - zcs->requestedParams.compressionLevel = ZSTD_CLEVEL_CUSTOM; + cctxParams.cParams = params.cParams; + cctxParams.fParams = params.fParams; + cctxParams.compressionLevel = ZSTD_CLEVEL_CUSTOM; return ZSTD_initCStream_internal_opaque(zcs, dict, dictSize, NULL, cctxParams, pledgedSrcSize); } diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index ea5828f35..1296cd316 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -62,7 +62,7 @@ static unsigned long long GetCurrentClockTimeMicroseconds(void) DEBUGLOG(MUTEX_WAIT_TIME_DLEVEL, "Thread took %llu microseconds to acquire mutex %s \n", \ elapsedTime, #mutex); \ } } \ - } else pthread_mutex_lock(mutex); \ + } else { pthread_mutex_lock(mutex); } \ } #else @@ -646,7 +646,7 @@ static size_t ZSTDMT_compress_advanced_opaque( } } /* for (chunkID=0; chunkID dstCapacity) { diff --git a/lib/zstd.h b/lib/zstd.h index 56316160a..eb2a857fe 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -498,6 +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. + * TODO: ZSTD_estimateCCtxSize_advanced_opaque() * 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); @@ -509,6 +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. + * TODO: ZSTD_estimateCStreamSize_advanced_opaque * 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, @@ -525,11 +527,10 @@ ZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t sr /*! ZSTD_estimate?DictSize() : * ZSTD_estimateCDictSize() will bet that src size is relatively "small", and content is copied, like ZSTD_createCDict(). * ZSTD_estimateCStreamSize_advanced() makes it possible to control precisely compression parameters, like ZSTD_createCDict_advanced(). + * TODO: ZSTD_estimateCDictSize_advanced_opaque(), can set by reference * Note : dictionary created "byReference" are smaller */ ZSTDLIB_API size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel); ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, unsigned byReference); - -// By reference ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced_opaque(size_t dictSize, ZSTD_CCtx_params* params); ZSTDLIB_API size_t ZSTD_estimateDDictSize(size_t dictSize, unsigned byReference); @@ -613,12 +614,11 @@ ZSTDLIB_API ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( ZSTD_CCtx_params* params); ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void); +ZSTDLIB_API size_t ZSTD_resetCCtxParams(ZSTD_CCtx_params* params); ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createAndInitCCtxParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); ZSTDLIB_API size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* params, ZSTD_compressionParameters cParams); ZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params); -ZSTDLIB_API - /*! ZSTD_getCParams() : * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize. @@ -647,6 +647,7 @@ ZSTDLIB_API size_t ZSTD_compress_advanced (ZSTD_CCtx* cctx, const void* dict,size_t dictSize, ZSTD_parameters params); + /*! ZSTD_compress_usingCDict_advanced() : * Same as ZSTD_compress_usingCDict(), with fine-tune control over frame parameters */ ZSTDLIB_API size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx, @@ -1002,6 +1003,7 @@ typedef enum { /* advanced parameters - may not remain available after API update */ ZSTD_p_forceMaxWindow=1100, /* Force back-reference distances to remain < windowSize, * even when referencing into Dictionary content (default:0) */ + ZSTD_p_test, } ZSTD_cParameter; diff --git a/programs/Makefile b/programs/Makefile index 2460a091f..a192716a6 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -47,7 +47,8 @@ DEBUGFLAGS = -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \ -Wstrict-prototypes -Wundef -Wpointer-arith -Wformat-security \ -Wvla -Wformat=2 -Winit-self -Wfloat-equal -Wwrite-strings \ -Wredundant-decls -CFLAGS += $(DEBUGFLAGS) $(MOREFLAGS) +ZSTD_DEBUG_FLAGS = -g -DZSTD_DEBUG=2 +CFLAGS += $(DEBUGFLAGS) $(MOREFLAGS) $(ZSTD_DEBUG_FLAGS) FLAGS = $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) diff --git a/programs/fileio.c b/programs/fileio.c index 1dd8008e8..b32dd83e0 100644 --- a/programs/fileio.c +++ b/programs/fileio.c @@ -213,6 +213,8 @@ void FIO_setOverlapLog(unsigned overlapLog){ DISPLAYLEVEL(2, "Setting overlapLog is useless in single-thread mode \n"); g_overlapLog = overlapLog; } +static U32 g_testParamFlag = 0; +void FIO_setTestParamFlag(unsigned testParamFlag) { g_testParamFlag = testParamFlag; } /*-************************************* @@ -411,6 +413,9 @@ static cRess_t FIO_createCResources(const char* dictFileName, int cLevel, CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_p_nbThreads, g_nbThreads) ); /* dictionary */ CHECK( ZSTD_CCtx_loadDictionary(ress.cctx, dictBuffer, dictBuffSize) ); + + /* Test */ + CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_p_test, g_testParamFlag) ); } #elif defined(ZSTD_MULTITHREAD) { ZSTD_parameters params = ZSTD_getParams(cLevel, srcSize, dictBuffSize); diff --git a/programs/zstdcli.c b/programs/zstdcli.c index b1268c1f3..88d4e1524 100644 --- a/programs/zstdcli.c +++ b/programs/zstdcli.c @@ -430,6 +430,7 @@ int main(int argCount, const char* argv[]) if (!strcmp(argument, "--keep")) { FIO_setRemoveSrcFile(0); continue; } if (!strcmp(argument, "--rm")) { FIO_setRemoveSrcFile(1); continue; } if (!strcmp(argument, "--priority=rt")) { setRealTimePrio = 1; continue; } + if (!strcmp(argument, "--testParam")) { FIO_setTestParamFlag(1); continue; } #ifdef ZSTD_GZCOMPRESS if (!strcmp(argument, "--format=gzip")) { suffix = GZ_EXTENSION; FIO_setCompressionType(FIO_gzipCompression); continue; } #endif diff --git a/tests/Makefile b/tests/Makefile index 3734f7737..55de2f58a 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -25,7 +25,7 @@ PRGDIR = ../programs PYTHON ?= python3 TESTARTEFACT := versionsTest namespaceTest -DEBUGLEVEL= 1 +DEBUGLEVEL= 2 DEBUGFLAGS= -g -DZSTD_DEBUG=$(DEBUGLEVEL) CPPFLAGS += -I$(ZSTDDIR) -I$(ZSTDDIR)/common -I$(ZSTDDIR)/compress \ -I$(ZSTDDIR)/dictBuilder -I$(ZSTDDIR)/deprecated -I$(PRGDIR) @@ -169,6 +169,12 @@ datagen : $(PRGDIR)/datagen.c datagencli.c roundTripCrash : $(ZSTD_FILES) roundTripCrash.c $(CC) $(FLAGS) $^ -o $@$(EXT) +OPAQUEFILES := $(ZSTD_FILES) $(ZDICT_FILES) roundTripCrashOpaque.c +roundTripCrashOpaque : LDFLAGS += $(MULTITHREAD_CPP) +roundTripCrashOpaque : LDFLAGS += $(MULTITHREAD_LD) +roundTripCrashOpaque : $(OPAQUEFILES) + $(CC) $(FLAGS) $^ -o $@$(EXT) + longmatch : $(ZSTD_FILES) longmatch.c $(CC) $(FLAGS) $^ -o $@$(EXT) diff --git a/tests/fullbench.c b/tests/fullbench.c index 45ef2b6a3..0c41aa3d5 100644 --- a/tests/fullbench.c +++ b/tests/fullbench.c @@ -136,8 +136,10 @@ size_t local_ZSTD_compressStream(void* dst, size_t dstCapacity, void* buff2, con buffIn.src = src; buffIn.size = srcSize; buffIn.pos = 0; + ZSTD_compressStream(g_cstream, &buffOut, &buffIn); ZSTD_endStream(g_cstream, &buffOut); + return buffOut.pos; } @@ -383,11 +385,13 @@ static size_t benchMem(const void* src, size_t srcSize, U32 benchNb) const BYTE* ip = dstBuff; const BYTE* iend; size_t frameHeaderSize, cBlockSize; + ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1); /* it would be better to use direct block compression here */ g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1); frameHeaderSize = ZSTD_getFrameHeader(&zfp, dstBuff, ZSTD_frameHeaderSize_min); if (frameHeaderSize==0) frameHeaderSize = ZSTD_frameHeaderSize_min; ip += frameHeaderSize; /* Skip frame Header */ + cBlockSize = ZSTD_getcBlockSize(ip, dstBuffSize, &bp); /* Get 1st block type */ if (bp.blockType != bt_compressed) { DISPLAY("ZSTD_decodeSeqHeaders : impossible to test on this sample (not compressible)\n"); @@ -395,6 +399,7 @@ static size_t benchMem(const void* src, size_t srcSize, U32 benchNb) } iend = ip + ZSTD_blockHeaderSize + cBlockSize; /* End of first block */ ip += ZSTD_blockHeaderSize; /* skip block header */ + ZSTD_decompressBegin(g_zdc); ip += ZSTD_decodeLiteralsBlock(g_zdc, ip, iend-ip); /* skip literal segment */ g_cSize = iend-ip; diff --git a/tests/roundTripCrashOpaque.c b/tests/roundTripCrashOpaque.c new file mode 100644 index 000000000..f9b6e7f83 --- /dev/null +++ b/tests/roundTripCrashOpaque.c @@ -0,0 +1,203 @@ +/** + * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +/* + This program takes a file in input, + performs a zstd round-trip test (compression - decompress) + compares the result with original + and generates a crash (double free) on corruption detection. +*/ + +/*=========================================== +* Dependencies +*==========================================*/ +#include /* size_t */ +#include /* malloc, free, exit */ +#include /* fprintf */ +#include /* stat */ +#include /* stat */ +#include "xxhash.h" + +#define ZSTD_STATIC_LINKING_ONLY +#include "zstd.h" + +/*=========================================== +* Macros +*==========================================*/ +#define MIN(a,b) ( (a) < (b) ? (a) : (b) ) + +#define CHECK_Z(f) { \ + size_t const err = f; \ + if (ZSTD_isError(err)) { \ + fprintf(stderr, \ + "Error=> %s: %s", \ + #f, ZSTD_getErrorName(err)); \ + exit(1); \ +} } + + +/** roundTripTest() : +* Compresses `srcBuff` into `compressedBuff`, +* then decompresses `compressedBuff` into `resultBuff`. +* @return : result of decompression, which should be == `srcSize` +* or an error code if either compression or decompression fails. +* Note : `compressedBuffCapacity` should be `>= ZSTD_compressBound(srcSize)` +* for compression to be guaranteed to work */ +static size_t roundTripTest(void* resultBuff, size_t resultBuffCapacity, + void* compressedBuff, size_t compressedBuffCapacity, + const void* srcBuff, size_t srcBuffSize) +{ + ZSTD_CCtx* const cctx = ZSTD_createCCtx(); + ZSTD_CCtx_params* const cctxParams = ZSTD_createCCtxParams(); + ZSTD_inBuffer inBuffer = { srcBuff, srcBuffSize, 0 }; + ZSTD_outBuffer outBuffer = {compressedBuff, compressedBuffCapacity, 0 }; + + ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_compressionLevel, 1); + ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_test, 1); + + ZSTD_CCtx_applyCCtxParams(cctx, cctxParams); + + CHECK_Z (ZSTD_compress_generic(cctx, &outBuffer, &inBuffer, ZSTD_e_end) ); + + ZSTD_freeCCtxParams(cctxParams); + ZSTD_freeCCtx(cctx); + + return ZSTD_decompress(resultBuff, resultBuffCapacity, compressedBuff, outBuffer.pos); +} + + +static size_t checkBuffers(const void* buff1, const void* buff2, size_t buffSize) +{ + const char* ip1 = (const char*)buff1; + const char* ip2 = (const char*)buff2; + size_t pos; + + for (pos=0; pos= `fileSize` */ +static void loadFile(void* buffer, const char* fileName, size_t fileSize) +{ + FILE* const f = fopen(fileName, "rb"); + if (isDirectory(fileName)) { + fprintf(stderr, "Ignoring %s directory \n", fileName); + exit(2); + } + if (f==NULL) { + fprintf(stderr, "Impossible to open %s \n", fileName); + exit(3); + } + { size_t const readSize = fread(buffer, 1, fileSize, f); + if (readSize != fileSize) { + fprintf(stderr, "Error reading %s \n", fileName); + exit(5); + } } + fclose(f); +} + + +static void fileCheck(const char* fileName) +{ + size_t const fileSize = getFileSize(fileName); + void* buffer = malloc(fileSize); + if (!buffer) { + fprintf(stderr, "not enough memory \n"); + exit(4); + } + loadFile(buffer, fileName, fileSize); + roundTripCheck(buffer, fileSize); + free (buffer); +} + +int main(int argCount, const char** argv) { + if (argCount < 2) { + fprintf(stderr, "Error : no argument : need input file \n"); + exit(9); + } + fileCheck(argv[1]); + fprintf(stderr, "no pb detected\n"); + return 0; +} diff --git a/tests/zstreamtest.c b/tests/zstreamtest.c index 3e551e331..0281a406d 100644 --- a/tests/zstreamtest.c +++ b/tests/zstreamtest.c @@ -1494,6 +1494,309 @@ _output_error: goto _cleanup; } +static int fuzzerTests_newAPI_opaque(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests) +{ + U32 const maxSrcLog = bigTests ? 24 : 22; + static const U32 maxSampleLog = 19; + size_t const srcBufferSize = (size_t)1<= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); } + else { DISPLAYUPDATE(2, "\r%6u ", testNb); } + FUZ_rand(&coreSeed); + lseed = coreSeed ^ prime32; + + /* states full reset (deliberately not synchronized) */ + /* some issues can only happen when reusing states */ + if ((FUZ_rand(&lseed) & 0xFF) == 131) { + DISPLAYLEVEL(5, "Creating new context \n"); + ZSTD_freeCCtx(zc); + zc = ZSTD_createCCtx(); + CHECK(zc==NULL, "ZSTD_createCCtx allocation error"); + resetAllowed=0; + } + if ((FUZ_rand(&lseed) & 0xFF) == 132) { + ZSTD_freeDStream(zd); + zd = ZSTD_createDStream(); + CHECK(zd==NULL, "ZSTD_createDStream allocation error"); + ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */ + } + + /* srcBuffer selection [0-4] */ + { U32 buffNb = FUZ_rand(&lseed) & 0x7F; + if (buffNb & 7) buffNb=2; /* most common : compressible (P) */ + else { + buffNb >>= 3; + if (buffNb & 7) { + const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */ + buffNb = tnb[buffNb >> 3]; + } else { + const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */ + buffNb = tnb[buffNb >> 3]; + } } + srcBuffer = cNoiseBuffer[buffNb]; + } + + /* compression init */ + CHECK_Z( ZSTD_CCtx_loadDictionary(zc, NULL, 0) ); /* cancel previous dict /*/ + if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */ + && oldTestLog /* at least one test happened */ && resetAllowed) { + maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2); + if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1; + { int const compressionLevel = (FUZ_rand(&lseed) % 5) + 1; + CHECK_Z (ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_compressionLevel, compressionLevel)); + } + } else { + U32 const testLog = FUZ_rand(&lseed) % maxSrcLog; + U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog; + U32 const cLevelCandidate = (FUZ_rand(&lseed) % + (ZSTD_maxCLevel() - + (MAX(testLog, dictLog) / 3))) + + 1; + U32 const cLevel = MIN(cLevelCandidate, cLevelMax); + maxTestSize = FUZ_rLogLength(&lseed, testLog); + oldTestLog = testLog; + /* random dictionary selection */ + dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0; + { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize); + dict = srcBuffer + dictStart; + if (!dictSize) dict=NULL; + } + { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? ZSTD_CONTENTSIZE_UNKNOWN : maxTestSize; + ZSTD_compressionParameters cParams = ZSTD_getCParams(cLevel, pledgedSrcSize, dictSize); + + /* mess with compression parameters */ + cParams.windowLog += (FUZ_rand(&lseed) & 3) - 1; + cParams.hashLog += (FUZ_rand(&lseed) & 3) - 1; + cParams.chainLog += (FUZ_rand(&lseed) & 3) - 1; + cParams.searchLog += (FUZ_rand(&lseed) & 3) - 1; + cParams.searchLength += (FUZ_rand(&lseed) & 3) - 1; + cParams.targetLength = (U32)(cParams.targetLength * (0.5 + ((double)(FUZ_rand(&lseed) & 127) / 128))); + cParams = ZSTD_adjustCParams(cParams, 0, 0); + + if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_windowLog, cParams.windowLog) ); + if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_hashLog, cParams.hashLog) ); + if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_chainLog, cParams.chainLog) ); + if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_searchLog, cParams.searchLog) ); + if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_minMatch, cParams.searchLength) ); + if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_targetLength, cParams.targetLength) ); + + /* unconditionally set, to be sync with decoder */ + /* mess with frame parameters */ + if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_checksumFlag, FUZ_rand(&lseed) & 1) ); + if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_dictIDFlag, FUZ_rand(&lseed) & 1) ); + if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_contentSizeFlag, FUZ_rand(&lseed) & 1) ); + if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setPledgedSrcSize(zc, pledgedSrcSize) ); + DISPLAYLEVEL(5, "pledgedSrcSize : %u \n", (U32)pledgedSrcSize); + + if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_refDictContent, FUZ_rand(&lseed) & 1) ); + /* multi-threading parameters */ + { U32 const nbThreadsCandidate = (FUZ_rand(&lseed) & 4) + 1; + U32 const nbThreads = MIN(nbThreadsCandidate, nbThreadsMax); + CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_nbThreads, nbThreads) ); + if (nbThreads > 1) { + U32 const jobLog = FUZ_rand(&lseed) % (testLog+1); + CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_overlapSizeLog, FUZ_rand(&lseed) % 10) ); + CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_jobSize, (U32)FUZ_rLogLength(&lseed, jobLog)) ); + } + } + + if (FUZ_rand(&lseed) & 1) CHECK_Z (ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_forceMaxWindow, FUZ_rand(&lseed) & 1) ); + + if (FUZ_rand(&lseed) & 1) CHECK_Z (ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_test, FUZ_rand(&lseed) & 1) ); + + + /* Apply parameters */ + + CHECK_Z (ZSTD_CCtx_applyCCtxParams(zc, cctxParams) ); + + if (FUZ_rand(&lseed) & 1) { + CHECK_Z( ZSTD_CCtx_loadDictionary(zc, dict, dictSize) ); + if (dict && dictSize) { + /* test that compression parameters are rejected (correctly) after loading a non-NULL dictionary */ + size_t const setError = ZSTD_CCtx_applyCCtxParams(zc, cctxParams); + CHECK(!ZSTD_isError(setError), "ZSTD_CCtx_applyCCtxParams should have failed"); + } } else { + CHECK_Z( ZSTD_CCtx_refPrefix(zc, dict, dictSize) ); + } + } } + + /* multi-segments compression test */ + XXH64_reset(&xxhState, 0); + { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ; + for (cSize=0, totalTestSize=0 ; (totalTestSize < maxTestSize) ; ) { + /* compress random chunks into randomly sized dst buffers */ + size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog); + size_t const srcSize = MIN(maxTestSize-totalTestSize, randomSrcSize); + size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize); + size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog+1); + size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize); + ZSTD_EndDirective const flush = (FUZ_rand(&lseed) & 15) ? ZSTD_e_continue : ZSTD_e_flush; + ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 }; + outBuff.size = outBuff.pos + dstBuffSize; + + CHECK_Z( ZSTD_compress_generic(zc, &outBuff, &inBuff, flush) ); + DISPLAYLEVEL(5, "compress consumed %u bytes (total : %u) \n", + (U32)inBuff.pos, (U32)(totalTestSize + inBuff.pos)); + + XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos); + memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos); + totalTestSize += inBuff.pos; + } + + /* final frame epilogue */ + { size_t remainingToFlush = (size_t)(-1); + while (remainingToFlush) { + ZSTD_inBuffer inBuff = { NULL, 0, 0 }; + size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog+1); + size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); + outBuff.size = outBuff.pos + adjustedDstSize; + DISPLAYLEVEL(5, "End-flush into dst buffer of size %u \n", (U32)adjustedDstSize); + remainingToFlush = ZSTD_compress_generic(zc, &outBuff, &inBuff, ZSTD_e_end); + CHECK(ZSTD_isError(remainingToFlush), + "ZSTD_compress_generic w/ ZSTD_e_end error : %s", + ZSTD_getErrorName(remainingToFlush) ); + } } + crcOrig = XXH64_digest(&xxhState); + cSize = outBuff.pos; + DISPLAYLEVEL(5, "Frame completed : %u bytes \n", (U32)cSize); + } + + /* multi - fragments decompression test */ + if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) { + DISPLAYLEVEL(5, "resetting DCtx (dict:%08X) \n", (U32)(size_t)dict); + CHECK_Z( ZSTD_resetDStream(zd) ); + } else { + DISPLAYLEVEL(5, "using dict of size %u \n", (U32)dictSize); + CHECK_Z( ZSTD_initDStream_usingDict(zd, dict, dictSize) ); + } + { size_t decompressionResult = 1; + ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; + ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; + for (totalGenSize = 0 ; decompressionResult ; ) { + size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); + size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); + size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize); + inBuff.size = inBuff.pos + readCSrcSize; + outBuff.size = inBuff.pos + dstBuffSize; + DISPLAYLEVEL(5, "ZSTD_decompressStream input %u bytes (pos:%u/%u)\n", + (U32)readCSrcSize, (U32)inBuff.pos, (U32)cSize); + decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff); + CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult)); + DISPLAYLEVEL(5, "inBuff.pos = %u \n", (U32)readCSrcSize); + } + CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)", (U32)outBuff.pos, (U32)totalTestSize); + CHECK (inBuff.pos != cSize, "compressed data should be fully read (%u != %u)", (U32)inBuff.pos, (U32)cSize); + { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0); + if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize); + CHECK (crcDest!=crcOrig, "decompressed data corrupted"); + } } + + /*===== noisy/erroneous src decompression test =====*/ + + /* add some noise */ + { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2; + U32 nn; for (nn=0; nn Date: Mon, 21 Aug 2017 01:59:08 -0700 Subject: [PATCH 14/52] Disable tests and refactor --- lib/common/zstd_internal.h | 18 ++- lib/compress/zstd_compress.c | 148 ++++++++---------- lib/compress/zstdmt_compress.c | 5 +- lib/compress/zstdmt_compress.h | 9 -- lib/zstd.h | 14 +- programs/Makefile | 3 +- programs/fileio.c | 6 +- programs/fileio.h | 3 + programs/zstdcli.c | 2 + tests/Makefile | 9 +- ...TripCrashOpaque.c => cctxParamRoundTrip.c} | 5 +- tests/fullbench.c | 5 - tests/zstreamtest.c | 4 +- 13 files changed, 102 insertions(+), 129 deletions(-) rename tests/{roundTripCrashOpaque.c => cctxParamRoundTrip.c} (98%) diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index 1cccca286..8b8419a4c 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -221,6 +221,7 @@ typedef struct seqDef_s { U16 matchLength; } seqDef; + typedef struct { seqDef* sequencesStart; seqDef* sequences; @@ -240,7 +241,6 @@ struct ZSTD_CCtx_params_s { ZSTD_frameParameters fParams; int compressionLevel; - U32 forceWindow; /* force back-references to respect limit of 1<seqStore); } -#if 0 -// TODO: get rid of this function -static ZSTD_parameters ZSTD_getParamsFromCCtxParams(const ZSTD_CCtx_params cctxParams) -{ - ZSTD_parameters params; - params.cParams = cctxParams.cParams; - params.fParams = cctxParams.fParams; - return params; -} -#endif -#if 0 -// TODO: get rid of this function too -static ZSTD_CCtx_params ZSTD_makeCCtxParamsFromParams(ZSTD_parameters params) { - ZSTD_CCtx_params cctxParams; - memset(&cctxParams, 0, sizeof(ZSTD_CCtx_params)); - cctxParams.cParams = params.cParams; - cctxParams.fParams = params.fParams; - return cctxParams; -} -#endif - -// TODO: get rid of this function too +/* TODO: get rid of this function if possible*/ static ZSTD_CCtx_params ZSTD_makeCCtxParamsFromCParams( ZSTD_compressionParameters cParams) { @@ -234,12 +220,6 @@ static ZSTD_CCtx_params ZSTD_makeCCtxParamsFromCParams( cctxParams.cParams = cParams; return cctxParams; } -#if 0 -// TODO: get rid of this function -static ZSTD_parameters ZSTD_getParamsFromCCtx(const ZSTD_CCtx* cctx) { - return ZSTD_getParamsFromCCtxParams(cctx->appliedParams); -} -#endif /* older variant; will be deprecated */ size_t ZSTD_setCCtxParameter(ZSTD_CCtx* cctx, ZSTD_CCtxParameter param, unsigned value) @@ -261,7 +241,6 @@ size_t ZSTD_setCCtxParameter(ZSTD_CCtx* cctx, ZSTD_CCtxParameter param, unsigned #define ZSTD_CLEVEL_CUSTOM 999 -#if 0 static void ZSTD_cLevelToCParams(ZSTD_CCtx* cctx) { if (cctx->requestedParams.compressionLevel==ZSTD_CLEVEL_CUSTOM) return; @@ -270,7 +249,6 @@ static void ZSTD_cLevelToCParams(ZSTD_CCtx* cctx) cctx->pledgedSrcSizePlusOne-1, 0); cctx->requestedParams.compressionLevel = ZSTD_CLEVEL_CUSTOM; } -#endif ZSTD_CCtx_params* ZSTD_createCCtxParams(void) { @@ -290,27 +268,15 @@ size_t ZSTD_resetCCtxParams(ZSTD_CCtx_params* params) return 0; } -size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* params, - ZSTD_compressionParameters cParams) +size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params) { - memset(params, 0, sizeof(ZSTD_CCtx_params)); - params->cParams = cParams; - params->compressionLevel = ZSTD_CLEVEL_CUSTOM; + memset(cctxParams, 0, sizeof(ZSTD_CCtx_params)); + cctxParams->cParams = params.cParams; + cctxParams->fParams = params.fParams; + cctxParams->compressionLevel = ZSTD_CLEVEL_CUSTOM; return 0; } -ZSTD_CCtx_params* ZSTD_createAndInitCCtxParams( - int compressionLevel, unsigned long long estimatedSrcSize, - size_t dictSize) -{ - ZSTD_CCtx_params* params = ZSTD_createCCtxParams(); - if (params == NULL) { return NULL; } - ZSTD_initCCtxParams(params, ZSTD_getCParams( - compressionLevel, estimatedSrcSize, dictSize)); - params->compressionLevel = ZSTD_CLEVEL_CUSTOM; - return params; -} - size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params) { if (params == NULL) { return 0; } @@ -318,8 +284,6 @@ size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params) return 0; } - - static void ZSTD_cLevelToCCtxParams(ZSTD_CCtx_params* params) { if (params->compressionLevel == ZSTD_CLEVEL_CUSTOM) return; @@ -341,6 +305,10 @@ size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned v switch(param) { case ZSTD_p_compressionLevel: + if (value == 0) return 0; + if (cctx->cdict) return ERROR(stage_wrong); + return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); + case ZSTD_p_windowLog: case ZSTD_p_hashLog: case ZSTD_p_chainLog: @@ -350,6 +318,7 @@ size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned v case ZSTD_p_compressionStrategy: if (value == 0) return 0; /* special value : 0 means "don't change anything" */ if (cctx->cdict) return ERROR(stage_wrong); + ZSTD_cLevelToCParams(cctx); return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); case ZSTD_p_contentSizeFlag: @@ -396,11 +365,12 @@ size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned v if (cctx->requestedParams.nbThreads <= 1) return ERROR(parameter_unsupported); assert(cctx->mtctx != NULL); return ZSTDMT_setMTCtxParameter(cctx->mtctx, ZSTDMT_p_overlapSectionLog, value); - +#if 0 case ZSTD_p_test : DEBUGLOG(2, "Setting test parameter = %u", value); cctx->requestedParams.testParam = (value > 0); return 0; +#endif default: return ERROR(parameter_unsupported); } @@ -477,12 +447,13 @@ size_t ZSTD_CCtxParam_setParameter( params->fParams.checksumFlag = value > 0; return 0; - case ZSTD_p_dictIDFlag : + case ZSTD_p_dictIDFlag : /* When applicable, dictionary's dictID is provided in frame header (default:1) */ DEBUGLOG(5, "set dictIDFlag = %u", (value>0)); params->fParams.noDictIDFlag = (value == 0); return 0; case ZSTD_p_dictMode : + /* restrict dictionary mode to "rawContent" or "fullDict" only */ ZSTD_STATIC_ASSERT((U32)ZSTD_dm_fullDict > (U32)ZSTD_dm_rawContent); if (value > (unsigned)ZSTD_dm_fullDict) { return ERROR(parameter_outOfBound); @@ -491,6 +462,7 @@ size_t ZSTD_CCtxParam_setParameter( return 0; case ZSTD_p_refDictContent : + /* dictionary content will be referenced, instead of copied */ params->dictContentByRef = value > 0; return 0; @@ -516,16 +488,18 @@ size_t ZSTD_CCtxParam_setParameter( if (params->nbThreads <= 1) { return ERROR(parameter_unsupported); } params->overlapSizeLog = value; return 0; - +#if 0 case ZSTD_p_test : DEBUGLOG(2, "setting opaque: ZSTD_p_test: %u", value); params->testParam = (value > 0); return 0; +#endif default: return ERROR(parameter_unsupported); } } +#if 0 static void ZSTD_debugPrintCCtxParams(ZSTD_CCtx_params* params) { DEBUGLOG(2, "======CCtxParams======"); @@ -551,19 +525,24 @@ static void ZSTD_debugPrintCCtxParams(ZSTD_CCtx_params* params) params->nbThreads, params->jobSize, params->overlapSizeLog); +#if 0 DEBUGLOG(2, "testParam: %u", params->testParam); +#endif } +#endif -// This function should probably be updated whenever ZSTD_CCtx_params is updated. -ZSTDLIB_API size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, ZSTD_CCtx_params* params) +/** + * This function should be updated whenever ZSTD_CCtx_params is updated. + * Parameters are copied manually before the dictionary is loaded. + * The multithreading parameters jobSize and overlapSizeLog are set only if + * nbThreads >= 1. + */ +size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, ZSTD_CCtx_params* params) { if (params == NULL) { return ERROR(GENERIC); } if (cctx->cdict) { return ERROR(stage_wrong); } - DEBUGLOG(2, "Applying cctx params\n"); - ZSTD_debugPrintCCtxParams(params); - /* Assume the compression and frame parameters are validated */ cctx->requestedParams.cParams = params->cParams; cctx->requestedParams.fParams = params->fParams; @@ -585,8 +564,10 @@ ZSTDLIB_API size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, ZSTD_CCtx_params* cctx, ZSTD_p_overlapSizeLog, params->overlapSizeLog) ); } - /* Copy test parameter */ +#if 0 + /* Copy test parameter */ cctx->requestedParams.testParam = params->testParam; +#endif return 0; } @@ -3247,7 +3228,6 @@ size_t ZSTD_compressContinue (ZSTD_CCtx* cctx, size_t ZSTD_getBlockSize(const ZSTD_CCtx* cctx) { - // TODO: Applied params compression level okay? Gets overwritten U32 const cLevel = cctx->appliedParams.compressionLevel; ZSTD_compressionParameters cParams = (cLevel == ZSTD_CLEVEL_CUSTOM) ? cctx->appliedParams.cParams : @@ -3710,7 +3690,8 @@ static size_t ZSTD_initCDict_internal_opaque(ZSTD_CDict* cdict, } cdict->dictContentSize = dictSize; - /* Frame parameters should be zero? */ + /* TODO: do the frame parameters need to be zero? + * does nbThreads need to be zero? */ CHECK_F( ZSTD_compressBegin_internal(cdict->refContext, cdict->dictContent, dictSize, NULL, @@ -3748,9 +3729,6 @@ static ZSTD_CDict* ZSTD_createCDict_advanced_opaque( { ZSTD_CDict* const cdict = (ZSTD_CDict*)ZSTD_malloc(sizeof(ZSTD_CDict), customMem); ZSTD_CCtx* const cctx = ZSTD_createCCtx_advanced(customMem); - /* Initialize to 0 to preserve semantics */ - ZSTD_frameParameters const fParams = { 0, 0, 0 }; - params.fParams = fParams; if (!cdict || !cctx) { ZSTD_free(cdict, customMem); @@ -3759,7 +3737,6 @@ static ZSTD_CDict* ZSTD_createCDict_advanced_opaque( } cdict->refContext = cctx; - if (ZSTD_isError( ZSTD_initCDict_internal_opaque( cdict, dictBuffer, dictSize, @@ -3777,8 +3754,6 @@ ZSTD_CDict* ZSTD_createCDict_advanced(const void* dictBuffer, size_t dictSize, ZSTD_compressionParameters cParams, ZSTD_customMem customMem) { ZSTD_CCtx_params cctxParams = ZSTD_makeCCtxParamsFromCParams(cParams); - ZSTD_frameParameters const fParams = { 0, 0, 0 }; - cctxParams.fParams = fParams; cctxParams.dictMode = dictMode; cctxParams.dictContentByRef = byReference; return ZSTD_createCDict_advanced_opaque(dictBuffer, dictSize, cctxParams, customMem); @@ -3837,6 +3812,7 @@ ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( cdict->refContext = ZSTD_initStaticCCtx(ptr, cctxSize); params->dictContentByRef = 1; + /* What if nbThreads > 1? */ if (ZSTD_isError( ZSTD_initCDict_internal_opaque(cdict, dict, dictSize, *params) )) return NULL; @@ -3970,11 +3946,10 @@ size_t ZSTD_CStreamOutSize(void) static size_t ZSTD_resetCStream_internal_opaque( ZSTD_CStream* zcs, - const void* dict, size_t dictSize, ZSTD_dictMode_e dictMode, + const void* dict, size_t dictSize, const ZSTD_CDict* cdict, ZSTD_CCtx_params params, unsigned long long pledgedSrcSize) { - params.dictMode = dictMode; DEBUGLOG(4, "ZSTD_resetCStream_internal"); /* params are supposed to be fully validated at this point */ assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); @@ -4004,8 +3979,7 @@ static size_t ZSTD_resetCStream_internal(ZSTD_CStream* zcs, ZSTD_CCtx_params cctxParams = zcs->requestedParams; cctxParams.cParams = params.cParams; cctxParams.fParams = params.fParams; - cctxParams.dictMode = dictMode; - return ZSTD_resetCStream_internal_opaque(zcs, dict, dictSize, dictMode, + return ZSTD_resetCStream_internal_opaque(zcs, dict, dictSize, cdict, cctxParams, pledgedSrcSize); } #endif @@ -4018,14 +3992,15 @@ size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize) if (params.compressionLevel != ZSTD_CLEVEL_CUSTOM) { params.cParams = ZSTD_getCParams(params.compressionLevel, pledgedSrcSize, 0 /* dictSize */); } - return ZSTD_resetCStream_internal_opaque(zcs, NULL, 0, params.dictMode, zcs->cdict, params, pledgedSrcSize); + return ZSTD_resetCStream_internal_opaque(zcs, NULL, 0, zcs->cdict, params, pledgedSrcSize); } -size_t ZSTD_initCStream_internal_opaque(ZSTD_CStream* zcs, - const void* dict, size_t dictSize, - const ZSTD_CDict* cdict, - ZSTD_CCtx_params params, - unsigned long long pledgedSrcSize) +size_t ZSTD_initCStream_internal_opaque( + ZSTD_CStream* zcs, + const void* dict, size_t dictSize, + const ZSTD_CDict* cdict, + ZSTD_CCtx_params params, + unsigned long long pledgedSrcSize) { assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); assert(!((dict) && (cdict))); /* either dict or cdict, not both */ @@ -4038,8 +4013,8 @@ size_t ZSTD_initCStream_internal_opaque(ZSTD_CStream* zcs, } ZSTD_freeCDict(zcs->cdictLocal); zcs->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, - zcs->requestedParams.dictContentByRef, - zcs->requestedParams.dictMode, + params.dictContentByRef, + params.dictMode, params.cParams, zcs->customMem); zcs->cdict = zcs->cdictLocal; if (zcs->cdictLocal == NULL) return ERROR(memory_allocation); @@ -4055,8 +4030,7 @@ size_t ZSTD_initCStream_internal_opaque(ZSTD_CStream* zcs, zcs->requestedParams = params; return ZSTD_resetCStream_internal_opaque( - zcs, NULL, 0, zcs->requestedParams.dictMode, zcs->cdict, - params, pledgedSrcSize); + zcs, NULL, 0, zcs->cdict, params, pledgedSrcSize); } #if 0 @@ -4339,7 +4313,7 @@ size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, } else #endif { - CHECK_F( ZSTD_resetCStream_internal_opaque(cctx, prefix, prefixSize, params.dictMode, cctx->cdict, params, cctx->pledgedSrcSizePlusOne-1) ); + CHECK_F( ZSTD_resetCStream_internal_opaque(cctx, prefix, prefixSize, cctx->cdict, params, cctx->pledgedSrcSizePlusOne-1) ); } } /* compression stage */ diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index 1296cd316..76a062f04 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -186,6 +186,10 @@ static void ZSTDMT_releaseBuffer(ZSTDMT_bufferPool* bufPool, buffer_t buf) ZSTD_free(buf.start, bufPool->cMem); } +/** + * TODO + * Resets parameters to zero for jobs? + */ static void ZSTDMT_zeroCCtxParams(ZSTD_CCtx_params* params) { params->forceWindow = 0; @@ -778,7 +782,6 @@ size_t ZSTDMT_initCStream_internal_opaque( } - /** ZSTDMT_initCStream_internal() : * internal usage only */ size_t ZSTDMT_initCStream_internal(ZSTDMT_CCtx* zcs, diff --git a/lib/compress/zstdmt_compress.h b/lib/compress/zstdmt_compress.h index 0b478b735..843a240aa 100644 --- a/lib/compress/zstdmt_compress.h +++ b/lib/compress/zstdmt_compress.h @@ -69,15 +69,6 @@ ZSTDLIB_API size_t ZSTDMT_compress_advanced(ZSTDMT_CCtx* mtctx, const ZSTD_CDict* cdict, ZSTD_parameters const params, unsigned overlapLog); -#if 0 -ZSTDLIB_API size_t ZSTDMT_compress_advanced_opaque( - ZSTDMT_CCtx* mtctx, - void* dst, size_t dstCapacity, - const void* src, size_t srcSize, - const ZSTD_CDict* cdict, - ZSTD_CCtx_params* const params, - unsigned overlapLog); -#endif ZSTDLIB_API size_t ZSTDMT_initCStream_advanced(ZSTDMT_CCtx* mtctx, const void* dict, size_t dictSize, /* dict can be released after init, a local copy is preserved within zcs */ diff --git a/lib/zstd.h b/lib/zstd.h index eb2a857fe..f02f28c82 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -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. - * TODO: ZSTD_estimateCStreamSize_advanced_opaque + * TODO: ZSTD_estimateCStreamSize_advanced_opaque() * 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, @@ -527,7 +527,7 @@ ZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t sr /*! ZSTD_estimate?DictSize() : * ZSTD_estimateCDictSize() will bet that src size is relatively "small", and content is copied, like ZSTD_createCDict(). * ZSTD_estimateCStreamSize_advanced() makes it possible to control precisely compression parameters, like ZSTD_createCDict_advanced(). - * TODO: ZSTD_estimateCDictSize_advanced_opaque(), can set by reference + * TODO: ZSTD_estimateCDictSize_advanced_opaque() * Note : dictionary created "byReference" are smaller */ ZSTDLIB_API size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel); ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, unsigned byReference); @@ -615,11 +615,9 @@ ZSTDLIB_API ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void); ZSTDLIB_API size_t ZSTD_resetCCtxParams(ZSTD_CCtx_params* params); -ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createAndInitCCtxParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); -ZSTDLIB_API size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* params, ZSTD_compressionParameters cParams); +ZSTDLIB_API size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params); ZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params); - /*! ZSTD_getCParams() : * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize. * `estimatedSrcSize` value is optional, select 0 if not known */ @@ -647,7 +645,6 @@ ZSTDLIB_API size_t ZSTD_compress_advanced (ZSTD_CCtx* cctx, const void* dict,size_t dictSize, ZSTD_parameters params); - /*! ZSTD_compress_usingCDict_advanced() : * Same as ZSTD_compress_usingCDict(), with fine-tune control over frame parameters */ ZSTDLIB_API size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx, @@ -1003,7 +1000,9 @@ typedef enum { /* advanced parameters - may not remain available after API update */ ZSTD_p_forceMaxWindow=1100, /* Force back-reference distances to remain < windowSize, * even when referencing into Dictionary content (default:0) */ +#if 0 ZSTD_p_test, +#endif } ZSTD_cParameter; @@ -1014,8 +1013,9 @@ typedef enum { * @result : 0, or an error code (which can be tested with ZSTD_isError()). */ ZSTDLIB_API size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned value); +/* TODO */ ZSTDLIB_API size_t ZSTD_CCtxParam_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, unsigned value); - +/* TODO */ ZSTDLIB_API size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, ZSTD_CCtx_params* params); /*! ZSTD_CCtx_setPledgedSrcSize() : diff --git a/programs/Makefile b/programs/Makefile index a192716a6..2460a091f 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -47,8 +47,7 @@ DEBUGFLAGS = -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \ -Wstrict-prototypes -Wundef -Wpointer-arith -Wformat-security \ -Wvla -Wformat=2 -Winit-self -Wfloat-equal -Wwrite-strings \ -Wredundant-decls -ZSTD_DEBUG_FLAGS = -g -DZSTD_DEBUG=2 -CFLAGS += $(DEBUGFLAGS) $(MOREFLAGS) $(ZSTD_DEBUG_FLAGS) +CFLAGS += $(DEBUGFLAGS) $(MOREFLAGS) FLAGS = $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) diff --git a/programs/fileio.c b/programs/fileio.c index b32dd83e0..1fb249d6b 100644 --- a/programs/fileio.c +++ b/programs/fileio.c @@ -213,9 +213,10 @@ void FIO_setOverlapLog(unsigned overlapLog){ DISPLAYLEVEL(2, "Setting overlapLog is useless in single-thread mode \n"); g_overlapLog = overlapLog; } +#if 0 static U32 g_testParamFlag = 0; void FIO_setTestParamFlag(unsigned testParamFlag) { g_testParamFlag = testParamFlag; } - +#endif /*-************************************* * Functions @@ -413,9 +414,10 @@ static cRess_t FIO_createCResources(const char* dictFileName, int cLevel, CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_p_nbThreads, g_nbThreads) ); /* dictionary */ CHECK( ZSTD_CCtx_loadDictionary(ress.cctx, dictBuffer, dictBuffSize) ); - +#if 0 /* Test */ CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_p_test, g_testParamFlag) ); +#endif } #elif defined(ZSTD_MULTITHREAD) { ZSTD_parameters params = ZSTD_getParams(cLevel, srcSize, dictBuffSize); diff --git a/programs/fileio.h b/programs/fileio.h index 9d9167df9..497e122e1 100644 --- a/programs/fileio.h +++ b/programs/fileio.h @@ -56,6 +56,9 @@ void FIO_setMemLimit(unsigned memLimit); void FIO_setNbThreads(unsigned nbThreads); void FIO_setBlockSize(unsigned blockSize); void FIO_setOverlapLog(unsigned overlapLog); +#if 0 +void FIO_setTestParamFlag(unsigned testParamFlag); +#endif /*-************************************* diff --git a/programs/zstdcli.c b/programs/zstdcli.c index 88d4e1524..5e95e4268 100644 --- a/programs/zstdcli.c +++ b/programs/zstdcli.c @@ -430,7 +430,9 @@ int main(int argCount, const char* argv[]) if (!strcmp(argument, "--keep")) { FIO_setRemoveSrcFile(0); continue; } if (!strcmp(argument, "--rm")) { FIO_setRemoveSrcFile(1); continue; } if (!strcmp(argument, "--priority=rt")) { setRealTimePrio = 1; continue; } +#if 0 if (!strcmp(argument, "--testParam")) { FIO_setTestParamFlag(1); continue; } +#endif #ifdef ZSTD_GZCOMPRESS if (!strcmp(argument, "--format=gzip")) { suffix = GZ_EXTENSION; FIO_setCompressionType(FIO_gzipCompression); continue; } #endif diff --git a/tests/Makefile b/tests/Makefile index 55de2f58a..e815f5850 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -25,7 +25,7 @@ PRGDIR = ../programs PYTHON ?= python3 TESTARTEFACT := versionsTest namespaceTest -DEBUGLEVEL= 2 +DEBUGLEVEL= 1 DEBUGFLAGS= -g -DZSTD_DEBUG=$(DEBUGLEVEL) CPPFLAGS += -I$(ZSTDDIR) -I$(ZSTDDIR)/common -I$(ZSTDDIR)/compress \ -I$(ZSTDDIR)/dictBuilder -I$(ZSTDDIR)/deprecated -I$(PRGDIR) @@ -169,10 +169,9 @@ datagen : $(PRGDIR)/datagen.c datagencli.c roundTripCrash : $(ZSTD_FILES) roundTripCrash.c $(CC) $(FLAGS) $^ -o $@$(EXT) -OPAQUEFILES := $(ZSTD_FILES) $(ZDICT_FILES) roundTripCrashOpaque.c -roundTripCrashOpaque : LDFLAGS += $(MULTITHREAD_CPP) -roundTripCrashOpaque : LDFLAGS += $(MULTITHREAD_LD) -roundTripCrashOpaque : $(OPAQUEFILES) +cctxParamRoundTrip : LDFLAGS += $(MULTITHREAD_CPP) +cctxParamRoundTrip : LDFLAGS += $(MULTITHREAD_LD) +cctxParamRoundTrip : $(ZSTD_FILES) cctxParamRoundTrip.c $(CC) $(FLAGS) $^ -o $@$(EXT) longmatch : $(ZSTD_FILES) longmatch.c diff --git a/tests/roundTripCrashOpaque.c b/tests/cctxParamRoundTrip.c similarity index 98% rename from tests/roundTripCrashOpaque.c rename to tests/cctxParamRoundTrip.c index f9b6e7f83..45a57a151 100644 --- a/tests/roundTripCrashOpaque.c +++ b/tests/cctxParamRoundTrip.c @@ -38,13 +38,16 @@ fprintf(stderr, \ "Error=> %s: %s", \ #f, ZSTD_getErrorName(err)); \ - exit(1); \ + crash(1); \ } } /** roundTripTest() : * Compresses `srcBuff` into `compressedBuff`, * then decompresses `compressedBuff` into `resultBuff`. +* +* Parameters are currently set manually. +* * @return : result of decompression, which should be == `srcSize` * or an error code if either compression or decompression fails. * Note : `compressedBuffCapacity` should be `>= ZSTD_compressBound(srcSize)` diff --git a/tests/fullbench.c b/tests/fullbench.c index 0c41aa3d5..45ef2b6a3 100644 --- a/tests/fullbench.c +++ b/tests/fullbench.c @@ -136,10 +136,8 @@ size_t local_ZSTD_compressStream(void* dst, size_t dstCapacity, void* buff2, con buffIn.src = src; buffIn.size = srcSize; buffIn.pos = 0; - ZSTD_compressStream(g_cstream, &buffOut, &buffIn); ZSTD_endStream(g_cstream, &buffOut); - return buffOut.pos; } @@ -385,13 +383,11 @@ static size_t benchMem(const void* src, size_t srcSize, U32 benchNb) const BYTE* ip = dstBuff; const BYTE* iend; size_t frameHeaderSize, cBlockSize; - ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1); /* it would be better to use direct block compression here */ g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1); frameHeaderSize = ZSTD_getFrameHeader(&zfp, dstBuff, ZSTD_frameHeaderSize_min); if (frameHeaderSize==0) frameHeaderSize = ZSTD_frameHeaderSize_min; ip += frameHeaderSize; /* Skip frame Header */ - cBlockSize = ZSTD_getcBlockSize(ip, dstBuffSize, &bp); /* Get 1st block type */ if (bp.blockType != bt_compressed) { DISPLAY("ZSTD_decodeSeqHeaders : impossible to test on this sample (not compressible)\n"); @@ -399,7 +395,6 @@ static size_t benchMem(const void* src, size_t srcSize, U32 benchNb) } iend = ip + ZSTD_blockHeaderSize + cBlockSize; /* End of first block */ ip += ZSTD_blockHeaderSize; /* skip block header */ - ZSTD_decompressBegin(g_zdc); ip += ZSTD_decodeLiteralsBlock(g_zdc, ip, iend-ip); /* skip literal segment */ g_cSize = iend-ip; diff --git a/tests/zstreamtest.c b/tests/zstreamtest.c index 0281a406d..ce50925a1 100644 --- a/tests/zstreamtest.c +++ b/tests/zstreamtest.c @@ -1655,9 +1655,9 @@ static int fuzzerTests_newAPI_opaque(U32 seed, U32 nbTests, unsigned startTest, } if (FUZ_rand(&lseed) & 1) CHECK_Z (ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_forceMaxWindow, FUZ_rand(&lseed) & 1) ); - +#if 0 if (FUZ_rand(&lseed) & 1) CHECK_Z (ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_test, FUZ_rand(&lseed) & 1) ); - +#endif /* Apply parameters */ From 91b30dbe847d8cad72b81de33e99dab650cb0f29 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Mon, 21 Aug 2017 10:09:06 -0700 Subject: [PATCH 15/52] Remove test parameter --- lib/common/zstd_internal.h | 40 ++++++------ lib/compress/zstd_compress.c | 108 ++++++++++++++------------------- lib/compress/zstdmt_compress.c | 12 ++-- lib/zstd.h | 26 +++++--- programs/fileio.c | 8 --- programs/fileio.h | 3 - programs/zstdcli.c | 3 - tests/Makefile | 2 +- tests/cctxParamRoundTrip.c | 25 ++++---- tests/zstreamtest.c | 7 --- 10 files changed, 98 insertions(+), 136 deletions(-) diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index 8b8419a4c..ee3164183 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -236,27 +236,6 @@ typedef struct { U32 repToConfirm[ZSTD_REP_NUM]; } seqStore_t; -struct ZSTD_CCtx_params_s { - ZSTD_compressionParameters cParams; - ZSTD_frameParameters fParams; - - int compressionLevel; - U32 forceWindow; /* force back-references to respect limit of 1<seqStore); } - -/* TODO: get rid of this function if possible*/ -static ZSTD_CCtx_params ZSTD_makeCCtxParamsFromCParams( - ZSTD_compressionParameters cParams) -{ - ZSTD_CCtx_params cctxParams; - memset(&cctxParams, 0, sizeof(ZSTD_CCtx_params)); - cctxParams.cParams = cParams; - return cctxParams; -} - /* older variant; will be deprecated */ size_t ZSTD_setCCtxParameter(ZSTD_CCtx* cctx, ZSTD_CCtxParameter param, unsigned value) { @@ -239,7 +228,6 @@ size_t ZSTD_setCCtxParameter(ZSTD_CCtx* cctx, ZSTD_CCtxParameter param, unsigned } } - #define ZSTD_CLEVEL_CUSTOM 999 static void ZSTD_cLevelToCParams(ZSTD_CCtx* cctx) { @@ -250,6 +238,16 @@ static void ZSTD_cLevelToCParams(ZSTD_CCtx* cctx) cctx->requestedParams.compressionLevel = ZSTD_CLEVEL_CUSTOM; } +static ZSTD_CCtx_params ZSTD_makeCCtxParamsFromCParams( + ZSTD_compressionParameters cParams) +{ + ZSTD_CCtx_params cctxParams; + memset(&cctxParams, 0, sizeof(ZSTD_CCtx_params)); + cctxParams.cParams = cParams; + cctxParams.compressionLevel = ZSTD_CLEVEL_CUSTOM; + return cctxParams; +} + ZSTD_CCtx_params* ZSTD_createCCtxParams(void) { ZSTD_CCtx_params* params = @@ -287,10 +285,8 @@ size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params) static void ZSTD_cLevelToCCtxParams(ZSTD_CCtx_params* params) { if (params->compressionLevel == ZSTD_CLEVEL_CUSTOM) return; - // TODO: src size, code duplication params->cParams = ZSTD_getCParams(params->compressionLevel, 0, 0); params->compressionLevel = ZSTD_CLEVEL_CUSTOM; - } #define CLAMPCHECK(val,min,max) { \ @@ -305,7 +301,7 @@ size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned v switch(param) { case ZSTD_p_compressionLevel: - if (value == 0) return 0; + if (value == 0) return 0; /* special value : 0 means "don't change anything" */ if (cctx->cdict) return ERROR(stage_wrong); return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); @@ -316,9 +312,9 @@ size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned v case ZSTD_p_minMatch: case ZSTD_p_targetLength: case ZSTD_p_compressionStrategy: - if (value == 0) return 0; /* special value : 0 means "don't change anything" */ + if (value == 0) return 0; if (cctx->cdict) return ERROR(stage_wrong); - ZSTD_cLevelToCParams(cctx); + ZSTD_cLevelToCParams(cctx); /* Can optimize if srcSize is known */ return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); case ZSTD_p_contentSizeFlag: @@ -365,12 +361,6 @@ size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned v if (cctx->requestedParams.nbThreads <= 1) return ERROR(parameter_unsupported); assert(cctx->mtctx != NULL); return ZSTDMT_setMTCtxParameter(cctx->mtctx, ZSTDMT_p_overlapSectionLog, value); -#if 0 - case ZSTD_p_test : - DEBUGLOG(2, "Setting test parameter = %u", value); - cctx->requestedParams.testParam = (value > 0); - return 0; -#endif default: return ERROR(parameter_unsupported); } @@ -488,12 +478,6 @@ size_t ZSTD_CCtxParam_setParameter( if (params->nbThreads <= 1) { return ERROR(parameter_unsupported); } params->overlapSizeLog = value; return 0; -#if 0 - case ZSTD_p_test : - DEBUGLOG(2, "setting opaque: ZSTD_p_test: %u", value); - params->testParam = (value > 0); - return 0; -#endif default: return ERROR(parameter_unsupported); } @@ -525,10 +509,6 @@ static void ZSTD_debugPrintCCtxParams(ZSTD_CCtx_params* params) params->nbThreads, params->jobSize, params->overlapSizeLog); -#if 0 - DEBUGLOG(2, "testParam: %u", - params->testParam); -#endif } #endif @@ -537,6 +517,8 @@ static void ZSTD_debugPrintCCtxParams(ZSTD_CCtx_params* params) * Parameters are copied manually before the dictionary is loaded. * The multithreading parameters jobSize and overlapSizeLog are set only if * nbThreads >= 1. + * + * Pledged srcSize is treated as unknown. */ size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, ZSTD_CCtx_params* params) { @@ -564,10 +546,6 @@ size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, ZSTD_CCtx_params* params) cctx, ZSTD_p_overlapSizeLog, params->overlapSizeLog) ); } -#if 0 - /* Copy test parameter */ - cctx->requestedParams.testParam = params->testParam; -#endif return 0; } @@ -3748,7 +3726,6 @@ static ZSTD_CDict* ZSTD_createCDict_advanced_opaque( } } - ZSTD_CDict* ZSTD_createCDict_advanced(const void* dictBuffer, size_t dictSize, unsigned byReference, ZSTD_dictMode_e dictMode, ZSTD_compressionParameters cParams, ZSTD_customMem customMem) @@ -3791,32 +3768,36 @@ ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( size_t dictSize, ZSTD_CCtx_params* params) { - size_t const cctxSize = ZSTD_estimateCCtxSize_advanced_opaque(params); - size_t const neededSize = sizeof(ZSTD_CDict) + (params->dictContentByRef ? 0 : dictSize) - + cctxSize; - ZSTD_CDict* const cdict = (ZSTD_CDict*) workspace; - void* ptr; - DEBUGLOG(5, "(size_t)workspace & 7 : %u", (U32)(size_t)workspace & 7); - if ((size_t)workspace & 7) return NULL; /* 8-aligned */ - DEBUGLOG(5, "(workspaceSize < neededSize) : (%u < %u) => %u", - (U32)workspaceSize, (U32)neededSize, (U32)(workspaceSize < neededSize)); - if (workspaceSize < neededSize) return NULL; + if (params == NULL) { return NULL; } + { ZSTD_CCtx_params cctxParams = *params; + size_t const cctxSize = ZSTD_estimateCCtxSize_advanced_opaque(params); + size_t const neededSize = sizeof(ZSTD_CDict) + + (cctxParams.dictContentByRef ? 0 : dictSize) + + cctxSize; + ZSTD_CDict* const cdict = (ZSTD_CDict*) workspace; + void* ptr; + DEBUGLOG(5, "(size_t)workspace & 7 : %u", (U32)(size_t)workspace & 7); + if ((size_t)workspace & 7) return NULL; /* 8-aligned */ + DEBUGLOG(5, "(workspaceSize < neededSize) : (%u < %u) => %u", + (U32)workspaceSize, (U32)neededSize, (U32)(workspaceSize < neededSize)); + if (workspaceSize < neededSize) return NULL; - if (!params->dictContentByRef) { - memcpy(cdict+1, dict, dictSize); - dict = cdict+1; - ptr = (char*)workspace + sizeof(ZSTD_CDict) + dictSize; - } else { - ptr = cdict+1; + if (!cctxParams.dictContentByRef) { + memcpy(cdict+1, dict, dictSize); + dict = cdict+1; + ptr = (char*)workspace + sizeof(ZSTD_CDict) + dictSize; + } else { + ptr = cdict+1; + } + cdict->refContext = ZSTD_initStaticCCtx(ptr, cctxSize); + cctxParams.dictContentByRef = 1; + + /* What if nbThreads > 1? */ + if (ZSTD_isError( ZSTD_initCDict_internal_opaque(cdict, dict, dictSize, cctxParams) )) + return NULL; + + return cdict; } - cdict->refContext = ZSTD_initStaticCCtx(ptr, cctxSize); - params->dictContentByRef = 1; - - /* What if nbThreads > 1? */ - if (ZSTD_isError( ZSTD_initCDict_internal_opaque(cdict, dict, dictSize, *params) )) - return NULL; - - return cdict; } /*! ZSTD_initStaticCDict_advanced() : @@ -3861,8 +3842,7 @@ size_t ZSTD_compressBegin_usingCDict_advanced( ZSTD_frameParameters const fParams, unsigned long long const pledgedSrcSize) { if (cdict==NULL) return ERROR(dictionary_wrong); - { - ZSTD_CCtx_params params = cdict->refContext->appliedParams; + { ZSTD_CCtx_params params = cdict->refContext->appliedParams; params.fParams = fParams; params.dictMode = ZSTD_dm_auto; DEBUGLOG(5, "ZSTD_compressBegin_usingCDict_advanced"); diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index 76a062f04..84f42220c 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -188,7 +188,8 @@ static void ZSTDMT_releaseBuffer(ZSTDMT_bufferPool* bufPool, buffer_t buf) /** * TODO - * Resets parameters to zero for jobs? + * + * Sets parameters to zero for jobs. Notably, nbThreads should be zero? */ static void ZSTDMT_zeroCCtxParams(ZSTD_CCtx_params* params) { @@ -564,7 +565,6 @@ static size_t ZSTDMT_compress_advanced_opaque( DEBUGLOG(4, "nbChunks : %2u (chunkSize : %u bytes) ", nbChunks, (U32)avgChunkSize); if (nbChunks==1) { /* fallback to single-thread mode */ ZSTD_CCtx* const cctx = mtctx->cctxPool->cctx[0]; - if (cdict) return ZSTD_compress_usingCDict_advanced(cctx, dst, dstCapacity, src, srcSize, cdict, cctxParams.fParams); return ZSTD_compress_advanced_opaque(cctx, dst, dstCapacity, src, srcSize, NULL, 0, requestedParams); } @@ -664,7 +664,6 @@ static size_t ZSTDMT_compress_advanced_opaque( if (!error) DEBUGLOG(4, "compressed size : %u ", (U32)dstPos); return error ? error : dstPos; } - } size_t ZSTDMT_compress_advanced(ZSTDMT_CCtx* mtctx, @@ -750,6 +749,7 @@ size_t ZSTDMT_initCStream_internal_opaque( if (dict) { DEBUGLOG(4,"cdictLocal: %08X", (U32)(size_t)zcs->cdictLocal); ZSTD_freeCDict(zcs->cdictLocal); + /* TODO: This will need a cctxParam version? */ zcs->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, 0 /* byRef */, ZSTD_dm_auto, /* note : a loadPrefix becomes an internal CDict */ params.cParams, zcs->cMem); @@ -779,12 +779,10 @@ size_t ZSTDMT_initCStream_internal_opaque( zcs->allJobsCompleted = 0; if (params.fParams.checksumFlag) XXH64_reset(&zcs->xxhState, 0); return 0; - } -/** ZSTDMT_initCStream_internal() : - * internal usage only */ -size_t ZSTDMT_initCStream_internal(ZSTDMT_CCtx* zcs, +/** ZSTDMT_initCStream_internal() */ +static size_t ZSTDMT_initCStream_internal(ZSTDMT_CCtx* zcs, const void* dict, size_t dictSize, const ZSTD_CDict* cdict, ZSTD_parameters params, unsigned long long pledgedSrcSize) { diff --git a/lib/zstd.h b/lib/zstd.h index f02f28c82..bbecd5ea2 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -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. - * TODO: ZSTD_estimateCCtxSize_advanced_opaque() + * ZSTD_estimateCCtxSize_advanced_opaque() can be used in tandem with ZSTD_CCtxParam_setParameter(). * 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. - * TODO: ZSTD_estimateCStreamSize_advanced_opaque() + * ZSTD_estimateCStreamSize_advanced_opaque() can be used in tandem with ZSTD_CCtxParam_setParameter(). * 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, @@ -527,7 +527,7 @@ ZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t sr /*! ZSTD_estimate?DictSize() : * ZSTD_estimateCDictSize() will bet that src size is relatively "small", and content is copied, like ZSTD_createCDict(). * ZSTD_estimateCStreamSize_advanced() makes it possible to control precisely compression parameters, like ZSTD_createCDict_advanced(). - * TODO: ZSTD_estimateCDictSize_advanced_opaque() + * ZSTD_estimateCDictSize_advanced_opaque() allows further compression parameters. ByReference can be set with ZSTD_CCtxParam_setParameter. * Note : dictionary created "byReference" are smaller */ ZSTDLIB_API size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel); ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, unsigned byReference); @@ -613,7 +613,10 @@ ZSTDLIB_API ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( const void* dict, size_t dictSize, ZSTD_CCtx_params* params); +/* TODO */ ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void); +/*! ZSTD_resetCCtxParams() : + * Reset params to default, with the default compression level. */ ZSTDLIB_API size_t ZSTD_resetCCtxParams(ZSTD_CCtx_params* params); ZSTDLIB_API size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params); ZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params); @@ -1000,9 +1003,6 @@ typedef enum { /* advanced parameters - may not remain available after API update */ ZSTD_p_forceMaxWindow=1100, /* Force back-reference distances to remain < windowSize, * even when referencing into Dictionary content (default:0) */ -#if 0 - ZSTD_p_test, -#endif } ZSTD_cParameter; @@ -1013,9 +1013,19 @@ typedef enum { * @result : 0, or an error code (which can be tested with ZSTD_isError()). */ ZSTDLIB_API size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned value); -/* TODO */ +/*! ZSTD_CCtxParam_setParameter() : + * Similar to ZSTD_CCtx_setParameter. + * Set one compression parameter, selected by enum ZSTD_cParameter. + * Parameters must be applied to a ZSTD_CCtx using ZSTD_CCtx_applyCCtxParams(). + * Note : when `value` is an enum, cast it to unsigned for proper type checking. + * @result : 0, or an error code (which can be tested with ZSTD_isError()). */ ZSTDLIB_API size_t ZSTD_CCtxParam_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, unsigned value); -/* TODO */ + +/*! ZSTD_CCtx_applyCCtxParams() : + * Apply a set of ZSTD_CCtx_params to the compression context. + * This must be done before the dictionary is loaded. + * The pledgedSrcSize is treated as unknown. + * Multithreading parameters are applied only if nbThreads > 1. */ ZSTDLIB_API size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, ZSTD_CCtx_params* params); /*! ZSTD_CCtx_setPledgedSrcSize() : diff --git a/programs/fileio.c b/programs/fileio.c index 1fb249d6b..669052d8b 100644 --- a/programs/fileio.c +++ b/programs/fileio.c @@ -213,10 +213,6 @@ void FIO_setOverlapLog(unsigned overlapLog){ DISPLAYLEVEL(2, "Setting overlapLog is useless in single-thread mode \n"); g_overlapLog = overlapLog; } -#if 0 -static U32 g_testParamFlag = 0; -void FIO_setTestParamFlag(unsigned testParamFlag) { g_testParamFlag = testParamFlag; } -#endif /*-************************************* * Functions @@ -414,10 +410,6 @@ static cRess_t FIO_createCResources(const char* dictFileName, int cLevel, CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_p_nbThreads, g_nbThreads) ); /* dictionary */ CHECK( ZSTD_CCtx_loadDictionary(ress.cctx, dictBuffer, dictBuffSize) ); -#if 0 - /* Test */ - CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_p_test, g_testParamFlag) ); -#endif } #elif defined(ZSTD_MULTITHREAD) { ZSTD_parameters params = ZSTD_getParams(cLevel, srcSize, dictBuffSize); diff --git a/programs/fileio.h b/programs/fileio.h index 497e122e1..9d9167df9 100644 --- a/programs/fileio.h +++ b/programs/fileio.h @@ -56,9 +56,6 @@ void FIO_setMemLimit(unsigned memLimit); void FIO_setNbThreads(unsigned nbThreads); void FIO_setBlockSize(unsigned blockSize); void FIO_setOverlapLog(unsigned overlapLog); -#if 0 -void FIO_setTestParamFlag(unsigned testParamFlag); -#endif /*-************************************* diff --git a/programs/zstdcli.c b/programs/zstdcli.c index 5e95e4268..b1268c1f3 100644 --- a/programs/zstdcli.c +++ b/programs/zstdcli.c @@ -430,9 +430,6 @@ int main(int argCount, const char* argv[]) if (!strcmp(argument, "--keep")) { FIO_setRemoveSrcFile(0); continue; } if (!strcmp(argument, "--rm")) { FIO_setRemoveSrcFile(1); continue; } if (!strcmp(argument, "--priority=rt")) { setRealTimePrio = 1; continue; } -#if 0 - if (!strcmp(argument, "--testParam")) { FIO_setTestParamFlag(1); continue; } -#endif #ifdef ZSTD_GZCOMPRESS if (!strcmp(argument, "--format=gzip")) { suffix = GZ_EXTENSION; FIO_setCompressionType(FIO_gzipCompression); continue; } #endif diff --git a/tests/Makefile b/tests/Makefile index e815f5850..228f4cdd2 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -217,7 +217,7 @@ clean: fuzzer$(EXT) fuzzer32$(EXT) zbufftest$(EXT) zbufftest32$(EXT) \ fuzzer-dll$(EXT) zstreamtest-dll$(EXT) zbufftest-dll$(EXT)\ zstreamtest$(EXT) zstreamtest32$(EXT) \ - datagen$(EXT) paramgrill$(EXT) roundTripCrash$(EXT) longmatch$(EXT) \ + datagen$(EXT) paramgrill$(EXT) roundTripCrash$(EXT) cctxParamRoundTrip$(EXT) longmatch$(EXT) \ symbols$(EXT) invalidDictionaries$(EXT) legacy$(EXT) poolTests$(EXT) \ decodecorpus$(EXT) @echo Cleaning completed diff --git a/tests/cctxParamRoundTrip.c b/tests/cctxParamRoundTrip.c index 45a57a151..906ad9cec 100644 --- a/tests/cctxParamRoundTrip.c +++ b/tests/cctxParamRoundTrip.c @@ -32,6 +32,15 @@ *==========================================*/ #define MIN(a,b) ( (a) < (b) ? (a) : (b) ) +static void crash(int errorCode){ + /* abort if AFL/libfuzzer, exit otherwise */ + #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION /* could also use __AFL_COMPILER */ + abort(); + #else + exit(errorCode); + #endif +} + #define CHECK_Z(f) { \ size_t const err = f; \ if (ZSTD_isError(err)) { \ @@ -41,7 +50,6 @@ crash(1); \ } } - /** roundTripTest() : * Compresses `srcBuff` into `compressedBuff`, * then decompresses `compressedBuff` into `resultBuff`. @@ -61,10 +69,8 @@ static size_t roundTripTest(void* resultBuff, size_t resultBuffCapacity, ZSTD_inBuffer inBuffer = { srcBuff, srcBuffSize, 0 }; ZSTD_outBuffer outBuffer = {compressedBuff, compressedBuffCapacity, 0 }; - ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_compressionLevel, 1); - ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_test, 1); - - ZSTD_CCtx_applyCCtxParams(cctx, cctxParams); + CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_compressionLevel, 1) ); + CHECK_Z( ZSTD_CCtx_applyCCtxParams(cctx, cctxParams) ); CHECK_Z (ZSTD_compress_generic(cctx, &outBuffer, &inBuffer, ZSTD_e_end) ); @@ -88,15 +94,6 @@ static size_t checkBuffers(const void* buff1, const void* buff2, size_t buffSize return pos; } -static void crash(int errorCode){ - /* abort if AFL/libfuzzer, exit otherwise */ - #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION /* could also use __AFL_COMPILER */ - abort(); - #else - exit(errorCode); - #endif -} - static void roundTripCheck(const void* srcBuff, size_t srcBuffSize) { size_t const cBuffSize = ZSTD_compressBound(srcBuffSize); diff --git a/tests/zstreamtest.c b/tests/zstreamtest.c index ce50925a1..ced9c57ff 100644 --- a/tests/zstreamtest.c +++ b/tests/zstreamtest.c @@ -1655,12 +1655,8 @@ static int fuzzerTests_newAPI_opaque(U32 seed, U32 nbTests, unsigned startTest, } if (FUZ_rand(&lseed) & 1) CHECK_Z (ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_forceMaxWindow, FUZ_rand(&lseed) & 1) ); -#if 0 - if (FUZ_rand(&lseed) & 1) CHECK_Z (ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_test, FUZ_rand(&lseed) & 1) ); -#endif /* Apply parameters */ - CHECK_Z (ZSTD_CCtx_applyCCtxParams(zc, cctxParams) ); if (FUZ_rand(&lseed) & 1) { @@ -1795,9 +1791,6 @@ _output_error: goto _cleanup; } - - - /*-******************************************************* * Command line *********************************************************/ From 502031ca10f07be4981f07cb6a7b87d8d1211806 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Mon, 21 Aug 2017 11:00:44 -0700 Subject: [PATCH 16/52] Use cctxParam version of createCDict internally --- lib/common/zstd_internal.h | 5 +++++ lib/compress/zstd_compress.c | 12 +++++++----- lib/compress/zstdmt_compress.c | 18 ++++++++---------- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index ee3164183..65719f541 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -360,6 +360,11 @@ size_t ZSTD_initCStream_internal_opaque( ZSTD_CCtx_params params, unsigned long long pledgedSrcSize); +/* INTERNAL */ +ZSTD_CDict* ZSTD_createCDict_advanced_opaque( + const void* dictBuffer, size_t dictSize, + ZSTD_CCtx_params params, ZSTD_customMem customMem); + /*! ZSTD_compressStream_generic() : * Private use only. To be called from zstdmt_compress.c in single-thread mode. */ size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs, diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index ee0365ca3..265be326f 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -3698,7 +3698,8 @@ static size_t ZSTD_initCDict_internal( } #endif -static ZSTD_CDict* ZSTD_createCDict_advanced_opaque( +/* Internal only */ +ZSTD_CDict* ZSTD_createCDict_advanced_opaque( const void* dictBuffer, size_t dictSize, ZSTD_CCtx_params params, ZSTD_customMem customMem) { @@ -3715,6 +3716,7 @@ static ZSTD_CDict* ZSTD_createCDict_advanced_opaque( } cdict->refContext = cctx; + /* TODO: What should be zero? */ if (ZSTD_isError( ZSTD_initCDict_internal_opaque( cdict, dictBuffer, dictSize, @@ -3992,10 +3994,10 @@ size_t ZSTD_initCStream_internal_opaque( return ERROR(memory_allocation); } ZSTD_freeCDict(zcs->cdictLocal); - zcs->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, - params.dictContentByRef, - params.dictMode, - params.cParams, zcs->customMem); + /* TODO opaque version: what needs to be zero? */ + zcs->cdictLocal = ZSTD_createCDict_advanced_opaque( + dict, dictSize, + params, zcs->customMem); zcs->cdict = zcs->cdictLocal; if (zcs->cdictLocal == NULL) return ERROR(memory_allocation); } else { diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index 84f42220c..e7a05f4b1 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -195,6 +195,7 @@ static void ZSTDMT_zeroCCtxParams(ZSTD_CCtx_params* params) { params->forceWindow = 0; params->dictMode = (ZSTD_dictMode_e)(0); + params->dictContentByRef = 0; params->nbThreads = 0; params->jobSize = 0; params->overlapSizeLog = 0; @@ -719,13 +720,9 @@ size_t ZSTDMT_initCStream_internal_opaque( const ZSTD_CDict* cdict, ZSTD_CCtx_params cctxParams, unsigned long long pledgedSrcSize) { - ZSTD_parameters params; - params.cParams = cctxParams.cParams; - params.fParams = cctxParams.fParams; - DEBUGLOG(4, "ZSTDMT_initCStream_internal"); /* params are supposed to be fully validated at this point */ - assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); + assert(!ZSTD_isError(ZSTD_checkCParams(cctxParams.cParams))); assert(!((dict) && (cdict))); /* either dict or cdict, not both */ /* TODO: Set stuff to 0 to preserve old semantics. */ @@ -749,10 +746,11 @@ size_t ZSTDMT_initCStream_internal_opaque( if (dict) { DEBUGLOG(4,"cdictLocal: %08X", (U32)(size_t)zcs->cdictLocal); ZSTD_freeCDict(zcs->cdictLocal); - /* TODO: This will need a cctxParam version? */ - zcs->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, - 0 /* byRef */, ZSTD_dm_auto, /* note : a loadPrefix becomes an internal CDict */ - params.cParams, zcs->cMem); + /* TODO: cctxParam version? Is this correct? + * by reference should be zero, mode should be ZSTD_dm_auto */ + zcs->cdictLocal = ZSTD_createCDict_advanced_opaque( + dict, dictSize, + cctxParams, zcs->cMem); zcs->cdict = zcs->cdictLocal; if (zcs->cdictLocal == NULL) return ERROR(memory_allocation); } else { @@ -777,7 +775,7 @@ size_t ZSTDMT_initCStream_internal_opaque( zcs->nextJobID = 0; zcs->frameEnded = 0; zcs->allJobsCompleted = 0; - if (params.fParams.checksumFlag) XXH64_reset(&zcs->xxhState, 0); + if (cctxParams.fParams.checksumFlag) XXH64_reset(&zcs->xxhState, 0); return 0; } From 25be09c6b47dc4cff63bfe447a1b3499e00d66e6 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Mon, 21 Aug 2017 11:34:53 -0700 Subject: [PATCH 17/52] Set some parameters to zero before initializing cdict --- lib/compress/zstd_compress.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 265be326f..530611dd5 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -3708,15 +3708,12 @@ ZSTD_CDict* ZSTD_createCDict_advanced_opaque( { ZSTD_CDict* const cdict = (ZSTD_CDict*)ZSTD_malloc(sizeof(ZSTD_CDict), customMem); ZSTD_CCtx* const cctx = ZSTD_createCCtx_advanced(customMem); - if (!cdict || !cctx) { ZSTD_free(cdict, customMem); ZSTD_freeCCtx(cctx); return NULL; } cdict->refContext = cctx; - - /* TODO: What should be zero? */ if (ZSTD_isError( ZSTD_initCDict_internal_opaque( cdict, dictBuffer, dictSize, @@ -4256,7 +4253,7 @@ size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuf return ZSTD_compressStream_generic(zcs, output, input, ZSTD_e_continue); } -/*! ZSTDMT_initCStream_internal() : +/*! ZSTDMT_initCStream_internal_opaque() : * Private use only. Init streaming operation. * expects params to be valid. * must receive dict, or cdict, or none, but not both. From 560b34f6d2c305fc91cf08838c807f13497a261c Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Mon, 21 Aug 2017 11:44:58 -0700 Subject: [PATCH 18/52] Return error code when initializing NULL cctxParams --- lib/compress/zstd_compress.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 530611dd5..69e977890 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -268,6 +268,7 @@ size_t ZSTD_resetCCtxParams(ZSTD_CCtx_params* params) size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params) { + if (!cctxParams) { return ERROR(GENERIC); } memset(cctxParams, 0, sizeof(ZSTD_CCtx_params)); cctxParams->cParams = params.cParams; cctxParams->fParams = params.fParams; From 73c73bf16a0e9aafe0776144469b1bbf9f26b193 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Mon, 21 Aug 2017 12:41:19 -0700 Subject: [PATCH 19/52] Reduce code duplication in zstreamtest --- lib/zstd.h | 2 +- tests/zstreamtest.c | 73 +++++++++++++++++++++++++++++---------------- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/lib/zstd.h b/lib/zstd.h index bbecd5ea2..a88382e5c 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -1002,7 +1002,7 @@ typedef enum { /* advanced parameters - may not remain available after API update */ ZSTD_p_forceMaxWindow=1100, /* Force back-reference distances to remain < windowSize, - * even when referencing into Dictionary content (default:0) */ + * even when referencing into Dictionary content (default:0) */ } ZSTD_cParameter; diff --git a/tests/zstreamtest.c b/tests/zstreamtest.c index ced9c57ff..8f974b461 100644 --- a/tests/zstreamtest.c +++ b/tests/zstreamtest.c @@ -1206,6 +1206,7 @@ _output_error: /* Tests for ZSTD_compress_generic() API */ +#if 0 static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests) { U32 const maxSrcLog = bigTests ? 24 : 22; @@ -1493,8 +1494,22 @@ _output_error: result = 1; goto _cleanup; } +#endif -static int fuzzerTests_newAPI_opaque(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests) +/** If useOpaqueAPI, sets param in cctxParams. + * Otherwise, sets the param in zc. */ +static size_t setCCtxParameter(ZSTD_CCtx* zc, ZSTD_CCtx_params* cctxParams, + ZSTD_cParameter param, unsigned value, + U32 useOpaqueAPI) +{ + if (useOpaqueAPI) { + return ZSTD_CCtxParam_setParameter(cctxParams, param, value); + } else { + return ZSTD_CCtx_setParameter(zc, param, value); + } +} + +static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests, U32 const useOpaqueAPI) { U32 const maxSrcLog = bigTests ? 24 : 22; static const U32 maxSampleLog = 19; @@ -1597,7 +1612,7 @@ static int fuzzerTests_newAPI_opaque(U32 seed, U32 nbTests, unsigned startTest, maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2); if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1; { int const compressionLevel = (FUZ_rand(&lseed) % 5) + 1; - CHECK_Z (ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_compressionLevel, compressionLevel)); + CHECK_Z (setCCtxParameter(zc, cctxParams, ZSTD_p_compressionLevel, compressionLevel, useOpaqueAPI) ); } } else { U32 const testLog = FUZ_rand(&lseed) % maxSrcLog; @@ -1627,45 +1642,53 @@ static int fuzzerTests_newAPI_opaque(U32 seed, U32 nbTests, unsigned startTest, cParams.targetLength = (U32)(cParams.targetLength * (0.5 + ((double)(FUZ_rand(&lseed) & 127) / 128))); cParams = ZSTD_adjustCParams(cParams, 0, 0); - if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_windowLog, cParams.windowLog) ); - if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_hashLog, cParams.hashLog) ); - if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_chainLog, cParams.chainLog) ); - if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_searchLog, cParams.searchLog) ); - if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_minMatch, cParams.searchLength) ); - if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_targetLength, cParams.targetLength) ); + if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_windowLog, cParams.windowLog, useOpaqueAPI) ); + if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_hashLog, cParams.hashLog, useOpaqueAPI) ); + if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_chainLog, cParams.chainLog, useOpaqueAPI) ); + if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_searchLog, cParams.searchLog, useOpaqueAPI) ); + if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_minMatch, cParams.searchLength, useOpaqueAPI) ); + if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_targetLength, cParams.targetLength, useOpaqueAPI) ); /* unconditionally set, to be sync with decoder */ /* mess with frame parameters */ - if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_checksumFlag, FUZ_rand(&lseed) & 1) ); - if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_dictIDFlag, FUZ_rand(&lseed) & 1) ); - if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_contentSizeFlag, FUZ_rand(&lseed) & 1) ); + if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_checksumFlag, FUZ_rand(&lseed) & 1, useOpaqueAPI) ); + if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_dictIDFlag, FUZ_rand(&lseed) & 1, useOpaqueAPI) ); + if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_contentSizeFlag, FUZ_rand(&lseed) & 1, useOpaqueAPI) ); if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setPledgedSrcSize(zc, pledgedSrcSize) ); DISPLAYLEVEL(5, "pledgedSrcSize : %u \n", (U32)pledgedSrcSize); - if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_refDictContent, FUZ_rand(&lseed) & 1) ); + if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_refDictContent, FUZ_rand(&lseed) & 1, useOpaqueAPI) ); /* multi-threading parameters */ { U32 const nbThreadsCandidate = (FUZ_rand(&lseed) & 4) + 1; U32 const nbThreads = MIN(nbThreadsCandidate, nbThreadsMax); - CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_nbThreads, nbThreads) ); + CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_nbThreads, nbThreads, useOpaqueAPI) ); if (nbThreads > 1) { U32 const jobLog = FUZ_rand(&lseed) % (testLog+1); - CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_overlapSizeLog, FUZ_rand(&lseed) % 10) ); - CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_jobSize, (U32)FUZ_rLogLength(&lseed, jobLog)) ); + CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_overlapSizeLog, FUZ_rand(&lseed) % 10, useOpaqueAPI) ); + CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_jobSize, (U32)FUZ_rLogLength(&lseed, jobLog), useOpaqueAPI) ); } } - if (FUZ_rand(&lseed) & 1) CHECK_Z (ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_forceMaxWindow, FUZ_rand(&lseed) & 1) ); + if (FUZ_rand(&lseed) & 1) CHECK_Z (setCCtxParameter(zc, cctxParams, ZSTD_p_forceMaxWindow, FUZ_rand(&lseed) & 1, useOpaqueAPI) ); /* Apply parameters */ - CHECK_Z (ZSTD_CCtx_applyCCtxParams(zc, cctxParams) ); + if (useOpaqueAPI) { + CHECK_Z (ZSTD_CCtx_applyCCtxParams(zc, cctxParams) ); + } if (FUZ_rand(&lseed) & 1) { CHECK_Z( ZSTD_CCtx_loadDictionary(zc, dict, dictSize) ); if (dict && dictSize) { /* test that compression parameters are rejected (correctly) after loading a non-NULL dictionary */ - size_t const setError = ZSTD_CCtx_applyCCtxParams(zc, cctxParams); - CHECK(!ZSTD_isError(setError), "ZSTD_CCtx_applyCCtxParams should have failed"); - } } else { + if (useOpaqueAPI) { + size_t const setError = ZSTD_CCtx_applyCCtxParams(zc, cctxParams); + CHECK(!ZSTD_isError(setError), "ZSTD_CCtx_applyCCtxParams should have failed"); + } else { + size_t const setError = ZSTD_CCtx_setParameter(zc, ZSTD_p_windowLog, cParams.windowLog-1); + CHECK(!ZSTD_isError(setError), "ZSTD_CCtx_setParameter should have failed"); + } + } + } else { CHECK_Z( ZSTD_CCtx_refPrefix(zc, dict, dictSize) ); } } } @@ -1810,7 +1833,7 @@ int FUZ_usage(const char* programName) return 0; } -typedef enum { simple_api, mt_api, advanced_api, advanced_api_opaque } e_api; +typedef enum { simple_api, mt_api, advanced_api } e_api; int main(int argc, const char** argv) { @@ -1826,6 +1849,7 @@ int main(int argc, const char** argv) e_api selected_api = simple_api; const char* const programName = argv[0]; ZSTD_customMem const customNULL = ZSTD_defaultCMem; + U32 useOpaqueAPI = 0; /* Check command line */ for(argNb=1; argNb Date: Mon, 21 Aug 2017 12:57:18 -0700 Subject: [PATCH 20/52] Pass ZSTD_CCtx_params as const ptr when possible --- lib/compress/zstd_compress.c | 18 +++++++++--------- lib/zstd.h | 10 +++++----- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 69e977890..b6774e947 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -521,7 +521,7 @@ static void ZSTD_debugPrintCCtxParams(ZSTD_CCtx_params* params) * * Pledged srcSize is treated as unknown. */ -size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, ZSTD_CCtx_params* params) +size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params) { if (params == NULL) { return ERROR(GENERIC); } if (cctx->cdict) { return ERROR(stage_wrong); } @@ -693,10 +693,10 @@ ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, u return ZSTD_adjustCParams_internal(cPar, srcSize, dictSize); } -size_t ZSTD_estimateCCtxSize_advanced_opaque(ZSTD_CCtx_params* params) +size_t ZSTD_estimateCCtxSize_advanced_opaque(const ZSTD_CCtx_params* params) { if (params == NULL) { return 0; } - { ZSTD_compressionParameters cParams = params->cParams; + { 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; @@ -725,7 +725,7 @@ size_t ZSTD_estimateCCtxSize_advanced_opaque(ZSTD_CCtx_params* params) size_t ZSTD_estimateCCtxSize_advanced(ZSTD_compressionParameters cParams) { - ZSTD_CCtx_params params = ZSTD_makeCCtxParamsFromCParams(cParams); + ZSTD_CCtx_params const params = ZSTD_makeCCtxParamsFromCParams(cParams); return ZSTD_estimateCCtxSize_advanced_opaque(¶ms); } @@ -735,7 +735,7 @@ size_t ZSTD_estimateCCtxSize(int compressionLevel) return ZSTD_estimateCCtxSize_advanced(cParams); } -size_t ZSTD_estimateCStreamSize_advanced_opaque(ZSTD_CCtx_params* params) +size_t ZSTD_estimateCStreamSize_advanced_opaque(const ZSTD_CCtx_params* params) { if (params == NULL) { return 0; } { size_t const CCtxSize = ZSTD_estimateCCtxSize_advanced_opaque(params); @@ -750,7 +750,7 @@ size_t ZSTD_estimateCStreamSize_advanced_opaque(ZSTD_CCtx_params* params) size_t ZSTD_estimateCStreamSize_advanced(ZSTD_compressionParameters cParams) { - ZSTD_CCtx_params params = ZSTD_makeCCtxParamsFromCParams(cParams); + ZSTD_CCtx_params const params = ZSTD_makeCCtxParamsFromCParams(cParams); return ZSTD_estimateCStreamSize_advanced_opaque(¶ms); } @@ -3608,7 +3608,7 @@ size_t ZSTD_compress(void* dst, size_t dstCapacity, const void* src, size_t srcS /* ===== Dictionary API ===== */ size_t ZSTD_estimateCDictSize_advanced_opaque( - size_t dictSize, ZSTD_CCtx_params* params) + size_t dictSize, const ZSTD_CCtx_params* params) { if (params == NULL) { return 0; } DEBUGLOG(5, "sizeof(ZSTD_CDict) : %u", (U32)sizeof(ZSTD_CDict)); @@ -3766,7 +3766,7 @@ size_t ZSTD_freeCDict(ZSTD_CDict* cdict) ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( void *workspace, size_t workspaceSize, const void* dict, size_t dictSize, - ZSTD_CCtx_params* params) + const ZSTD_CCtx_params* params) { if (params == NULL) { return NULL; } { ZSTD_CCtx_params cctxParams = *params; @@ -3928,7 +3928,7 @@ static size_t ZSTD_resetCStream_internal_opaque( ZSTD_CStream* zcs, const void* dict, size_t dictSize, const ZSTD_CDict* cdict, - ZSTD_CCtx_params params, unsigned long long pledgedSrcSize) + const ZSTD_CCtx_params params, unsigned long long pledgedSrcSize) { DEBUGLOG(4, "ZSTD_resetCStream_internal"); /* params are supposed to be fully validated at this point */ diff --git a/lib/zstd.h b/lib/zstd.h index a88382e5c..0fd97cd6f 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -502,7 +502,7 @@ ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict); * 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); -ZSTDLIB_API size_t ZSTD_estimateCCtxSize_advanced_opaque(ZSTD_CCtx_params* params); +ZSTDLIB_API size_t ZSTD_estimateCCtxSize_advanced_opaque(const ZSTD_CCtx_params* params); ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void); /*! ZSTD_estimate?StreamSize() : @@ -520,7 +520,7 @@ ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void); * In this case, get total size by adding ZSTD_estimate?DictSize */ ZSTDLIB_API size_t ZSTD_estimateCStreamSize(int compressionLevel); ZSTDLIB_API size_t ZSTD_estimateCStreamSize_advanced(ZSTD_compressionParameters cParams); -ZSTDLIB_API size_t ZSTD_estimateCStreamSize_advanced_opaque(ZSTD_CCtx_params* params); +ZSTDLIB_API size_t ZSTD_estimateCStreamSize_advanced_opaque(const ZSTD_CCtx_params* params); ZSTDLIB_API size_t ZSTD_estimateDStreamSize(size_t windowSize); ZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize); @@ -531,7 +531,7 @@ ZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t sr * Note : dictionary created "byReference" are smaller */ ZSTDLIB_API size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel); ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, unsigned byReference); -ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced_opaque(size_t dictSize, ZSTD_CCtx_params* params); +ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced_opaque(size_t dictSize, const ZSTD_CCtx_params* params); ZSTDLIB_API size_t ZSTD_estimateDDictSize(size_t dictSize, unsigned byReference); @@ -611,7 +611,7 @@ ZSTDLIB_API ZSTD_CDict* ZSTD_initStaticCDict( ZSTDLIB_API ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( void* workspace, size_t workspaceSize, const void* dict, size_t dictSize, - ZSTD_CCtx_params* params); + const ZSTD_CCtx_params* params); /* TODO */ ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void); @@ -1026,7 +1026,7 @@ ZSTDLIB_API size_t ZSTD_CCtxParam_setParameter(ZSTD_CCtx_params* params, ZSTD_cP * This must be done before the dictionary is loaded. * The pledgedSrcSize is treated as unknown. * Multithreading parameters are applied only if nbThreads > 1. */ -ZSTDLIB_API size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, ZSTD_CCtx_params* params); +ZSTDLIB_API size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params); /*! ZSTD_CCtx_setPledgedSrcSize() : * Total input data size to be compressed as a single frame. From 1c0dbe81b17addc1f48be895326124158ea5a2c3 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Mon, 21 Aug 2017 13:18:00 -0700 Subject: [PATCH 21/52] Add documentation for CCtx_params --- lib/compress/zstd_compress.c | 8 +++-- lib/zstd.h | 60 +++++++++++++++++++++++------------- 2 files changed, 44 insertions(+), 24 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index b6774e947..c37b2ad49 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -471,12 +471,12 @@ size_t ZSTD_CCtxParam_setParameter( return 0; case ZSTD_p_jobSize : - if (params->nbThreads <= 1) { return ERROR(parameter_unsupported); } + if (params->nbThreads <= 1) return ERROR(parameter_unsupported); params->jobSize = value; return 0; case ZSTD_p_overlapSizeLog : - if (params->nbThreads <= 1) { return ERROR(parameter_unsupported); } + if (params->nbThreads <= 1) return ERROR(parameter_unsupported); params->overlapSizeLog = value; return 0; @@ -526,6 +526,10 @@ size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params if (params == NULL) { return ERROR(GENERIC); } if (cctx->cdict) { return ERROR(stage_wrong); } + /* TODO: some parameters can be set even if cctx->cdict. + * They can be set directly using ZSTD_CCtx_setParameter? + */ + /* Assume the compression and frame parameters are validated */ cctx->requestedParams.cParams = params->cParams; cctx->requestedParams.fParams = params->fParams; diff --git a/lib/zstd.h b/lib/zstd.h index 0fd97cd6f..09b89911f 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -613,13 +613,7 @@ ZSTDLIB_API ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( const void* dict, size_t dictSize, const ZSTD_CCtx_params* params); -/* TODO */ -ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void); -/*! ZSTD_resetCCtxParams() : - * Reset params to default, with the default compression level. */ -ZSTDLIB_API size_t ZSTD_resetCCtxParams(ZSTD_CCtx_params* params); -ZSTDLIB_API size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params); -ZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params); + /*! ZSTD_getCParams() : * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize. @@ -1013,21 +1007,6 @@ typedef enum { * @result : 0, or an error code (which can be tested with ZSTD_isError()). */ ZSTDLIB_API size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned value); -/*! ZSTD_CCtxParam_setParameter() : - * Similar to ZSTD_CCtx_setParameter. - * Set one compression parameter, selected by enum ZSTD_cParameter. - * Parameters must be applied to a ZSTD_CCtx using ZSTD_CCtx_applyCCtxParams(). - * Note : when `value` is an enum, cast it to unsigned for proper type checking. - * @result : 0, or an error code (which can be tested with ZSTD_isError()). */ -ZSTDLIB_API size_t ZSTD_CCtxParam_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, unsigned value); - -/*! ZSTD_CCtx_applyCCtxParams() : - * Apply a set of ZSTD_CCtx_params to the compression context. - * This must be done before the dictionary is loaded. - * The pledgedSrcSize is treated as unknown. - * Multithreading parameters are applied only if nbThreads > 1. */ -ZSTDLIB_API size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params); - /*! ZSTD_CCtx_setPledgedSrcSize() : * Total input data size to be compressed as a single frame. * This value will be controlled at the end, and result in error if not respected. @@ -1131,7 +1110,44 @@ size_t ZSTD_compress_generic_simpleArgs ( const void* src, size_t srcSize, size_t* srcPos, ZSTD_EndDirective endOp); +/** ZSTD_CCtx_params : + * + * - ZSTD_createCCtxParams() : Create a ZSTD_CCtx_params structure + * - ZSTD_CCtxParam_setParameter() : Push parameters one by one into an + * existing ZSTD_CCtx_params structure. This is similar to + * ZSTD_CCtx_setParameter(). + * - ZSTD_CCtx_applyCCtxParams() : Apply parameters to an existing CCtx. These + * parameters will be applied to all subsequent compression jobs. + * - ZSTD_compress_generic() : Do compression using the CCtx. + * - ZSTD_freeCCtxParams() : Free the memory. */ +ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void); + +/*! ZSTD_resetCCtxParams() : + * Reset params to default, with the default compression level. */ +ZSTDLIB_API size_t ZSTD_resetCCtxParams(ZSTD_CCtx_params* params); + +/*! ZSTD_initCCtxParams() : + * Set the compression and frame parameters of cctxParams according to params. + * All other parameters are reset to their default values. */ +ZSTDLIB_API size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params); + +ZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params); + +/*! ZSTD_CCtxParam_setParameter() : + * Similar to ZSTD_CCtx_setParameter. + * Set one compression parameter, selected by enum ZSTD_cParameter. + * Parameters must be applied to a ZSTD_CCtx using ZSTD_CCtx_applyCCtxParams(). + * Note : when `value` is an enum, cast it to unsigned for proper type checking. + * @result : 0, or an error code (which can be tested with ZSTD_isError()). */ +ZSTDLIB_API size_t ZSTD_CCtxParam_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, unsigned value); + +/*! ZSTD_CCtx_applyCCtxParams() : + * Apply a set of ZSTD_CCtx_params to the compression context. + * This must be done before the dictionary is loaded. + * The pledgedSrcSize is treated as unknown. + * Multithreading parameters are applied only if nbThreads > 1. */ +ZSTDLIB_API size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params); /** Block functions From fd8a25786efe8f6696e8c964dc8b6f247c759203 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Mon, 21 Aug 2017 13:23:35 -0700 Subject: [PATCH 22/52] Check parameters are valid in initCCtxParams --- lib/compress/zstd_compress.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index c37b2ad49..a85683059 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -266,9 +266,10 @@ size_t ZSTD_resetCCtxParams(ZSTD_CCtx_params* params) return 0; } -size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params) +size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* cctxParams, ZSTD_parameters const params) { if (!cctxParams) { return ERROR(GENERIC); } + CHECK_F( ZSTD_checkCParams(params.cParams) ); memset(cctxParams, 0, sizeof(ZSTD_CCtx_params)); cctxParams->cParams = params.cParams; cctxParams->fParams = params.fParams; From 5b956f4753c87905e8ae69ae3458e5ef56e44f07 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Mon, 21 Aug 2017 14:49:16 -0700 Subject: [PATCH 23/52] Comment out CCtx_param versions of CDict functions --- lib/common/zstd_internal.h | 5 -- lib/compress/zstd_compress.c | 119 +++++++++++++++++++++++++-------- lib/compress/zstdmt_compress.c | 9 ++- lib/zstd.h | 9 --- 4 files changed, 96 insertions(+), 46 deletions(-) diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index 65719f541..ee3164183 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -360,11 +360,6 @@ size_t ZSTD_initCStream_internal_opaque( ZSTD_CCtx_params params, unsigned long long pledgedSrcSize); -/* INTERNAL */ -ZSTD_CDict* ZSTD_createCDict_advanced_opaque( - const void* dictBuffer, size_t dictSize, - ZSTD_CCtx_params params, ZSTD_customMem customMem); - /*! ZSTD_compressStream_generic() : * Private use only. To be called from zstdmt_compress.c in single-thread mode. */ size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs, diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index a85683059..649cf8780 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -3611,7 +3611,7 @@ size_t ZSTD_compress(void* dst, size_t dstCapacity, const void* src, size_t srcS /* ===== Dictionary API ===== */ - +#if 0 size_t ZSTD_estimateCDictSize_advanced_opaque( size_t dictSize, const ZSTD_CCtx_params* params) { @@ -3623,14 +3623,17 @@ size_t ZSTD_estimateCDictSize_advanced_opaque( + (params->dictContentByRef ? 0 : dictSize); } +#endif /*! ZSTD_estimateCDictSize_advanced() : * Estimate amount of memory that will be needed to create a dictionary with following arguments */ size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, unsigned byReference) { - ZSTD_CCtx_params params = ZSTD_makeCCtxParamsFromCParams(cParams); - params.dictContentByRef = byReference; - return ZSTD_estimateCDictSize_advanced_opaque(dictSize, ¶ms); + DEBUGLOG(5, "sizeof(ZSTD_CDict) : %u", (U32)sizeof(ZSTD_CDict)); + DEBUGLOG(5, "CCtx estimate : %u", + (U32)ZSTD_estimateCCtxSize_advanced(cParams)); + return sizeof(ZSTD_CDict) + ZSTD_estimateCCtxSize_advanced(cParams) + + (byReference ? 0 : dictSize); } size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel) @@ -3657,6 +3660,7 @@ static ZSTD_parameters ZSTD_makeParams(ZSTD_compressionParameters cParams, ZSTD_ } #endif +#if 0 static size_t ZSTD_initCDict_internal_opaque(ZSTD_CDict* cdict, const void* dictBuffer, size_t dictSize, ZSTD_CCtx_params params) @@ -3683,27 +3687,44 @@ static size_t ZSTD_initCDict_internal_opaque(ZSTD_CDict* cdict, ZSTDb_not_buffered) ); return 0; } +#endif -#if 0 static size_t ZSTD_initCDict_internal( ZSTD_CDict* cdict, const void* dictBuffer, size_t dictSize, unsigned byReference, ZSTD_dictMode_e dictMode, ZSTD_compressionParameters cParams) { - ZSTD_CCtx_params cctxParams = cdict->refContext->requestedParams; - ZSTD_frameParameters const fParams = { 0 /* contentSizeFlag */, + DEBUGLOG(5, "ZSTD_initCDict_internal, mode %u", (U32)dictMode); + if ((byReference) || (!dictBuffer) || (!dictSize)) { + cdict->dictBuffer = NULL; + cdict->dictContent = dictBuffer; + } else { + void* const internalBuffer = ZSTD_malloc(dictSize, cdict->refContext->customMem); + cdict->dictBuffer = internalBuffer; + cdict->dictContent = internalBuffer; + if (!internalBuffer) return ERROR(memory_allocation); + memcpy(internalBuffer, dictBuffer, dictSize); + } + cdict->dictContentSize = dictSize; + + { ZSTD_frameParameters const fParams = { 0 /*contentSizeFlag */, 0 /* checksumFlag */, 0 /* noDictIDFlag */ }; /* dummy */ - cctxParams.cParams = cParams; - cctxParams.fParams = fParams; - cctxParams.dictMode = dictMode; - cctxParams.dictContentByRef = byReference; - CHECK_F (ZSTD_initCDict_internal_opaque( - cdict, dictBuffer, dictSize, cctxParams) ); + ZSTD_CCtx_params cctxParams = cdict->refContext->requestedParams; + cctxParams.fParams = fParams; + cctxParams.cParams = cParams; + cctxParams.dictContentByRef = byReference; + cctxParams.dictMode = dictMode; + CHECK_F( ZSTD_compressBegin_internal(cdict->refContext, + cdict->dictContent, dictSize, + NULL, + cctxParams, ZSTD_CONTENTSIZE_UNKNOWN, + ZSTDb_not_buffered) ); + } return 0; } -#endif +#if 0 /* Internal only */ ZSTD_CDict* ZSTD_createCDict_advanced_opaque( const void* dictBuffer, size_t dictSize, @@ -3730,15 +3751,33 @@ ZSTD_CDict* ZSTD_createCDict_advanced_opaque( return cdict; } } +#endif ZSTD_CDict* ZSTD_createCDict_advanced(const void* dictBuffer, size_t dictSize, unsigned byReference, ZSTD_dictMode_e dictMode, ZSTD_compressionParameters cParams, ZSTD_customMem customMem) { - ZSTD_CCtx_params cctxParams = ZSTD_makeCCtxParamsFromCParams(cParams); - cctxParams.dictMode = dictMode; - cctxParams.dictContentByRef = byReference; - return ZSTD_createCDict_advanced_opaque(dictBuffer, dictSize, cctxParams, customMem); + DEBUGLOG(5, "ZSTD_createCDict_advanced, mode %u", dictMode); + if (!customMem.customAlloc ^ !customMem.customFree) return NULL; + + { ZSTD_CDict* const cdict = (ZSTD_CDict*)ZSTD_malloc(sizeof(ZSTD_CDict), customMem); + ZSTD_CCtx* const cctx = ZSTD_createCCtx_advanced(customMem); + if (!cdict || !cctx) { + ZSTD_free(cdict, customMem); + ZSTD_freeCCtx(cctx); + return NULL; + } + cdict->refContext = cctx; + if (ZSTD_isError( ZSTD_initCDict_internal( + cdict, + dictBuffer, dictSize, + byReference, dictMode, + cParams) )) { + ZSTD_freeCDict(cdict); + return NULL; + } + return cdict; + } } ZSTD_CDict* ZSTD_createCDict(const void* dict, size_t dictSize, int compressionLevel) @@ -3768,6 +3807,7 @@ size_t ZSTD_freeCDict(ZSTD_CDict* cdict) } } +#if 0 ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( void *workspace, size_t workspaceSize, const void* dict, size_t dictSize, @@ -3804,6 +3844,7 @@ ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( return cdict; } } +#endif /*! ZSTD_initStaticCDict_advanced() : * Generate a digested dictionary in provided memory area. @@ -3823,13 +3864,35 @@ ZSTD_CDict* ZSTD_initStaticCDict(void* workspace, size_t workspaceSize, unsigned byReference, ZSTD_dictMode_e dictMode, ZSTD_compressionParameters cParams) { - ZSTD_CCtx_params params = ZSTD_makeCCtxParamsFromCParams(cParams); - params.dictMode = dictMode; - params.dictContentByRef = byReference; - return ZSTD_initStaticCDict_advanced_opaque( - workspace, workspaceSize, dict, dictSize, - ¶ms); + size_t const cctxSize = ZSTD_estimateCCtxSize_advanced(cParams); + size_t const neededSize = sizeof(ZSTD_CDict) + + (byReference ? 0 : dictSize) + + cctxSize; + ZSTD_CDict* const cdict = (ZSTD_CDict*) workspace; + void* ptr; + DEBUGLOG(5, "(size_t)workspace & 7 : %u", (U32)(size_t)workspace & 7); + if ((size_t)workspace & 7) return NULL; /* 8-aligned */ + DEBUGLOG(5, "(workspaceSize < neededSize) : (%u < %u) => %u", + (U32)workspaceSize, (U32)neededSize, (U32)(workspaceSize < neededSize)); + if (workspaceSize < neededSize) return NULL; + + if (!byReference) { + memcpy(cdict+1, dict, dictSize); + dict = cdict+1; + ptr = (char*)workspace + sizeof(ZSTD_CDict) + dictSize; + } else { + ptr = cdict+1; + } + cdict->refContext = ZSTD_initStaticCCtx(ptr, cctxSize); + + if (ZSTD_isError( ZSTD_initCDict_internal(cdict, dict, dictSize, + 1 /* byReference */, + dictMode, cParams) )) + return NULL; + + return cdict; } + #if 0 static ZSTD_parameters ZSTD_getParamsFromCDict(const ZSTD_CDict* cdict) { return ZSTD_getParamsFromCCtx(cdict->refContext); @@ -3997,10 +4060,12 @@ size_t ZSTD_initCStream_internal_opaque( return ERROR(memory_allocation); } ZSTD_freeCDict(zcs->cdictLocal); - /* TODO opaque version: what needs to be zero? */ - zcs->cdictLocal = ZSTD_createCDict_advanced_opaque( + + /* Is a CCtx_params version needed? */ + zcs->cdictLocal = ZSTD_createCDict_advanced( dict, dictSize, - params, zcs->customMem); + params.dictContentByRef, params.dictMode, + params.cParams, zcs->customMem); zcs->cdict = zcs->cdictLocal; if (zcs->cdictLocal == NULL) return ERROR(memory_allocation); } else { diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index e7a05f4b1..02dad6c0b 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -746,11 +746,10 @@ size_t ZSTDMT_initCStream_internal_opaque( if (dict) { DEBUGLOG(4,"cdictLocal: %08X", (U32)(size_t)zcs->cdictLocal); ZSTD_freeCDict(zcs->cdictLocal); - /* TODO: cctxParam version? Is this correct? - * by reference should be zero, mode should be ZSTD_dm_auto */ - zcs->cdictLocal = ZSTD_createCDict_advanced_opaque( - dict, dictSize, - cctxParams, zcs->cMem); + /* TODO: cctxParam version? Is this correct? */ + zcs->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, + 0 /* byRef */, ZSTD_dm_auto, /* note : a loadPrefix becomes an internal CDict */ + cctxParams.cParams, zcs->cMem); zcs->cdict = zcs->cdictLocal; if (zcs->cdictLocal == NULL) return ERROR(memory_allocation); } else { diff --git a/lib/zstd.h b/lib/zstd.h index 09b89911f..3e7574c78 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -527,11 +527,9 @@ ZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t sr /*! ZSTD_estimate?DictSize() : * ZSTD_estimateCDictSize() will bet that src size is relatively "small", and content is copied, like ZSTD_createCDict(). * ZSTD_estimateCStreamSize_advanced() makes it possible to control precisely compression parameters, like ZSTD_createCDict_advanced(). - * ZSTD_estimateCDictSize_advanced_opaque() allows further compression parameters. ByReference can be set with ZSTD_CCtxParam_setParameter. * Note : dictionary created "byReference" are smaller */ ZSTDLIB_API size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel); ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, unsigned byReference); -ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced_opaque(size_t dictSize, const ZSTD_CCtx_params* params); ZSTDLIB_API size_t ZSTD_estimateDDictSize(size_t dictSize, unsigned byReference); @@ -608,13 +606,6 @@ ZSTDLIB_API ZSTD_CDict* ZSTD_initStaticCDict( unsigned byReference, ZSTD_dictMode_e dictMode, ZSTD_compressionParameters cParams); -ZSTDLIB_API ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( - void* workspace, size_t workspaceSize, - const void* dict, size_t dictSize, - const ZSTD_CCtx_params* params); - - - /*! ZSTD_getCParams() : * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize. * `estimatedSrcSize` value is optional, select 0 if not known */ From 60e1bc617cd9cb2b254967b419d9f5dc194582c9 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Mon, 21 Aug 2017 15:39:37 -0700 Subject: [PATCH 24/52] Explicitly create a job cctxParam for multithreading --- lib/compress/zstdmt_compress.c | 42 +++++++++++++++------------------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index 02dad6c0b..cf362a756 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -186,19 +186,17 @@ static void ZSTDMT_releaseBuffer(ZSTDMT_bufferPool* bufPool, buffer_t buf) ZSTD_free(buf.start, bufPool->cMem); } -/** - * TODO - * - * Sets parameters to zero for jobs. Notably, nbThreads should be zero? - */ -static void ZSTDMT_zeroCCtxParams(ZSTD_CCtx_params* params) +/* TODO: Set relevant job parameters, initialize others to default. + * Notably, nbThreads should be zero. */ +static ZSTD_CCtx_params ZSTDMT_makeJobCCtxParams(ZSTD_CCtx_params const params) { - params->forceWindow = 0; - params->dictMode = (ZSTD_dictMode_e)(0); - params->dictContentByRef = 0; - params->nbThreads = 0; - params->jobSize = 0; - params->overlapSizeLog = 0; + ZSTD_CCtx_params jobParams; + memset(&jobParams, 0, sizeof(jobParams)); + + jobParams.cParams = params.cParams; + jobParams.fParams = params.fParams; + jobParams.compressionLevel = params.compressionLevel; + return jobParams; } /* ===== CCtx Pool ===== */ @@ -560,8 +558,7 @@ static size_t ZSTDMT_compress_advanced_opaque( unsigned const compressWithinDst = (dstCapacity >= ZSTD_compressBound(srcSize)) ? nbChunks : (unsigned)(dstCapacity / ZSTD_compressBound(avgChunkSize)); /* presumes avgChunkSize >= 256 KB, which should be the case */ size_t frameStartPos = 0, dstBufferPos = 0; XXH64_state_t xxh64; - ZSTD_CCtx_params requestedParams = cctxParams; - ZSTDMT_zeroCCtxParams(&requestedParams); + ZSTD_CCtx_params const requestedParams = ZSTDMT_makeJobCCtxParams(cctxParams); DEBUGLOG(4, "nbChunks : %2u (chunkSize : %u bytes) ", nbChunks, (U32)avgChunkSize); if (nbChunks==1) { /* fallback to single-thread mode */ @@ -720,19 +717,17 @@ size_t ZSTDMT_initCStream_internal_opaque( const ZSTD_CDict* cdict, ZSTD_CCtx_params cctxParams, unsigned long long pledgedSrcSize) { + ZSTD_CCtx_params const requestedParams = ZSTDMT_makeJobCCtxParams(cctxParams); DEBUGLOG(4, "ZSTDMT_initCStream_internal"); /* params are supposed to be fully validated at this point */ assert(!ZSTD_isError(ZSTD_checkCParams(cctxParams.cParams))); assert(!((dict) && (cdict))); /* either dict or cdict, not both */ - /* TODO: Set stuff to 0 to preserve old semantics. */ - ZSTDMT_zeroCCtxParams(&cctxParams); - if (zcs->nbThreads==1) { DEBUGLOG(4, "single thread mode"); return ZSTD_initCStream_internal_opaque(zcs->cctxPool->cctx[0], dict, dictSize, cdict, - cctxParams, pledgedSrcSize); + requestedParams, pledgedSrcSize); } if (zcs->allJobsCompleted == 0) { /* previous compression not correctly finished */ @@ -749,7 +744,7 @@ size_t ZSTDMT_initCStream_internal_opaque( /* TODO: cctxParam version? Is this correct? */ zcs->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, 0 /* byRef */, ZSTD_dm_auto, /* note : a loadPrefix becomes an internal CDict */ - cctxParams.cParams, zcs->cMem); + requestedParams.cParams, zcs->cMem); zcs->cdict = zcs->cdictLocal; if (zcs->cdictLocal == NULL) return ERROR(memory_allocation); } else { @@ -804,13 +799,12 @@ size_t ZSTDMT_initCStream_usingCDict(ZSTDMT_CCtx* mtctx, ZSTD_frameParameters fParams, unsigned long long pledgedSrcSize) { - ZSTD_CCtx_params params = ZSTD_getCCtxParamsFromCDict(cdict); + ZSTD_CCtx_params requestedParams = + ZSTDMT_makeJobCCtxParams(ZSTD_getCCtxParamsFromCDict(cdict)); if (cdict==NULL) return ERROR(dictionary_wrong); /* method incompatible with NULL cdict */ - ZSTDMT_zeroCCtxParams(¶ms); - params.fParams = fParams; - + requestedParams.fParams = fParams; return ZSTDMT_initCStream_internal_opaque(mtctx, NULL, 0 /*dictSize*/, cdict, - params, pledgedSrcSize); + requestedParams, pledgedSrcSize); } From 8fd16367761528cd1f49bcd0ab58f73e70a2e29a Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Mon, 21 Aug 2017 18:10:44 -0700 Subject: [PATCH 25/52] Remove unused functions --- lib/common/zstd_internal.h | 4 +- lib/compress/zstd_compress.c | 200 +++------------------------------ lib/compress/zstdmt_compress.c | 12 +- tests/cctxParamRoundTrip.c | 2 + 4 files changed, 25 insertions(+), 193 deletions(-) diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index ee3164183..4e56f6a92 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -348,12 +348,12 @@ MEM_STATIC U32 ZSTD_highbit32(U32 val) void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx); -/*! ZSTD_initCStream_internal_opaque() : +/*! ZSTD_initCStream_internal() : * Private use only. Init streaming operation. * expects params to be valid. * must receive dict, or cdict, or none, but not both. * @return : 0, or an error code */ -size_t ZSTD_initCStream_internal_opaque( +size_t ZSTD_initCStream_internal( ZSTD_CStream* zcs, const void* dict, size_t dictSize, const ZSTD_CDict* cdict, diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 649cf8780..cfefe5c20 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -80,14 +80,8 @@ struct ZSTD_CCtx_s { U32 nextToUpdate3; /* index from which to continue dictionary update */ U32 hashLog3; /* dispatch table : larger == faster, more memory */ U32 loadedDictEnd; /* index of end of dictionary */ -#if 0 - U32 forceWindow; /* force back-references to respect limit of 1<cdict) { return ERROR(stage_wrong); } - /* TODO: some parameters can be set even if cctx->cdict. - * They can be set directly using ZSTD_CCtx_setParameter? - */ - /* Assume the compression and frame parameters are validated */ cctx->requestedParams.cParams = params->cParams; cctx->requestedParams.fParams = params->fParams; @@ -3611,19 +3594,6 @@ size_t ZSTD_compress(void* dst, size_t dstCapacity, const void* src, size_t srcS /* ===== Dictionary API ===== */ -#if 0 -size_t ZSTD_estimateCDictSize_advanced_opaque( - size_t dictSize, const ZSTD_CCtx_params* params) -{ - if (params == NULL) { return 0; } - DEBUGLOG(5, "sizeof(ZSTD_CDict) : %u", (U32)sizeof(ZSTD_CDict)); - DEBUGLOG(5, "CCtx estimate : %u", - (U32)ZSTD_estimateCCtxSize_advanced_opaque(params)); - return sizeof(ZSTD_CDict) + ZSTD_estimateCCtxSize_advanced_opaque(params) - + (params->dictContentByRef ? 0 : dictSize); - -} -#endif /*! ZSTD_estimateCDictSize_advanced() : * Estimate amount of memory that will be needed to create a dictionary with following arguments */ @@ -3650,45 +3620,6 @@ size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict) return ZSTD_sizeof_CCtx(cdict->refContext) + (cdict->dictBuffer ? cdict->dictContentSize : 0) + sizeof(*cdict); } -#if 0 -static ZSTD_parameters ZSTD_makeParams(ZSTD_compressionParameters cParams, ZSTD_frameParameters fParams) -{ - ZSTD_parameters params; - params.cParams = cParams; - params.fParams = fParams; - return params; -} -#endif - -#if 0 -static size_t ZSTD_initCDict_internal_opaque(ZSTD_CDict* cdict, - const void* dictBuffer, size_t dictSize, - ZSTD_CCtx_params params) -{ - DEBUGLOG(5, "ZSTD_initCDict_internal_opaque, mode %u", (U32)params.dictMode); - if ((params.dictContentByRef) || (!dictBuffer) || (!dictSize)) { - cdict->dictBuffer = NULL; - cdict->dictContent = dictBuffer; - } else { - void* const internalBuffer = ZSTD_malloc(dictSize, cdict->refContext->customMem); - cdict->dictBuffer = internalBuffer; - cdict->dictContent = internalBuffer; - if (!internalBuffer) return ERROR(memory_allocation); - memcpy(internalBuffer, dictBuffer, dictSize); - } - cdict->dictContentSize = dictSize; - - /* TODO: do the frame parameters need to be zero? - * does nbThreads need to be zero? */ - CHECK_F( ZSTD_compressBegin_internal(cdict->refContext, - cdict->dictContent, dictSize, - NULL, - params, ZSTD_CONTENTSIZE_UNKNOWN, - ZSTDb_not_buffered) ); - return 0; -} -#endif - static size_t ZSTD_initCDict_internal( ZSTD_CDict* cdict, const void* dictBuffer, size_t dictSize, @@ -3710,6 +3641,7 @@ static size_t ZSTD_initCDict_internal( { ZSTD_frameParameters const fParams = { 0 /*contentSizeFlag */, 0 /* checksumFlag */, 0 /* noDictIDFlag */ }; /* dummy */ + /* TODO: correct? */ ZSTD_CCtx_params cctxParams = cdict->refContext->requestedParams; cctxParams.fParams = fParams; cctxParams.cParams = cParams; @@ -3724,35 +3656,6 @@ static size_t ZSTD_initCDict_internal( return 0; } -#if 0 -/* Internal only */ -ZSTD_CDict* ZSTD_createCDict_advanced_opaque( - const void* dictBuffer, size_t dictSize, - ZSTD_CCtx_params params, ZSTD_customMem customMem) -{ - DEBUGLOG(5, "ZSTD_createCDict_advanced_opaque, mode %u", (U32)params.dictMode); - if (!customMem.customAlloc ^ !customMem.customFree) return NULL; - - { ZSTD_CDict* const cdict = (ZSTD_CDict*)ZSTD_malloc(sizeof(ZSTD_CDict), customMem); - ZSTD_CCtx* const cctx = ZSTD_createCCtx_advanced(customMem); - if (!cdict || !cctx) { - ZSTD_free(cdict, customMem); - ZSTD_freeCCtx(cctx); - return NULL; - } - cdict->refContext = cctx; - if (ZSTD_isError( ZSTD_initCDict_internal_opaque( - cdict, - dictBuffer, dictSize, - params) )) { - ZSTD_freeCDict(cdict); - return NULL; - } - return cdict; - } -} -#endif - ZSTD_CDict* ZSTD_createCDict_advanced(const void* dictBuffer, size_t dictSize, unsigned byReference, ZSTD_dictMode_e dictMode, ZSTD_compressionParameters cParams, ZSTD_customMem customMem) @@ -3807,45 +3710,6 @@ size_t ZSTD_freeCDict(ZSTD_CDict* cdict) } } -#if 0 -ZSTD_CDict* ZSTD_initStaticCDict_advanced_opaque( - void *workspace, size_t workspaceSize, const void* dict, - size_t dictSize, - const ZSTD_CCtx_params* params) -{ - if (params == NULL) { return NULL; } - { ZSTD_CCtx_params cctxParams = *params; - size_t const cctxSize = ZSTD_estimateCCtxSize_advanced_opaque(params); - size_t const neededSize = sizeof(ZSTD_CDict) - + (cctxParams.dictContentByRef ? 0 : dictSize) - + cctxSize; - ZSTD_CDict* const cdict = (ZSTD_CDict*) workspace; - void* ptr; - DEBUGLOG(5, "(size_t)workspace & 7 : %u", (U32)(size_t)workspace & 7); - if ((size_t)workspace & 7) return NULL; /* 8-aligned */ - DEBUGLOG(5, "(workspaceSize < neededSize) : (%u < %u) => %u", - (U32)workspaceSize, (U32)neededSize, (U32)(workspaceSize < neededSize)); - if (workspaceSize < neededSize) return NULL; - - if (!cctxParams.dictContentByRef) { - memcpy(cdict+1, dict, dictSize); - dict = cdict+1; - ptr = (char*)workspace + sizeof(ZSTD_CDict) + dictSize; - } else { - ptr = cdict+1; - } - cdict->refContext = ZSTD_initStaticCCtx(ptr, cctxSize); - cctxParams.dictContentByRef = 1; - - /* What if nbThreads > 1? */ - if (ZSTD_isError( ZSTD_initCDict_internal_opaque(cdict, dict, dictSize, cctxParams) )) - return NULL; - - return cdict; - } -} -#endif - /*! ZSTD_initStaticCDict_advanced() : * Generate a digested dictionary in provided memory area. * workspace: The memory area to emplace the dictionary into. @@ -3893,12 +3757,6 @@ ZSTD_CDict* ZSTD_initStaticCDict(void* workspace, size_t workspaceSize, return cdict; } -#if 0 -static ZSTD_parameters ZSTD_getParamsFromCDict(const ZSTD_CDict* cdict) { - return ZSTD_getParamsFromCCtx(cdict->refContext); -}a -#endif - ZSTD_CCtx_params ZSTD_getCCtxParamsFromCDict(const ZSTD_CDict* cdict) { return cdict->refContext->appliedParams; } @@ -3910,7 +3768,7 @@ size_t ZSTD_compressBegin_usingCDict_advanced( ZSTD_frameParameters const fParams, unsigned long long const pledgedSrcSize) { if (cdict==NULL) return ERROR(dictionary_wrong); - { ZSTD_CCtx_params params = cdict->refContext->appliedParams; + { ZSTD_CCtx_params params = ZSTD_getCCtxParamsFromCDict(cdict); params.fParams = fParams; params.dictMode = ZSTD_dm_auto; DEBUGLOG(5, "ZSTD_compressBegin_usingCDict_advanced"); @@ -3992,7 +3850,7 @@ size_t ZSTD_CStreamOutSize(void) return ZSTD_compressBound(ZSTD_BLOCKSIZE_MAX) + ZSTD_blockHeaderSize + 4 /* 32-bits hash */ ; } -static size_t ZSTD_resetCStream_internal_opaque( +static size_t ZSTD_resetCStream_internal( ZSTD_CStream* zcs, const void* dict, size_t dictSize, const ZSTD_CDict* cdict, @@ -4018,20 +3876,6 @@ static size_t ZSTD_resetCStream_internal_opaque( return 0; /* ready to go */ } -#if 0 -static size_t ZSTD_resetCStream_internal(ZSTD_CStream* zcs, - const void* dict, size_t dictSize, ZSTD_dictMode_e dictMode, - const ZSTD_CDict* cdict, - ZSTD_parameters params, unsigned long long pledgedSrcSize) -{ - ZSTD_CCtx_params cctxParams = zcs->requestedParams; - cctxParams.cParams = params.cParams; - cctxParams.fParams = params.fParams; - return ZSTD_resetCStream_internal_opaque(zcs, dict, dictSize, - cdict, cctxParams, pledgedSrcSize); -} -#endif - size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize) { ZSTD_CCtx_params params = zcs->requestedParams; @@ -4040,10 +3884,14 @@ size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize) if (params.compressionLevel != ZSTD_CLEVEL_CUSTOM) { params.cParams = ZSTD_getCParams(params.compressionLevel, pledgedSrcSize, 0 /* dictSize */); } - return ZSTD_resetCStream_internal_opaque(zcs, NULL, 0, zcs->cdict, params, pledgedSrcSize); + return ZSTD_resetCStream_internal(zcs, NULL, 0, zcs->cdict, params, pledgedSrcSize); } -size_t ZSTD_initCStream_internal_opaque( +/*! ZSTD_initCStream_internal() : + * Note : not static (but hidden) (not exposed). Used by zstdmt_compress.c + * Assumption 1 : params are valid + * Assumption 2 : either dict, or cdict, is defined, not both */ +size_t ZSTD_initCStream_internal( ZSTD_CStream* zcs, const void* dict, size_t dictSize, const ZSTD_CDict* cdict, @@ -4079,28 +3927,10 @@ size_t ZSTD_initCStream_internal_opaque( } zcs->requestedParams = params; - return ZSTD_resetCStream_internal_opaque( + return ZSTD_resetCStream_internal( zcs, NULL, 0, zcs->cdict, params, pledgedSrcSize); } -#if 0 -/*! ZSTD_initCStream_internal() : - * Note : not static, but hidden (not exposed). Used by zstdmt_compress.c - * Assumption 1 : params are valid - * Assumption 2 : either dict, or cdict, is defined, not both */ -static size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, - const void* dict, size_t dictSize, const ZSTD_CDict* cdict, - ZSTD_parameters params, unsigned long long pledgedSrcSize) -{ - ZSTD_CCtx_params cctxParams = zcs->requestedParams; - cctxParams.cParams = params.cParams; - cctxParams.fParams = params.fParams; - cctxParams.compressionLevel = ZSTD_CLEVEL_CUSTOM; - return ZSTD_initCStream_internal_opaque(zcs, dict, dictSize, cdict, - cctxParams, pledgedSrcSize); -} -#endif - /* ZSTD_initCStream_usingCDict_advanced() : * same as ZSTD_initCStream_usingCDict(), with control over frame parameters */ size_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, @@ -4112,7 +3942,7 @@ size_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, { ZSTD_CCtx_params params = ZSTD_getCCtxParamsFromCDict(cdict); params.fParams = fParams; params.compressionLevel = ZSTD_CLEVEL_CUSTOM; - return ZSTD_initCStream_internal_opaque(zcs, + return ZSTD_initCStream_internal(zcs, NULL, 0, cdict, params, pledgedSrcSize); } @@ -4134,7 +3964,7 @@ size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, cctxParams.cParams = params.cParams; cctxParams.fParams = params.fParams; cctxParams.compressionLevel = ZSTD_CLEVEL_CUSTOM; - return ZSTD_initCStream_internal_opaque(zcs, dict, dictSize, NULL, cctxParams, pledgedSrcSize); + return ZSTD_initCStream_internal(zcs, dict, dictSize, NULL, cctxParams, pledgedSrcSize); } size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel) @@ -4144,7 +3974,7 @@ size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t di cctxParams.cParams = params.cParams; cctxParams.fParams = params.fParams; cctxParams.compressionLevel = compressionLevel; - return ZSTD_initCStream_internal_opaque(zcs, dict, dictSize, NULL, cctxParams, 0); + return ZSTD_initCStream_internal(zcs, dict, dictSize, NULL, cctxParams, 0); } size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize) @@ -4363,7 +4193,7 @@ size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, } else #endif { - CHECK_F( ZSTD_resetCStream_internal_opaque(cctx, prefix, prefixSize, cctx->cdict, params, cctx->pledgedSrcSizePlusOne-1) ); + CHECK_F( ZSTD_resetCStream_internal(cctx, prefix, prefixSize, cctx->cdict, params, cctx->pledgedSrcSizePlusOne-1) ); } } /* compression stage */ diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index cf362a756..ebedec904 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -186,12 +186,12 @@ static void ZSTDMT_releaseBuffer(ZSTDMT_bufferPool* bufPool, buffer_t buf) ZSTD_free(buf.start, bufPool->cMem); } -/* TODO: Set relevant job parameters, initialize others to default. - * Notably, nbThreads should be zero. */ +/* Sets parameterse relevant to the compression job, initializing others to + * default values. Notably, nbThreads should probably be zero. */ static ZSTD_CCtx_params ZSTDMT_makeJobCCtxParams(ZSTD_CCtx_params const params) { ZSTD_CCtx_params jobParams; - memset(&jobParams, 0, sizeof(jobParams)); + memset(&jobParams, 0, sizeof(ZSTD_CCtx_params)); jobParams.cParams = params.cParams; jobParams.fParams = params.fParams; @@ -725,9 +725,9 @@ size_t ZSTDMT_initCStream_internal_opaque( if (zcs->nbThreads==1) { DEBUGLOG(4, "single thread mode"); - return ZSTD_initCStream_internal_opaque(zcs->cctxPool->cctx[0], - dict, dictSize, cdict, - requestedParams, pledgedSrcSize); + return ZSTD_initCStream_internal(zcs->cctxPool->cctx[0], + dict, dictSize, cdict, + requestedParams, pledgedSrcSize); } if (zcs->allJobsCompleted == 0) { /* previous compression not correctly finished */ diff --git a/tests/cctxParamRoundTrip.c b/tests/cctxParamRoundTrip.c index 906ad9cec..5ce47d4d1 100644 --- a/tests/cctxParamRoundTrip.c +++ b/tests/cctxParamRoundTrip.c @@ -70,6 +70,8 @@ static size_t roundTripTest(void* resultBuff, size_t resultBuffCapacity, ZSTD_outBuffer outBuffer = {compressedBuff, compressedBuffCapacity, 0 }; CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_compressionLevel, 1) ); + CHECK_Z (ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_nbThreads, 3) ); + CHECK_Z (ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_compressionStrategy, ZSTD_lazy) ); CHECK_Z( ZSTD_CCtx_applyCCtxParams(cctx, cctxParams) ); CHECK_Z (ZSTD_compress_generic(cctx, &outBuffer, &inBuffer, ZSTD_e_end) ); From 23fc0e41fac27cca8ff41a283238f29d190e5462 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Tue, 22 Aug 2017 14:24:47 -0700 Subject: [PATCH 26/52] Remove 'opaque' naming from internal functions --- lib/common/zstd_internal.h | 12 ++++---- lib/compress/zstd_compress.c | 26 ++++++++--------- lib/compress/zstdmt_compress.c | 52 +++++++++++++++------------------- 3 files changed, 42 insertions(+), 48 deletions(-) diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index 4e56f6a92..a68c7cbce 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -372,17 +372,17 @@ size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs, ZSTD_CCtx_params ZSTD_getCCtxParamsFromCDict(const ZSTD_CDict* cdict); /* INTERNAL */ -size_t ZSTD_compressBegin_advanced_opaque(ZSTD_CCtx* cctx, +size_t ZSTD_compressBegin_advanced_internal(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_CCtx_params params, unsigned long long pledgedSrcSize); /* INTERNAL */ -size_t ZSTD_compress_advanced_opaque(ZSTD_CCtx* cctx, - void* dst, size_t dstCapacity, - const void* src, size_t srcSize, - const void* dict,size_t dictSize, - ZSTD_CCtx_params params); +size_t ZSTD_compress_advanced_internal(ZSTD_CCtx* cctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const void* dict,size_t dictSize, + ZSTD_CCtx_params params); typedef struct { blockType_e blockType; diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index cfefe5c20..65081b239 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -3418,7 +3418,7 @@ static size_t ZSTD_compressBegin_internal(ZSTD_CCtx* cctx, return ZSTD_compress_insertDictionary(cctx, dict, dictSize, params.dictMode); } -size_t ZSTD_compressBegin_advanced_opaque( +size_t ZSTD_compressBegin_advanced_internal( ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_CCtx_params params, @@ -3443,8 +3443,8 @@ size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, cctxParams.fParams = params.fParams; cctxParams.dictMode = ZSTD_dm_auto; - return ZSTD_compressBegin_advanced_opaque(cctx, dict, dictSize, cctxParams, - pledgedSrcSize); + return ZSTD_compressBegin_advanced_internal(cctx, dict, dictSize, cctxParams, + pledgedSrcSize); } @@ -3535,11 +3535,11 @@ static size_t ZSTD_compress_internal (ZSTD_CCtx* cctx, cctxParams.cParams = params.cParams; cctxParams.fParams = params.fParams; cctxParams.dictMode = ZSTD_dm_auto; - return ZSTD_compress_advanced_opaque(cctx, - dst, dstCapacity, - src, srcSize, - dict, dictSize, - cctxParams); + return ZSTD_compress_advanced_internal(cctx, + dst, dstCapacity, + src, srcSize, + dict, dictSize, + cctxParams); } size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx, @@ -3553,7 +3553,7 @@ size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx, } /* Internal */ -size_t ZSTD_compress_advanced_opaque( +size_t ZSTD_compress_advanced_internal( ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize, @@ -3888,7 +3888,7 @@ size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize) } /*! ZSTD_initCStream_internal() : - * Note : not static (but hidden) (not exposed). Used by zstdmt_compress.c + * Note : not static, but hidden (not exposed). Used by zstdmt_compress.c * Assumption 1 : params are valid * Assumption 2 : either dict, or cdict, is defined, not both */ size_t ZSTD_initCStream_internal( @@ -4154,12 +4154,12 @@ size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuf return ZSTD_compressStream_generic(zcs, output, input, ZSTD_e_continue); } -/*! ZSTDMT_initCStream_internal_opaque() : +/*! ZSTDMT_initCStream_internal() : * Private use only. Init streaming operation. * expects params to be valid. * must receive dict, or cdict, or none, but not both. * @return : 0, or an error code */ -size_t ZSTDMT_initCStream_internal_opaque(ZSTDMT_CCtx* zcs, +size_t ZSTDMT_initCStream_internal(ZSTDMT_CCtx* zcs, const void* dict, size_t dictSize, const ZSTD_CDict* cdict, ZSTD_CCtx_params params, unsigned long long pledgedSrcSize); @@ -4188,7 +4188,7 @@ size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, #ifdef ZSTD_MULTITHREAD if (params.nbThreads > 1) { DEBUGLOG(4, "call ZSTDMT_initCStream_internal as nbThreads=%u", params.nbThreads); - CHECK_F( ZSTDMT_initCStream_internal_opaque(cctx->mtctx, prefix, prefixSize, cctx->cdict, params, cctx->pledgedSrcSizePlusOne-1) ); + CHECK_F( ZSTDMT_initCStream_internal(cctx->mtctx, prefix, prefixSize, cctx->cdict, params, cctx->pledgedSrcSizePlusOne-1) ); cctx->streamStage = zcss_load; } else #endif diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index ebedec904..4b8360933 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -342,7 +342,7 @@ void ZSTDMT_compressChunk(void* jobDescription) } else { /* srcStart points at reloaded section */ if (!job->firstChunk) job->params.fParams.contentSizeFlag = 0; /* ensure no srcSize control */ { size_t const dictModeError = ZSTD_setCCtxParameter(cctx, ZSTD_p_forceRawDict, 1); /* Force loading dictionary in "content-only" mode (no header analysis) */ - size_t const initError = ZSTD_compressBegin_advanced_opaque(cctx, job->srcStart, job->dictSize, job->params, job->fullFrameSize); + size_t const initError = ZSTD_compressBegin_advanced_internal(cctx, job->srcStart, job->dictSize, job->params, job->fullFrameSize); if (ZSTD_isError(initError) || ZSTD_isError(dictModeError)) { job->cSize = initError; goto _endJob; } ZSTD_setCCtxParameter(cctx, ZSTD_p_forceWindow, 1); } } @@ -540,7 +540,7 @@ static unsigned computeNbChunks(size_t srcSize, unsigned windowLog, unsigned nbT return (multiplier>1) ? nbChunksLarge : nbChunksSmall; } -static size_t ZSTDMT_compress_advanced_opaque( +static size_t ZSTDMT_compress_advanced_internal( ZSTDMT_CCtx* mtctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize, @@ -564,7 +564,7 @@ static size_t ZSTDMT_compress_advanced_opaque( if (nbChunks==1) { /* fallback to single-thread mode */ ZSTD_CCtx* const cctx = mtctx->cctxPool->cctx[0]; if (cdict) return ZSTD_compress_usingCDict_advanced(cctx, dst, dstCapacity, src, srcSize, cdict, cctxParams.fParams); - return ZSTD_compress_advanced_opaque(cctx, dst, dstCapacity, src, srcSize, NULL, 0, requestedParams); + return ZSTD_compress_advanced_internal(cctx, dst, dstCapacity, src, srcSize, NULL, 0, requestedParams); } assert(avgChunkSize >= 256 KB); /* condition for ZSTD_compressBound(A) + ZSTD_compressBound(B) <= ZSTD_compressBound(A+B), which is required for compressWithinDst */ ZSTDMT_setBufferSize(mtctx->bufPool, ZSTD_compressBound(avgChunkSize) ); @@ -674,10 +674,10 @@ size_t ZSTDMT_compress_advanced(ZSTDMT_CCtx* mtctx, ZSTD_CCtx_params cctxParams = mtctx->params; cctxParams.cParams = params.cParams; cctxParams.fParams = params.fParams; - return ZSTDMT_compress_advanced_opaque(mtctx, - dst, dstCapacity, - src, srcSize, - cdict, cctxParams, overlapLog); + return ZSTDMT_compress_advanced_internal(mtctx, + dst, dstCapacity, + src, srcSize, + cdict, cctxParams, overlapLog); } @@ -712,7 +712,7 @@ static void ZSTDMT_waitForAllJobsCompleted(ZSTDMT_CCtx* zcs) } } -size_t ZSTDMT_initCStream_internal_opaque( +size_t ZSTDMT_initCStream_internal( ZSTDMT_CCtx* zcs, const void* dict, size_t dictSize, const ZSTD_CDict* cdict, ZSTD_CCtx_params cctxParams, unsigned long long pledgedSrcSize) @@ -773,25 +773,17 @@ size_t ZSTDMT_initCStream_internal_opaque( return 0; } -/** ZSTDMT_initCStream_internal() */ -static size_t ZSTDMT_initCStream_internal(ZSTDMT_CCtx* zcs, - const void* dict, size_t dictSize, const ZSTD_CDict* cdict, - ZSTD_parameters params, unsigned long long pledgedSrcSize) -{ - ZSTD_CCtx_params cctxParams = zcs->params; - cctxParams.cParams = params.cParams; - cctxParams.fParams = params.fParams; - return ZSTDMT_initCStream_internal_opaque(zcs, dict, dictSize, cdict, - cctxParams, pledgedSrcSize); -} - size_t ZSTDMT_initCStream_advanced(ZSTDMT_CCtx* mtctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize) { + ZSTD_CCtx_params cctxParams = mtctx->params; DEBUGLOG(5, "ZSTDMT_initCStream_advanced"); - return ZSTDMT_initCStream_internal(mtctx, dict, dictSize, NULL, params, pledgedSrcSize); + cctxParams.cParams = params.cParams; + cctxParams.fParams = params.fParams; + return ZSTDMT_initCStream_internal(mtctx, dict, dictSize, NULL, + cctxParams, pledgedSrcSize); } size_t ZSTDMT_initCStream_usingCDict(ZSTDMT_CCtx* mtctx, @@ -799,12 +791,11 @@ size_t ZSTDMT_initCStream_usingCDict(ZSTDMT_CCtx* mtctx, ZSTD_frameParameters fParams, unsigned long long pledgedSrcSize) { - ZSTD_CCtx_params requestedParams = - ZSTDMT_makeJobCCtxParams(ZSTD_getCCtxParamsFromCDict(cdict)); + ZSTD_CCtx_params requestedParams = ZSTD_getCCtxParamsFromCDict(cdict); if (cdict==NULL) return ERROR(dictionary_wrong); /* method incompatible with NULL cdict */ requestedParams.fParams = fParams; - return ZSTDMT_initCStream_internal_opaque(mtctx, NULL, 0 /*dictSize*/, cdict, - requestedParams, pledgedSrcSize); + return ZSTDMT_initCStream_internal(mtctx, NULL, 0 /*dictSize*/, cdict, + requestedParams, pledgedSrcSize); } @@ -814,13 +805,16 @@ size_t ZSTDMT_resetCStream(ZSTDMT_CCtx* zcs, unsigned long long pledgedSrcSize) { if (zcs->nbThreads==1) return ZSTD_resetCStream(zcs->cctxPool->cctx[0], pledgedSrcSize); - return ZSTDMT_initCStream_internal_opaque(zcs, NULL, 0, 0, zcs->params, - pledgedSrcSize); + return ZSTDMT_initCStream_internal(zcs, NULL, 0, 0, zcs->params, + pledgedSrcSize); } size_t ZSTDMT_initCStream(ZSTDMT_CCtx* zcs, int compressionLevel) { ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, 0); - return ZSTDMT_initCStream_internal(zcs, NULL, 0, NULL, params, 0); + ZSTD_CCtx_params cctxParams = zcs->params; + cctxParams.cParams = params.cParams; + cctxParams.fParams = params.fParams; + return ZSTDMT_initCStream_internal(zcs, NULL, 0, NULL, cctxParams, 0); } @@ -973,7 +967,7 @@ size_t ZSTDMT_compressStream_generic(ZSTDMT_CCtx* mtctx, && (mtctx->inBuff.filled==0) /* nothing buffered */ && (endOp==ZSTD_e_end) /* end order */ && (output->size - output->pos >= ZSTD_compressBound(input->size - input->pos)) ) { /* enough room */ - size_t const cSize = ZSTDMT_compress_advanced_opaque(mtctx, + size_t const cSize = ZSTDMT_compress_advanced_internal(mtctx, (char*)output->dst + output->pos, output->size - output->pos, (const char*)input->src + input->pos, input->size - input->pos, mtctx->cdict, mtctx->params, mtctx->overlapLog); From 11303778d0fbc2559e7bf3e6b4f6b25b8748451a Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Tue, 22 Aug 2017 14:53:13 -0700 Subject: [PATCH 27/52] Add function to make cctxParams from ZSTD_parameters --- lib/compress/zstd_compress.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 65081b239..d6f50e60c 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -271,6 +271,15 @@ size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params) return 0; } +static ZSTD_CCtx_params ZSTD_assignParamsToCCtxParams( + ZSTD_CCtx_params cctxParams, ZSTD_parameters params) +{ + ZSTD_CCtx_params ret = cctxParams; + ret.cParams = params.cParams; + ret.fParams = params.fParams; + return ret; +} + static void ZSTD_cLevelToCCtxParams(ZSTD_CCtx_params* params) { if (params->compressionLevel == ZSTD_CLEVEL_CUSTOM) return; @@ -3438,11 +3447,9 @@ size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, ZSTD_parameters params, unsigned long long pledgedSrcSize) { - ZSTD_CCtx_params cctxParams = cctx->requestedParams; - cctxParams.cParams = params.cParams; - cctxParams.fParams = params.fParams; + ZSTD_CCtx_params cctxParams = + ZSTD_assignParamsToCCtxParams(cctx->requestedParams, params); cctxParams.dictMode = ZSTD_dm_auto; - return ZSTD_compressBegin_advanced_internal(cctx, dict, dictSize, cctxParams, pledgedSrcSize); } @@ -3531,9 +3538,8 @@ static size_t ZSTD_compress_internal (ZSTD_CCtx* cctx, const void* dict,size_t dictSize, ZSTD_parameters params) { - ZSTD_CCtx_params cctxParams = cctx->requestedParams; - cctxParams.cParams = params.cParams; - cctxParams.fParams = params.fParams; + ZSTD_CCtx_params cctxParams = + ZSTD_assignParamsToCCtxParams(cctx->requestedParams, params); cctxParams.dictMode = ZSTD_dm_auto; return ZSTD_compress_advanced_internal(cctx, dst, dstCapacity, @@ -3959,10 +3965,9 @@ size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize) { - ZSTD_CCtx_params cctxParams = zcs->requestedParams; + ZSTD_CCtx_params cctxParams = + ZSTD_assignParamsToCCtxParams(zcs->requestedParams, params); CHECK_F( ZSTD_checkCParams(params.cParams) ); - cctxParams.cParams = params.cParams; - cctxParams.fParams = params.fParams; cctxParams.compressionLevel = ZSTD_CLEVEL_CUSTOM; return ZSTD_initCStream_internal(zcs, dict, dictSize, NULL, cctxParams, pledgedSrcSize); } @@ -3970,9 +3975,8 @@ size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel) { ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, dictSize); - ZSTD_CCtx_params cctxParams = zcs->requestedParams; - cctxParams.cParams = params.cParams; - cctxParams.fParams = params.fParams; + ZSTD_CCtx_params cctxParams = + ZSTD_assignParamsToCCtxParams(zcs->requestedParams, params); cctxParams.compressionLevel = compressionLevel; return ZSTD_initCStream_internal(zcs, dict, dictSize, NULL, cctxParams, 0); } From 6f1a21c7e9a41adbecba62b3f9e7eb6741f0eaef Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Wed, 23 Aug 2017 10:24:19 -0700 Subject: [PATCH 28/52] Remove formatting-only changes --- lib/common/zstd_internal.h | 1 - lib/compress/zstd_compress.c | 53 +++--- lib/compress/zstdmt_compress.c | 4 +- lib/zstd.h | 3 +- programs/fileio.c | 1 + tests/zstreamtest.c | 293 +-------------------------------- 6 files changed, 28 insertions(+), 327 deletions(-) diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index a68c7cbce..53631b936 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -304,7 +304,6 @@ struct ZSTD_CCtx_params_s { unsigned overlapSizeLog; }; /* typedef'd to ZSTD_CCtx_params within "zstd.h" */ - const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx); void ZSTD_seqToCodes(const seqStore_t* seqStorePtr); diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index d6f50e60c..d1d321011 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -215,6 +215,7 @@ size_t ZSTD_setCCtxParameter(ZSTD_CCtx* cctx, ZSTD_CCtxParameter param, unsigned } } + #define ZSTD_CLEVEL_CUSTOM 999 static void ZSTD_cLevelToCParams(ZSTD_CCtx* cctx) { @@ -3151,10 +3152,8 @@ static size_t ZSTD_compressContinue_internal (ZSTD_CCtx* cctx, if (cctx->stage==ZSTDcs_created) return ERROR(stage_wrong); /* missing init (ZSTD_compressBegin) */ if (frame && (cctx->stage==ZSTDcs_init)) { - fhSize = ZSTD_writeFrameHeader( - dst, dstCapacity, - cctx->appliedParams, - cctx->pledgedSrcSizePlusOne-1, cctx->dictID); + fhSize = ZSTD_writeFrameHeader(dst, dstCapacity,cctx->appliedParams, + cctx->pledgedSrcSizePlusOne-1, cctx->dictID); if (ZSTD_isError(fhSize)) return fhSize; dstCapacity -= fhSize; dst = (char*)dst + fhSize; @@ -3444,8 +3443,7 @@ size_t ZSTD_compressBegin_advanced_internal( * @return : 0, or an error code */ size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, - ZSTD_parameters params, - unsigned long long pledgedSrcSize) + ZSTD_parameters params, unsigned long long pledgedSrcSize) { ZSTD_CCtx_params cctxParams = ZSTD_assignParamsToCCtxParams(cctx->requestedParams, params); @@ -3571,11 +3569,8 @@ size_t ZSTD_compress_advanced_internal( return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize); } -size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, void* dst, - size_t dstCapacity, - const void* src, size_t srcSize, - const void* dict, size_t dictSize, - int compressionLevel) +size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize, + const void* dict, size_t dictSize, int compressionLevel) { ZSTD_parameters params = ZSTD_getParams(compressionLevel, srcSize, dict ? dictSize : 0); params.fParams.contentSizeFlag = 1; @@ -3606,8 +3601,7 @@ size_t ZSTD_compress(void* dst, size_t dstCapacity, const void* src, size_t srcS size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, unsigned byReference) { DEBUGLOG(5, "sizeof(ZSTD_CDict) : %u", (U32)sizeof(ZSTD_CDict)); - DEBUGLOG(5, "CCtx estimate : %u", - (U32)ZSTD_estimateCCtxSize_advanced(cParams)); + DEBUGLOG(5, "CCtx estimate : %u", (U32)ZSTD_estimateCCtxSize_advanced(cParams)); return sizeof(ZSTD_CDict) + ZSTD_estimateCCtxSize_advanced(cParams) + (byReference ? 0 : dictSize); } @@ -3645,7 +3639,7 @@ static size_t ZSTD_initCDict_internal( } cdict->dictContentSize = dictSize; - { ZSTD_frameParameters const fParams = { 0 /*contentSizeFlag */, + { ZSTD_frameParameters const fParams = { 0 /* contentSizeFlag */, 0 /* checksumFlag */, 0 /* noDictIDFlag */ }; /* dummy */ /* TODO: correct? */ ZSTD_CCtx_params cctxParams = cdict->refContext->requestedParams; @@ -3666,7 +3660,7 @@ ZSTD_CDict* ZSTD_createCDict_advanced(const void* dictBuffer, size_t dictSize, unsigned byReference, ZSTD_dictMode_e dictMode, ZSTD_compressionParameters cParams, ZSTD_customMem customMem) { - DEBUGLOG(5, "ZSTD_createCDict_advanced, mode %u", dictMode); + DEBUGLOG(5, "ZSTD_createCDict_advanced, mode %u", (U32)dictMode); if (!customMem.customAlloc ^ !customMem.customFree) return NULL; { ZSTD_CDict* const cdict = (ZSTD_CDict*)ZSTD_malloc(sizeof(ZSTD_CDict), customMem); @@ -3677,8 +3671,7 @@ ZSTD_CDict* ZSTD_createCDict_advanced(const void* dictBuffer, size_t dictSize, return NULL; } cdict->refContext = cctx; - if (ZSTD_isError( ZSTD_initCDict_internal( - cdict, + if (ZSTD_isError( ZSTD_initCDict_internal(cdict, dictBuffer, dictSize, byReference, dictMode, cParams) )) { @@ -3735,8 +3728,7 @@ ZSTD_CDict* ZSTD_initStaticCDict(void* workspace, size_t workspaceSize, ZSTD_compressionParameters cParams) { size_t const cctxSize = ZSTD_estimateCCtxSize_advanced(cParams); - size_t const neededSize = sizeof(ZSTD_CDict) - + (byReference ? 0 : dictSize) + size_t const neededSize = sizeof(ZSTD_CDict) + (byReference ? 0 : dictSize) + cctxSize; ZSTD_CDict* const cdict = (ZSTD_CDict*) workspace; void* ptr; @@ -3755,9 +3747,10 @@ ZSTD_CDict* ZSTD_initStaticCDict(void* workspace, size_t workspaceSize, } cdict->refContext = ZSTD_initStaticCCtx(ptr, cctxSize); - if (ZSTD_isError( ZSTD_initCDict_internal(cdict, dict, dictSize, - 1 /* byReference */, - dictMode, cParams) )) + if (ZSTD_isError( ZSTD_initCDict_internal(cdict, + dict, dictSize, + 1 /* byReference */, dictMode, + cParams) )) return NULL; return cdict; @@ -3897,12 +3890,10 @@ size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize) * Note : not static, but hidden (not exposed). Used by zstdmt_compress.c * Assumption 1 : params are valid * Assumption 2 : either dict, or cdict, is defined, not both */ -size_t ZSTD_initCStream_internal( - ZSTD_CStream* zcs, - const void* dict, size_t dictSize, - const ZSTD_CDict* cdict, - ZSTD_CCtx_params params, - unsigned long long pledgedSrcSize) +size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, + const void* dict, size_t dictSize, + const ZSTD_CDict* cdict, + ZSTD_CCtx_params params, unsigned long long pledgedSrcSize) { assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); assert(!((dict) && (cdict))); /* either dict or cdict, not both */ @@ -3931,10 +3922,10 @@ size_t ZSTD_initCStream_internal( zcs->cdictLocal = NULL; zcs->cdict = cdict; } + zcs->requestedParams = params; - return ZSTD_resetCStream_internal( - zcs, NULL, 0, zcs->cdict, params, pledgedSrcSize); + return ZSTD_resetCStream_internal(zcs, NULL, 0, zcs->cdict, params, pledgedSrcSize); } /* ZSTD_initCStream_usingCDict_advanced() : @@ -4167,6 +4158,7 @@ size_t ZSTDMT_initCStream_internal(ZSTDMT_CCtx* zcs, const void* dict, size_t dictSize, const ZSTD_CDict* cdict, ZSTD_CCtx_params params, unsigned long long pledgedSrcSize); + size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, ZSTD_outBuffer* output, ZSTD_inBuffer* input, @@ -4182,7 +4174,6 @@ size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, const void* const prefix = cctx->prefix; size_t const prefixSize = cctx->prefixSize; ZSTD_CCtx_params params = cctx->requestedParams; - if (params.compressionLevel != ZSTD_CLEVEL_CUSTOM) params.cParams = ZSTD_getCParams(params.compressionLevel, cctx->pledgedSrcSizePlusOne-1, 0 /*dictSize*/); diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index 4b8360933..a52cb8aa0 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -62,7 +62,7 @@ static unsigned long long GetCurrentClockTimeMicroseconds(void) DEBUGLOG(MUTEX_WAIT_TIME_DLEVEL, "Thread took %llu microseconds to acquire mutex %s \n", \ elapsedTime, #mutex); \ } } \ - } else { pthread_mutex_lock(mutex); } \ + } else pthread_mutex_lock(mutex); \ } #else @@ -743,7 +743,7 @@ size_t ZSTDMT_initCStream_internal( ZSTD_freeCDict(zcs->cdictLocal); /* TODO: cctxParam version? Is this correct? */ zcs->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, - 0 /* byRef */, ZSTD_dm_auto, /* note : a loadPrefix becomes an internal CDict */ + 0 /* byRef */, ZSTD_dm_auto, /* note : a loadPrefix becomes an internal CDict */ requestedParams.cParams, zcs->cMem); zcs->cdict = zcs->cdictLocal; if (zcs->cdictLocal == NULL) return ERROR(memory_allocation); diff --git a/lib/zstd.h b/lib/zstd.h index 3e7574c78..e207e46bf 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -987,7 +987,7 @@ typedef enum { /* advanced parameters - may not remain available after API update */ ZSTD_p_forceMaxWindow=1100, /* Force back-reference distances to remain < windowSize, - * even when referencing into Dictionary content (default:0) */ + * even when referencing into Dictionary content (default:0) */ } ZSTD_cParameter; @@ -1101,6 +1101,7 @@ size_t ZSTD_compress_generic_simpleArgs ( const void* src, size_t srcSize, size_t* srcPos, ZSTD_EndDirective endOp); + /** ZSTD_CCtx_params : * * - ZSTD_createCCtxParams() : Create a ZSTD_CCtx_params structure diff --git a/programs/fileio.c b/programs/fileio.c index 669052d8b..1dd8008e8 100644 --- a/programs/fileio.c +++ b/programs/fileio.c @@ -214,6 +214,7 @@ void FIO_setOverlapLog(unsigned overlapLog){ g_overlapLog = overlapLog; } + /*-************************************* * Functions ***************************************/ diff --git a/tests/zstreamtest.c b/tests/zstreamtest.c index 8f974b461..98e121949 100644 --- a/tests/zstreamtest.c +++ b/tests/zstreamtest.c @@ -1204,298 +1204,6 @@ _output_error: goto _cleanup; } - -/* Tests for ZSTD_compress_generic() API */ -#if 0 -static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests) -{ - U32 const maxSrcLog = bigTests ? 24 : 22; - static const U32 maxSampleLog = 19; - size_t const srcBufferSize = (size_t)1<= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); } - else { DISPLAYUPDATE(2, "\r%6u ", testNb); } - FUZ_rand(&coreSeed); - lseed = coreSeed ^ prime32; - - /* states full reset (deliberately not synchronized) */ - /* some issues can only happen when reusing states */ - if ((FUZ_rand(&lseed) & 0xFF) == 131) { - DISPLAYLEVEL(5, "Creating new context \n"); - ZSTD_freeCCtx(zc); - zc = ZSTD_createCCtx(); - CHECK(zc==NULL, "ZSTD_createCCtx allocation error"); - resetAllowed=0; - } - if ((FUZ_rand(&lseed) & 0xFF) == 132) { - ZSTD_freeDStream(zd); - zd = ZSTD_createDStream(); - CHECK(zd==NULL, "ZSTD_createDStream allocation error"); - ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */ - } - - /* srcBuffer selection [0-4] */ - { U32 buffNb = FUZ_rand(&lseed) & 0x7F; - if (buffNb & 7) buffNb=2; /* most common : compressible (P) */ - else { - buffNb >>= 3; - if (buffNb & 7) { - const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */ - buffNb = tnb[buffNb >> 3]; - } else { - const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */ - buffNb = tnb[buffNb >> 3]; - } } - srcBuffer = cNoiseBuffer[buffNb]; - } - - /* compression init */ - CHECK_Z( ZSTD_CCtx_loadDictionary(zc, NULL, 0) ); /* cancel previous dict /*/ - if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */ - && oldTestLog /* at least one test happened */ && resetAllowed) { - maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2); - if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1; - { int const compressionLevel = (FUZ_rand(&lseed) % 5) + 1; - CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_compressionLevel, compressionLevel) ); - } - } else { - U32 const testLog = FUZ_rand(&lseed) % maxSrcLog; - U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog; - U32 const cLevelCandidate = (FUZ_rand(&lseed) % - (ZSTD_maxCLevel() - - (MAX(testLog, dictLog) / 3))) + - 1; - U32 const cLevel = MIN(cLevelCandidate, cLevelMax); - maxTestSize = FUZ_rLogLength(&lseed, testLog); - oldTestLog = testLog; - /* random dictionary selection */ - dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0; - { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize); - dict = srcBuffer + dictStart; - if (!dictSize) dict=NULL; - } - { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? ZSTD_CONTENTSIZE_UNKNOWN : maxTestSize; - ZSTD_compressionParameters cParams = ZSTD_getCParams(cLevel, pledgedSrcSize, dictSize); - - /* mess with compression parameters */ - cParams.windowLog += (FUZ_rand(&lseed) & 3) - 1; - cParams.hashLog += (FUZ_rand(&lseed) & 3) - 1; - cParams.chainLog += (FUZ_rand(&lseed) & 3) - 1; - cParams.searchLog += (FUZ_rand(&lseed) & 3) - 1; - cParams.searchLength += (FUZ_rand(&lseed) & 3) - 1; - cParams.targetLength = (U32)(cParams.targetLength * (0.5 + ((double)(FUZ_rand(&lseed) & 127) / 128))); - cParams = ZSTD_adjustCParams(cParams, 0, 0); - - if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_windowLog, cParams.windowLog) ); - if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_hashLog, cParams.hashLog) ); - if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_chainLog, cParams.chainLog) ); - if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_searchLog, cParams.searchLog) ); - if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_minMatch, cParams.searchLength) ); - if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_targetLength, cParams.targetLength) ); - - /* unconditionally set, to be sync with decoder */ - if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_refDictContent, FUZ_rand(&lseed) & 1) ); - if (FUZ_rand(&lseed) & 1) { - CHECK_Z( ZSTD_CCtx_loadDictionary(zc, dict, dictSize) ); - if (dict && dictSize) { - /* test that compression parameters are rejected (correctly) after loading a non-NULL dictionary */ - size_t const setError = ZSTD_CCtx_setParameter(zc, ZSTD_p_windowLog, cParams.windowLog-1) ; - CHECK(!ZSTD_isError(setError), "ZSTD_CCtx_setParameter should have failed"); - } } else { - CHECK_Z( ZSTD_CCtx_refPrefix(zc, dict, dictSize) ); - } - - /* mess with frame parameters */ - if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_checksumFlag, FUZ_rand(&lseed) & 1) ); - if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_dictIDFlag, FUZ_rand(&lseed) & 1) ); - if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_contentSizeFlag, FUZ_rand(&lseed) & 1) ); - if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setPledgedSrcSize(zc, pledgedSrcSize) ); - DISPLAYLEVEL(5, "pledgedSrcSize : %u \n", (U32)pledgedSrcSize); - - /* multi-threading parameters */ - { U32 const nbThreadsCandidate = (FUZ_rand(&lseed) & 4) + 1; - U32 const nbThreads = MIN(nbThreadsCandidate, nbThreadsMax); - CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_nbThreads, nbThreads) ); - if (nbThreads > 1) { - U32 const jobLog = FUZ_rand(&lseed) % (testLog+1); - CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_overlapSizeLog, FUZ_rand(&lseed) % 10) ); - CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_jobSize, (U32)FUZ_rLogLength(&lseed, jobLog)) ); - } } } } - - /* multi-segments compression test */ - XXH64_reset(&xxhState, 0); - { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ; - for (cSize=0, totalTestSize=0 ; (totalTestSize < maxTestSize) ; ) { - /* compress random chunks into randomly sized dst buffers */ - size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog); - size_t const srcSize = MIN(maxTestSize-totalTestSize, randomSrcSize); - size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize); - size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog+1); - size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize); - ZSTD_EndDirective const flush = (FUZ_rand(&lseed) & 15) ? ZSTD_e_continue : ZSTD_e_flush; - ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 }; - outBuff.size = outBuff.pos + dstBuffSize; - - CHECK_Z( ZSTD_compress_generic(zc, &outBuff, &inBuff, flush) ); - DISPLAYLEVEL(5, "compress consumed %u bytes (total : %u) \n", - (U32)inBuff.pos, (U32)(totalTestSize + inBuff.pos)); - - XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos); - memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos); - totalTestSize += inBuff.pos; - } - - /* final frame epilogue */ - { size_t remainingToFlush = (size_t)(-1); - while (remainingToFlush) { - ZSTD_inBuffer inBuff = { NULL, 0, 0 }; - size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog+1); - size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); - outBuff.size = outBuff.pos + adjustedDstSize; - DISPLAYLEVEL(5, "End-flush into dst buffer of size %u \n", (U32)adjustedDstSize); - remainingToFlush = ZSTD_compress_generic(zc, &outBuff, &inBuff, ZSTD_e_end); - CHECK(ZSTD_isError(remainingToFlush), - "ZSTD_compress_generic w/ ZSTD_e_end error : %s", - ZSTD_getErrorName(remainingToFlush) ); - } } - crcOrig = XXH64_digest(&xxhState); - cSize = outBuff.pos; - DISPLAYLEVEL(5, "Frame completed : %u bytes \n", (U32)cSize); - } - - /* multi - fragments decompression test */ - if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) { - DISPLAYLEVEL(5, "resetting DCtx (dict:%08X) \n", (U32)(size_t)dict); - CHECK_Z( ZSTD_resetDStream(zd) ); - } else { - DISPLAYLEVEL(5, "using dict of size %u \n", (U32)dictSize); - CHECK_Z( ZSTD_initDStream_usingDict(zd, dict, dictSize) ); - } - { size_t decompressionResult = 1; - ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; - ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; - for (totalGenSize = 0 ; decompressionResult ; ) { - size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); - size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); - size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize); - inBuff.size = inBuff.pos + readCSrcSize; - outBuff.size = inBuff.pos + dstBuffSize; - DISPLAYLEVEL(5, "ZSTD_decompressStream input %u bytes (pos:%u/%u)\n", - (U32)readCSrcSize, (U32)inBuff.pos, (U32)cSize); - decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff); - CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult)); - DISPLAYLEVEL(5, "inBuff.pos = %u \n", (U32)readCSrcSize); - } - CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)", (U32)outBuff.pos, (U32)totalTestSize); - CHECK (inBuff.pos != cSize, "compressed data should be fully read (%u != %u)", (U32)inBuff.pos, (U32)cSize); - { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0); - if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize); - CHECK (crcDest!=crcOrig, "decompressed data corrupted"); - } } - - /*===== noisy/erroneous src decompression test =====*/ - - /* add some noise */ - { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2; - U32 nn; for (nn=0; nn Date: Wed, 23 Aug 2017 12:03:30 -0700 Subject: [PATCH 29/52] Add prototype support for customMem with cctxParams --- lib/common/zstd_internal.h | 4 ++ lib/compress/zstd_compress.c | 123 ++++++++++++++++++----------------- lib/zstd.h | 4 +- 3 files changed, 71 insertions(+), 60 deletions(-) diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index 53631b936..4e2403bee 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -302,6 +302,10 @@ struct ZSTD_CCtx_params_s { U32 nbThreads; unsigned jobSize; unsigned overlapSizeLog; + + /* For use with createCCtxParams() and freeCCtxParams() only */ + ZSTD_customMem customMem; + }; /* typedef'd to ZSTD_CCtx_params within "zstd.h" */ const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx); diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index d1d321011..b375d369a 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -217,13 +217,22 @@ size_t ZSTD_setCCtxParameter(ZSTD_CCtx* cctx, ZSTD_CCtxParameter param, unsigned #define ZSTD_CLEVEL_CUSTOM 999 +static void ZSTD_cLevelToCCtxParams_srcSize(ZSTD_CCtx_params* params, size_t srcSize) +{ + if (params->compressionLevel == ZSTD_CLEVEL_CUSTOM) return; + params->cParams = ZSTD_getCParams(params->compressionLevel, srcSize, 0); + params->compressionLevel = ZSTD_CLEVEL_CUSTOM; +} + static void ZSTD_cLevelToCParams(ZSTD_CCtx* cctx) { - if (cctx->requestedParams.compressionLevel==ZSTD_CLEVEL_CUSTOM) return; - cctx->requestedParams.cParams = - ZSTD_getCParams(cctx->requestedParams.compressionLevel, - cctx->pledgedSrcSizePlusOne-1, 0); - cctx->requestedParams.compressionLevel = ZSTD_CLEVEL_CUSTOM; + ZSTD_cLevelToCCtxParams_srcSize( + &cctx->requestedParams, cctx->pledgedSrcSizePlusOne-1); +} + +static void ZSTD_cLevelToCCtxParams(ZSTD_CCtx_params* params) +{ + ZSTD_cLevelToCCtxParams_srcSize(params, 0); } static ZSTD_CCtx_params ZSTD_makeCCtxParamsFromCParams( @@ -236,16 +245,31 @@ static ZSTD_CCtx_params ZSTD_makeCCtxParamsFromCParams( return cctxParams; } -ZSTD_CCtx_params* ZSTD_createCCtxParams(void) +static ZSTD_CCtx_params* ZSTD_createCCtxParams_advanced( + ZSTD_customMem customMem) { - ZSTD_CCtx_params* params = - (ZSTD_CCtx_params*)ZSTD_calloc(sizeof(ZSTD_CCtx_params), - ZSTD_defaultCMem); + ZSTD_CCtx_params* params; + if (!customMem.customAlloc ^ !customMem.customFree) return NULL; + params = (ZSTD_CCtx_params*)ZSTD_calloc( + sizeof(ZSTD_CCtx_params), customMem); if (!params) { return NULL; } + params->customMem = customMem; params->compressionLevel = ZSTD_CLEVEL_DEFAULT; return params; } +ZSTD_CCtx_params* ZSTD_createCCtxParams(void) +{ + return ZSTD_createCCtxParams_advanced(ZSTD_defaultCMem); +} + +size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params) +{ + if (params == NULL) { return 0; } + ZSTD_free(params, params->customMem); + return 0; +} + size_t ZSTD_resetCCtxParams(ZSTD_CCtx_params* params) { if (!params) { return ERROR(GENERIC); } @@ -265,13 +289,6 @@ size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params) return 0; } -size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params) -{ - if (params == NULL) { return 0; } - ZSTD_free(params, ZSTD_defaultCMem); - return 0; -} - static ZSTD_CCtx_params ZSTD_assignParamsToCCtxParams( ZSTD_CCtx_params cctxParams, ZSTD_parameters params) { @@ -281,13 +298,6 @@ static ZSTD_CCtx_params ZSTD_assignParamsToCCtxParams( return ret; } -static void ZSTD_cLevelToCCtxParams(ZSTD_CCtx_params* params) -{ - if (params->compressionLevel == ZSTD_CLEVEL_CUSTOM) return; - params->cParams = ZSTD_getCParams(params->compressionLevel, 0, 0); - params->compressionLevel = ZSTD_CLEVEL_CUSTOM; -} - #define CLAMPCHECK(val,min,max) { \ if (((val)<(min)) | ((val)>(max))) { \ return ERROR(parameter_outOfBound); \ @@ -311,7 +321,7 @@ size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned v case ZSTD_p_minMatch: case ZSTD_p_targetLength: case ZSTD_p_compressionStrategy: - if (value == 0) return 0; + if (value == 0) return 0; /* special value : 0 means "don't change anything" */ if (cctx->cdict) return ERROR(stage_wrong); ZSTD_cLevelToCParams(cctx); /* Can optimize if srcSize is known */ return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); @@ -329,9 +339,8 @@ size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned v case ZSTD_p_forceMaxWindow : /* Force back-references to remain < windowSize, * even when referencing into Dictionary content * default : 0 when using a CDict, 1 when using a Prefix */ - cctx->requestedParams.forceWindow = value>0; cctx->loadedDictEnd = 0; - return 0; + return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); case ZSTD_p_nbThreads: if (value==0) return 0; @@ -521,7 +530,6 @@ static void ZSTD_debugPrintCCtxParams(ZSTD_CCtx_params* params) */ size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params) { - if (params == NULL) { return ERROR(GENERIC); } if (cctx->cdict) { return ERROR(stage_wrong); } /* Assume the compression and frame parameters are validated */ @@ -545,6 +553,8 @@ size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params cctx, ZSTD_p_overlapSizeLog, params->overlapSizeLog) ); } + + /* customMem is used only for create/free params and can be ignored */ return 0; } @@ -693,32 +703,29 @@ ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, u size_t ZSTD_estimateCCtxSize_advanced_opaque(const ZSTD_CCtx_params* params) { - if (params == NULL) { 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); + 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<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; + 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) diff --git a/lib/zstd.h b/lib/zstd.h index e207e46bf..4cff6974b 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -1111,7 +1111,9 @@ size_t ZSTD_compress_generic_simpleArgs ( * - ZSTD_CCtx_applyCCtxParams() : Apply parameters to an existing CCtx. These * parameters will be applied to all subsequent compression jobs. * - ZSTD_compress_generic() : Do compression using the CCtx. - * - ZSTD_freeCCtxParams() : Free the memory. */ + * - ZSTD_freeCCtxParams() : Free the memory. + * + * This can be used with ZSTD_estimateCCtxSize_opaque() for static allocation. */ ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void); From 64ce49426bf4fb7873027f040a7a3fa1fd666563 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Wed, 23 Aug 2017 12:30:47 -0700 Subject: [PATCH 30/52] Fix cstream compression level --- lib/compress/zstd_compress.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index b375d369a..bfe021949 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -3457,14 +3457,16 @@ size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, pledgedSrcSize); } - size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel) { ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, dictSize); - return ZSTD_compressBegin_advanced(cctx, dict, dictSize, params, 0); + ZSTD_CCtx_params cctxParams = + ZSTD_assignParamsToCCtxParams(cctx->requestedParams, params); + cctxParams.dictMode = ZSTD_dm_auto; + return ZSTD_compressBegin_internal(cctx, dict, dictSize, NULL, + cctxParams, 0, ZSTDb_not_buffered); } - size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel) { return ZSTD_compressBegin_usingDict(cctx, NULL, 0, compressionLevel); @@ -3928,6 +3930,7 @@ size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, zcs->cdict = cdict; } + params.compressionLevel = ZSTD_CLEVEL_CUSTOM; zcs->requestedParams = params; return ZSTD_resetCStream_internal(zcs, NULL, 0, zcs->cdict, params, pledgedSrcSize); @@ -3943,7 +3946,6 @@ size_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, if (!cdict) return ERROR(dictionary_wrong); { ZSTD_CCtx_params params = ZSTD_getCCtxParamsFromCDict(cdict); params.fParams = fParams; - params.compressionLevel = ZSTD_CLEVEL_CUSTOM; return ZSTD_initCStream_internal(zcs, NULL, 0, cdict, params, pledgedSrcSize); @@ -3964,7 +3966,6 @@ size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, ZSTD_CCtx_params cctxParams = ZSTD_assignParamsToCCtxParams(zcs->requestedParams, params); CHECK_F( ZSTD_checkCParams(params.cParams) ); - cctxParams.compressionLevel = ZSTD_CLEVEL_CUSTOM; return ZSTD_initCStream_internal(zcs, dict, dictSize, NULL, cctxParams, pledgedSrcSize); } @@ -3973,15 +3974,16 @@ size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t di ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, dictSize); ZSTD_CCtx_params cctxParams = ZSTD_assignParamsToCCtxParams(zcs->requestedParams, params); - cctxParams.compressionLevel = compressionLevel; return ZSTD_initCStream_internal(zcs, dict, dictSize, NULL, cctxParams, 0); } size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize) { + ZSTD_CCtx_params cctxParams; ZSTD_parameters params = ZSTD_getParams(compressionLevel, pledgedSrcSize, 0); params.fParams.contentSizeFlag = (pledgedSrcSize>0); - return ZSTD_initCStream_advanced(zcs, NULL, 0, params, pledgedSrcSize); + cctxParams = ZSTD_assignParamsToCCtxParams(zcs->requestedParams, params); + return ZSTD_initCStream_internal(zcs, NULL, 0, NULL, cctxParams, pledgedSrcSize); } size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel) From 1c81f725ff7d8caa9136154fb2fdd19488f23083 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Wed, 23 Aug 2017 15:47:15 -0700 Subject: [PATCH 31/52] Remove duplicated testing code --- lib/compress/zstd_compress.c | 2 +- tests/Makefile | 7 +- tests/cctxParamRoundTrip.c | 205 ----------------------------------- tests/roundTripCrash.c | 79 +++++++++++--- 4 files changed, 67 insertions(+), 226 deletions(-) delete mode 100644 tests/cctxParamRoundTrip.c diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index bfe021949..40b6625fc 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -217,7 +217,7 @@ size_t ZSTD_setCCtxParameter(ZSTD_CCtx* cctx, ZSTD_CCtxParameter param, unsigned #define ZSTD_CLEVEL_CUSTOM 999 -static void ZSTD_cLevelToCCtxParams_srcSize(ZSTD_CCtx_params* params, size_t srcSize) +static void ZSTD_cLevelToCCtxParams_srcSize(ZSTD_CCtx_params* params, U64 srcSize) { if (params->compressionLevel == ZSTD_CLEVEL_CUSTOM) return; params->cParams = ZSTD_getCParams(params->compressionLevel, srcSize, 0); diff --git a/tests/Makefile b/tests/Makefile index 228f4cdd2..3734f7737 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -169,11 +169,6 @@ datagen : $(PRGDIR)/datagen.c datagencli.c roundTripCrash : $(ZSTD_FILES) roundTripCrash.c $(CC) $(FLAGS) $^ -o $@$(EXT) -cctxParamRoundTrip : LDFLAGS += $(MULTITHREAD_CPP) -cctxParamRoundTrip : LDFLAGS += $(MULTITHREAD_LD) -cctxParamRoundTrip : $(ZSTD_FILES) cctxParamRoundTrip.c - $(CC) $(FLAGS) $^ -o $@$(EXT) - longmatch : $(ZSTD_FILES) longmatch.c $(CC) $(FLAGS) $^ -o $@$(EXT) @@ -217,7 +212,7 @@ clean: fuzzer$(EXT) fuzzer32$(EXT) zbufftest$(EXT) zbufftest32$(EXT) \ fuzzer-dll$(EXT) zstreamtest-dll$(EXT) zbufftest-dll$(EXT)\ zstreamtest$(EXT) zstreamtest32$(EXT) \ - datagen$(EXT) paramgrill$(EXT) roundTripCrash$(EXT) cctxParamRoundTrip$(EXT) longmatch$(EXT) \ + datagen$(EXT) paramgrill$(EXT) roundTripCrash$(EXT) longmatch$(EXT) \ symbols$(EXT) invalidDictionaries$(EXT) legacy$(EXT) poolTests$(EXT) \ decodecorpus$(EXT) @echo Cleaning completed diff --git a/tests/cctxParamRoundTrip.c b/tests/cctxParamRoundTrip.c deleted file mode 100644 index 5ce47d4d1..000000000 --- a/tests/cctxParamRoundTrip.c +++ /dev/null @@ -1,205 +0,0 @@ -/** - * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - */ - -/* - This program takes a file in input, - performs a zstd round-trip test (compression - decompress) - compares the result with original - and generates a crash (double free) on corruption detection. -*/ - -/*=========================================== -* Dependencies -*==========================================*/ -#include /* size_t */ -#include /* malloc, free, exit */ -#include /* fprintf */ -#include /* stat */ -#include /* stat */ -#include "xxhash.h" - -#define ZSTD_STATIC_LINKING_ONLY -#include "zstd.h" - -/*=========================================== -* Macros -*==========================================*/ -#define MIN(a,b) ( (a) < (b) ? (a) : (b) ) - -static void crash(int errorCode){ - /* abort if AFL/libfuzzer, exit otherwise */ - #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION /* could also use __AFL_COMPILER */ - abort(); - #else - exit(errorCode); - #endif -} - -#define CHECK_Z(f) { \ - size_t const err = f; \ - if (ZSTD_isError(err)) { \ - fprintf(stderr, \ - "Error=> %s: %s", \ - #f, ZSTD_getErrorName(err)); \ - crash(1); \ -} } - -/** roundTripTest() : -* Compresses `srcBuff` into `compressedBuff`, -* then decompresses `compressedBuff` into `resultBuff`. -* -* Parameters are currently set manually. -* -* @return : result of decompression, which should be == `srcSize` -* or an error code if either compression or decompression fails. -* Note : `compressedBuffCapacity` should be `>= ZSTD_compressBound(srcSize)` -* for compression to be guaranteed to work */ -static size_t roundTripTest(void* resultBuff, size_t resultBuffCapacity, - void* compressedBuff, size_t compressedBuffCapacity, - const void* srcBuff, size_t srcBuffSize) -{ - ZSTD_CCtx* const cctx = ZSTD_createCCtx(); - ZSTD_CCtx_params* const cctxParams = ZSTD_createCCtxParams(); - ZSTD_inBuffer inBuffer = { srcBuff, srcBuffSize, 0 }; - ZSTD_outBuffer outBuffer = {compressedBuff, compressedBuffCapacity, 0 }; - - CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_compressionLevel, 1) ); - CHECK_Z (ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_nbThreads, 3) ); - CHECK_Z (ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_compressionStrategy, ZSTD_lazy) ); - CHECK_Z( ZSTD_CCtx_applyCCtxParams(cctx, cctxParams) ); - - CHECK_Z (ZSTD_compress_generic(cctx, &outBuffer, &inBuffer, ZSTD_e_end) ); - - ZSTD_freeCCtxParams(cctxParams); - ZSTD_freeCCtx(cctx); - - return ZSTD_decompress(resultBuff, resultBuffCapacity, compressedBuff, outBuffer.pos); -} - - -static size_t checkBuffers(const void* buff1, const void* buff2, size_t buffSize) -{ - const char* ip1 = (const char*)buff1; - const char* ip2 = (const char*)buff2; - size_t pos; - - for (pos=0; pos= `fileSize` */ -static void loadFile(void* buffer, const char* fileName, size_t fileSize) -{ - FILE* const f = fopen(fileName, "rb"); - if (isDirectory(fileName)) { - fprintf(stderr, "Ignoring %s directory \n", fileName); - exit(2); - } - if (f==NULL) { - fprintf(stderr, "Impossible to open %s \n", fileName); - exit(3); - } - { size_t const readSize = fread(buffer, 1, fileSize, f); - if (readSize != fileSize) { - fprintf(stderr, "Error reading %s \n", fileName); - exit(5); - } } - fclose(f); -} - - -static void fileCheck(const char* fileName) -{ - size_t const fileSize = getFileSize(fileName); - void* buffer = malloc(fileSize); - if (!buffer) { - fprintf(stderr, "not enough memory \n"); - exit(4); - } - loadFile(buffer, fileName, fileSize); - roundTripCheck(buffer, fileSize); - free (buffer); -} - -int main(int argCount, const char** argv) { - if (argCount < 2) { - fprintf(stderr, "Error : no argument : need input file \n"); - exit(9); - } - fileCheck(argv[1]); - fprintf(stderr, "no pb detected\n"); - return 0; -} diff --git a/tests/roundTripCrash.c b/tests/roundTripCrash.c index 77c6737ee..fb14fa87b 100644 --- a/tests/roundTripCrash.c +++ b/tests/roundTripCrash.c @@ -20,9 +20,12 @@ #include /* size_t */ #include /* malloc, free, exit */ #include /* fprintf */ +#include /* strcmp */ #include /* stat */ #include /* stat */ #include "xxhash.h" + +#define ZSTD_STATIC_LINKING_ONLY #include "zstd.h" /*=========================================== @@ -30,6 +33,24 @@ *==========================================*/ #define MIN(a,b) ( (a) < (b) ? (a) : (b) ) +static void crash(int errorCode){ + /* abort if AFL/libfuzzer, exit otherwise */ + #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION /* could also use __AFL_COMPILER */ + abort(); + #else + exit(errorCode); + #endif +} + +#define CHECK_Z(f) { \ + size_t const err = f; \ + if (ZSTD_isError(err)) { \ + fprintf(stderr, \ + "Error=> %s: %s", \ + #f, ZSTD_getErrorName(err)); \ + crash(1); \ +} } + /** roundTripTest() : * Compresses `srcBuff` into `compressedBuff`, * then decompresses `compressedBuff` into `resultBuff`. @@ -54,6 +75,35 @@ static size_t roundTripTest(void* resultBuff, size_t resultBuffCapacity, return ZSTD_decompress(resultBuff, resultBuffCapacity, compressedBuff, cSize); } +/** cctxParamRoundTripTest() : + * Same as roundTripTest() except allows experimenting with ZSTD_CCtx_params. */ +static size_t cctxParamRoundTripTest(void* resultBuff, size_t resultBuffCapacity, + void* compressedBuff, size_t compressedBuffCapacity, + const void* srcBuff, size_t srcBuffSize) +{ + ZSTD_CCtx* const cctx = ZSTD_createCCtx(); + ZSTD_CCtx_params* const cctxParams = ZSTD_createCCtxParams(); + ZSTD_inBuffer inBuffer = { srcBuff, srcBuffSize, 0 }; + ZSTD_outBuffer outBuffer = {compressedBuff, compressedBuffCapacity, 0 }; + + static const int maxClevel = 19; + size_t const hashLength = MIN(128, srcBuffSize); + unsigned const h32 = XXH32(srcBuff, hashLength, 0); + int const cLevel = h32 % maxClevel; + + /* Set parameters */ + CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_compressionLevel, cLevel) ); + + /* Apply parameters */ + CHECK_Z( ZSTD_CCtx_applyCCtxParams(cctx, cctxParams) ); + + CHECK_Z (ZSTD_compress_generic(cctx, &outBuffer, &inBuffer, ZSTD_e_end) ); + + ZSTD_freeCCtxParams(cctxParams); + ZSTD_freeCCtx(cctx); + + return ZSTD_decompress(resultBuff, resultBuffCapacity, compressedBuff, outBuffer.pos); +} static size_t checkBuffers(const void* buff1, const void* buff2, size_t buffSize) { @@ -68,16 +118,7 @@ static size_t checkBuffers(const void* buff1, const void* buff2, size_t buffSize return pos; } -static void crash(int errorCode){ - /* abort if AFL/libfuzzer, exit otherwise */ - #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION /* could also use __AFL_COMPILER */ - abort(); - #else - exit(errorCode); - #endif -} - -static void roundTripCheck(const void* srcBuff, size_t srcBuffSize) +static void roundTripCheck(const void* srcBuff, size_t srcBuffSize, int testCCtxParams) { size_t const cBuffSize = ZSTD_compressBound(srcBuffSize); void* cBuff = malloc(cBuffSize); @@ -88,7 +129,9 @@ static void roundTripCheck(const void* srcBuff, size_t srcBuffSize) exit (1); } - { size_t const result = roundTripTest(rBuff, cBuffSize, cBuff, cBuffSize, srcBuff, srcBuffSize); + { size_t const result = testCCtxParams ? + cctxParamRoundTripTest(rBuff, cBuffSize, cBuff, cBuffSize, srcBuff, srcBuffSize) + : roundTripTest(rBuff, cBuffSize, cBuff, cBuffSize, srcBuff, srcBuffSize); if (ZSTD_isError(result)) { fprintf(stderr, "roundTripTest error : %s \n", ZSTD_getErrorName(result)); crash(1); @@ -162,7 +205,7 @@ static void loadFile(void* buffer, const char* fileName, size_t fileSize) } -static void fileCheck(const char* fileName) +static void fileCheck(const char* fileName, int testCCtxParams) { size_t const fileSize = getFileSize(fileName); void* buffer = malloc(fileSize); @@ -171,16 +214,24 @@ static void fileCheck(const char* fileName) exit(4); } loadFile(buffer, fileName, fileSize); - roundTripCheck(buffer, fileSize); + roundTripCheck(buffer, fileSize, testCCtxParams); free (buffer); } int main(int argCount, const char** argv) { + int argNb = 1; + int testCCtxParams = 0; if (argCount < 2) { fprintf(stderr, "Error : no argument : need input file \n"); exit(9); } - fileCheck(argv[1]); + + if (!strcmp(argv[argNb], "--cctxParams")) { + testCCtxParams = 1; + argNb++; + } + + fileCheck(argv[argNb], testCCtxParams); fprintf(stderr, "no pb detected\n"); return 0; } From bf3108fb50e0039a0fd15b3386366d40f0fd5a58 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Wed, 23 Aug 2017 17:03:31 -0700 Subject: [PATCH 32/52] Ensure zstdmt uses 'job version' of cctx parameters --- lib/compress/zstd_compress.c | 2 -- lib/compress/zstdmt_compress.c | 20 ++++++++++---------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 40b6625fc..8bf01c905 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -3648,11 +3648,9 @@ static size_t ZSTD_initCDict_internal( { ZSTD_frameParameters const fParams = { 0 /* contentSizeFlag */, 0 /* checksumFlag */, 0 /* noDictIDFlag */ }; /* dummy */ - /* TODO: correct? */ ZSTD_CCtx_params cctxParams = cdict->refContext->requestedParams; cctxParams.fParams = fParams; cctxParams.cParams = cParams; - cctxParams.dictContentByRef = byReference; cctxParams.dictMode = dictMode; CHECK_F( ZSTD_compressBegin_internal(cdict->refContext, cdict->dictContent, dictSize, diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index a52cb8aa0..927fff472 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -548,9 +548,10 @@ static size_t ZSTDMT_compress_advanced_internal( ZSTD_CCtx_params const cctxParams, unsigned overlapLog) { + ZSTD_CCtx_params const requestedParams = ZSTDMT_makeJobCCtxParams(cctxParams); unsigned const overlapRLog = (overlapLog>9) ? 0 : 9-overlapLog; - size_t const overlapSize = (overlapRLog>=9) ? 0 : (size_t)1 << (cctxParams.cParams.windowLog - overlapRLog); - unsigned nbChunks = computeNbChunks(srcSize, cctxParams.cParams.windowLog, mtctx->nbThreads); + size_t const overlapSize = (overlapRLog>=9) ? 0 : (size_t)1 << (requestedParams.cParams.windowLog - overlapRLog); + unsigned nbChunks = computeNbChunks(srcSize, requestedParams.cParams.windowLog, mtctx->nbThreads); size_t const proposedChunkSize = (srcSize + (nbChunks-1)) / nbChunks; size_t const avgChunkSize = ((proposedChunkSize & 0x1FFFF) < 0x7FFF) ? proposedChunkSize + 0xFFFF : proposedChunkSize; /* avoid too small last block */ const char* const srcStart = (const char*)src; @@ -558,12 +559,11 @@ static size_t ZSTDMT_compress_advanced_internal( unsigned const compressWithinDst = (dstCapacity >= ZSTD_compressBound(srcSize)) ? nbChunks : (unsigned)(dstCapacity / ZSTD_compressBound(avgChunkSize)); /* presumes avgChunkSize >= 256 KB, which should be the case */ size_t frameStartPos = 0, dstBufferPos = 0; XXH64_state_t xxh64; - ZSTD_CCtx_params const requestedParams = ZSTDMT_makeJobCCtxParams(cctxParams); DEBUGLOG(4, "nbChunks : %2u (chunkSize : %u bytes) ", nbChunks, (U32)avgChunkSize); if (nbChunks==1) { /* fallback to single-thread mode */ ZSTD_CCtx* const cctx = mtctx->cctxPool->cctx[0]; - if (cdict) return ZSTD_compress_usingCDict_advanced(cctx, dst, dstCapacity, src, srcSize, cdict, cctxParams.fParams); + if (cdict) return ZSTD_compress_usingCDict_advanced(cctx, dst, dstCapacity, src, srcSize, cdict, requestedParams.fParams); return ZSTD_compress_advanced_internal(cctx, dst, dstCapacity, src, srcSize, NULL, 0, requestedParams); } assert(avgChunkSize >= 256 KB); /* condition for ZSTD_compressBound(A) + ZSTD_compressBound(B) <= ZSTD_compressBound(A+B), which is required for compressWithinDst */ @@ -605,7 +605,7 @@ static size_t ZSTDMT_compress_advanced_internal( mtctx->jobs[u].jobCompleted_mutex = &mtctx->jobCompleted_mutex; mtctx->jobs[u].jobCompleted_cond = &mtctx->jobCompleted_cond; - if (cctxParams.fParams.checksumFlag) { + if (requestedParams.fParams.checksumFlag) { XXH64_update(&xxh64, srcStart + frameStartPos, chunkSize); } @@ -648,8 +648,8 @@ static size_t ZSTDMT_compress_advanced_internal( } } /* for (chunkID=0; chunkID dstCapacity) { error = ERROR(dstSize_tooSmall); @@ -720,7 +720,7 @@ size_t ZSTDMT_initCStream_internal( ZSTD_CCtx_params const requestedParams = ZSTDMT_makeJobCCtxParams(cctxParams); DEBUGLOG(4, "ZSTDMT_initCStream_internal"); /* params are supposed to be fully validated at this point */ - assert(!ZSTD_isError(ZSTD_checkCParams(cctxParams.cParams))); + assert(!ZSTD_isError(ZSTD_checkCParams(requestedParams.cParams))); assert(!((dict) && (cdict))); /* either dict or cdict, not both */ if (zcs->nbThreads==1) { @@ -736,7 +736,7 @@ size_t ZSTDMT_initCStream_internal( zcs->allJobsCompleted = 1; } - zcs->params = cctxParams; + zcs->params = requestedParams; zcs->frameContentSize = pledgedSrcSize; if (dict) { DEBUGLOG(4,"cdictLocal: %08X", (U32)(size_t)zcs->cdictLocal); @@ -769,7 +769,7 @@ size_t ZSTDMT_initCStream_internal( zcs->nextJobID = 0; zcs->frameEnded = 0; zcs->allJobsCompleted = 0; - if (cctxParams.fParams.checksumFlag) XXH64_reset(&zcs->xxhState, 0); + if (requestedParams.fParams.checksumFlag) XXH64_reset(&zcs->xxhState, 0); return 0; } From fd9bf425160fbfd58ecac5da1058b28fcb88641a Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Wed, 23 Aug 2017 19:11:05 -0700 Subject: [PATCH 33/52] Fix forceWindow and dictMode setting for zstdmt jobs --- lib/compress/zstdmt_compress.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index 927fff472..6661e0a73 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -341,10 +341,21 @@ void ZSTDMT_compressChunk(void* jobDescription) if (ZSTD_isError(initError)) { job->cSize = initError; goto _endJob; } } else { /* srcStart points at reloaded section */ if (!job->firstChunk) job->params.fParams.contentSizeFlag = 0; /* ensure no srcSize control */ - { size_t const dictModeError = ZSTD_setCCtxParameter(cctx, ZSTD_p_forceRawDict, 1); /* Force loading dictionary in "content-only" mode (no header analysis) */ - size_t const initError = ZSTD_compressBegin_advanced_internal(cctx, job->srcStart, job->dictSize, job->params, job->fullFrameSize); - if (ZSTD_isError(initError) || ZSTD_isError(dictModeError)) { job->cSize = initError; goto _endJob; } - ZSTD_setCCtxParameter(cctx, ZSTD_p_forceWindow, 1); + { ZSTD_CCtx_params jobParams = job->params; + /* Force loading dictionary in "content-only" mode (no header analysis) */ + size_t const dictModeError = + ZSTD_CCtxParam_setParameter(&jobParams, ZSTD_p_dictMode, 1); + size_t const forceWindowError = + ZSTD_CCtxParam_setParameter(&jobParams, ZSTD_p_forceMaxWindow, !job->firstChunk); + /* TODO: new/old api do not interact well here (or with + * ZSTD_setCCtxParameter). + * ZSTD_compressBegin_advanced copies params directly to + * appliedParams. ZSTD_CCtx_setParameter sets params in requested + * parameters. They should not be mixed -- parameters should be passed + * directly */ + size_t const initError = ZSTD_compressBegin_advanced_internal(cctx, job->srcStart, job->dictSize, jobParams, job->fullFrameSize); + if (ZSTD_isError(initError) || ZSTD_isError(dictModeError) || + ZSTD_isError(forceWindowError)) { job->cSize = initError; goto _endJob; } } } if (!job->firstChunk) { /* flush and overwrite frame header when it's not first segment */ size_t const hSize = ZSTD_compressContinue(cctx, dstBuff.start, dstBuff.size, src, 0); From 2fbf0285b2a1ac16e18007889b9422d88dfbdffc Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Thu, 24 Aug 2017 11:25:41 -0700 Subject: [PATCH 34/52] Fix interaction with ZSTD_setCCtxParameter() and cleanup --- lib/compress/zstd_compress.c | 35 ++++------------------------------ lib/compress/zstdmt_compress.c | 12 ++++-------- 2 files changed, 8 insertions(+), 39 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 8bf01c905..f3f602230 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -198,18 +198,22 @@ size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs) const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx) { return &(ctx->seqStore); } /* older variant; will be deprecated */ +/* Both requested and applied params need to be set as this function can be + * called before/after ZSTD_parameters have been applied. */ size_t ZSTD_setCCtxParameter(ZSTD_CCtx* cctx, ZSTD_CCtxParameter param, unsigned value) { switch(param) { case ZSTD_p_forceWindow : cctx->requestedParams.forceWindow = value>0; + cctx->appliedParams.forceWindow = value>0; cctx->loadedDictEnd = 0; return 0; ZSTD_STATIC_ASSERT(ZSTD_dm_auto==0); ZSTD_STATIC_ASSERT(ZSTD_dm_rawContent==1); case ZSTD_p_forceRawDict : cctx->requestedParams.dictMode = (ZSTD_dictMode_e)(value>0); + cctx->appliedParams.dictMode = (ZSTD_dictMode_e)(value>0); return 0; default: return ERROR(parameter_unsupported); } @@ -491,35 +495,6 @@ size_t ZSTD_CCtxParam_setParameter( } } -#if 0 -static void ZSTD_debugPrintCCtxParams(ZSTD_CCtx_params* params) -{ - DEBUGLOG(2, "======CCtxParams======"); - DEBUGLOG(2, "cParams: %u %u %u %u %u %u %u", - params->cParams.windowLog, - params->cParams.chainLog, - params->cParams.hashLog, - params->cParams.searchLog, - params->cParams.searchLength, - params->cParams.targetLength, - params->cParams.strategy); - DEBUGLOG(2, "fParams: %u %u %u", - params->fParams.contentSizeFlag, - params->fParams.checksumFlag, - params->fParams.noDictIDFlag); - DEBUGLOG(2, "cLevel, forceWindow: %u %u", - params->compressionLevel, - params->forceWindow); - DEBUGLOG(2, "dictionary: %u %u", - params->dictMode, - params->dictContentByRef); - DEBUGLOG(2, "multithreading: %u %u %u", - params->nbThreads, - params->jobSize, - params->overlapSizeLog); -} -#endif - /** * This function should be updated whenever ZSTD_CCtx_params is updated. * Parameters are copied manually before the dictionary is loaded. @@ -3910,8 +3885,6 @@ size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, return ERROR(memory_allocation); } ZSTD_freeCDict(zcs->cdictLocal); - - /* Is a CCtx_params version needed? */ zcs->cdictLocal = ZSTD_createCDict_advanced( dict, dictSize, params.dictContentByRef, params.dictMode, diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index 6661e0a73..f7f02cfbb 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -186,7 +186,7 @@ static void ZSTDMT_releaseBuffer(ZSTDMT_bufferPool* bufPool, buffer_t buf) ZSTD_free(buf.start, bufPool->cMem); } -/* Sets parameterse relevant to the compression job, initializing others to +/* Sets parameters relevant to the compression job, initializing others to * default values. Notably, nbThreads should probably be zero. */ static ZSTD_CCtx_params ZSTDMT_makeJobCCtxParams(ZSTD_CCtx_params const params) { @@ -347,12 +347,9 @@ void ZSTDMT_compressChunk(void* jobDescription) ZSTD_CCtxParam_setParameter(&jobParams, ZSTD_p_dictMode, 1); size_t const forceWindowError = ZSTD_CCtxParam_setParameter(&jobParams, ZSTD_p_forceMaxWindow, !job->firstChunk); - /* TODO: new/old api do not interact well here (or with - * ZSTD_setCCtxParameter). - * ZSTD_compressBegin_advanced copies params directly to - * appliedParams. ZSTD_CCtx_setParameter sets params in requested - * parameters. They should not be mixed -- parameters should be passed - * directly */ + /* Note: ZSTD_setCCtxParameter() should not be used here. + * ZSTD_compressBegin_advanced_internal() copies the ZSTD_CCtx_params + * directly to appliedParams. */ size_t const initError = ZSTD_compressBegin_advanced_internal(cctx, job->srcStart, job->dictSize, jobParams, job->fullFrameSize); if (ZSTD_isError(initError) || ZSTD_isError(dictModeError) || ZSTD_isError(forceWindowError)) { job->cSize = initError; goto _endJob; } @@ -752,7 +749,6 @@ size_t ZSTDMT_initCStream_internal( if (dict) { DEBUGLOG(4,"cdictLocal: %08X", (U32)(size_t)zcs->cdictLocal); ZSTD_freeCDict(zcs->cdictLocal); - /* TODO: cctxParam version? Is this correct? */ zcs->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, 0 /* byRef */, ZSTD_dm_auto, /* note : a loadPrefix becomes an internal CDict */ requestedParams.cParams, zcs->cMem); From 15fdeb9e41306e8bb8d6a909a7be643768d75e6b Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Thu, 24 Aug 2017 16:28:49 -0700 Subject: [PATCH 35/52] Enforce nbThreads<=1 for estimateCCtxSize --- lib/compress/zstd_compress.c | 63 ++++++++++++++++++++---------------- lib/zstd.h | 7 ++-- tests/Makefile | 1 + 3 files changed, 41 insertions(+), 30 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index f3f602230..4513d9831 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -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); + /* 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<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) diff --git a/lib/zstd.h b/lib/zstd.h index 4cff6974b..03994f9a8 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -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); diff --git a/tests/Makefile b/tests/Makefile index 3734f7737..006059b72 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -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) From eb7bbab36a1a5b81be860234cd6ec602184d4fc7 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Fri, 25 Aug 2017 10:48:07 -0700 Subject: [PATCH 36/52] Remove ZSTD_p_refDictContent and dictContentByRef --- lib/common/zstd_internal.h | 1 - lib/compress/zstd_compress.c | 26 ++++++++++++++++---------- lib/compress/zstdmt_compress.c | 2 +- lib/zstd.h | 13 +++++++++---- tests/zstreamtest.c | 7 +++++-- 5 files changed, 31 insertions(+), 18 deletions(-) diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index 4e2403bee..393a17247 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -294,7 +294,6 @@ struct ZSTD_CCtx_params_s { U32 forceWindow; /* force back-references to respect limit of * 1<requestedParams, param, value); case ZSTD_p_dictMode: - case ZSTD_p_refDictContent: if (cctx->cdict) return ERROR(stage_wrong); /* must be set before loading */ return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); @@ -463,11 +462,6 @@ size_t ZSTD_CCtxParam_setParameter( params->dictMode = (ZSTD_dictMode_e)value; return 0; - case ZSTD_p_refDictContent : - /* dictionary content will be referenced, instead of copied */ - params->dictContentByRef = value > 0; - return 0; - case ZSTD_p_forceMaxWindow : params->forceWindow = value > 0; return 0; @@ -514,7 +508,6 @@ size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params /* Assume dictionary parameters are validated */ cctx->requestedParams.dictMode = params->dictMode; - cctx->requestedParams.dictContentByRef = params->dictContentByRef; /* Set force window explicitly since it sets cctx->loadedDictEnd */ CHECK_F( ZSTD_CCtx_setParameter( @@ -541,7 +534,8 @@ ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long lo return 0; } -ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize) +size_t ZSTD_CCtx_loadDictionary_internal( + ZSTD_CCtx* cctx, const void* dict, size_t dictSize, unsigned byReference) { if (cctx->streamStage != zcss_init) return ERROR(stage_wrong); if (cctx->staticSize) return ERROR(memory_allocation); /* no malloc for static CCtx */ @@ -557,7 +551,7 @@ ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, s ZSTD_getCParams(cctx->requestedParams.compressionLevel, 0, dictSize); cctx->cdictLocal = ZSTD_createCDict_advanced( dict, dictSize, - cctx->requestedParams.dictContentByRef, + byReference, cctx->requestedParams.dictMode, cParams, cctx->customMem); cctx->cdict = cctx->cdictLocal; @@ -567,6 +561,18 @@ ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, s return 0; } +ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_byReference( + ZSTD_CCtx* cctx, const void* dict, size_t dictSize) +{ + return ZSTD_CCtx_loadDictionary_internal(cctx, dict, dictSize, 1); +} + +ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize) +{ + return ZSTD_CCtx_loadDictionary_internal(cctx, dict, dictSize, 0); +} + + size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict) { if (cctx->streamStage != zcss_init) return ERROR(stage_wrong); @@ -3896,7 +3902,7 @@ size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, ZSTD_freeCDict(zcs->cdictLocal); zcs->cdictLocal = ZSTD_createCDict_advanced( dict, dictSize, - params.dictContentByRef, params.dictMode, + 0 /* byReference */, params.dictMode, params.cParams, zcs->customMem); zcs->cdict = zcs->cdictLocal; if (zcs->cdictLocal == NULL) return ERROR(memory_allocation); diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index f7f02cfbb..fae17e656 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -344,7 +344,7 @@ void ZSTDMT_compressChunk(void* jobDescription) { ZSTD_CCtx_params jobParams = job->params; /* Force loading dictionary in "content-only" mode (no header analysis) */ size_t const dictModeError = - ZSTD_CCtxParam_setParameter(&jobParams, ZSTD_p_dictMode, 1); + ZSTD_CCtxParam_setParameter(&jobParams, ZSTD_p_dictMode, (U32)ZSTD_dm_rawContent); size_t const forceWindowError = ZSTD_CCtxParam_setParameter(&jobParams, ZSTD_p_forceMaxWindow, !job->firstChunk); /* Note: ZSTD_setCCtxParameter() should not be used here. diff --git a/lib/zstd.h b/lib/zstd.h index 03994f9a8..01fbb18cd 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -970,8 +970,6 @@ typedef enum { /* dictionary parameters (must be set before ZSTD_CCtx_loadDictionary) */ ZSTD_p_dictMode=300, /* Select how dictionary content must be interpreted. Value must be from type ZSTD_dictMode_e. * default : 0==auto : dictionary will be "full" if it respects specification, otherwise it will be "rawContent" */ - ZSTD_p_refDictContent, /* Dictionary content will be referenced, instead of copied (default:0==byCopy). - * It requires that dictionary buffer outlives its users */ /* multi-threading parameters */ ZSTD_p_nbThreads=400, /* Select how many threads a compression job can spawn (default:1) @@ -1015,8 +1013,9 @@ ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long lo * @result : 0, or an error code (which can be tested with ZSTD_isError()). * Special : Adding a NULL (or 0-size) dictionary invalidates any previous dictionary, * meaning "return to no-dictionary mode". - * Note 1 : `dict` content will be copied internally, - * except if ZSTD_p_refDictContent is set before loading. + * Note 1 : `dict` content will be copied internally. Use + * ZSTD_CCtx_loadDictionary_byReference() to reference dictionary + * content instead. * Note 2 : Loading a dictionary involves building tables, which are dependent on compression parameters. * For this reason, compression parameters cannot be changed anymore after loading a dictionary. * It's also a CPU-heavy operation, with non-negligible impact on latency. @@ -1024,6 +1023,12 @@ ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long lo * To return to "no-dictionary" situation, load a NULL dictionary */ ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize); +/*! ZSTD_CCtx_loadDictionary_byReference() : + * Same as ZSTD_CCtx_loadDictionary() except dictionary content will be + * referenced, instead of copied. The dictionary buffer must outlive its users. + */ +ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_byReference(ZSTD_CCtx* cctx, const void* dict, size_t dictSize); + /*! ZSTD_CCtx_refCDict() : * Reference a prepared dictionary, to be used for all next compression jobs. * Note that compression parameters are enforced from within CDict, diff --git a/tests/zstreamtest.c b/tests/zstreamtest.c index 98e121949..0373676d0 100644 --- a/tests/zstreamtest.c +++ b/tests/zstreamtest.c @@ -1366,7 +1366,6 @@ static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setPledgedSrcSize(zc, pledgedSrcSize) ); DISPLAYLEVEL(5, "pledgedSrcSize : %u \n", (U32)pledgedSrcSize); - if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_refDictContent, FUZ_rand(&lseed) & 1, useOpaqueAPI) ); /* multi-threading parameters */ { U32 const nbThreadsCandidate = (FUZ_rand(&lseed) & 4) + 1; U32 const nbThreads = MIN(nbThreadsCandidate, nbThreadsMax); @@ -1386,7 +1385,11 @@ static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double } if (FUZ_rand(&lseed) & 1) { - CHECK_Z( ZSTD_CCtx_loadDictionary(zc, dict, dictSize) ); + if (FUZ_rand(&lseed) & 1) { + CHECK_Z( ZSTD_CCtx_loadDictionary(zc, dict, dictSize) ); + } else { + CHECK_Z( ZSTD_CCtx_loadDictionary_byReference(zc, dict, dictSize) ); + } if (dict && dictSize) { /* test that compression parameters are rejected (correctly) after loading a non-NULL dictionary */ if (useOpaqueAPI) { From de5193422d458be51531cd429d1d9b5cd5d78a92 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Fri, 25 Aug 2017 11:36:17 -0700 Subject: [PATCH 37/52] Distinguish between jobParams and cctxParams in zstdmt --- lib/compress/zstdmt_compress.c | 36 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index fae17e656..cf21177e0 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -556,10 +556,10 @@ static size_t ZSTDMT_compress_advanced_internal( ZSTD_CCtx_params const cctxParams, unsigned overlapLog) { - ZSTD_CCtx_params const requestedParams = ZSTDMT_makeJobCCtxParams(cctxParams); + ZSTD_CCtx_params const jobParams = ZSTDMT_makeJobCCtxParams(cctxParams); unsigned const overlapRLog = (overlapLog>9) ? 0 : 9-overlapLog; - size_t const overlapSize = (overlapRLog>=9) ? 0 : (size_t)1 << (requestedParams.cParams.windowLog - overlapRLog); - unsigned nbChunks = computeNbChunks(srcSize, requestedParams.cParams.windowLog, mtctx->nbThreads); + size_t const overlapSize = (overlapRLog>=9) ? 0 : (size_t)1 << (cctxParams.cParams.windowLog - overlapRLog); + unsigned nbChunks = computeNbChunks(srcSize, cctxParams.cParams.windowLog, mtctx->nbThreads); size_t const proposedChunkSize = (srcSize + (nbChunks-1)) / nbChunks; size_t const avgChunkSize = ((proposedChunkSize & 0x1FFFF) < 0x7FFF) ? proposedChunkSize + 0xFFFF : proposedChunkSize; /* avoid too small last block */ const char* const srcStart = (const char*)src; @@ -571,8 +571,8 @@ static size_t ZSTDMT_compress_advanced_internal( DEBUGLOG(4, "nbChunks : %2u (chunkSize : %u bytes) ", nbChunks, (U32)avgChunkSize); if (nbChunks==1) { /* fallback to single-thread mode */ ZSTD_CCtx* const cctx = mtctx->cctxPool->cctx[0]; - if (cdict) return ZSTD_compress_usingCDict_advanced(cctx, dst, dstCapacity, src, srcSize, cdict, requestedParams.fParams); - return ZSTD_compress_advanced_internal(cctx, dst, dstCapacity, src, srcSize, NULL, 0, requestedParams); + if (cdict) return ZSTD_compress_usingCDict_advanced(cctx, dst, dstCapacity, src, srcSize, cdict, jobParams.fParams); + return ZSTD_compress_advanced_internal(cctx, dst, dstCapacity, src, srcSize, NULL, 0, jobParams); } assert(avgChunkSize >= 256 KB); /* condition for ZSTD_compressBound(A) + ZSTD_compressBound(B) <= ZSTD_compressBound(A+B), which is required for compressWithinDst */ ZSTDMT_setBufferSize(mtctx->bufPool, ZSTD_compressBound(avgChunkSize) ); @@ -601,7 +601,7 @@ static size_t ZSTDMT_compress_advanced_internal( mtctx->jobs[u].srcSize = chunkSize; mtctx->jobs[u].cdict = mtctx->nextJobID==0 ? cdict : NULL; mtctx->jobs[u].fullFrameSize = srcSize; - mtctx->jobs[u].params = requestedParams; + mtctx->jobs[u].params = jobParams; /* do not calculate checksum within sections, but write it in header for first section */ if (u!=0) mtctx->jobs[u].params.fParams.checksumFlag = 0; mtctx->jobs[u].dstBuff = dstBuffer; @@ -613,7 +613,7 @@ static size_t ZSTDMT_compress_advanced_internal( mtctx->jobs[u].jobCompleted_mutex = &mtctx->jobCompleted_mutex; mtctx->jobs[u].jobCompleted_cond = &mtctx->jobCompleted_cond; - if (requestedParams.fParams.checksumFlag) { + if (cctxParams.fParams.checksumFlag) { XXH64_update(&xxh64, srcStart + frameStartPos, chunkSize); } @@ -656,8 +656,8 @@ static size_t ZSTDMT_compress_advanced_internal( } } /* for (chunkID=0; chunkID dstCapacity) { error = ERROR(dstSize_tooSmall); @@ -725,17 +725,17 @@ size_t ZSTDMT_initCStream_internal( const ZSTD_CDict* cdict, ZSTD_CCtx_params cctxParams, unsigned long long pledgedSrcSize) { - ZSTD_CCtx_params const requestedParams = ZSTDMT_makeJobCCtxParams(cctxParams); DEBUGLOG(4, "ZSTDMT_initCStream_internal"); /* params are supposed to be fully validated at this point */ - assert(!ZSTD_isError(ZSTD_checkCParams(requestedParams.cParams))); + assert(!ZSTD_isError(ZSTD_checkCParams(cctxParams.cParams))); assert(!((dict) && (cdict))); /* either dict or cdict, not both */ if (zcs->nbThreads==1) { + ZSTD_CCtx_params const jobParams = ZSTDMT_makeJobCCtxParams(cctxParams); DEBUGLOG(4, "single thread mode"); return ZSTD_initCStream_internal(zcs->cctxPool->cctx[0], dict, dictSize, cdict, - requestedParams, pledgedSrcSize); + jobParams, pledgedSrcSize); } if (zcs->allJobsCompleted == 0) { /* previous compression not correctly finished */ @@ -744,14 +744,14 @@ size_t ZSTDMT_initCStream_internal( zcs->allJobsCompleted = 1; } - zcs->params = requestedParams; + zcs->params = cctxParams; zcs->frameContentSize = pledgedSrcSize; if (dict) { DEBUGLOG(4,"cdictLocal: %08X", (U32)(size_t)zcs->cdictLocal); ZSTD_freeCDict(zcs->cdictLocal); zcs->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, 0 /* byRef */, ZSTD_dm_auto, /* note : a loadPrefix becomes an internal CDict */ - requestedParams.cParams, zcs->cMem); + cctxParams.cParams, zcs->cMem); zcs->cdict = zcs->cdictLocal; if (zcs->cdictLocal == NULL) return ERROR(memory_allocation); } else { @@ -776,7 +776,7 @@ size_t ZSTDMT_initCStream_internal( zcs->nextJobID = 0; zcs->frameEnded = 0; zcs->allJobsCompleted = 0; - if (requestedParams.fParams.checksumFlag) XXH64_reset(&zcs->xxhState, 0); + if (cctxParams.fParams.checksumFlag) XXH64_reset(&zcs->xxhState, 0); return 0; } @@ -798,11 +798,11 @@ size_t ZSTDMT_initCStream_usingCDict(ZSTDMT_CCtx* mtctx, ZSTD_frameParameters fParams, unsigned long long pledgedSrcSize) { - ZSTD_CCtx_params requestedParams = ZSTD_getCCtxParamsFromCDict(cdict); + ZSTD_CCtx_params params = ZSTD_getCCtxParamsFromCDict(cdict); if (cdict==NULL) return ERROR(dictionary_wrong); /* method incompatible with NULL cdict */ - requestedParams.fParams = fParams; + params.fParams = fParams; return ZSTDMT_initCStream_internal(mtctx, NULL, 0 /*dictSize*/, cdict, - requestedParams, pledgedSrcSize); + params, pledgedSrcSize); } From 991115372341f7c7d28d81304b0dbae6c30d2ba9 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Fri, 25 Aug 2017 13:14:51 -0700 Subject: [PATCH 38/52] Move jobSize and overlapLog in zstdmt to cctxParams --- lib/compress/zstd_compress.c | 23 ++++---- lib/compress/zstdmt_compress.c | 100 ++++++++++++++++++++------------- tests/Makefile | 2 +- tests/roundTripCrash.c | 3 + 4 files changed, 77 insertions(+), 51 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 5c7a724cc..c87037196 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -307,6 +307,10 @@ static ZSTD_CCtx_params ZSTD_assignParamsToCCtxParams( return ERROR(parameter_outOfBound); \ } } +size_t ZSTDMT_CCtxParam_setMTCtxParameter( + ZSTD_CCtx_params* params, ZSDTMT_parameter parameter, unsigned value); +size_t ZSTDMT_initializeCCtxParameters(ZSTD_CCtx_params* params, unsigned nbThreads); + size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned value) { if (cctx->streamStage != zcss_init) return ERROR(stage_wrong); @@ -359,19 +363,20 @@ size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned v cctx->mtctx = ZSTDMT_createCCtx_advanced(value, cctx->customMem); if (cctx->mtctx == NULL) return ERROR(memory_allocation); } - cctx->requestedParams.nbThreads = value; - return 0; + + /* Need to initialize overlapSizeLog */ + return ZSTDMT_initializeCCtxParameters(&cctx->requestedParams, value); case ZSTD_p_jobSize: if (cctx->requestedParams.nbThreads <= 1) return ERROR(parameter_unsupported); assert(cctx->mtctx != NULL); - return ZSTDMT_setMTCtxParameter(cctx->mtctx, ZSTDMT_p_sectionSize, value); + return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); case ZSTD_p_overlapSizeLog: DEBUGLOG(5, " setting overlap with nbThreads == %u", cctx->requestedParams.nbThreads); if (cctx->requestedParams.nbThreads <= 1) return ERROR(parameter_unsupported); assert(cctx->mtctx != NULL); - return ZSTDMT_setMTCtxParameter(cctx->mtctx, ZSTDMT_p_overlapSectionLog, value); + return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); default: return ERROR(parameter_unsupported); } @@ -471,19 +476,15 @@ size_t ZSTD_CCtxParam_setParameter( #ifndef ZSTD_MULTITHREAD if (value > 1) return ERROR(parameter_unsupported); #endif - /* Do checks when applying params to cctx */ - params->nbThreads = value; - return 0; + return ZSTDMT_initializeCCtxParameters(params, value); case ZSTD_p_jobSize : if (params->nbThreads <= 1) return ERROR(parameter_unsupported); - params->jobSize = value; - return 0; + return ZSTDMT_CCtxParam_setMTCtxParameter(params, ZSTDMT_p_sectionSize, value); case ZSTD_p_overlapSizeLog : if (params->nbThreads <= 1) return ERROR(parameter_unsupported); - params->overlapSizeLog = value; - return 0; + return ZSTDMT_CCtxParam_setMTCtxParameter(params, ZSTDMT_p_overlapSectionLog, value); default: return ERROR(parameter_unsupported); } diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index cf21177e0..7c3bd0198 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -404,15 +404,12 @@ struct ZSTDMT_CCtx_s { inBuff_t inBuff; ZSTD_CCtx_params params; XXH64_state_t xxhState; - unsigned nbThreads; unsigned jobIDMask; unsigned doneJobID; unsigned nextJobID; unsigned frameEnded; unsigned allJobsCompleted; - unsigned overlapLog; unsigned long long frameContentSize; - size_t sectionSize; ZSTD_customMem cMem; ZSTD_CDict* cdictLocal; const ZSTD_CDict* cdict; @@ -427,6 +424,15 @@ static ZSTDMT_jobDescription* ZSTDMT_allocJobsTable(U32* nbJobsPtr, ZSTD_customM nbJobs * sizeof(ZSTDMT_jobDescription), cMem); } +/* Internal only */ +size_t ZSTDMT_initializeCCtxParameters(ZSTD_CCtx_params* params, unsigned nbThreads) +{ + params->nbThreads = nbThreads; + params->overlapSizeLog = ZSTDMT_OVERLAPLOG_DEFAULT; + params->jobSize = 0; + return 0; +} + ZSTDMT_CCtx* ZSTDMT_createCCtx_advanced(unsigned nbThreads, ZSTD_customMem cMem) { ZSTDMT_CCtx* mtctx; @@ -441,11 +447,9 @@ ZSTDMT_CCtx* ZSTDMT_createCCtx_advanced(unsigned nbThreads, ZSTD_customMem cMem) mtctx = (ZSTDMT_CCtx*) ZSTD_calloc(sizeof(ZSTDMT_CCtx), cMem); if (!mtctx) return NULL; + ZSTDMT_initializeCCtxParameters(&mtctx->params, nbThreads); mtctx->cMem = cMem; - mtctx->nbThreads = nbThreads; mtctx->allJobsCompleted = 1; - mtctx->sectionSize = 0; - mtctx->overlapLog = ZSTDMT_OVERLAPLOG_DEFAULT; mtctx->factory = POOL_create(nbThreads, 1); mtctx->jobs = ZSTDMT_allocJobsTable(&nbJobs, cMem); mtctx->jobIDMask = nbJobs - 1; @@ -516,22 +520,35 @@ size_t ZSTDMT_sizeof_CCtx(ZSTDMT_CCtx* mtctx) + ZSTD_sizeof_CDict(mtctx->cdictLocal); } -size_t ZSTDMT_setMTCtxParameter(ZSTDMT_CCtx* mtctx, ZSDTMT_parameter parameter, unsigned value) -{ +/* Internal only */ +size_t ZSTDMT_CCtxParam_setMTCtxParameter( + ZSTD_CCtx_params* params, ZSDTMT_parameter parameter, unsigned value) { switch(parameter) { case ZSTDMT_p_sectionSize : - mtctx->sectionSize = value; + params->jobSize = value; return 0; case ZSTDMT_p_overlapSectionLog : DEBUGLOG(5, "ZSTDMT_p_overlapSectionLog : %u", value); - mtctx->overlapLog = (value >= 9) ? 9 : value; + params->overlapSizeLog = (value >= 9) ? 9 : value; return 0; default : return ERROR(parameter_unsupported); } } +size_t ZSTDMT_setMTCtxParameter(ZSTDMT_CCtx* mtctx, ZSDTMT_parameter parameter, unsigned value) +{ + switch(parameter) + { + case ZSTDMT_p_sectionSize : + return ZSTDMT_CCtxParam_setMTCtxParameter(&mtctx->params, parameter, value); + case ZSTDMT_p_overlapSectionLog : + return ZSTDMT_CCtxParam_setMTCtxParameter(&mtctx->params, parameter, value); + default : + return ERROR(parameter_unsupported); + } +} /* ------------------------------------------ */ /* ===== Multi-threaded compression ===== */ @@ -553,13 +570,12 @@ static size_t ZSTDMT_compress_advanced_internal( void* dst, size_t dstCapacity, const void* src, size_t srcSize, const ZSTD_CDict* cdict, - ZSTD_CCtx_params const cctxParams, - unsigned overlapLog) + ZSTD_CCtx_params const params) { - ZSTD_CCtx_params const jobParams = ZSTDMT_makeJobCCtxParams(cctxParams); - unsigned const overlapRLog = (overlapLog>9) ? 0 : 9-overlapLog; - size_t const overlapSize = (overlapRLog>=9) ? 0 : (size_t)1 << (cctxParams.cParams.windowLog - overlapRLog); - unsigned nbChunks = computeNbChunks(srcSize, cctxParams.cParams.windowLog, mtctx->nbThreads); + ZSTD_CCtx_params const jobParams = ZSTDMT_makeJobCCtxParams(params); + unsigned const overlapRLog = (params.overlapSizeLog>9) ? 0 : 9-params.overlapSizeLog; + size_t const overlapSize = (overlapRLog>=9) ? 0 : (size_t)1 << (params.cParams.windowLog - overlapRLog); + unsigned nbChunks = computeNbChunks(srcSize, params.cParams.windowLog, params.nbThreads); size_t const proposedChunkSize = (srcSize + (nbChunks-1)) / nbChunks; size_t const avgChunkSize = ((proposedChunkSize & 0x1FFFF) < 0x7FFF) ? proposedChunkSize + 0xFFFF : proposedChunkSize; /* avoid too small last block */ const char* const srcStart = (const char*)src; @@ -567,6 +583,8 @@ static size_t ZSTDMT_compress_advanced_internal( unsigned const compressWithinDst = (dstCapacity >= ZSTD_compressBound(srcSize)) ? nbChunks : (unsigned)(dstCapacity / ZSTD_compressBound(avgChunkSize)); /* presumes avgChunkSize >= 256 KB, which should be the case */ size_t frameStartPos = 0, dstBufferPos = 0; XXH64_state_t xxh64; + assert(jobParams.nbThreads == 0); + assert(mtctx->cctxPool.totalCCtx == params.nbThreads); DEBUGLOG(4, "nbChunks : %2u (chunkSize : %u bytes) ", nbChunks, (U32)avgChunkSize); if (nbChunks==1) { /* fallback to single-thread mode */ @@ -613,7 +631,7 @@ static size_t ZSTDMT_compress_advanced_internal( mtctx->jobs[u].jobCompleted_mutex = &mtctx->jobCompleted_mutex; mtctx->jobs[u].jobCompleted_cond = &mtctx->jobCompleted_cond; - if (cctxParams.fParams.checksumFlag) { + if (params.fParams.checksumFlag) { XXH64_update(&xxh64, srcStart + frameStartPos, chunkSize); } @@ -656,8 +674,8 @@ static size_t ZSTDMT_compress_advanced_internal( } } /* for (chunkID=0; chunkID dstCapacity) { error = ERROR(dstSize_tooSmall); @@ -682,10 +700,11 @@ size_t ZSTDMT_compress_advanced(ZSTDMT_CCtx* mtctx, ZSTD_CCtx_params cctxParams = mtctx->params; cctxParams.cParams = params.cParams; cctxParams.fParams = params.fParams; + cctxParams.overlapSizeLog = overlapLog; return ZSTDMT_compress_advanced_internal(mtctx, dst, dstCapacity, src, srcSize, - cdict, cctxParams, overlapLog); + cdict, cctxParams); } @@ -722,20 +741,22 @@ static void ZSTDMT_waitForAllJobsCompleted(ZSTDMT_CCtx* zcs) size_t ZSTDMT_initCStream_internal( ZSTDMT_CCtx* zcs, const void* dict, size_t dictSize, - const ZSTD_CDict* cdict, ZSTD_CCtx_params cctxParams, + const ZSTD_CDict* cdict, ZSTD_CCtx_params params, unsigned long long pledgedSrcSize) { DEBUGLOG(4, "ZSTDMT_initCStream_internal"); /* params are supposed to be fully validated at this point */ - assert(!ZSTD_isError(ZSTD_checkCParams(cctxParams.cParams))); + assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); assert(!((dict) && (cdict))); /* either dict or cdict, not both */ + assert(mtctx->cctxPool.totalCCtx == params.nbThreads); - if (zcs->nbThreads==1) { - ZSTD_CCtx_params const jobParams = ZSTDMT_makeJobCCtxParams(cctxParams); + if (params.nbThreads==1) { + ZSTD_CCtx_params const singleThreadParams = ZSTDMT_makeJobCCtxParams(params); DEBUGLOG(4, "single thread mode"); + assert(singleThreadParams.nbThreads == 0); return ZSTD_initCStream_internal(zcs->cctxPool->cctx[0], dict, dictSize, cdict, - jobParams, pledgedSrcSize); + singleThreadParams, pledgedSrcSize); } if (zcs->allJobsCompleted == 0) { /* previous compression not correctly finished */ @@ -744,14 +765,14 @@ size_t ZSTDMT_initCStream_internal( zcs->allJobsCompleted = 1; } - zcs->params = cctxParams; + zcs->params = params; zcs->frameContentSize = pledgedSrcSize; if (dict) { DEBUGLOG(4,"cdictLocal: %08X", (U32)(size_t)zcs->cdictLocal); ZSTD_freeCDict(zcs->cdictLocal); zcs->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, 0 /* byRef */, ZSTD_dm_auto, /* note : a loadPrefix becomes an internal CDict */ - cctxParams.cParams, zcs->cMem); + params.cParams, zcs->cMem); zcs->cdict = zcs->cdictLocal; if (zcs->cdictLocal == NULL) return ERROR(memory_allocation); } else { @@ -761,10 +782,10 @@ size_t ZSTDMT_initCStream_internal( zcs->cdict = cdict; } - zcs->targetDictSize = (zcs->overlapLog==0) ? 0 : (size_t)1 << (zcs->params.cParams.windowLog - (9 - zcs->overlapLog)); - DEBUGLOG(4, "overlapLog : %u ", zcs->overlapLog); + zcs->targetDictSize = (params.overlapSizeLog==0) ? 0 : (size_t)1 << (params.cParams.windowLog - (9 - params.overlapSizeLog)); + DEBUGLOG(4, "overlapLog : %u ", params.overlapSizeLog); DEBUGLOG(4, "overlap Size : %u KB", (U32)(zcs->targetDictSize>>10)); - zcs->targetSectionSize = zcs->sectionSize ? zcs->sectionSize : (size_t)1 << (zcs->params.cParams.windowLog + 2); + zcs->targetSectionSize = params.jobSize ? params.jobSize : (size_t)1 << (params.cParams.windowLog + 2); zcs->targetSectionSize = MAX(ZSTDMT_SECTION_SIZE_MIN, zcs->targetSectionSize); zcs->targetSectionSize = MAX(zcs->targetDictSize, zcs->targetSectionSize); DEBUGLOG(4, "Section Size : %u KB", (U32)(zcs->targetSectionSize>>10)); @@ -776,7 +797,7 @@ size_t ZSTDMT_initCStream_internal( zcs->nextJobID = 0; zcs->frameEnded = 0; zcs->allJobsCompleted = 0; - if (cctxParams.fParams.checksumFlag) XXH64_reset(&zcs->xxhState, 0); + if (params.fParams.checksumFlag) XXH64_reset(&zcs->xxhState, 0); return 0; } @@ -798,11 +819,12 @@ size_t ZSTDMT_initCStream_usingCDict(ZSTDMT_CCtx* mtctx, ZSTD_frameParameters fParams, unsigned long long pledgedSrcSize) { - ZSTD_CCtx_params params = ZSTD_getCCtxParamsFromCDict(cdict); + ZSTD_CCtx_params cctxParams = mtctx->params; + cctxParams.cParams = ZSTD_getCCtxParamsFromCDict(cdict).cParams; + cctxParams.fParams = fParams; if (cdict==NULL) return ERROR(dictionary_wrong); /* method incompatible with NULL cdict */ - params.fParams = fParams; return ZSTDMT_initCStream_internal(mtctx, NULL, 0 /*dictSize*/, cdict, - params, pledgedSrcSize); + cctxParams, pledgedSrcSize); } @@ -810,7 +832,7 @@ size_t ZSTDMT_initCStream_usingCDict(ZSTDMT_CCtx* mtctx, * pledgedSrcSize is optional and can be zero == unknown */ size_t ZSTDMT_resetCStream(ZSTDMT_CCtx* zcs, unsigned long long pledgedSrcSize) { - if (zcs->nbThreads==1) + if (zcs->params.nbThreads==1) return ZSTD_resetCStream(zcs->cctxPool->cctx[0], pledgedSrcSize); return ZSTDMT_initCStream_internal(zcs, NULL, 0, 0, zcs->params, pledgedSrcSize); @@ -965,7 +987,7 @@ size_t ZSTDMT_compressStream_generic(ZSTDMT_CCtx* mtctx, /* current frame being ended. Only flush/end are allowed. Or start new frame with init */ return ERROR(stage_wrong); } - if (mtctx->nbThreads==1) { /* delegate to single-thread (synchronous) */ + if (mtctx->params.nbThreads==1) { /* delegate to single-thread (synchronous) */ return ZSTD_compressStream_generic(mtctx->cctxPool->cctx[0], output, input, endOp); } @@ -977,7 +999,7 @@ size_t ZSTDMT_compressStream_generic(ZSTDMT_CCtx* mtctx, size_t const cSize = ZSTDMT_compress_advanced_internal(mtctx, (char*)output->dst + output->pos, output->size - output->pos, (const char*)input->src + input->pos, input->size - input->pos, - mtctx->cdict, mtctx->params, mtctx->overlapLog); + mtctx->cdict, mtctx->params); if (ZSTD_isError(cSize)) return cSize; input->pos = input->size; output->pos += cSize; @@ -1052,7 +1074,7 @@ static size_t ZSTDMT_flushStream_internal(ZSTDMT_CCtx* zcs, ZSTD_outBuffer* outp size_t ZSTDMT_flushStream(ZSTDMT_CCtx* zcs, ZSTD_outBuffer* output) { DEBUGLOG(5, "ZSTDMT_flushStream"); - if (zcs->nbThreads==1) + if (zcs->params.nbThreads==1) return ZSTD_flushStream(zcs->cctxPool->cctx[0], output); return ZSTDMT_flushStream_internal(zcs, output, 0 /* endFrame */); } @@ -1060,7 +1082,7 @@ size_t ZSTDMT_flushStream(ZSTDMT_CCtx* zcs, ZSTD_outBuffer* output) size_t ZSTDMT_endStream(ZSTDMT_CCtx* zcs, ZSTD_outBuffer* output) { DEBUGLOG(4, "ZSTDMT_endStream"); - if (zcs->nbThreads==1) + if (zcs->params.nbThreads==1) return ZSTD_endStream(zcs->cctxPool->cctx[0], output); return ZSTDMT_flushStream_internal(zcs, output, 1 /* endFrame */); } diff --git a/tests/Makefile b/tests/Makefile index 006059b72..f6389bd91 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -167,7 +167,7 @@ datagen : $(PRGDIR)/datagen.c datagencli.c $(CC) $(FLAGS) $^ -o $@$(EXT) roundTripCrash : $(ZSTD_FILES) roundTripCrash.c - $(CC) $(FLAGS) $^ -o $@$(EXT) + $(CC) $(FLAGS) $(MULTITHREAD) $^ -o $@$(EXT) longmatch : $(ZSTD_FILES) longmatch.c $(CC) $(FLAGS) $^ -o $@$(EXT) diff --git a/tests/roundTripCrash.c b/tests/roundTripCrash.c index fb14fa87b..cb0221c58 100644 --- a/tests/roundTripCrash.c +++ b/tests/roundTripCrash.c @@ -93,6 +93,9 @@ static size_t cctxParamRoundTripTest(void* resultBuff, size_t resultBuffCapacity /* Set parameters */ CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_compressionLevel, cLevel) ); + CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_nbThreads, 2) ); + CHECK_Z( ZSTD_CCtxParam_setParameter(cctxParams, ZSTD_p_overlapSizeLog, 5) ); + /* Apply parameters */ CHECK_Z( ZSTD_CCtx_applyCCtxParams(cctx, cctxParams) ); From 0744592d385df6718f46b7e7d8201609e0459a40 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Fri, 25 Aug 2017 13:23:16 -0700 Subject: [PATCH 39/52] Add function initializing cctxParams from clevel --- lib/compress/zstd_compress.c | 7 ++++++- lib/compress/zstdmt_compress.c | 4 ++-- lib/zstd.h | 11 ++++++++--- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index c87037196..c8cfd6c94 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -282,7 +282,12 @@ size_t ZSTD_resetCCtxParams(ZSTD_CCtx_params* params) return 0; } -size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params) +size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* cctxParams, int compressionLevel) { + ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, 0); + return ZSTD_initCCtxParams_advanced(cctxParams, params); +} + +size_t ZSTD_initCCtxParams_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params) { if (!cctxParams) { return ERROR(GENERIC); } CHECK_F( ZSTD_checkCParams(params.cParams) ); diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index 7c3bd0198..c93d784da 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -584,7 +584,7 @@ static size_t ZSTDMT_compress_advanced_internal( size_t frameStartPos = 0, dstBufferPos = 0; XXH64_state_t xxh64; assert(jobParams.nbThreads == 0); - assert(mtctx->cctxPool.totalCCtx == params.nbThreads); + assert(mtctx->cctxPool->totalCCtx == params.nbThreads); DEBUGLOG(4, "nbChunks : %2u (chunkSize : %u bytes) ", nbChunks, (U32)avgChunkSize); if (nbChunks==1) { /* fallback to single-thread mode */ @@ -748,7 +748,7 @@ size_t ZSTDMT_initCStream_internal( /* params are supposed to be fully validated at this point */ assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); assert(!((dict) && (cdict))); /* either dict or cdict, not both */ - assert(mtctx->cctxPool.totalCCtx == params.nbThreads); + assert(zcs->cctxPool->totalCCtx == params.nbThreads); if (params.nbThreads==1) { ZSTD_CCtx_params const singleThreadParams = ZSTDMT_makeJobCCtxParams(params); diff --git a/lib/zstd.h b/lib/zstd.h index 01fbb18cd..bfbe82849 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -1128,9 +1128,14 @@ ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void); ZSTDLIB_API size_t ZSTD_resetCCtxParams(ZSTD_CCtx_params* params); /*! ZSTD_initCCtxParams() : - * Set the compression and frame parameters of cctxParams according to params. - * All other parameters are reset to their default values. */ -ZSTDLIB_API size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params); + * Initializes the compression parameters of cctxParams according to + * compression level. All other parameters are reset to their default values. */ +ZSTDLIB_API size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* cctxParams, int compressionLevel); + +/*! ZSTD_initCCtxParams_advanced() : + * Initializes the compression and frame parameters of cctxParams according to + * params. All other parameters are reset to their default values. */ +ZSTDLIB_API size_t ZSTD_initCCtxParams_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params); ZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params); From 18224608ffa214221b16e55db8759f5486985568 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Fri, 25 Aug 2017 13:58:41 -0700 Subject: [PATCH 40/52] Remove ZSTD_setCCtxParameter() --- lib/compress/zstd_compress.c | 22 ---------------------- lib/compress/zstdmt_compress.c | 3 --- lib/zstd.h | 30 ++++++++++++------------------ tests/fuzzer.c | 1 - 4 files changed, 12 insertions(+), 44 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index c8cfd6c94..74cbe53b0 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -197,28 +197,6 @@ size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs) /* private API call, for dictBuilder only */ const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx) { return &(ctx->seqStore); } -/* older variant; will be deprecated */ -/* Both requested and applied params need to be set as this function can be - * called before/after ZSTD_parameters have been applied. */ -size_t ZSTD_setCCtxParameter(ZSTD_CCtx* cctx, ZSTD_CCtxParameter param, unsigned value) -{ - switch(param) - { - case ZSTD_p_forceWindow : - cctx->requestedParams.forceWindow = value>0; - cctx->appliedParams.forceWindow = value>0; - cctx->loadedDictEnd = 0; - return 0; - ZSTD_STATIC_ASSERT(ZSTD_dm_auto==0); - ZSTD_STATIC_ASSERT(ZSTD_dm_rawContent==1); - case ZSTD_p_forceRawDict : - cctx->requestedParams.dictMode = (ZSTD_dictMode_e)(value>0); - cctx->appliedParams.dictMode = (ZSTD_dictMode_e)(value>0); - return 0; - default: return ERROR(parameter_unsupported); - } -} - #define ZSTD_CLEVEL_CUSTOM 999 static void ZSTD_cLevelToCCtxParams_srcSize(ZSTD_CCtx_params* params, U64 srcSize) diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index c93d784da..5f0f254c2 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -347,9 +347,6 @@ void ZSTDMT_compressChunk(void* jobDescription) ZSTD_CCtxParam_setParameter(&jobParams, ZSTD_p_dictMode, (U32)ZSTD_dm_rawContent); size_t const forceWindowError = ZSTD_CCtxParam_setParameter(&jobParams, ZSTD_p_forceMaxWindow, !job->firstChunk); - /* Note: ZSTD_setCCtxParameter() should not be used here. - * ZSTD_compressBegin_advanced_internal() copies the ZSTD_CCtx_params - * directly to appliedParams. */ size_t const initError = ZSTD_compressBegin_advanced_internal(cctx, job->srcStart, job->dictSize, jobParams, job->fullFrameSize); if (ZSTD_isError(initError) || ZSTD_isError(dictModeError) || ZSTD_isError(forceWindowError)) { job->cSize = initError; goto _endJob; } diff --git a/lib/zstd.h b/lib/zstd.h index bfbe82849..0c970f3f9 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -558,17 +558,6 @@ ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem); ZSTDLIB_API ZSTD_CCtx* ZSTD_initStaticCCtx(void* workspace, size_t workspaceSize); -/* !!! To be deprecated !!! */ -typedef enum { - ZSTD_p_forceWindow, /* Force back-references to remain < windowSize, even when referencing Dictionary content (default:0) */ - ZSTD_p_forceRawDict /* Force loading dictionary in "content-only" mode (no header analysis) */ -} ZSTD_CCtxParameter; -/*! ZSTD_setCCtxParameter() : - * Set advanced parameters, selected through enum ZSTD_CCtxParameter - * @result : 0, or an error code (which can be tested with ZSTD_isError()) */ -ZSTDLIB_API size_t ZSTD_setCCtxParameter(ZSTD_CCtx* cctx, ZSTD_CCtxParameter param, unsigned value); - - /*! ZSTD_createCDict_byReference() : * Create a digested dictionary for compression * Dictionary content is simply referenced, and therefore stays in dictBuffer. @@ -1119,22 +1108,25 @@ size_t ZSTD_compress_generic_simpleArgs ( * - ZSTD_freeCCtxParams() : Free the memory. * * This can be used with ZSTD_estimateCCtxSize_opaque() for static allocation - * for single-threaded compression. */ - + * for single-threaded compression. + */ ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void); /*! ZSTD_resetCCtxParams() : - * Reset params to default, with the default compression level. */ + * Reset params to default, with the default compression level. + */ ZSTDLIB_API size_t ZSTD_resetCCtxParams(ZSTD_CCtx_params* params); /*! ZSTD_initCCtxParams() : * Initializes the compression parameters of cctxParams according to - * compression level. All other parameters are reset to their default values. */ + * compression level. All other parameters are reset to their default values. + */ ZSTDLIB_API size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* cctxParams, int compressionLevel); /*! ZSTD_initCCtxParams_advanced() : * Initializes the compression and frame parameters of cctxParams according to - * params. All other parameters are reset to their default values. */ + * params. All other parameters are reset to their default values. + */ ZSTDLIB_API size_t ZSTD_initCCtxParams_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params); ZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params); @@ -1144,14 +1136,16 @@ ZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params); * Set one compression parameter, selected by enum ZSTD_cParameter. * Parameters must be applied to a ZSTD_CCtx using ZSTD_CCtx_applyCCtxParams(). * Note : when `value` is an enum, cast it to unsigned for proper type checking. - * @result : 0, or an error code (which can be tested with ZSTD_isError()). */ + * @result : 0, or an error code (which can be tested with ZSTD_isError()). + */ ZSTDLIB_API size_t ZSTD_CCtxParam_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, unsigned value); /*! ZSTD_CCtx_applyCCtxParams() : * Apply a set of ZSTD_CCtx_params to the compression context. * This must be done before the dictionary is loaded. * The pledgedSrcSize is treated as unknown. - * Multithreading parameters are applied only if nbThreads > 1. */ + * Multithreading parameters are applied only if nbThreads > 1. + */ ZSTDLIB_API size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params); /** diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 046c67ea8..3f7595e99 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -1334,7 +1334,6 @@ static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, U32 const maxD } CHECK_Z( ZSTD_copyCCtx(ctx, refCtx, 0) ); } - ZSTD_setCCtxParameter(ctx, ZSTD_p_forceWindow, FUZ_rand(&lseed) & 1); { U32 const nbChunks = (FUZ_rand(&lseed) & 127) + 2; U32 n; From 2adde898c800e30ec43afef5eec5f5caf7ffc950 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Fri, 25 Aug 2017 16:13:40 -0700 Subject: [PATCH 41/52] Fix typo with ZSTDMT_parameter --- lib/common/zstd_internal.h | 2 +- lib/compress/zstd_compress.c | 2 +- lib/compress/zstdmt_compress.c | 4 ++-- lib/compress/zstdmt_compress.h | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index 393a17247..53de69739 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -297,7 +297,7 @@ struct ZSTD_CCtx_params_s { ZSTD_dictMode_e dictMode; /* select restricting dictionary to "rawContent" * or "fullDict" only */ - /* Multithreading: used only to set mtctx parameters */ + /* Multithreading: used to pass parameters to mtctx */ U32 nbThreads; unsigned jobSize; unsigned overlapSizeLog; diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 74cbe53b0..6f4121c75 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -291,7 +291,7 @@ static ZSTD_CCtx_params ZSTD_assignParamsToCCtxParams( } } size_t ZSTDMT_CCtxParam_setMTCtxParameter( - ZSTD_CCtx_params* params, ZSDTMT_parameter parameter, unsigned value); + ZSTD_CCtx_params* params, ZSTDMT_parameter parameter, unsigned value); size_t ZSTDMT_initializeCCtxParameters(ZSTD_CCtx_params* params, unsigned nbThreads); size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned value) diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index 5f0f254c2..8687ea3c0 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -519,7 +519,7 @@ size_t ZSTDMT_sizeof_CCtx(ZSTDMT_CCtx* mtctx) /* Internal only */ size_t ZSTDMT_CCtxParam_setMTCtxParameter( - ZSTD_CCtx_params* params, ZSDTMT_parameter parameter, unsigned value) { + ZSTD_CCtx_params* params, ZSTDMT_parameter parameter, unsigned value) { switch(parameter) { case ZSTDMT_p_sectionSize : @@ -534,7 +534,7 @@ size_t ZSTDMT_CCtxParam_setMTCtxParameter( } } -size_t ZSTDMT_setMTCtxParameter(ZSTDMT_CCtx* mtctx, ZSDTMT_parameter parameter, unsigned value) +size_t ZSTDMT_setMTCtxParameter(ZSTDMT_CCtx* mtctx, ZSTDMT_parameter parameter, unsigned value) { switch(parameter) { diff --git a/lib/compress/zstdmt_compress.h b/lib/compress/zstdmt_compress.h index 843a240aa..4381af363 100644 --- a/lib/compress/zstdmt_compress.h +++ b/lib/compress/zstdmt_compress.h @@ -80,19 +80,19 @@ ZSTDLIB_API size_t ZSTDMT_initCStream_usingCDict(ZSTDMT_CCtx* mtctx, ZSTD_frameParameters fparams, unsigned long long pledgedSrcSize); /* note : zero means empty */ -/* ZSDTMT_parameter : +/* ZSTDMT_parameter : * List of parameters that can be set using ZSTDMT_setMTCtxParameter() */ typedef enum { ZSTDMT_p_sectionSize, /* size of input "section". Each section is compressed in parallel. 0 means default, which is dynamically determined within compression functions */ ZSTDMT_p_overlapSectionLog /* Log of overlapped section; 0 == no overlap, 6(default) == use 1/8th of window, >=9 == use full window */ -} ZSDTMT_parameter; +} ZSTDMT_parameter; /* ZSTDMT_setMTCtxParameter() : * allow setting individual parameters, one at a time, among a list of enums defined in ZSTDMT_parameter. * The function must be called typically after ZSTD_createCCtx(). * Parameters not explicitly reset by ZSTDMT_init*() remain the same in consecutive compression sessions. * @return : 0, or an error code (which can be tested using ZSTD_isError()) */ -ZSTDLIB_API size_t ZSTDMT_setMTCtxParameter(ZSTDMT_CCtx* mtctx, ZSDTMT_parameter parameter, unsigned value); +ZSTDLIB_API size_t ZSTDMT_setMTCtxParameter(ZSTDMT_CCtx* mtctx, ZSTDMT_parameter parameter, unsigned value); /*! ZSTDMT_compressStream_generic() : From 024098a47dba898d60e578cbc93bdc9fca6d8016 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Fri, 25 Aug 2017 17:58:28 -0700 Subject: [PATCH 42/52] Fix parameter retrieval from cdict --- lib/common/zstd_internal.h | 4 ++-- lib/compress/zstd_compress.c | 15 ++++++++------- lib/compress/zstdmt_compress.c | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index 53de69739..bfec42fad 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -369,9 +369,9 @@ size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs, ZSTD_inBuffer* input, ZSTD_EndDirective const flushMode); -/*! ZSTD_getParamsFromCDict() : +/*! ZSTD_getCParamsFromCDict() : * as the name implies */ -ZSTD_CCtx_params ZSTD_getCCtxParamsFromCDict(const ZSTD_CDict* cdict); +ZSTD_compressionParameters ZSTD_getCParamsFromCDict(const ZSTD_CDict* cdict); /* INTERNAL */ size_t ZSTD_compressBegin_advanced_internal(ZSTD_CCtx* cctx, diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 6f4121c75..1f570a92a 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -477,7 +477,7 @@ size_t ZSTD_CCtxParam_setParameter( * This function should be updated whenever ZSTD_CCtx_params is updated. * Parameters are copied manually before the dictionary is loaded. * The multithreading parameters jobSize and overlapSizeLog are set only if - * nbThreads >= 1. + * nbThreads > 1. * * Pledged srcSize is treated as unknown. */ @@ -3735,8 +3735,8 @@ ZSTD_CDict* ZSTD_initStaticCDict(void* workspace, size_t workspaceSize, return cdict; } -ZSTD_CCtx_params ZSTD_getCCtxParamsFromCDict(const ZSTD_CDict* cdict) { - return cdict->refContext->appliedParams; +ZSTD_compressionParameters ZSTD_getCParamsFromCDict(const ZSTD_CDict* cdict) { + return cdict->refContext->appliedParams.cParams; } /* ZSTD_compressBegin_usingCDict_advanced() : @@ -3746,7 +3746,8 @@ size_t ZSTD_compressBegin_usingCDict_advanced( ZSTD_frameParameters const fParams, unsigned long long const pledgedSrcSize) { if (cdict==NULL) return ERROR(dictionary_wrong); - { ZSTD_CCtx_params params = ZSTD_getCCtxParamsFromCDict(cdict); + { ZSTD_CCtx_params params = cctx->requestedParams; + params.cParams = ZSTD_getCParamsFromCDict(cdict); params.fParams = fParams; params.dictMode = ZSTD_dm_auto; DEBUGLOG(5, "ZSTD_compressBegin_usingCDict_advanced"); @@ -3892,8 +3893,7 @@ size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, if (zcs->cdictLocal == NULL) return ERROR(memory_allocation); } else { if (cdict) { - ZSTD_CCtx_params const cdictParams = ZSTD_getCCtxParamsFromCDict(cdict); - params.cParams = cdictParams.cParams; /* cParams are enforced from cdict */ + params.cParams = ZSTD_getCParamsFromCDict(cdict); /* cParams are enforced from cdict */ } ZSTD_freeCDict(zcs->cdictLocal); zcs->cdictLocal = NULL; @@ -3914,7 +3914,8 @@ size_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize) { /* cannot handle NULL cdict (does not know what to do) */ if (!cdict) return ERROR(dictionary_wrong); - { ZSTD_CCtx_params params = ZSTD_getCCtxParamsFromCDict(cdict); + { ZSTD_CCtx_params params = zcs->requestedParams; + params.cParams = ZSTD_getCParamsFromCDict(cdict); params.fParams = fParams; return ZSTD_initCStream_internal(zcs, NULL, 0, cdict, diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index 8687ea3c0..b1a681e74 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -817,7 +817,7 @@ size_t ZSTDMT_initCStream_usingCDict(ZSTDMT_CCtx* mtctx, unsigned long long pledgedSrcSize) { ZSTD_CCtx_params cctxParams = mtctx->params; - cctxParams.cParams = ZSTD_getCCtxParamsFromCDict(cdict).cParams; + cctxParams.cParams = ZSTD_getCParamsFromCDict(cdict); cctxParams.fParams = fParams; if (cdict==NULL) return ERROR(dictionary_wrong); /* method incompatible with NULL cdict */ return ZSTDMT_initCStream_internal(mtctx, NULL, 0 /*dictSize*/, cdict, From 0e56a84a1ea5c22530eeafe8938d8f56e45cce4b Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Mon, 28 Aug 2017 19:25:17 -0700 Subject: [PATCH 43/52] Fix getting cParams from CCtxParams --- lib/compress/zstd_compress.c | 47 ++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 1f570a92a..4a8e9c8bd 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -197,12 +197,19 @@ size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs) /* private API call, for dictBuilder only */ const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx) { return &(ctx->seqStore); } - #define ZSTD_CLEVEL_CUSTOM 999 + +static ZSTD_compressionParameters ZSTD_getCParamsFromCCtxParams( + ZSTD_CCtx_params params, U64 srcSizeHint, size_t dictSize) +{ + return (params.compressionLevel == ZSTD_CLEVEL_CUSTOM ? + params.cParams : + ZSTD_getCParams(params.compressionLevel, srcSizeHint, dictSize)); +} + static void ZSTD_cLevelToCCtxParams_srcSize(ZSTD_CCtx_params* params, U64 srcSize) { - if (params->compressionLevel == ZSTD_CLEVEL_CUSTOM) return; - params->cParams = ZSTD_getCParams(params->compressionLevel, srcSize, 0); + params->cParams = ZSTD_getCParamsFromCCtxParams(*params, srcSize, 0); params->compressionLevel = ZSTD_CLEVEL_CUSTOM; } @@ -254,15 +261,14 @@ size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params) size_t ZSTD_resetCCtxParams(ZSTD_CCtx_params* params) { - if (!params) { return ERROR(GENERIC); } - memset(params, 0, sizeof(ZSTD_CCtx_params)); - params->compressionLevel = ZSTD_CLEVEL_DEFAULT; - return 0; + return ZSTD_initCCtxParams(params, ZSTD_CLEVEL_DEFAULT); } size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* cctxParams, int compressionLevel) { - ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, 0); - return ZSTD_initCCtxParams_advanced(cctxParams, params); + if (!cctxParams) { return ERROR(GENERIC); } + memset(cctxParams, 0, sizeof(ZSTD_CCtx_params)); + cctxParams->compressionLevel = compressionLevel; + return 0; } size_t ZSTD_initCCtxParams_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params) @@ -530,9 +536,7 @@ size_t ZSTD_CCtx_loadDictionary_internal( cctx->cdict = NULL; } else { ZSTD_compressionParameters const cParams = - cctx->requestedParams.compressionLevel == ZSTD_CLEVEL_CUSTOM ? - cctx->requestedParams.cParams : - ZSTD_getCParams(cctx->requestedParams.compressionLevel, 0, dictSize); + ZSTD_getCParamsFromCCtxParams(cctx->requestedParams, 0, dictSize); cctx->cdictLocal = ZSTD_createCDict_advanced( dict, dictSize, byReference, @@ -672,7 +676,8 @@ size_t ZSTD_estimateCCtxSize_advanced_opaque(const ZSTD_CCtx_params* params) if (params->nbThreads > 1) { return 0; } - { ZSTD_compressionParameters const cParams = params->cParams; + { ZSTD_compressionParameters const cParams = + ZSTD_getCParamsFromCCtxParams(*params, 0, 0); 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; @@ -3182,10 +3187,8 @@ size_t ZSTD_compressContinue (ZSTD_CCtx* cctx, size_t ZSTD_getBlockSize(const ZSTD_CCtx* cctx) { - U32 const cLevel = cctx->appliedParams.compressionLevel; - ZSTD_compressionParameters cParams = (cLevel == ZSTD_CLEVEL_CUSTOM) ? - cctx->appliedParams.cParams : - ZSTD_getCParams(cLevel, 0, 0); + ZSTD_compressionParameters cParams = + ZSTD_getCParamsFromCCtxParams(cctx->appliedParams, 0, 0); return MIN (ZSTD_BLOCKSIZE_MAX, 1 << cParams.windowLog); } @@ -3859,10 +3862,8 @@ size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize) { ZSTD_CCtx_params params = zcs->requestedParams; params.fParams.contentSizeFlag = (pledgedSrcSize > 0); + params.cParams = ZSTD_getCParamsFromCCtxParams(params, pledgedSrcSize, 0); DEBUGLOG(5, "ZSTD_resetCStream"); - if (params.compressionLevel != ZSTD_CLEVEL_CUSTOM) { - params.cParams = ZSTD_getCParams(params.compressionLevel, pledgedSrcSize, 0 /* dictSize */); - } return ZSTD_resetCStream_internal(zcs, NULL, 0, zcs->cdict, params, pledgedSrcSize); } @@ -4152,9 +4153,8 @@ size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, const void* const prefix = cctx->prefix; size_t const prefixSize = cctx->prefixSize; ZSTD_CCtx_params params = cctx->requestedParams; - if (params.compressionLevel != ZSTD_CLEVEL_CUSTOM) - params.cParams = ZSTD_getCParams(params.compressionLevel, - cctx->pledgedSrcSizePlusOne-1, 0 /*dictSize*/); + params.cParams = ZSTD_getCParamsFromCCtxParams( + cctx->requestedParams, cctx->pledgedSrcSizePlusOne-1, 0 /*dictSize*/); cctx->prefix = NULL; cctx->prefixSize = 0; /* single usage */ assert(prefix==NULL || cctx->cdict==NULL); /* only one can be set */ @@ -4382,6 +4382,7 @@ ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long l if (compressionLevel > ZSTD_MAX_CLEVEL) compressionLevel = ZSTD_MAX_CLEVEL; { ZSTD_compressionParameters const cp = ZSTD_defaultCParameters[tableID][compressionLevel]; return ZSTD_adjustCParams_internal(cp, srcSizeHint, dictSize); } + } /*! ZSTD_getParams() : From b5b9275e6780107aaf54212a038b3daeba247400 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Tue, 29 Aug 2017 10:49:29 -0700 Subject: [PATCH 44/52] Rename estimateCCtxSize_advanced() and estimateCStreamSize_advanced() --- lib/compress/zstd_compress.c | 25 +++++++++++++------------ lib/zstd.h | 26 +++++++++++++------------- tests/paramgrill.c | 4 ++-- tests/zstreamtest.c | 2 +- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 4a8e9c8bd..26de0efd8 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -670,7 +670,7 @@ ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, u return ZSTD_adjustCParams_internal(cPar, srcSize, dictSize); } -size_t ZSTD_estimateCCtxSize_advanced_opaque(const ZSTD_CCtx_params* params) +size_t ZSTD_estimateCCtxSize_advanced_usingCCtxParams(const ZSTD_CCtx_params* params) { /* Estimate CCtx size is supported for single-threaded compression only. */ if (params->nbThreads > 1) { @@ -703,24 +703,24 @@ size_t ZSTD_estimateCCtxSize_advanced_opaque(const ZSTD_CCtx_params* params) } } -size_t ZSTD_estimateCCtxSize_advanced(ZSTD_compressionParameters cParams) +size_t ZSTD_estimateCCtxSize_advanced_usingCParams(ZSTD_compressionParameters cParams) { ZSTD_CCtx_params const params = ZSTD_makeCCtxParamsFromCParams(cParams); - return ZSTD_estimateCCtxSize_advanced_opaque(¶ms); + return ZSTD_estimateCCtxSize_advanced_usingCCtxParams(¶ms); } size_t ZSTD_estimateCCtxSize(int compressionLevel) { ZSTD_compressionParameters const cParams = ZSTD_getCParams(compressionLevel, 0, 0); - return ZSTD_estimateCCtxSize_advanced(cParams); + return ZSTD_estimateCCtxSize_advanced_usingCParams(cParams); } -size_t ZSTD_estimateCStreamSize_advanced_opaque(const ZSTD_CCtx_params* params) +size_t ZSTD_estimateCStreamSize_advanced_usingCCtxParams(const ZSTD_CCtx_params* params) { if (params->nbThreads > 1) { return 0; } - { size_t const CCtxSize = ZSTD_estimateCCtxSize_advanced_opaque(params); + { size_t const CCtxSize = ZSTD_estimateCCtxSize_advanced_usingCCtxParams(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; @@ -730,15 +730,15 @@ size_t ZSTD_estimateCStreamSize_advanced_opaque(const ZSTD_CCtx_params* params) } } -size_t ZSTD_estimateCStreamSize_advanced(ZSTD_compressionParameters cParams) +size_t ZSTD_estimateCStreamSize_advanced_usingCParams(ZSTD_compressionParameters cParams) { ZSTD_CCtx_params const params = ZSTD_makeCCtxParamsFromCParams(cParams); - return ZSTD_estimateCStreamSize_advanced_opaque(¶ms); + return ZSTD_estimateCStreamSize_advanced_usingCCtxParams(¶ms); } size_t ZSTD_estimateCStreamSize(int compressionLevel) { ZSTD_compressionParameters const cParams = ZSTD_getCParams(compressionLevel, 0, 0); - return ZSTD_estimateCStreamSize_advanced(cParams); + return ZSTD_estimateCStreamSize_advanced_usingCParams(cParams); } static U32 ZSTD_equivalentCParams(ZSTD_compressionParameters cParams1, @@ -3585,8 +3585,9 @@ size_t ZSTD_compress(void* dst, size_t dstCapacity, const void* src, size_t srcS size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, unsigned byReference) { DEBUGLOG(5, "sizeof(ZSTD_CDict) : %u", (U32)sizeof(ZSTD_CDict)); - DEBUGLOG(5, "CCtx estimate : %u", (U32)ZSTD_estimateCCtxSize_advanced(cParams)); - return sizeof(ZSTD_CDict) + ZSTD_estimateCCtxSize_advanced(cParams) + DEBUGLOG(5, "CCtx estimate : %u", + (U32)ZSTD_estimateCCtxSize_advanced_usingCParams(cParams)); + return sizeof(ZSTD_CDict) + ZSTD_estimateCCtxSize_advanced_usingCParams(cParams) + (byReference ? 0 : dictSize); } @@ -3709,7 +3710,7 @@ ZSTD_CDict* ZSTD_initStaticCDict(void* workspace, size_t workspaceSize, unsigned byReference, ZSTD_dictMode_e dictMode, ZSTD_compressionParameters cParams) { - size_t const cctxSize = ZSTD_estimateCCtxSize_advanced(cParams); + size_t const cctxSize = ZSTD_estimateCCtxSize_advanced_usingCParams(cParams); size_t const neededSize = sizeof(ZSTD_CDict) + (byReference ? 0 : dictSize) + cctxSize; ZSTD_CDict* const cdict = (ZSTD_CDict*) workspace; diff --git a/lib/zstd.h b/lib/zstd.h index 0c970f3f9..6d91e92a4 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -496,21 +496,21 @@ ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict); * of a future {D,C}Ctx, before its creation. * ZSTD_estimateCCtxSize() will provide a budget large enough for any compression level up to selected one. * 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(). Only single-threaded compression is supported. This function will return 0 if ZSTD_p_nbThreads is set to a value > 1. + * If srcSize is known to always be small, ZSTD_estimateCCtxSize_advanced_usingCParams() can provide a tighter estimation. + * ZSTD_estimateCCtxSize_advanced_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel. + * ZSTD_estimateCCtxSize_advanced_usingCCtxParams() 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); -ZSTDLIB_API size_t ZSTD_estimateCCtxSize_advanced_opaque(const ZSTD_CCtx_params* params); +ZSTDLIB_API size_t ZSTD_estimateCCtxSize_advanced_usingCParams(ZSTD_compressionParameters cParams); +ZSTDLIB_API size_t ZSTD_estimateCCtxSize_advanced_usingCCtxParams(const ZSTD_CCtx_params* params); ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void); -/*! ZSTD_estimate?StreamSize() : +/*! ZSTD_estimateCStreamSize() : * ZSTD_estimateCStreamSize() will provide a budget large enough for any compression level up to selected one. * 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(). Only single-threaded compression is supported. This function will return 0 if ZSTD_p_nbThreads is set to a value > 1. + * If srcSize is known to always be small, ZSTD_estimateCStreamSize_advanced_usingCParams() can provide a tighter estimation. + * ZSTD_estimateCStreamSize_advanced_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel. + * ZSTD_estimateCStreamSize_advanced_usingCCtxParams() 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, @@ -519,14 +519,14 @@ ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void); * an internal ?Dict will be created, which additional size is not estimated here. * In this case, get total size by adding ZSTD_estimate?DictSize */ ZSTDLIB_API size_t ZSTD_estimateCStreamSize(int compressionLevel); -ZSTDLIB_API size_t ZSTD_estimateCStreamSize_advanced(ZSTD_compressionParameters cParams); -ZSTDLIB_API size_t ZSTD_estimateCStreamSize_advanced_opaque(const ZSTD_CCtx_params* params); +ZSTDLIB_API size_t ZSTD_estimateCStreamSize_advanced_usingCParams(ZSTD_compressionParameters cParams); +ZSTDLIB_API size_t ZSTD_estimateCStreamSize_advanced_usingCCtxParams(const ZSTD_CCtx_params* params); ZSTDLIB_API size_t ZSTD_estimateDStreamSize(size_t windowSize); ZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize); -/*! ZSTD_estimate?DictSize() : +/*! ZSTD_estimateCDictSize() : * ZSTD_estimateCDictSize() will bet that src size is relatively "small", and content is copied, like ZSTD_createCDict(). - * ZSTD_estimateCStreamSize_advanced() makes it possible to control precisely compression parameters, like ZSTD_createCDict_advanced(). + * ZSTD_estimateCStreamSize_advanced_usingCParams() makes it possible to control precisely compression parameters, like ZSTD_createCDict_advanced(). * Note : dictionary created "byReference" are smaller */ ZSTDLIB_API size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel); ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, unsigned byReference); diff --git a/tests/paramgrill.c b/tests/paramgrill.c index da06ccb52..1818e5121 100644 --- a/tests/paramgrill.c +++ b/tests/paramgrill.c @@ -390,8 +390,8 @@ static int BMK_seed(winnerInfo_t* winners, const ZSTD_compressionParameters para double W_DMemUsed_note = W_ratioNote * ( 40 + 9*cLevel) - log((double)W_DMemUsed); double O_DMemUsed_note = O_ratioNote * ( 40 + 9*cLevel) - log((double)O_DMemUsed); - size_t W_CMemUsed = (1 << params.windowLog) + ZSTD_estimateCCtxSize_advanced(params); - size_t O_CMemUsed = (1 << winners[cLevel].params.windowLog) + ZSTD_estimateCCtxSize_advanced(winners[cLevel].params); + size_t W_CMemUsed = (1 << params.windowLog) + ZSTD_estimateCCtxSize_advanced_usingCParams(params); + size_t O_CMemUsed = (1 << winners[cLevel].params.windowLog) + ZSTD_estimateCCtxSize_advanced_usingCParams(winners[cLevel].params); double W_CMemUsed_note = W_ratioNote * ( 50 + 13*cLevel) - log((double)W_CMemUsed); double O_CMemUsed_note = O_ratioNote * ( 50 + 13*cLevel) - log((double)O_CMemUsed); diff --git a/tests/zstreamtest.c b/tests/zstreamtest.c index 0373676d0..0bf833c51 100644 --- a/tests/zstreamtest.c +++ b/tests/zstreamtest.c @@ -197,7 +197,7 @@ static int basicUnitTests(U32 seed, double compressibility, ZSTD_customMem custo /* context size functions */ DISPLAYLEVEL(3, "test%3i : estimate CStream size : ", testNb++); { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBufferSize, dictSize); - size_t const s = ZSTD_estimateCStreamSize_advanced(cParams) + size_t const s = ZSTD_estimateCStreamSize_advanced_usingCParams(cParams) /* uses ZSTD_initCStream_usingDict() */ + ZSTD_estimateCDictSize_advanced(dictSize, cParams, 0); if (ZSTD_isError(s)) goto _output_error; From c88fb9267f6bad0d0477960d535a553ee33b7840 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Tue, 29 Aug 2017 11:55:02 -0700 Subject: [PATCH 45/52] Replace 'byReference' with enum --- lib/compress/zstd_compress.c | 40 ++++++++++++++++++------------- lib/compress/zstdmt_compress.c | 2 +- lib/decompress/zstd_decompress.c | 27 +++++++++++---------- lib/zstd.h | 28 ++++++++++++++-------- programs/bench.c | 2 +- tests/fuzzer.c | 18 +++++++------- tests/zstreamtest.c | 6 ++--- zlibWrapper/examples/zwrapbench.c | 2 +- 8 files changed, 70 insertions(+), 55 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 26de0efd8..511d1c3a0 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -525,7 +525,8 @@ ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long lo } size_t ZSTD_CCtx_loadDictionary_internal( - ZSTD_CCtx* cctx, const void* dict, size_t dictSize, unsigned byReference) + ZSTD_CCtx* cctx, const void* dict, size_t dictSize, + ZSTD_dictLoadMethod_e dictLoadMethod) { if (cctx->streamStage != zcss_init) return ERROR(stage_wrong); if (cctx->staticSize) return ERROR(memory_allocation); /* no malloc for static CCtx */ @@ -539,7 +540,7 @@ size_t ZSTD_CCtx_loadDictionary_internal( ZSTD_getCParamsFromCCtxParams(cctx->requestedParams, 0, dictSize); cctx->cdictLocal = ZSTD_createCDict_advanced( dict, dictSize, - byReference, + dictLoadMethod, cctx->requestedParams.dictMode, cParams, cctx->customMem); cctx->cdict = cctx->cdictLocal; @@ -552,12 +553,12 @@ size_t ZSTD_CCtx_loadDictionary_internal( ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_byReference( ZSTD_CCtx* cctx, const void* dict, size_t dictSize) { - return ZSTD_CCtx_loadDictionary_internal(cctx, dict, dictSize, 1); + return ZSTD_CCtx_loadDictionary_internal(cctx, dict, dictSize, ZSTD_dlm_byRef); } ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize) { - return ZSTD_CCtx_loadDictionary_internal(cctx, dict, dictSize, 0); + return ZSTD_CCtx_loadDictionary_internal(cctx, dict, dictSize, ZSTD_dlm_byCopy); } @@ -3582,13 +3583,15 @@ size_t ZSTD_compress(void* dst, size_t dstCapacity, const void* src, size_t srcS /*! ZSTD_estimateCDictSize_advanced() : * Estimate amount of memory that will be needed to create a dictionary with following arguments */ -size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, unsigned byReference) +size_t ZSTD_estimateCDictSize_advanced( + size_t dictSize, ZSTD_compressionParameters cParams, + ZSTD_dictLoadMethod_e dictLoadMethod) { DEBUGLOG(5, "sizeof(ZSTD_CDict) : %u", (U32)sizeof(ZSTD_CDict)); DEBUGLOG(5, "CCtx estimate : %u", (U32)ZSTD_estimateCCtxSize_advanced_usingCParams(cParams)); return sizeof(ZSTD_CDict) + ZSTD_estimateCCtxSize_advanced_usingCParams(cParams) - + (byReference ? 0 : dictSize); + + (dictLoadMethod == ZSTD_dlm_byRef ? 0 : dictSize); } size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel) @@ -3608,11 +3611,12 @@ size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict) static size_t ZSTD_initCDict_internal( ZSTD_CDict* cdict, const void* dictBuffer, size_t dictSize, - unsigned byReference, ZSTD_dictMode_e dictMode, + ZSTD_dictLoadMethod_e dictLoadMethod, + ZSTD_dictMode_e dictMode, ZSTD_compressionParameters cParams) { DEBUGLOG(5, "ZSTD_initCDict_internal, mode %u", (U32)dictMode); - if ((byReference) || (!dictBuffer) || (!dictSize)) { + if ((dictLoadMethod == ZSTD_dlm_byRef) || (!dictBuffer) || (!dictSize)) { cdict->dictBuffer = NULL; cdict->dictContent = dictBuffer; } else { @@ -3640,7 +3644,8 @@ static size_t ZSTD_initCDict_internal( } ZSTD_CDict* ZSTD_createCDict_advanced(const void* dictBuffer, size_t dictSize, - unsigned byReference, ZSTD_dictMode_e dictMode, + ZSTD_dictLoadMethod_e dictLoadMethod, + ZSTD_dictMode_e dictMode, ZSTD_compressionParameters cParams, ZSTD_customMem customMem) { DEBUGLOG(5, "ZSTD_createCDict_advanced, mode %u", (U32)dictMode); @@ -3656,7 +3661,7 @@ ZSTD_CDict* ZSTD_createCDict_advanced(const void* dictBuffer, size_t dictSize, cdict->refContext = cctx; if (ZSTD_isError( ZSTD_initCDict_internal(cdict, dictBuffer, dictSize, - byReference, dictMode, + dictLoadMethod, dictMode, cParams) )) { ZSTD_freeCDict(cdict); return NULL; @@ -3669,7 +3674,7 @@ ZSTD_CDict* ZSTD_createCDict(const void* dict, size_t dictSize, int compressionL { ZSTD_compressionParameters cParams = ZSTD_getCParams(compressionLevel, 0, dictSize); return ZSTD_createCDict_advanced(dict, dictSize, - 0 /* byReference */, ZSTD_dm_auto, + ZSTD_dlm_byCopy, ZSTD_dm_auto, cParams, ZSTD_defaultCMem); } @@ -3677,7 +3682,7 @@ ZSTD_CDict* ZSTD_createCDict_byReference(const void* dict, size_t dictSize, int { ZSTD_compressionParameters cParams = ZSTD_getCParams(compressionLevel, 0, dictSize); return ZSTD_createCDict_advanced(dict, dictSize, - 1 /* byReference */, ZSTD_dm_auto, + ZSTD_dlm_byRef, ZSTD_dm_auto, cParams, ZSTD_defaultCMem); } @@ -3707,11 +3712,12 @@ size_t ZSTD_freeCDict(ZSTD_CDict* cdict) */ ZSTD_CDict* ZSTD_initStaticCDict(void* workspace, size_t workspaceSize, const void* dict, size_t dictSize, - unsigned byReference, ZSTD_dictMode_e dictMode, + ZSTD_dictLoadMethod_e dictLoadMethod, + ZSTD_dictMode_e dictMode, ZSTD_compressionParameters cParams) { size_t const cctxSize = ZSTD_estimateCCtxSize_advanced_usingCParams(cParams); - size_t const neededSize = sizeof(ZSTD_CDict) + (byReference ? 0 : dictSize) + size_t const neededSize = sizeof(ZSTD_CDict) + (dictLoadMethod == ZSTD_dlm_byRef ? 0 : dictSize) + cctxSize; ZSTD_CDict* const cdict = (ZSTD_CDict*) workspace; void* ptr; @@ -3721,7 +3727,7 @@ ZSTD_CDict* ZSTD_initStaticCDict(void* workspace, size_t workspaceSize, (U32)workspaceSize, (U32)neededSize, (U32)(workspaceSize < neededSize)); if (workspaceSize < neededSize) return NULL; - if (!byReference) { + if (dictLoadMethod == ZSTD_dlm_byCopy) { memcpy(cdict+1, dict, dictSize); dict = cdict+1; ptr = (char*)workspace + sizeof(ZSTD_CDict) + dictSize; @@ -3732,7 +3738,7 @@ ZSTD_CDict* ZSTD_initStaticCDict(void* workspace, size_t workspaceSize, if (ZSTD_isError( ZSTD_initCDict_internal(cdict, dict, dictSize, - 1 /* byReference */, dictMode, + ZSTD_dlm_byRef, dictMode, cParams) )) return NULL; @@ -3889,7 +3895,7 @@ size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, ZSTD_freeCDict(zcs->cdictLocal); zcs->cdictLocal = ZSTD_createCDict_advanced( dict, dictSize, - 0 /* byReference */, params.dictMode, + ZSTD_dlm_byCopy, params.dictMode, params.cParams, zcs->customMem); zcs->cdict = zcs->cdictLocal; if (zcs->cdictLocal == NULL) return ERROR(memory_allocation); diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index b1a681e74..e64a1f694 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -768,7 +768,7 @@ size_t ZSTDMT_initCStream_internal( DEBUGLOG(4,"cdictLocal: %08X", (U32)(size_t)zcs->cdictLocal); ZSTD_freeCDict(zcs->cdictLocal); zcs->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, - 0 /* byRef */, ZSTD_dm_auto, /* note : a loadPrefix becomes an internal CDict */ + ZSTD_dlm_byCopy, ZSTD_dm_auto, /* note : a loadPrefix becomes an internal CDict */ params.cParams, zcs->cMem); zcs->cdict = zcs->cdictLocal; if (zcs->cdictLocal == NULL) return ERROR(memory_allocation); diff --git a/lib/decompress/zstd_decompress.c b/lib/decompress/zstd_decompress.c index 92e80c1ac..6f4e72391 100644 --- a/lib/decompress/zstd_decompress.c +++ b/lib/decompress/zstd_decompress.c @@ -1993,9 +1993,9 @@ static size_t ZSTD_loadEntropy_inDDict(ZSTD_DDict* ddict) } -static size_t ZSTD_initDDict_internal(ZSTD_DDict* ddict, const void* dict, size_t dictSize, unsigned byReference) +static size_t ZSTD_initDDict_internal(ZSTD_DDict* ddict, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod) { - if ((byReference) || (!dict) || (!dictSize)) { + if ((dictLoadMethod == ZSTD_dlm_byRef) || (!dict) || (!dictSize)) { ddict->dictBuffer = NULL; ddict->dictContent = dict; } else { @@ -2014,7 +2014,7 @@ static size_t ZSTD_initDDict_internal(ZSTD_DDict* ddict, const void* dict, size_ return 0; } -ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize, unsigned byReference, ZSTD_customMem customMem) +ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_customMem customMem) { if (!customMem.customAlloc ^ !customMem.customFree) return NULL; @@ -2022,7 +2022,7 @@ ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize, unsigne if (!ddict) return NULL; ddict->cMem = customMem; - if (ZSTD_isError( ZSTD_initDDict_internal(ddict, dict, dictSize, byReference) )) { + if (ZSTD_isError( ZSTD_initDDict_internal(ddict, dict, dictSize, dictLoadMethod) )) { ZSTD_freeDDict(ddict); return NULL; } @@ -2038,7 +2038,7 @@ ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize, unsigne ZSTD_DDict* ZSTD_createDDict(const void* dict, size_t dictSize) { ZSTD_customMem const allocator = { NULL, NULL, NULL }; - return ZSTD_createDDict_advanced(dict, dictSize, 0, allocator); + return ZSTD_createDDict_advanced(dict, dictSize, ZSTD_dlm_byCopy, allocator); } /*! ZSTD_createDDict_byReference() : @@ -2048,25 +2048,26 @@ ZSTD_DDict* ZSTD_createDDict(const void* dict, size_t dictSize) ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize) { ZSTD_customMem const allocator = { NULL, NULL, NULL }; - return ZSTD_createDDict_advanced(dictBuffer, dictSize, 1, allocator); + return ZSTD_createDDict_advanced(dictBuffer, dictSize, ZSTD_dlm_byRef, allocator); } ZSTD_DDict* ZSTD_initStaticDDict(void* workspace, size_t workspaceSize, const void* dict, size_t dictSize, - unsigned byReference) + ZSTD_dictLoadMethod_e dictLoadMethod) { - size_t const neededSpace = sizeof(ZSTD_DDict) + (byReference ? 0 : dictSize); + size_t const neededSpace = + sizeof(ZSTD_DDict) + (dictLoadMethod == ZSTD_dlm_byRef ? 0 : dictSize); ZSTD_DDict* const ddict = (ZSTD_DDict*)workspace; assert(workspace != NULL); assert(dict != NULL); if ((size_t)workspace & 7) return NULL; /* 8-aligned */ if (workspaceSize < neededSpace) return NULL; - if (!byReference) { + if (dictLoadMethod == ZSTD_dlm_byCopy) { memcpy(ddict+1, dict, dictSize); /* local copy */ dict = ddict+1; } - if (ZSTD_isError( ZSTD_initDDict_internal(ddict, dict, dictSize, 1 /* byRef */) )) + if (ZSTD_isError( ZSTD_initDDict_internal(ddict, dict, dictSize, ZSTD_dlm_byRef) )) return NULL; return ddict; } @@ -2084,10 +2085,10 @@ size_t ZSTD_freeDDict(ZSTD_DDict* ddict) /*! ZSTD_estimateDDictSize() : * Estimate amount of memory that will be needed to create a dictionary for decompression. - * Note : dictionary created "byReference" are smaller */ -size_t ZSTD_estimateDDictSize(size_t dictSize, unsigned byReference) + * Note : dictionary created by reference using ZSTD_dlm_byRef are smaller */ +size_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod) { - return sizeof(ZSTD_DDict) + (byReference ? 0 : dictSize); + return sizeof(ZSTD_DDict) + (dictLoadMethod == ZSTD_dlm_byRef ? 0 : dictSize); } size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict) diff --git a/lib/zstd.h b/lib/zstd.h index 6d91e92a4..9ff8ad99f 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -524,13 +524,19 @@ ZSTDLIB_API size_t ZSTD_estimateCStreamSize_advanced_usingCCtxParams(const ZSTD_ ZSTDLIB_API size_t ZSTD_estimateDStreamSize(size_t windowSize); ZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize); -/*! ZSTD_estimateCDictSize() : +typedef enum { + ZSTD_dlm_byCopy = 0, /* Copy dictionary content internally. */ + ZSTD_dlm_byRef, /* Reference dictionary content -- the dictionary buffer must outlives its users. */ +} ZSTD_dictLoadMethod_e; + +/*! ZSTD_estimate?DictSize() : * ZSTD_estimateCDictSize() will bet that src size is relatively "small", and content is copied, like ZSTD_createCDict(). * ZSTD_estimateCStreamSize_advanced_usingCParams() makes it possible to control precisely compression parameters, like ZSTD_createCDict_advanced(). - * Note : dictionary created "byReference" are smaller */ + * Note : dictionary created by reference using ZSTD_dlm_byRef are smaller + */ ZSTDLIB_API size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel); -ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, unsigned byReference); -ZSTDLIB_API size_t ZSTD_estimateDDictSize(size_t dictSize, unsigned byReference); +ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, ZSTD_dictLoadMethod_e dictLoadMethod); +ZSTDLIB_API size_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod); /*************************************** @@ -564,7 +570,6 @@ ZSTDLIB_API ZSTD_CCtx* ZSTD_initStaticCCtx(void* workspace, size_t workspaceSize * It is important that dictBuffer outlives CDict, it must remain read accessible throughout the lifetime of CDict */ ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel); - typedef enum { ZSTD_dm_auto=0, /* dictionary is "full" if it starts with ZSTD_MAGIC_DICTIONARY, otherwise it is "rawContent" */ ZSTD_dm_rawContent, /* ensures dictionary is always loaded as rawContent, even if it starts with ZSTD_MAGIC_DICTIONARY */ ZSTD_dm_fullDict /* refuses to load a dictionary if it does not respect Zstandard's specification */ @@ -572,7 +577,8 @@ typedef enum { ZSTD_dm_auto=0, /* dictionary is "full" if it starts with /*! ZSTD_createCDict_advanced() : * Create a ZSTD_CDict using external alloc and free, and customized compression parameters */ ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize, - unsigned byReference, ZSTD_dictMode_e dictMode, + ZSTD_dictLoadMethod_e dictLoadMethod, + ZSTD_dictMode_e dictMode, ZSTD_compressionParameters cParams, ZSTD_customMem customMem); @@ -592,7 +598,7 @@ ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictS ZSTDLIB_API ZSTD_CDict* ZSTD_initStaticCDict( void* workspace, size_t workspaceSize, const void* dict, size_t dictSize, - unsigned byReference, ZSTD_dictMode_e dictMode, + ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictMode_e dictMode, ZSTD_compressionParameters cParams); /*! ZSTD_getCParams() : @@ -670,7 +676,8 @@ ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, siz /*! ZSTD_createDDict_advanced() : * Create a ZSTD_DDict using external alloc and free, optionally by reference */ ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize, - unsigned byReference, ZSTD_customMem customMem); + ZSTD_dictLoadMethod_e dictLoadMethod, + ZSTD_customMem customMem); /*! ZSTD_initStaticDDict() : * Generate a digested dictionary in provided memory area. @@ -685,7 +692,7 @@ ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictS */ ZSTDLIB_API ZSTD_DDict* ZSTD_initStaticDDict(void* workspace, size_t workspaceSize, const void* dict, size_t dictSize, - unsigned byReference); + ZSTD_dictLoadMethod_e dictLoadMethod); /*! ZSTD_getDictID_fromDict() : * Provides the dictID stored within dictionary. @@ -1018,6 +1025,7 @@ ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, s */ ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_byReference(ZSTD_CCtx* cctx, const void* dict, size_t dictSize); + /*! ZSTD_CCtx_refCDict() : * Reference a prepared dictionary, to be used for all next compression jobs. * Note that compression parameters are enforced from within CDict, @@ -1096,7 +1104,7 @@ size_t ZSTD_compress_generic_simpleArgs ( ZSTD_EndDirective endOp); -/** ZSTD_CCtx_params : +/** ZSTD_CCtx_params * * - ZSTD_createCCtxParams() : Create a ZSTD_CCtx_params structure * - ZSTD_CCtxParam_setParameter() : Push parameters one by one into an diff --git a/programs/bench.c b/programs/bench.c index f9493e3b0..7731d079e 100644 --- a/programs/bench.c +++ b/programs/bench.c @@ -284,7 +284,7 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, if (comprParams->searchLength) zparams.cParams.searchLength = comprParams->searchLength; if (comprParams->targetLength) zparams.cParams.targetLength = comprParams->targetLength; if (comprParams->strategy) zparams.cParams.strategy = comprParams->strategy; - cdict = ZSTD_createCDict_advanced(dictBuffer, dictBufferSize, 1 /*byRef*/, ZSTD_dm_auto, zparams.cParams, cmem); + cdict = ZSTD_createCDict_advanced(dictBuffer, dictBufferSize, ZSTD_dlm_byRef, ZSTD_dm_auto, zparams.cParams, cmem); if (cdict==NULL) EXM_THROW(1, "ZSTD_createCDict_advanced() allocation failure"); #endif do { diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 3f7595e99..9f661175a 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -598,10 +598,10 @@ static int basicUnitTests(U32 seed, double compressibility) } DISPLAYLEVEL(4, "test%3i : decompress with static DDict : ", testNb++); - { size_t const ddictBufferSize = ZSTD_estimateDDictSize(dictSize, 0); + { size_t const ddictBufferSize = ZSTD_estimateDDictSize(dictSize, ZSTD_dlm_byCopy); void* ddictBuffer = malloc(ddictBufferSize); if (ddictBuffer == NULL) goto _output_error; - { ZSTD_DDict* const ddict = ZSTD_initStaticDDict(ddictBuffer, ddictBufferSize, CNBuffer, dictSize, 0); + { ZSTD_DDict* const ddict = ZSTD_initStaticDDict(ddictBuffer, ddictBufferSize, CNBuffer, dictSize, ZSTD_dlm_byCopy); size_t const r = ZSTD_decompress_usingDDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, ddict); if (r != CNBuffSize - dictSize) goto _output_error; } @@ -687,14 +687,14 @@ static int basicUnitTests(U32 seed, double compressibility) DISPLAYLEVEL(4, "test%3i : estimate CDict size : ", testNb++); { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize); - size_t const estimatedSize = ZSTD_estimateCDictSize_advanced(dictSize, cParams, 1 /*byReference*/); + size_t const estimatedSize = ZSTD_estimateCDictSize_advanced(dictSize, cParams, ZSTD_dlm_byRef); DISPLAYLEVEL(4, "OK : %u \n", (U32)estimatedSize); } DISPLAYLEVEL(4, "test%3i : compress with CDict ", testNb++); { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize); ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize, - 1 /* byReference */, ZSTD_dm_auto, + ZSTD_dlm_byRef, ZSTD_dm_auto, cParams, ZSTD_defaultCMem); DISPLAYLEVEL(4, "(size : %u) : ", (U32)ZSTD_sizeof_CDict(cdict)); cSize = ZSTD_compress_usingCDict(cctx, compressedBuffer, compressedBufferSize, @@ -720,12 +720,12 @@ static int basicUnitTests(U32 seed, double compressibility) DISPLAYLEVEL(4, "test%3i : compress with static CDict : ", testNb++); { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize); - size_t const cdictSize = ZSTD_estimateCDictSize_advanced(dictSize, cParams, 0); + size_t const cdictSize = ZSTD_estimateCDictSize_advanced(dictSize, cParams, ZSTD_dlm_byCopy); void* const cdictBuffer = malloc(cdictSize); if (cdictBuffer==NULL) goto _output_error; { ZSTD_CDict* const cdict = ZSTD_initStaticCDict(cdictBuffer, cdictSize, dictBuffer, dictSize, - 0 /* by Reference */, ZSTD_dm_auto, + ZSTD_dlm_byCopy, ZSTD_dm_auto, cParams); if (cdict == NULL) { DISPLAY("ZSTD_initStaticCDict failed "); @@ -745,7 +745,7 @@ static int basicUnitTests(U32 seed, double compressibility) DISPLAYLEVEL(4, "test%3i : ZSTD_compress_usingCDict_advanced, no contentSize, no dictID : ", testNb++); { ZSTD_frameParameters const fParams = { 0 /* frameSize */, 1 /* checksum */, 1 /* noDictID*/ }; ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize); - ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize, 1 /*byRef*/, ZSTD_dm_auto, cParams, ZSTD_defaultCMem); + ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dm_auto, cParams, ZSTD_defaultCMem); cSize = ZSTD_compress_usingCDict_advanced(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize, cdict, fParams); ZSTD_freeCDict(cdict); @@ -796,7 +796,7 @@ static int basicUnitTests(U32 seed, double compressibility) DISPLAYLEVEL(4, "test%3i : Building cdict w/ ZSTD_dm_fullDict on a good dictionary : ", testNb++); { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize); - ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize, 1 /*byRef*/, ZSTD_dm_fullDict, cParams, ZSTD_defaultCMem); + ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dm_fullDict, cParams, ZSTD_defaultCMem); if (cdict==NULL) goto _output_error; ZSTD_freeCDict(cdict); } @@ -804,7 +804,7 @@ static int basicUnitTests(U32 seed, double compressibility) DISPLAYLEVEL(4, "test%3i : Building cdict w/ ZSTD_dm_fullDict on a rawContent (must fail) : ", testNb++); { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize); - ZSTD_CDict* const cdict = ZSTD_createCDict_advanced((const char*)dictBuffer+1, dictSize-1, 1 /*byRef*/, ZSTD_dm_fullDict, cParams, ZSTD_defaultCMem); + ZSTD_CDict* const cdict = ZSTD_createCDict_advanced((const char*)dictBuffer+1, dictSize-1, ZSTD_dlm_byRef, ZSTD_dm_fullDict, cParams, ZSTD_defaultCMem); if (cdict!=NULL) goto _output_error; ZSTD_freeCDict(cdict); } diff --git a/tests/zstreamtest.c b/tests/zstreamtest.c index 0bf833c51..5d6ef4168 100644 --- a/tests/zstreamtest.c +++ b/tests/zstreamtest.c @@ -199,7 +199,7 @@ static int basicUnitTests(U32 seed, double compressibility, ZSTD_customMem custo { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBufferSize, dictSize); size_t const s = ZSTD_estimateCStreamSize_advanced_usingCParams(cParams) /* uses ZSTD_initCStream_usingDict() */ - + ZSTD_estimateCDictSize_advanced(dictSize, cParams, 0); + + ZSTD_estimateCDictSize_advanced(dictSize, cParams, ZSTD_dlm_byCopy); if (ZSTD_isError(s)) goto _output_error; DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s); } @@ -275,7 +275,7 @@ static int basicUnitTests(U32 seed, double compressibility, ZSTD_customMem custo DISPLAYLEVEL(5, " (windowSize : %u) ", (U32)fhi.windowSize); { size_t const s = ZSTD_estimateDStreamSize(fhi.windowSize) /* uses ZSTD_initDStream_usingDict() */ - + ZSTD_estimateDDictSize(dictSize, 0); + + ZSTD_estimateDDictSize(dictSize, ZSTD_dlm_byCopy); if (ZSTD_isError(s)) goto _output_error; DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s); } } @@ -477,7 +477,7 @@ static int basicUnitTests(U32 seed, double compressibility, ZSTD_customMem custo DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_usingCDict_advanced with masked dictID : ", testNb++); { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBufferSize, dictionary.filled); ZSTD_frameParameters const fParams = { 1 /* contentSize */, 1 /* checksum */, 1 /* noDictID */}; - ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictionary.start, dictionary.filled, 1 /* byReference */, ZSTD_dm_auto, cParams, customMem); + ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictionary.start, dictionary.filled, ZSTD_dlm_byRef, ZSTD_dm_auto, cParams, customMem); size_t const initError = ZSTD_initCStream_usingCDict_advanced(zc, cdict, fParams, CNBufferSize); if (ZSTD_isError(initError)) goto _output_error; cSize = 0; diff --git a/zlibWrapper/examples/zwrapbench.c b/zlibWrapper/examples/zwrapbench.c index 1fc2117f6..d99424a7e 100644 --- a/zlibWrapper/examples/zwrapbench.c +++ b/zlibWrapper/examples/zwrapbench.c @@ -236,7 +236,7 @@ static int BMK_benchMem(z_const void* srcBuffer, size_t srcSize, if (compressor == BMK_ZSTD) { ZSTD_parameters const zparams = ZSTD_getParams(cLevel, avgSize, dictBufferSize); ZSTD_customMem const cmem = { NULL, NULL, NULL }; - ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictBufferSize, 1 /*byRef*/, ZSTD_dm_auto, zparams.cParams, cmem); + ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictBufferSize, ZSTD_dlm_byRef, ZSTD_dm_auto, zparams.cParams, cmem); if (cdict==NULL) EXM_THROW(1, "ZSTD_createCDict_advanced() allocation failure"); do { From c7a18b7c219b10c1ca8b33cd0d3f2614cae7ff54 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Tue, 29 Aug 2017 15:10:42 -0700 Subject: [PATCH 46/52] Localize 'dictMode' from cctx to function param --- lib/common/zstd_internal.h | 4 +- lib/compress/zstd_compress.c | 104 ++++++++++++++++----------------- lib/compress/zstdmt_compress.c | 10 ++-- lib/zstd.h | 23 ++++---- 4 files changed, 69 insertions(+), 72 deletions(-) diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index bfec42fad..6097f8e6f 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -293,9 +293,6 @@ struct ZSTD_CCtx_params_s { int compressionLevel; U32 forceWindow; /* force back-references to respect limit of * 1<requestedParams, param, value); - case ZSTD_p_dictMode: - if (cctx->cdict) return ERROR(stage_wrong); /* must be set before loading */ - return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); - case ZSTD_p_forceMaxWindow : /* Force back-references to remain < windowSize, * even when referencing into Dictionary content * default : 0 when using a CDict, 1 when using a Prefix */ @@ -447,15 +448,6 @@ size_t ZSTD_CCtxParam_setParameter( params->fParams.noDictIDFlag = (value == 0); return 0; - case ZSTD_p_dictMode : - /* restrict dictionary mode to "rawContent" or "fullDict" only */ - ZSTD_STATIC_ASSERT((U32)ZSTD_dm_fullDict > (U32)ZSTD_dm_rawContent); - if (value > (unsigned)ZSTD_dm_fullDict) { - return ERROR(parameter_outOfBound); - } - params->dictMode = (ZSTD_dictMode_e)value; - return 0; - case ZSTD_p_forceMaxWindow : params->forceWindow = value > 0; return 0; @@ -496,9 +488,6 @@ size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params cctx->requestedParams.fParams = params->fParams; cctx->requestedParams.compressionLevel = params->compressionLevel; - /* Assume dictionary parameters are validated */ - cctx->requestedParams.dictMode = params->dictMode; - /* Set force window explicitly since it sets cctx->loadedDictEnd */ CHECK_F( ZSTD_CCtx_setParameter( cctx, ZSTD_p_forceMaxWindow, params->forceWindow) ); @@ -524,9 +513,9 @@ ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long lo return 0; } -size_t ZSTD_CCtx_loadDictionary_internal( - ZSTD_CCtx* cctx, const void* dict, size_t dictSize, - ZSTD_dictLoadMethod_e dictLoadMethod) +size_t ZSTD_CCtx_loadDictionary_advanced( + ZSTD_CCtx* cctx, const void* dict, size_t dictSize, + ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictMode_e dictMode) { if (cctx->streamStage != zcss_init) return ERROR(stage_wrong); if (cctx->staticSize) return ERROR(memory_allocation); /* no malloc for static CCtx */ @@ -541,7 +530,7 @@ size_t ZSTD_CCtx_loadDictionary_internal( cctx->cdictLocal = ZSTD_createCDict_advanced( dict, dictSize, dictLoadMethod, - cctx->requestedParams.dictMode, + dictMode, cParams, cctx->customMem); cctx->cdict = cctx->cdictLocal; if (cctx->cdictLocal == NULL) @@ -553,12 +542,14 @@ size_t ZSTD_CCtx_loadDictionary_internal( ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_byReference( ZSTD_CCtx* cctx, const void* dict, size_t dictSize) { - return ZSTD_CCtx_loadDictionary_internal(cctx, dict, dictSize, ZSTD_dlm_byRef); + return ZSTD_CCtx_loadDictionary_advanced( + cctx, dict, dictSize, ZSTD_dlm_byRef, ZSTD_dm_auto); } ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize) { - return ZSTD_CCtx_loadDictionary_internal(cctx, dict, dictSize, ZSTD_dlm_byCopy); + return ZSTD_CCtx_loadDictionary_advanced( + cctx, dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dm_auto); } @@ -566,20 +557,27 @@ size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict) { if (cctx->streamStage != zcss_init) return ERROR(stage_wrong); cctx->cdict = cdict; - cctx->prefix = NULL; /* exclusive */ - cctx->prefixSize = 0; + memset(&cctx->prefixDict, 0, sizeof(ZSTD_prefixDict)); /* exclusive */ return 0; } size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize) +{ + return ZSTD_CCtx_refPrefix_advanced(cctx, prefix, prefixSize, ZSTD_dm_rawContent); +} + +size_t ZSTD_CCtx_refPrefix_advanced( + ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize, ZSTD_dictMode_e dictMode) { if (cctx->streamStage != zcss_init) return ERROR(stage_wrong); cctx->cdict = NULL; /* prefix discards any prior cdict */ - cctx->prefix = prefix; - cctx->prefixSize = prefixSize; + cctx->prefixDict.dict = prefix; + cctx->prefixDict.dictSize = prefixSize; + cctx->prefixDict.dictMode = dictMode; return 0; } + static void ZSTD_startNewCompression(ZSTD_CCtx* cctx) { cctx->streamStage = zcss_init; @@ -3387,13 +3385,14 @@ static size_t ZSTD_compress_insertDictionary(ZSTD_CCtx* cctx, * @return : 0, or an error code */ static size_t ZSTD_compressBegin_internal(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, + ZSTD_dictMode_e dictMode, const ZSTD_CDict* cdict, ZSTD_CCtx_params params, U64 pledgedSrcSize, ZSTD_buffered_policy_e zbuff) { DEBUGLOG(4, "ZSTD_compressBegin_internal"); DEBUGLOG(4, "dict ? %s", dict ? "dict" : (cdict ? "cdict" : "none")); - DEBUGLOG(4, "dictMode : %u", (U32)(params.dictMode)); + DEBUGLOG(4, "dictMode : %u", (U32)dictMode); /* params are supposed to be fully validated at this point */ assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); assert(!((dict) && (cdict))); /* either dict or cdict, not both */ @@ -3406,18 +3405,19 @@ static size_t ZSTD_compressBegin_internal(ZSTD_CCtx* cctx, CHECK_F( ZSTD_resetCCtx_internal(cctx, params, pledgedSrcSize, ZSTDcrp_continue, zbuff) ); - return ZSTD_compress_insertDictionary(cctx, dict, dictSize, params.dictMode); + return ZSTD_compress_insertDictionary(cctx, dict, dictSize, dictMode); } size_t ZSTD_compressBegin_advanced_internal( ZSTD_CCtx* cctx, const void* dict, size_t dictSize, + ZSTD_dictMode_e dictMode, ZSTD_CCtx_params params, unsigned long long pledgedSrcSize) { /* compression parameters verification and optimization */ CHECK_F( ZSTD_checkCParams(params.cParams) ); - return ZSTD_compressBegin_internal(cctx, dict, dictSize, NULL, + return ZSTD_compressBegin_internal(cctx, dict, dictSize, dictMode, NULL, params, pledgedSrcSize, ZSTDb_not_buffered); } @@ -3430,8 +3430,8 @@ size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, { ZSTD_CCtx_params cctxParams = ZSTD_assignParamsToCCtxParams(cctx->requestedParams, params); - cctxParams.dictMode = ZSTD_dm_auto; - return ZSTD_compressBegin_advanced_internal(cctx, dict, dictSize, cctxParams, + return ZSTD_compressBegin_advanced_internal(cctx, dict, dictSize, ZSTD_dm_auto, + cctxParams, pledgedSrcSize); } @@ -3440,8 +3440,7 @@ size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t di ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, dictSize); ZSTD_CCtx_params cctxParams = ZSTD_assignParamsToCCtxParams(cctx->requestedParams, params); - cctxParams.dictMode = ZSTD_dm_auto; - return ZSTD_compressBegin_internal(cctx, dict, dictSize, NULL, + return ZSTD_compressBegin_internal(cctx, dict, dictSize, ZSTD_dm_auto, NULL, cctxParams, 0, ZSTDb_not_buffered); } @@ -3523,7 +3522,6 @@ static size_t ZSTD_compress_internal (ZSTD_CCtx* cctx, { ZSTD_CCtx_params cctxParams = ZSTD_assignParamsToCCtxParams(cctx->requestedParams, params); - cctxParams.dictMode = ZSTD_dm_auto; return ZSTD_compress_advanced_internal(cctx, dst, dstCapacity, src, srcSize, @@ -3549,7 +3547,7 @@ size_t ZSTD_compress_advanced_internal( const void* dict,size_t dictSize, ZSTD_CCtx_params params) { - CHECK_F( ZSTD_compressBegin_internal(cctx, dict, dictSize, NULL, + CHECK_F( ZSTD_compressBegin_internal(cctx, dict, dictSize, ZSTD_dm_auto, NULL, params, srcSize, ZSTDb_not_buffered) ); return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize); } @@ -3633,9 +3631,8 @@ static size_t ZSTD_initCDict_internal( ZSTD_CCtx_params cctxParams = cdict->refContext->requestedParams; cctxParams.fParams = fParams; cctxParams.cParams = cParams; - cctxParams.dictMode = dictMode; CHECK_F( ZSTD_compressBegin_internal(cdict->refContext, - cdict->dictContent, dictSize, + cdict->dictContent, dictSize, dictMode, NULL, cctxParams, ZSTD_CONTENTSIZE_UNKNOWN, ZSTDb_not_buffered) ); @@ -3759,10 +3756,9 @@ size_t ZSTD_compressBegin_usingCDict_advanced( { ZSTD_CCtx_params params = cctx->requestedParams; params.cParams = ZSTD_getCParamsFromCDict(cdict); params.fParams = fParams; - params.dictMode = ZSTD_dm_auto; DEBUGLOG(5, "ZSTD_compressBegin_usingCDict_advanced"); return ZSTD_compressBegin_internal(cctx, - NULL, 0, + NULL, 0, ZSTD_dm_auto, cdict, params, pledgedSrcSize, ZSTDb_not_buffered); @@ -3841,7 +3837,7 @@ size_t ZSTD_CStreamOutSize(void) static size_t ZSTD_resetCStream_internal( ZSTD_CStream* zcs, - const void* dict, size_t dictSize, + const void* dict, size_t dictSize, ZSTD_dictMode_e dictMode, const ZSTD_CDict* cdict, const ZSTD_CCtx_params params, unsigned long long pledgedSrcSize) { @@ -3851,7 +3847,7 @@ static size_t ZSTD_resetCStream_internal( assert(!((dict) && (cdict))); /* either dict or cdict, not both */ CHECK_F( ZSTD_compressBegin_internal(zcs, - dict, dictSize, + dict, dictSize, dictMode, cdict, params, pledgedSrcSize, ZSTDb_buffered) ); @@ -3871,7 +3867,7 @@ size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize) params.fParams.contentSizeFlag = (pledgedSrcSize > 0); params.cParams = ZSTD_getCParamsFromCCtxParams(params, pledgedSrcSize, 0); DEBUGLOG(5, "ZSTD_resetCStream"); - return ZSTD_resetCStream_internal(zcs, NULL, 0, zcs->cdict, params, pledgedSrcSize); + return ZSTD_resetCStream_internal(zcs, NULL, 0, ZSTD_dm_auto, zcs->cdict, params, pledgedSrcSize); } /*! ZSTD_initCStream_internal() : @@ -3895,7 +3891,7 @@ size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, ZSTD_freeCDict(zcs->cdictLocal); zcs->cdictLocal = ZSTD_createCDict_advanced( dict, dictSize, - ZSTD_dlm_byCopy, params.dictMode, + ZSTD_dlm_byCopy, ZSTD_dm_auto, params.cParams, zcs->customMem); zcs->cdict = zcs->cdictLocal; if (zcs->cdictLocal == NULL) return ERROR(memory_allocation); @@ -3911,7 +3907,7 @@ size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, params.compressionLevel = ZSTD_CLEVEL_CUSTOM; zcs->requestedParams = params; - return ZSTD_resetCStream_internal(zcs, NULL, 0, zcs->cdict, params, pledgedSrcSize); + return ZSTD_resetCStream_internal(zcs, NULL, 0, ZSTD_dm_auto, zcs->cdict, params, pledgedSrcSize); } /* ZSTD_initCStream_usingCDict_advanced() : @@ -4157,23 +4153,27 @@ size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, /* transparent initialization stage */ if (cctx->streamStage == zcss_init) { - const void* const prefix = cctx->prefix; - size_t const prefixSize = cctx->prefixSize; + ZSTD_prefixDict const prefixDict = cctx->prefixDict; ZSTD_CCtx_params params = cctx->requestedParams; params.cParams = ZSTD_getCParamsFromCCtxParams( cctx->requestedParams, cctx->pledgedSrcSizePlusOne-1, 0 /*dictSize*/); - cctx->prefix = NULL; cctx->prefixSize = 0; /* single usage */ - assert(prefix==NULL || cctx->cdict==NULL); /* only one can be set */ + memset(&cctx->prefixDict, 0, sizeof(ZSTD_prefixDict)); /* single usage */ + assert(prefixDict.dict==NULL || cctx->cdict==NULL); /* only one can be set */ #ifdef ZSTD_MULTITHREAD if (params.nbThreads > 1) { DEBUGLOG(4, "call ZSTDMT_initCStream_internal as nbThreads=%u", params.nbThreads); - CHECK_F( ZSTDMT_initCStream_internal(cctx->mtctx, prefix, prefixSize, cctx->cdict, params, cctx->pledgedSrcSizePlusOne-1) ); + CHECK_F( ZSTDMT_initCStream_internal( + cctx->mtctx, prefixDict.dict, prefixDict.dictSize, + cctx->cdict, params, cctx->pledgedSrcSizePlusOne-1) ); cctx->streamStage = zcss_load; } else #endif { - CHECK_F( ZSTD_resetCStream_internal(cctx, prefix, prefixSize, cctx->cdict, params, cctx->pledgedSrcSizePlusOne-1) ); + CHECK_F( ZSTD_resetCStream_internal( + cctx, prefixDict.dict, prefixDict.dictSize, + prefixDict.dictMode, cctx->cdict, params, + cctx->pledgedSrcSizePlusOne-1) ); } } /* compression stage */ diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index e64a1f694..41278677f 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -343,13 +343,13 @@ void ZSTDMT_compressChunk(void* jobDescription) if (!job->firstChunk) job->params.fParams.contentSizeFlag = 0; /* ensure no srcSize control */ { ZSTD_CCtx_params jobParams = job->params; /* Force loading dictionary in "content-only" mode (no header analysis) */ - size_t const dictModeError = - ZSTD_CCtxParam_setParameter(&jobParams, ZSTD_p_dictMode, (U32)ZSTD_dm_rawContent); size_t const forceWindowError = ZSTD_CCtxParam_setParameter(&jobParams, ZSTD_p_forceMaxWindow, !job->firstChunk); - size_t const initError = ZSTD_compressBegin_advanced_internal(cctx, job->srcStart, job->dictSize, jobParams, job->fullFrameSize); - if (ZSTD_isError(initError) || ZSTD_isError(dictModeError) || - ZSTD_isError(forceWindowError)) { job->cSize = initError; goto _endJob; } + size_t const initError = ZSTD_compressBegin_advanced_internal(cctx, job->srcStart, job->dictSize, ZSTD_dm_rawContent, jobParams, job->fullFrameSize); + if (ZSTD_isError(initError) || ZSTD_isError(forceWindowError)) { + job->cSize = initError; + goto _endJob; + } } } if (!job->firstChunk) { /* flush and overwrite frame header when it's not first segment */ size_t const hSize = ZSTD_compressContinue(cctx, dstBuff.start, dstBuff.size, src, 0); diff --git a/lib/zstd.h b/lib/zstd.h index 9ff8ad99f..8ac564884 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -963,10 +963,6 @@ typedef enum { ZSTD_p_checksumFlag, /* A 32-bits checksum of content is written at end of frame (default:0) */ ZSTD_p_dictIDFlag, /* When applicable, dictID of dictionary is provided in frame header (default:1) */ - /* dictionary parameters (must be set before ZSTD_CCtx_loadDictionary) */ - ZSTD_p_dictMode=300, /* Select how dictionary content must be interpreted. Value must be from type ZSTD_dictMode_e. - * default : 0==auto : dictionary will be "full" if it respects specification, otherwise it will be "rawContent" */ - /* multi-threading parameters */ ZSTD_p_nbThreads=400, /* Select how many threads a compression job can spawn (default:1) * More threads improve speed, but also increase memory usage. @@ -1011,19 +1007,19 @@ ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long lo * meaning "return to no-dictionary mode". * Note 1 : `dict` content will be copied internally. Use * ZSTD_CCtx_loadDictionary_byReference() to reference dictionary - * content instead. + * content instead. The dictionary buffer must then outlive its + * users. * Note 2 : Loading a dictionary involves building tables, which are dependent on compression parameters. * For this reason, compression parameters cannot be changed anymore after loading a dictionary. * It's also a CPU-heavy operation, with non-negligible impact on latency. * Note 3 : Dictionary will be used for all future compression jobs. - * To return to "no-dictionary" situation, load a NULL dictionary */ -ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize); - -/*! ZSTD_CCtx_loadDictionary_byReference() : - * Same as ZSTD_CCtx_loadDictionary() except dictionary content will be - * referenced, instead of copied. The dictionary buffer must outlive its users. + * To return to "no-dictionary" situation, load a NULL dictionary + * Note 5 : Use ZSTD_CCtx_loadDictionary_advanced() to select how dictionary + * content will be interpreted. */ +ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize); ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_byReference(ZSTD_CCtx* cctx, const void* dict, size_t dictSize); +ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictMode_e dictMode); /*! ZSTD_CCtx_refCDict() : @@ -1050,8 +1046,11 @@ ZSTDLIB_API size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); * Note 1 : Prefix buffer is referenced. It must outlive compression job. * Note 2 : Referencing a prefix involves building tables, which are dependent on compression parameters. * It's a CPU-heavy operation, with non-negligible impact on latency. - * Note 3 : it's possible to alter ZSTD_p_dictMode using ZSTD_CCtx_setParameter() */ + * Note 3 : By default, the prefix is treated as raw content + * (ZSTD_dm_rawContent). Use ZSTD_CCtx_refPrefix_advanced() to alter + * dictMode. */ ZSTDLIB_API size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize); +ZSTDLIB_API size_t ZSTD_CCtx_refPrefix_advanced(ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize, ZSTD_dictMode_e dictMode); From 4e835720bf17a3c4cb448804a701e605d7889f5f Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Tue, 29 Aug 2017 16:18:21 -0700 Subject: [PATCH 47/52] Delay creation of ZSTDMT_CCtx --- lib/compress/zstd_compress.c | 39 +++++++++++++++------------------- lib/compress/zstdmt_compress.c | 2 +- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 990f3aa6b..4251bbb01 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -122,7 +122,7 @@ struct ZSTD_CCtx_s { /* Dictionary */ ZSTD_CDict* cdictLocal; const ZSTD_CDict* cdict; - ZSTD_prefixDict prefixDict; + ZSTD_prefixDict prefixDict; /* single-usage dictionary */ /* Multi-threading */ ZSTDMT_CCtx* mtctx; @@ -342,30 +342,16 @@ size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned v case ZSTD_p_nbThreads: if (value==0) return 0; DEBUGLOG(5, " setting nbThreads : %u", value); -#ifndef ZSTD_MULTITHREAD - if (value > 1) return ERROR(parameter_unsupported); -#endif - if ((value>1) && (cctx->requestedParams.nbThreads != value)) { - if (cctx->staticSize) /* MT not compatible with static alloc */ - return ERROR(parameter_unsupported); - ZSTDMT_freeCCtx(cctx->mtctx); - cctx->requestedParams.nbThreads = 1; - cctx->mtctx = ZSTDMT_createCCtx_advanced(value, cctx->customMem); - if (cctx->mtctx == NULL) return ERROR(memory_allocation); + if (value > 1 && cctx->staticSize) { + return ERROR(parameter_unsupported); /* MT not compatible with static alloc */ } - - /* Need to initialize overlapSizeLog */ - return ZSTDMT_initializeCCtxParameters(&cctx->requestedParams, value); + return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); case ZSTD_p_jobSize: - if (cctx->requestedParams.nbThreads <= 1) return ERROR(parameter_unsupported); - assert(cctx->mtctx != NULL); return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); case ZSTD_p_overlapSizeLog: DEBUGLOG(5, " setting overlap with nbThreads == %u", cctx->requestedParams.nbThreads); - if (cctx->requestedParams.nbThreads <= 1) return ERROR(parameter_unsupported); - assert(cctx->mtctx != NULL); return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); default: return ERROR(parameter_unsupported); @@ -453,7 +439,7 @@ size_t ZSTD_CCtxParam_setParameter( return 0; case ZSTD_p_nbThreads : - if (value == 0) { return 0; } + if (value == 0) return 0; #ifndef ZSTD_MULTITHREAD if (value > 1) return ERROR(parameter_unsupported); #endif @@ -481,7 +467,8 @@ size_t ZSTD_CCtxParam_setParameter( */ size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params) { - if (cctx->cdict) { return ERROR(stage_wrong); } + if (cctx->streamStage != zcss_init) return ERROR(stage_wrong); + if (cctx->cdict) return ERROR(stage_wrong); /* Assume the compression and frame parameters are validated */ cctx->requestedParams.cParams = params->cParams; @@ -498,7 +485,6 @@ size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params CHECK_F( ZSTD_CCtx_setParameter(cctx, ZSTD_p_jobSize, params->jobSize) ); CHECK_F( ZSTD_CCtx_setParameter( cctx, ZSTD_p_overlapSizeLog, params->overlapSizeLog) ); - } /* customMem is used only for create/free params and can be ignored */ @@ -4162,11 +4148,19 @@ size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, #ifdef ZSTD_MULTITHREAD if (params.nbThreads > 1) { + if (cctx->mtctx == NULL || cctx->appliedParams.nbThreads != params.nbThreads) { + ZSTDMT_freeCCtx(cctx->mtctx); + cctx->mtctx = ZSTDMT_createCCtx_advanced(params.nbThreads, cctx->customMem); + if (cctx->mtctx == NULL) return ERROR(memory_allocation); + } + DEBUGLOG(4, "call ZSTDMT_initCStream_internal as nbThreads=%u", params.nbThreads); + CHECK_F( ZSTDMT_initCStream_internal( cctx->mtctx, prefixDict.dict, prefixDict.dictSize, cctx->cdict, params, cctx->pledgedSrcSizePlusOne-1) ); cctx->streamStage = zcss_load; + cctx->appliedParams.nbThreads = params.nbThreads; } else #endif { @@ -4178,7 +4172,8 @@ size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, /* compression stage */ #ifdef ZSTD_MULTITHREAD - if (cctx->requestedParams.nbThreads > 1) { + if (cctx->appliedParams.nbThreads > 1) { + assert(cctx->mtctx != NULL); size_t const flushMin = ZSTDMT_compressStream_generic(cctx->mtctx, output, input, endOp); DEBUGLOG(5, "ZSTDMT_compressStream_generic : %u", (U32)flushMin); if ( ZSTD_isError(flushMin) diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index 41278677f..bf418df54 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -342,9 +342,9 @@ void ZSTDMT_compressChunk(void* jobDescription) } else { /* srcStart points at reloaded section */ if (!job->firstChunk) job->params.fParams.contentSizeFlag = 0; /* ensure no srcSize control */ { ZSTD_CCtx_params jobParams = job->params; - /* Force loading dictionary in "content-only" mode (no header analysis) */ size_t const forceWindowError = ZSTD_CCtxParam_setParameter(&jobParams, ZSTD_p_forceMaxWindow, !job->firstChunk); + /* Force loading dictionary in "content-only" mode (no header analysis) */ size_t const initError = ZSTD_compressBegin_advanced_internal(cctx, job->srcStart, job->dictSize, ZSTD_dm_rawContent, jobParams, job->fullFrameSize); if (ZSTD_isError(initError) || ZSTD_isError(forceWindowError)) { job->cSize = initError; From 82d636b76adfcfc7f253fee4663d7a1acaab37d8 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Tue, 29 Aug 2017 18:03:06 -0700 Subject: [PATCH 48/52] Rename applyCCtxParams() --- lib/compress/zstd_compress.c | 3 ++- lib/zstd.h | 9 +++++---- tests/roundTripCrash.c | 2 +- tests/zstreamtest.c | 6 +++--- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 4251bbb01..436e73cee 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -465,7 +465,8 @@ size_t ZSTD_CCtxParam_setParameter( * * Pledged srcSize is treated as unknown. */ -size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params) +size_t ZSTD_CCtx_setParametersUsingCCtxParams( + ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params) { if (cctx->streamStage != zcss_init) return ERROR(stage_wrong); if (cctx->cdict) return ERROR(stage_wrong); diff --git a/lib/zstd.h b/lib/zstd.h index 8ac564884..e062f6d2b 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -1109,7 +1109,7 @@ size_t ZSTD_compress_generic_simpleArgs ( * - ZSTD_CCtxParam_setParameter() : Push parameters one by one into an * existing ZSTD_CCtx_params structure. This is similar to * ZSTD_CCtx_setParameter(). - * - ZSTD_CCtx_applyCCtxParams() : Apply parameters to an existing CCtx. These + * - ZSTD_CCtx_setParametersUsingCCtxParams() : Apply parameters to an existing CCtx. These * parameters will be applied to all subsequent compression jobs. * - ZSTD_compress_generic() : Do compression using the CCtx. * - ZSTD_freeCCtxParams() : Free the memory. @@ -1141,19 +1141,20 @@ ZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params); /*! ZSTD_CCtxParam_setParameter() : * Similar to ZSTD_CCtx_setParameter. * Set one compression parameter, selected by enum ZSTD_cParameter. - * Parameters must be applied to a ZSTD_CCtx using ZSTD_CCtx_applyCCtxParams(). + * Parameters must be applied to a ZSTD_CCtx using ZSTD_CCtx_setParametersUsingCCtxParams(). * Note : when `value` is an enum, cast it to unsigned for proper type checking. * @result : 0, or an error code (which can be tested with ZSTD_isError()). */ ZSTDLIB_API size_t ZSTD_CCtxParam_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, unsigned value); -/*! ZSTD_CCtx_applyCCtxParams() : +/*! ZSTD_CCtx_setParametersUsingCCtxParams() : * Apply a set of ZSTD_CCtx_params to the compression context. * This must be done before the dictionary is loaded. * The pledgedSrcSize is treated as unknown. * Multithreading parameters are applied only if nbThreads > 1. */ -ZSTDLIB_API size_t ZSTD_CCtx_applyCCtxParams(ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params); +ZSTDLIB_API size_t ZSTD_CCtx_setParametersUsingCCtxParams( + ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params); /** Block functions diff --git a/tests/roundTripCrash.c b/tests/roundTripCrash.c index cb0221c58..41e5d4472 100644 --- a/tests/roundTripCrash.c +++ b/tests/roundTripCrash.c @@ -98,7 +98,7 @@ static size_t cctxParamRoundTripTest(void* resultBuff, size_t resultBuffCapacity /* Apply parameters */ - CHECK_Z( ZSTD_CCtx_applyCCtxParams(cctx, cctxParams) ); + CHECK_Z( ZSTD_CCtx_setParametersUsingCCtxParams(cctx, cctxParams) ); CHECK_Z (ZSTD_compress_generic(cctx, &outBuffer, &inBuffer, ZSTD_e_end) ); diff --git a/tests/zstreamtest.c b/tests/zstreamtest.c index 5d6ef4168..f8683ac78 100644 --- a/tests/zstreamtest.c +++ b/tests/zstreamtest.c @@ -1381,7 +1381,7 @@ static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double /* Apply parameters */ if (useOpaqueAPI) { - CHECK_Z (ZSTD_CCtx_applyCCtxParams(zc, cctxParams) ); + CHECK_Z (ZSTD_CCtx_setParametersUsingCCtxParams(zc, cctxParams) ); } if (FUZ_rand(&lseed) & 1) { @@ -1393,8 +1393,8 @@ static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double if (dict && dictSize) { /* test that compression parameters are rejected (correctly) after loading a non-NULL dictionary */ if (useOpaqueAPI) { - size_t const setError = ZSTD_CCtx_applyCCtxParams(zc, cctxParams); - CHECK(!ZSTD_isError(setError), "ZSTD_CCtx_applyCCtxParams should have failed"); + size_t const setError = ZSTD_CCtx_setParametersUsingCCtxParams(zc, cctxParams); + CHECK(!ZSTD_isError(setError), "ZSTD_CCtx_setParametersUsingCCtxParams should have failed"); } else { size_t const setError = ZSTD_CCtx_setParameter(zc, ZSTD_p_windowLog, cParams.windowLog-1); CHECK(!ZSTD_isError(setError), "ZSTD_CCtx_setParameter should have failed"); From 623e3cd40b368b48a62456a0f8f950474b969238 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Tue, 29 Aug 2017 18:04:32 -0700 Subject: [PATCH 49/52] Use ZSTD_dm_rawContent in zstdmt_compress --- lib/compress/zstdmt_compress.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index bf418df54..86afe5065 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -768,7 +768,7 @@ size_t ZSTDMT_initCStream_internal( DEBUGLOG(4,"cdictLocal: %08X", (U32)(size_t)zcs->cdictLocal); ZSTD_freeCDict(zcs->cdictLocal); zcs->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, - ZSTD_dlm_byCopy, ZSTD_dm_auto, /* note : a loadPrefix becomes an internal CDict */ + ZSTD_dlm_byCopy, ZSTD_dm_rawContent, /* note : a loadPrefix becomes an internal CDict */ params.cParams, zcs->cMem); zcs->cdict = zcs->cdictLocal; if (zcs->cdictLocal == NULL) return ERROR(memory_allocation); From a6e20e1bd7942ed6cae5382b81e11b81ea2fa701 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Tue, 29 Aug 2017 18:36:18 -0700 Subject: [PATCH 50/52] Add test for raw content starting with dict header --- lib/compress/zstd_compress.c | 1 - tests/fuzzer.c | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 436e73cee..7249f6fca 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -4174,7 +4174,6 @@ size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, /* compression stage */ #ifdef ZSTD_MULTITHREAD if (cctx->appliedParams.nbThreads > 1) { - assert(cctx->mtctx != NULL); size_t const flushMin = ZSTDMT_compressStream_generic(cctx->mtctx, output, input, endOp); DEBUGLOG(5, "ZSTDMT_compressStream_generic : %u", (U32)flushMin); if ( ZSTD_isError(flushMin) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 9f661175a..db4b33519 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -810,6 +810,26 @@ static int basicUnitTests(U32 seed, double compressibility) } DISPLAYLEVEL(4, "OK \n"); + DISPLAYLEVEL(4, "test%3i : Loading rawContent starting with dict header w/ ZSTD_dm_auto should fail", testNb++); + { + size_t ret; + MEM_writeLE32(dictBuffer+2, ZSTD_MAGIC_DICTIONARY); + ret = ZSTD_CCtx_loadDictionary_advanced( + cctx, (const char*)dictBuffer+2, dictSize-2, ZSTD_dlm_byRef, ZSTD_dm_auto); + if (!ZSTD_isError(ret)) goto _output_error; + } + DISPLAYLEVEL(4, "OK \n"); + + DISPLAYLEVEL(4, "test%3i : Loading rawContent starting with dict header w/ ZSTD_dm_rawContent should pass", testNb++); + { + size_t ret; + MEM_writeLE32(dictBuffer+2, ZSTD_MAGIC_DICTIONARY); + ret = ZSTD_CCtx_loadDictionary_advanced( + cctx, (const char*)dictBuffer+2, dictSize-2, ZSTD_dlm_byRef, ZSTD_dm_rawContent); + if (ZSTD_isError(ret)) goto _output_error; + } + DISPLAYLEVEL(4, "OK \n"); + ZSTD_freeCCtx(cctx); free(dictBuffer); free(samplesSizes); From ee65701720f710cf4b568a42f2917faa4b33d2be Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Tue, 29 Aug 2017 20:27:35 -0700 Subject: [PATCH 51/52] Minor fixes; remove formatting only changes --- lib/common/zstd_internal.h | 16 ++++++++-------- lib/compress/zstd_compress.c | 31 ++++++++++++++----------------- tests/fuzzer.c | 4 ++-- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index 6097f8e6f..19c0a6261 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -352,12 +352,10 @@ void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx); * expects params to be valid. * must receive dict, or cdict, or none, but not both. * @return : 0, or an error code */ -size_t ZSTD_initCStream_internal( - ZSTD_CStream* zcs, - const void* dict, size_t dictSize, - const ZSTD_CDict* cdict, - ZSTD_CCtx_params params, - unsigned long long pledgedSrcSize); +size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, + const void* dict, size_t dictSize, + const ZSTD_CDict* cdict, + ZSTD_CCtx_params params, unsigned long long pledgedSrcSize); /*! ZSTD_compressStream_generic() : * Private use only. To be called from zstdmt_compress.c in single-thread mode. */ @@ -370,14 +368,16 @@ size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs, * as the name implies */ ZSTD_compressionParameters ZSTD_getCParamsFromCDict(const ZSTD_CDict* cdict); -/* INTERNAL */ +/* ZSTD_compressBegin_advanced_internal() : + * Private use only. To be called from zstdmt_compress.c. */ size_t ZSTD_compressBegin_advanced_internal(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_dictMode_e dictMode, ZSTD_CCtx_params params, unsigned long long pledgedSrcSize); -/* INTERNAL */ +/* ZSTD_compress_advanced_internal() : + * Private use only. To be called from zstdmt_compress.c. */ size_t ZSTD_compress_advanced_internal(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize, diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 7249f6fca..206e0976a 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -516,8 +516,7 @@ size_t ZSTD_CCtx_loadDictionary_advanced( ZSTD_getCParamsFromCCtxParams(cctx->requestedParams, 0, dictSize); cctx->cdictLocal = ZSTD_createCDict_advanced( dict, dictSize, - dictLoadMethod, - dictMode, + dictLoadMethod, dictMode, cParams, cctx->customMem); cctx->cdict = cctx->cdictLocal; if (cctx->cdictLocal == NULL) @@ -564,7 +563,6 @@ size_t ZSTD_CCtx_refPrefix_advanced( return 0; } - static void ZSTD_startNewCompression(ZSTD_CCtx* cctx) { cctx->streamStage = zcss_init; @@ -785,7 +783,7 @@ static size_t ZSTD_resetCCtx_internal(ZSTD_CCtx* zc, if (crp == ZSTDcrp_continue) { if (ZSTD_equivalentParams(params, zc->appliedParams)) { - DEBUGLOG(5, "ZSTD_equivalentCParams()==1"); + DEBUGLOG(5, "ZSTD_equivalentParams()==1"); zc->entropy->hufCTable_repeatMode = HUF_repeat_none; zc->entropy->offcode_repeatMode = FSE_repeat_none; zc->entropy->matchlength_repeatMode = FSE_repeat_none; @@ -3122,7 +3120,7 @@ static size_t ZSTD_compressContinue_internal (ZSTD_CCtx* cctx, if (cctx->stage==ZSTDcs_created) return ERROR(stage_wrong); /* missing init (ZSTD_compressBegin) */ if (frame && (cctx->stage==ZSTDcs_init)) { - fhSize = ZSTD_writeFrameHeader(dst, dstCapacity,cctx->appliedParams, + fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, cctx->appliedParams, cctx->pledgedSrcSizePlusOne-1, cctx->dictID); if (ZSTD_isError(fhSize)) return fhSize; dstCapacity -= fhSize; @@ -3582,7 +3580,7 @@ size_t ZSTD_estimateCDictSize_advanced( size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel) { ZSTD_compressionParameters const cParams = ZSTD_getCParams(compressionLevel, 0, dictSize); - return ZSTD_estimateCDictSize_advanced(dictSize, cParams, 0); + return ZSTD_estimateCDictSize_advanced(dictSize, cParams, ZSTD_dlm_byCopy); } size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict) @@ -3624,6 +3622,7 @@ static size_t ZSTD_initCDict_internal( cctxParams, ZSTD_CONTENTSIZE_UNKNOWN, ZSTDb_not_buffered) ); } + return 0; } @@ -3637,6 +3636,7 @@ ZSTD_CDict* ZSTD_createCDict_advanced(const void* dictBuffer, size_t dictSize, { ZSTD_CDict* const cdict = (ZSTD_CDict*)ZSTD_malloc(sizeof(ZSTD_CDict), customMem); ZSTD_CCtx* const cctx = ZSTD_createCCtx_advanced(customMem); + if (!cdict || !cctx) { ZSTD_free(cdict, customMem); ZSTD_freeCCtx(cctx); @@ -3650,6 +3650,7 @@ ZSTD_CDict* ZSTD_createCDict_advanced(const void* dictBuffer, size_t dictSize, ZSTD_freeCDict(cdict); return NULL; } + return cdict; } } @@ -3822,11 +3823,10 @@ size_t ZSTD_CStreamOutSize(void) return ZSTD_compressBound(ZSTD_BLOCKSIZE_MAX) + ZSTD_blockHeaderSize + 4 /* 32-bits hash */ ; } -static size_t ZSTD_resetCStream_internal( - ZSTD_CStream* zcs, - const void* dict, size_t dictSize, ZSTD_dictMode_e dictMode, - const ZSTD_CDict* cdict, - const ZSTD_CCtx_params params, unsigned long long pledgedSrcSize) +static size_t ZSTD_resetCStream_internal(ZSTD_CStream* zcs, + const void* dict, size_t dictSize, ZSTD_dictMode_e dictMode, + const ZSTD_CDict* cdict, + const ZSTD_CCtx_params params, unsigned long long pledgedSrcSize) { DEBUGLOG(4, "ZSTD_resetCStream_internal"); /* params are supposed to be fully validated at this point */ @@ -3862,9 +3862,8 @@ size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize) * Assumption 1 : params are valid * Assumption 2 : either dict, or cdict, is defined, not both */ size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, - const void* dict, size_t dictSize, - const ZSTD_CDict* cdict, - ZSTD_CCtx_params params, unsigned long long pledgedSrcSize) + const void* dict, size_t dictSize, const ZSTD_CDict* cdict, + ZSTD_CCtx_params params, unsigned long long pledgedSrcSize) { assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); assert(!((dict) && (cdict))); /* either dict or cdict, not both */ @@ -3876,8 +3875,7 @@ size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, return ERROR(memory_allocation); } ZSTD_freeCDict(zcs->cdictLocal); - zcs->cdictLocal = ZSTD_createCDict_advanced( - dict, dictSize, + zcs->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dm_auto, params.cParams, zcs->customMem); zcs->cdict = zcs->cdictLocal; @@ -4156,7 +4154,6 @@ size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, } DEBUGLOG(4, "call ZSTDMT_initCStream_internal as nbThreads=%u", params.nbThreads); - CHECK_F( ZSTDMT_initCStream_internal( cctx->mtctx, prefixDict.dict, prefixDict.dictSize, cctx->cdict, params, cctx->pledgedSrcSizePlusOne-1) ); diff --git a/tests/fuzzer.c b/tests/fuzzer.c index db4b33519..439ab39d9 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -813,7 +813,7 @@ static int basicUnitTests(U32 seed, double compressibility) DISPLAYLEVEL(4, "test%3i : Loading rawContent starting with dict header w/ ZSTD_dm_auto should fail", testNb++); { size_t ret; - MEM_writeLE32(dictBuffer+2, ZSTD_MAGIC_DICTIONARY); + MEM_writeLE32((char*)dictBuffer+2, ZSTD_MAGIC_DICTIONARY); ret = ZSTD_CCtx_loadDictionary_advanced( cctx, (const char*)dictBuffer+2, dictSize-2, ZSTD_dlm_byRef, ZSTD_dm_auto); if (!ZSTD_isError(ret)) goto _output_error; @@ -823,7 +823,7 @@ static int basicUnitTests(U32 seed, double compressibility) DISPLAYLEVEL(4, "test%3i : Loading rawContent starting with dict header w/ ZSTD_dm_rawContent should pass", testNb++); { size_t ret; - MEM_writeLE32(dictBuffer+2, ZSTD_MAGIC_DICTIONARY); + MEM_writeLE32((char*)dictBuffer+2, ZSTD_MAGIC_DICTIONARY); ret = ZSTD_CCtx_loadDictionary_advanced( cctx, (const char*)dictBuffer+2, dictSize-2, ZSTD_dlm_byRef, ZSTD_dm_rawContent); if (ZSTD_isError(ret)) goto _output_error; From 90a31bfa162ce0aaac9f3645ab553bbc417b5bc3 Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Wed, 30 Aug 2017 14:36:54 -0700 Subject: [PATCH 52/52] Pass dictMode to ZSTDMT_initCStream; fix nits - Return error code in estimate{CCtx,CStream}Size functions --- lib/compress/zstd_compress.c | 45 +++++++++++++++------------------- lib/compress/zstdmt_compress.c | 15 ++++++------ lib/zstd.h | 8 +++--- tests/zstreamtest.c | 22 +++++++++++++++++ 4 files changed, 54 insertions(+), 36 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 206e0976a..5adeb480b 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -233,7 +233,7 @@ static ZSTD_CCtx_params ZSTD_makeCCtxParamsFromCParams( ZSTD_compressionParameters cParams) { ZSTD_CCtx_params cctxParams; - memset(&cctxParams, 0, sizeof(ZSTD_CCtx_params)); + memset(&cctxParams, 0, sizeof(cctxParams)); cctxParams.cParams = cParams; cctxParams.compressionLevel = ZSTD_CLEVEL_CUSTOM; return cctxParams; @@ -271,7 +271,7 @@ size_t ZSTD_resetCCtxParams(ZSTD_CCtx_params* params) size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* cctxParams, int compressionLevel) { if (!cctxParams) { return ERROR(GENERIC); } - memset(cctxParams, 0, sizeof(ZSTD_CCtx_params)); + memset(cctxParams, 0, sizeof(*cctxParams)); cctxParams->compressionLevel = compressionLevel; return 0; } @@ -280,7 +280,7 @@ size_t ZSTD_initCCtxParams_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameter { if (!cctxParams) { return ERROR(GENERIC); } CHECK_F( ZSTD_checkCParams(params.cParams) ); - memset(cctxParams, 0, sizeof(ZSTD_CCtx_params)); + memset(cctxParams, 0, sizeof(*cctxParams)); cctxParams->cParams = params.cParams; cctxParams->fParams = params.fParams; cctxParams->compressionLevel = ZSTD_CLEVEL_CUSTOM; @@ -543,7 +543,7 @@ size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict) { if (cctx->streamStage != zcss_init) return ERROR(stage_wrong); cctx->cdict = cdict; - memset(&cctx->prefixDict, 0, sizeof(ZSTD_prefixDict)); /* exclusive */ + memset(&cctx->prefixDict, 0, sizeof(cctx->prefixDict)); /* exclusive */ return 0; } @@ -657,9 +657,7 @@ ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, u size_t ZSTD_estimateCCtxSize_advanced_usingCCtxParams(const ZSTD_CCtx_params* params) { /* Estimate CCtx size is supported for single-threaded compression only. */ - if (params->nbThreads > 1) { - return 0; - } + if (params->nbThreads > 1) { return ERROR(GENERIC); } { ZSTD_compressionParameters const cParams = ZSTD_getCParamsFromCCtxParams(*params, 0, 0); size_t const blockSize = MIN(ZSTD_BLOCKSIZE_MAX, (size_t)1 << cParams.windowLog); @@ -701,9 +699,7 @@ size_t ZSTD_estimateCCtxSize(int compressionLevel) size_t ZSTD_estimateCStreamSize_advanced_usingCCtxParams(const ZSTD_CCtx_params* params) { - if (params->nbThreads > 1) { - return 0; - } + if (params->nbThreads > 1) { return ERROR(GENERIC); } { size_t const CCtxSize = ZSTD_estimateCCtxSize_advanced_usingCCtxParams(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; @@ -3171,7 +3167,7 @@ size_t ZSTD_compressContinue (ZSTD_CCtx* cctx, size_t ZSTD_getBlockSize(const ZSTD_CCtx* cctx) { - ZSTD_compressionParameters cParams = + ZSTD_compressionParameters const cParams = ZSTD_getCParamsFromCCtxParams(cctx->appliedParams, 0, 0); return MIN (ZSTD_BLOCKSIZE_MAX, 1 << cParams.windowLog); } @@ -3413,7 +3409,7 @@ size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize) { - ZSTD_CCtx_params cctxParams = + ZSTD_CCtx_params const cctxParams = ZSTD_assignParamsToCCtxParams(cctx->requestedParams, params); return ZSTD_compressBegin_advanced_internal(cctx, dict, dictSize, ZSTD_dm_auto, cctxParams, @@ -3423,7 +3419,7 @@ size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel) { ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, dictSize); - ZSTD_CCtx_params cctxParams = + ZSTD_CCtx_params const cctxParams = ZSTD_assignParamsToCCtxParams(cctx->requestedParams, params); return ZSTD_compressBegin_internal(cctx, dict, dictSize, ZSTD_dm_auto, NULL, cctxParams, 0, ZSTDb_not_buffered); @@ -3505,7 +3501,7 @@ static size_t ZSTD_compress_internal (ZSTD_CCtx* cctx, const void* dict,size_t dictSize, ZSTD_parameters params) { - ZSTD_CCtx_params cctxParams = + ZSTD_CCtx_params const cctxParams = ZSTD_assignParamsToCCtxParams(cctx->requestedParams, params); return ZSTD_compress_advanced_internal(cctx, dst, dstCapacity, @@ -3611,10 +3607,7 @@ static size_t ZSTD_initCDict_internal( } cdict->dictContentSize = dictSize; - { ZSTD_frameParameters const fParams = { 0 /* contentSizeFlag */, - 0 /* checksumFlag */, 0 /* noDictIDFlag */ }; /* dummy */ - ZSTD_CCtx_params cctxParams = cdict->refContext->requestedParams; - cctxParams.fParams = fParams; + { ZSTD_CCtx_params cctxParams = cdict->refContext->requestedParams; cctxParams.cParams = cParams; CHECK_F( ZSTD_compressBegin_internal(cdict->refContext, cdict->dictContent, dictSize, dictMode, @@ -3923,7 +3916,7 @@ size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize) { - ZSTD_CCtx_params cctxParams = + ZSTD_CCtx_params const cctxParams = ZSTD_assignParamsToCCtxParams(zcs->requestedParams, params); CHECK_F( ZSTD_checkCParams(params.cParams) ); return ZSTD_initCStream_internal(zcs, dict, dictSize, NULL, cctxParams, pledgedSrcSize); @@ -3932,7 +3925,7 @@ size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel) { ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, dictSize); - ZSTD_CCtx_params cctxParams = + ZSTD_CCtx_params const cctxParams = ZSTD_assignParamsToCCtxParams(zcs->requestedParams, params); return ZSTD_initCStream_internal(zcs, dict, dictSize, NULL, cctxParams, 0); } @@ -3940,9 +3933,9 @@ size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t di size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize) { ZSTD_CCtx_params cctxParams; - ZSTD_parameters params = ZSTD_getParams(compressionLevel, pledgedSrcSize, 0); - params.fParams.contentSizeFlag = (pledgedSrcSize>0); + ZSTD_parameters const params = ZSTD_getParams(compressionLevel, pledgedSrcSize, 0); cctxParams = ZSTD_assignParamsToCCtxParams(zcs->requestedParams, params); + cctxParams.fParams.contentSizeFlag = (pledgedSrcSize>0); return ZSTD_initCStream_internal(zcs, NULL, 0, NULL, cctxParams, pledgedSrcSize); } @@ -4122,7 +4115,8 @@ size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuf * must receive dict, or cdict, or none, but not both. * @return : 0, or an error code */ size_t ZSTDMT_initCStream_internal(ZSTDMT_CCtx* zcs, - const void* dict, size_t dictSize, const ZSTD_CDict* cdict, + const void* dict, size_t dictSize, ZSTD_dictMode_e dictMode, + const ZSTD_CDict* cdict, ZSTD_CCtx_params params, unsigned long long pledgedSrcSize); @@ -4142,7 +4136,7 @@ size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, ZSTD_CCtx_params params = cctx->requestedParams; params.cParams = ZSTD_getCParamsFromCCtxParams( cctx->requestedParams, cctx->pledgedSrcSizePlusOne-1, 0 /*dictSize*/); - memset(&cctx->prefixDict, 0, sizeof(ZSTD_prefixDict)); /* single usage */ + memset(&cctx->prefixDict, 0, sizeof(cctx->prefixDict)); /* single usage */ assert(prefixDict.dict==NULL || cctx->cdict==NULL); /* only one can be set */ #ifdef ZSTD_MULTITHREAD @@ -4155,7 +4149,8 @@ size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, DEBUGLOG(4, "call ZSTDMT_initCStream_internal as nbThreads=%u", params.nbThreads); CHECK_F( ZSTDMT_initCStream_internal( - cctx->mtctx, prefixDict.dict, prefixDict.dictSize, + cctx->mtctx, + prefixDict.dict, prefixDict.dictSize, ZSTD_dm_rawContent, cctx->cdict, params, cctx->pledgedSrcSizePlusOne-1) ); cctx->streamStage = zcss_load; cctx->appliedParams.nbThreads = params.nbThreads; diff --git a/lib/compress/zstdmt_compress.c b/lib/compress/zstdmt_compress.c index 86afe5065..166f99d72 100644 --- a/lib/compress/zstdmt_compress.c +++ b/lib/compress/zstdmt_compress.c @@ -191,7 +191,7 @@ static void ZSTDMT_releaseBuffer(ZSTDMT_bufferPool* bufPool, buffer_t buf) static ZSTD_CCtx_params ZSTDMT_makeJobCCtxParams(ZSTD_CCtx_params const params) { ZSTD_CCtx_params jobParams; - memset(&jobParams, 0, sizeof(ZSTD_CCtx_params)); + memset(&jobParams, 0, sizeof(jobParams)); jobParams.cParams = params.cParams; jobParams.fParams = params.fParams; @@ -737,7 +737,8 @@ static void ZSTDMT_waitForAllJobsCompleted(ZSTDMT_CCtx* zcs) } size_t ZSTDMT_initCStream_internal( - ZSTDMT_CCtx* zcs, const void* dict, size_t dictSize, + ZSTDMT_CCtx* zcs, + const void* dict, size_t dictSize, ZSTD_dictMode_e dictMode, const ZSTD_CDict* cdict, ZSTD_CCtx_params params, unsigned long long pledgedSrcSize) { @@ -768,7 +769,7 @@ size_t ZSTDMT_initCStream_internal( DEBUGLOG(4,"cdictLocal: %08X", (U32)(size_t)zcs->cdictLocal); ZSTD_freeCDict(zcs->cdictLocal); zcs->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, - ZSTD_dlm_byCopy, ZSTD_dm_rawContent, /* note : a loadPrefix becomes an internal CDict */ + ZSTD_dlm_byCopy, dictMode, /* note : a loadPrefix becomes an internal CDict */ params.cParams, zcs->cMem); zcs->cdict = zcs->cdictLocal; if (zcs->cdictLocal == NULL) return ERROR(memory_allocation); @@ -807,7 +808,7 @@ size_t ZSTDMT_initCStream_advanced(ZSTDMT_CCtx* mtctx, DEBUGLOG(5, "ZSTDMT_initCStream_advanced"); cctxParams.cParams = params.cParams; cctxParams.fParams = params.fParams; - return ZSTDMT_initCStream_internal(mtctx, dict, dictSize, NULL, + return ZSTDMT_initCStream_internal(mtctx, dict, dictSize, ZSTD_dm_auto, NULL, cctxParams, pledgedSrcSize); } @@ -820,7 +821,7 @@ size_t ZSTDMT_initCStream_usingCDict(ZSTDMT_CCtx* mtctx, cctxParams.cParams = ZSTD_getCParamsFromCDict(cdict); cctxParams.fParams = fParams; if (cdict==NULL) return ERROR(dictionary_wrong); /* method incompatible with NULL cdict */ - return ZSTDMT_initCStream_internal(mtctx, NULL, 0 /*dictSize*/, cdict, + return ZSTDMT_initCStream_internal(mtctx, NULL, 0 /*dictSize*/, ZSTD_dm_auto, cdict, cctxParams, pledgedSrcSize); } @@ -831,7 +832,7 @@ size_t ZSTDMT_resetCStream(ZSTDMT_CCtx* zcs, unsigned long long pledgedSrcSize) { if (zcs->params.nbThreads==1) return ZSTD_resetCStream(zcs->cctxPool->cctx[0], pledgedSrcSize); - return ZSTDMT_initCStream_internal(zcs, NULL, 0, 0, zcs->params, + return ZSTDMT_initCStream_internal(zcs, NULL, 0, ZSTD_dm_auto, 0, zcs->params, pledgedSrcSize); } @@ -840,7 +841,7 @@ size_t ZSTDMT_initCStream(ZSTDMT_CCtx* zcs, int compressionLevel) { ZSTD_CCtx_params cctxParams = zcs->params; cctxParams.cParams = params.cParams; cctxParams.fParams = params.fParams; - return ZSTDMT_initCStream_internal(zcs, NULL, 0, NULL, cctxParams, 0); + return ZSTDMT_initCStream_internal(zcs, NULL, 0, ZSTD_dm_auto, NULL, cctxParams, 0); } diff --git a/lib/zstd.h b/lib/zstd.h index e062f6d2b..c11964408 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -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_usingCParams() can provide a tighter estimation. * ZSTD_estimateCCtxSize_advanced_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel. - * ZSTD_estimateCCtxSize_advanced_usingCCtxParams() 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. + * ZSTD_estimateCCtxSize_advanced_usingCCtxParams() can be used in tandem with ZSTD_CCtxParam_setParameter(). Only single-threaded compression is supported. This function will return an error code if ZSTD_p_nbThreads is > 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_usingCParams(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_usingCParams() can provide a tighter estimation. * ZSTD_estimateCStreamSize_advanced_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel. - * ZSTD_estimateCStreamSize_advanced_usingCCtxParams() 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. + * ZSTD_estimateCStreamSize_advanced_usingCCtxParams() can be used in tandem with ZSTD_CCtxParam_setParameter(). Only single-threaded compression is supported. This function will return an error code 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, @@ -727,9 +727,9 @@ ZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize); ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem); ZSTDLIB_API ZSTD_CStream* ZSTD_initStaticCStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticCCtx() */ ZSTDLIB_API size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize); /**< pledgedSrcSize must be correct, a size of 0 means unknown. for a frame size of 0 use initCStream_advanced */ -ZSTDLIB_API size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel); /**< creates of an internal CDict (incompatible with static CCtx), except if dict == NULL or dictSize < 8, in which case no dict is used. */ +ZSTDLIB_API size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel); /**< creates of an internal CDict (incompatible with static CCtx), except if dict == NULL or dictSize < 8, in which case no dict is used. Note: dict is loaded with ZSTD_dm_auto (treated as a full zstd dictionary if it begins with ZSTD_MAGIC_DICTIONARY, else as raw content) and ZSTD_dlm_byCopy.*/ ZSTDLIB_API size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize, - ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize is optional and can be 0 (meaning unknown). note: if the contentSizeFlag is set, pledgedSrcSize == 0 means the source size is actually 0 */ + ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize is optional and can be 0 (meaning unknown). note: if the contentSizeFlag is set, pledgedSrcSize == 0 means the source size is actually 0. dict is loaded with ZSTD_dm_auto and ZSTD_dlm_byCopy. */ ZSTDLIB_API size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict); /**< note : cdict will just be referenced, and must outlive compression session */ ZSTDLIB_API size_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, const ZSTD_CDict* cdict, ZSTD_frameParameters fParams, unsigned long long pledgedSrcSize); /**< same as ZSTD_initCStream_usingCDict(), with control over frame parameters */ diff --git a/tests/zstreamtest.c b/tests/zstreamtest.c index f8683ac78..8be6a5910 100644 --- a/tests/zstreamtest.c +++ b/tests/zstreamtest.c @@ -149,6 +149,8 @@ static int basicUnitTests(U32 seed, double compressibility, ZSTD_customMem custo U32 testNb = 1; ZSTD_CStream* zc = ZSTD_createCStream_advanced(customMem); ZSTD_DStream* zd = ZSTD_createDStream_advanced(customMem); + ZSTDMT_CCtx* mtctx = ZSTDMT_createCCtx(2); + ZSTD_inBuffer inBuff, inBuff2; ZSTD_outBuffer outBuff; buffer_t dictionary = g_nullBuffer; @@ -605,6 +607,25 @@ static int basicUnitTests(U32 seed, double compressibility, ZSTD_customMem custo if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != ZSTD_CONTENTSIZE_UNKNOWN) goto _output_error; DISPLAYLEVEL(3, "OK \n"); + /* Basic multithreading compression test */ + DISPLAYLEVEL(3, "test%3i : compress %u bytes with multiple threads : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); + { ZSTD_parameters const params = ZSTD_getParams(1, 0, 0); + size_t const r = ZSTDMT_initCStream_advanced(mtctx, CNBuffer, dictSize, params, CNBufferSize); + if (ZSTD_isError(r)) goto _output_error; } + outBuff.dst = (char*)(compressedBuffer); + outBuff.size = compressedBufferSize; + outBuff.pos = 0; + inBuff.src = CNBuffer; + inBuff.size = CNBufferSize; + inBuff.pos = 0; + { size_t const r = ZSTDMT_compressStream_generic(mtctx, &outBuff, &inBuff, ZSTD_e_end); + if (ZSTD_isError(r)) goto _output_error; } + if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ + { size_t const r = ZSTDMT_endStream(mtctx, &outBuff); + if (r != 0) goto _output_error; } /* error, or some data not flushed */ + DISPLAYLEVEL(3, "OK \n"); + + /* Overlen overwriting window data bug */ DISPLAYLEVEL(3, "test%3i : wildcopy doesn't overwrite potential match data : ", testNb++); { /* This test has a window size of 1024 bytes and consists of 3 blocks: @@ -643,6 +664,7 @@ _end: FUZ_freeDictionary(dictionary); ZSTD_freeCStream(zc); ZSTD_freeDStream(zd); + ZSTDMT_freeCCtx(mtctx); free(CNBuffer); free(compressedBuffer); free(decodedBuffer);