From 6c6e1751f60c19ecdddad8da250520a19e4b139f Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 27 Jun 2016 15:28:45 +0200 Subject: [PATCH] use ZSTD_getParams() to simplify code --- lib/common/zstd.h | 10 +++++----- lib/compress/zbuff_compress.c | 4 +--- lib/compress/zstd_compress.c | 10 +++------- programs/fileio.c | 4 +--- programs/fuzzer.c | 12 ++++-------- programs/zbufftest.c | 10 +++------- 6 files changed, 17 insertions(+), 33 deletions(-) diff --git a/lib/common/zstd.h b/lib/common/zstd.h index af74d830..848aaa9b 100644 --- a/lib/common/zstd.h +++ b/lib/common/zstd.h @@ -269,6 +269,11 @@ ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictS ZSTDLIB_API unsigned ZSTD_maxCLevel (void); +/*! ZSTD_getParams() : +* same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of a `ZSTD_compressionParameters`. +* All fields of `ZSTD_frameParameters` are set to default (0) */ +ZSTD_parameters ZSTD_getParams(int compressionLevel, U64 srcSize, size_t dictSize); + /*! ZSTD_getCParams() : * @return ZSTD_compressionParameters structure for a selected compression level and srcSize. * `srcSize` value is optional, select 0 if not known */ @@ -283,11 +288,6 @@ ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params); * both values are optional, select `0` if unknown. */ ZSTDLIB_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, U64 srcSize, size_t dictSize); -/*! ZSTD_getParams() : -* same as ZSTD_getCParams(), but @return a `ZSTD_parameters` object instead of a `ZSTD_compressionParameters`. -* All fields of `ZSTD_frameParameters` are set to default (0) */ -ZSTD_parameters ZSTD_getParams(int compressionLevel, U64 srcSize, size_t dictSize); - /*! ZSTD_compress_advanced() : * Same as ZSTD_compress_usingDict(), with fine-tune control of each compression parameter */ ZSTDLIB_API size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx, diff --git a/lib/compress/zbuff_compress.c b/lib/compress/zbuff_compress.c index 6ed5e52a..ef8448ce 100644 --- a/lib/compress/zbuff_compress.c +++ b/lib/compress/zbuff_compress.c @@ -170,9 +170,7 @@ size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* zbc, size_t ZBUFF_compressInitDictionary(ZBUFF_CCtx* zbc, const void* dict, size_t dictSize, int compressionLevel) { - ZSTD_parameters params; - memset(¶ms, 0, sizeof(params)); - params.cParams = ZSTD_getCParams(compressionLevel, 0, dictSize); + ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, dictSize); return ZBUFF_compressInit_advanced(zbc, dict, dictSize, params, 0); } diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 0e40a7ea..44636dd4 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -2435,9 +2435,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 params; - memset(¶ms, 0, sizeof(params)); - params.cParams = ZSTD_getCParams(compressionLevel, 0, dictSize); + ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, dictSize); ZSTD_LOG_BLOCK("%p: ZSTD_compressBegin_usingDict compressionLevel=%d\n", cctx->base, compressionLevel); return ZSTD_compressBegin_internal(cctx, dict, dictSize, params, 0); } @@ -2547,11 +2545,9 @@ size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx, 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; - memset(¶ms, 0, sizeof(params)); - ZSTD_LOG_BLOCK("%p: ZSTD_compress_usingDict srcSize=%d dictSize=%d compressionLevel=%d\n", ctx->base, (int)srcSize, (int)dictSize, compressionLevel); - params.cParams = ZSTD_getCParams(compressionLevel, srcSize, dictSize); + ZSTD_parameters params = ZSTD_getParams(compressionLevel, srcSize, dictSize); params.fParams.contentSizeFlag = 1; + ZSTD_LOG_BLOCK("%p: ZSTD_compress_usingDict srcSize=%d dictSize=%d compressionLevel=%d\n", ctx->base, (int)srcSize, (int)dictSize, compressionLevel); return ZSTD_compress_internal(ctx, dst, dstCapacity, src, srcSize, dict, dictSize, params); } diff --git a/programs/fileio.c b/programs/fileio.c index 5e7b26dc..c5a985c7 100644 --- a/programs/fileio.c +++ b/programs/fileio.c @@ -315,9 +315,7 @@ static int FIO_compressFilename_internal(cRess_t ress, U64 const fileSize = UTIL_getFileSize(srcFileName); /* init */ - { ZSTD_parameters params; - memset(¶ms, 0, sizeof(params)); - params.cParams = ZSTD_getCParams(cLevel, fileSize, ress.dictBufferSize); + { ZSTD_parameters params = ZSTD_getParams(cLevel, fileSize, ress.dictBufferSize); params.fParams.contentSizeFlag = 1; params.fParams.checksumFlag = g_checksumFlag; params.fParams.noDictIDFlag = !g_dictIDFlag; diff --git a/programs/fuzzer.c b/programs/fuzzer.c index 1862e46a..cd87775e 100644 --- a/programs/fuzzer.c +++ b/programs/fuzzer.c @@ -216,10 +216,8 @@ static int basicUnitTests(U32 seed, double compressibility) DISPLAYLEVEL(4, "test%3i : check content size on duplicated context : ", testNb++); { size_t const testSize = CNBuffSize / 3; - { ZSTD_compressionParameters const cPar = ZSTD_getCParams(2, testSize, dictSize); - ZSTD_frameParameters const fPar = { 1 , 0 , 0 }; - ZSTD_parameters p; - p.cParams = cPar; p.fParams = fPar; + { ZSTD_parameters p = ZSTD_getParams(2, testSize, dictSize); + p.fParams.contentSizeFlag = 1; CHECK( ZSTD_compressBegin_advanced(ctxOrig, CNBuffer, dictSize, p, testSize-1) ); } CHECK( ZSTD_copyCCtx(ctxDuplicated, ctxOrig) ); @@ -277,10 +275,8 @@ static int basicUnitTests(U32 seed, double compressibility) DISPLAYLEVEL(4, "OK \n"); DISPLAYLEVEL(4, "test%3i : compress without dictID : ", testNb++); - { ZSTD_frameParameters const fParams = { 0 /*contentSize*/, 0 /*checksum*/, 1 /*NoDictID*/ }; - ZSTD_compressionParameters const cParams = ZSTD_getCParams(3, CNBuffSize, dictSize); - ZSTD_parameters p; - p.cParams = cParams; p.fParams = fParams; + { ZSTD_parameters p = ZSTD_getParams(3, CNBuffSize, dictSize); + p.fParams.noDictIDFlag = 1; cSize = ZSTD_compress_advanced(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize), CNBuffer, CNBuffSize, dictBuffer, dictSize, p); diff --git a/programs/zbufftest.c b/programs/zbufftest.c index 41dfa338..3e36d015 100644 --- a/programs/zbufftest.c +++ b/programs/zbufftest.c @@ -381,13 +381,9 @@ static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compres { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize); dict = srcBuffer + dictStart; } - { ZSTD_compressionParameters const cPar = ZSTD_getCParams(cLevel, 0, dictSize); - U32 const checksum = FUZ_rand(&lseed) & 1; - U32 const noDictIDFlag = FUZ_rand(&lseed) & 1; - ZSTD_frameParameters const fPar = { 0, checksum, noDictIDFlag }; - ZSTD_parameters params; - params.cParams = cPar; - params.fParams = fPar; + { ZSTD_parameters params = ZSTD_getParams(cLevel, 0, dictSize); + params.fParams.checksumFlag = FUZ_rand(&lseed) & 1; + params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1; { size_t const initError = ZBUFF_compressInit_advanced(zc, dict, dictSize, params, 0); CHECK (ZBUFF_isError(initError),"init error : %s", ZBUFF_getErrorName(initError)); } } }