1
0
mirror of https://github.com/facebook/zstd.git synced 2025-08-05 19:15:58 +03:00

Merge pull request #3403 from facebook/setCParams

ZSTD_CCtx_setCParams
This commit is contained in:
Yann Collet
2022-12-28 14:07:13 -08:00
committed by GitHub
3 changed files with 58 additions and 13 deletions

View File

@@ -357,10 +357,13 @@ size_t ZSTD_CCtxParams_init(ZSTD_CCtx_params* cctxParams, int compressionLevel)
#define ZSTD_NO_CLEVEL 0 #define ZSTD_NO_CLEVEL 0
/** /**
* Initializes the cctxParams from params and compressionLevel. * Initializes `cctxParams` from `params` and `compressionLevel`.
* @param compressionLevel If params are derived from a compression level then that compression level, otherwise ZSTD_NO_CLEVEL. * @param compressionLevel If params are derived from a compression level then that compression level, otherwise ZSTD_NO_CLEVEL.
*/ */
static void ZSTD_CCtxParams_init_internal(ZSTD_CCtx_params* cctxParams, ZSTD_parameters const* params, int compressionLevel) static void
ZSTD_CCtxParams_init_internal(ZSTD_CCtx_params* cctxParams,
const ZSTD_parameters* params,
int compressionLevel)
{ {
assert(!ZSTD_checkCParams(params->cParams)); assert(!ZSTD_checkCParams(params->cParams));
ZSTD_memset(cctxParams, 0, sizeof(*cctxParams)); ZSTD_memset(cctxParams, 0, sizeof(*cctxParams));
@@ -679,7 +682,7 @@ size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, int value)
if (ZSTD_isUpdateAuthorized(param)) { if (ZSTD_isUpdateAuthorized(param)) {
cctx->cParamsChanged = 1; cctx->cParamsChanged = 1;
} else { } else {
RETURN_ERROR(stage_wrong, "can only set params in ctx init stage"); RETURN_ERROR(stage_wrong, "can only set params in cctx init stage");
} } } }
switch(param) switch(param)
@@ -815,14 +818,14 @@ size_t ZSTD_CCtxParams_setParameter(ZSTD_CCtx_params* CCtxParams,
case ZSTD_c_forceAttachDict : { case ZSTD_c_forceAttachDict : {
const ZSTD_dictAttachPref_e pref = (ZSTD_dictAttachPref_e)value; const ZSTD_dictAttachPref_e pref = (ZSTD_dictAttachPref_e)value;
BOUNDCHECK(ZSTD_c_forceAttachDict, pref); BOUNDCHECK(ZSTD_c_forceAttachDict, (int)pref);
CCtxParams->attachDictPref = pref; CCtxParams->attachDictPref = pref;
return CCtxParams->attachDictPref; return CCtxParams->attachDictPref;
} }
case ZSTD_c_literalCompressionMode : { case ZSTD_c_literalCompressionMode : {
const ZSTD_paramSwitch_e lcm = (ZSTD_paramSwitch_e)value; const ZSTD_paramSwitch_e lcm = (ZSTD_paramSwitch_e)value;
BOUNDCHECK(ZSTD_c_literalCompressionMode, lcm); BOUNDCHECK(ZSTD_c_literalCompressionMode, (int)lcm);
CCtxParams->literalCompressionMode = lcm; CCtxParams->literalCompressionMode = lcm;
return CCtxParams->literalCompressionMode; return CCtxParams->literalCompressionMode;
} }
@@ -1125,6 +1128,21 @@ size_t ZSTD_CCtx_setParametersUsingCCtxParams(
return 0; return 0;
} }
size_t ZSTD_CCtx_setCParams(ZSTD_CCtx* cctx, ZSTD_compressionParameters cparams)
{
DEBUGLOG(4, "ZSTD_CCtx_setCParams");
assert(cctx != NULL);
if (cctx->streamStage != zcss_init) {
/* All parameters in @cparams are allowed to be updated during MT compression.
* This must be signaled, so that MT compression picks up the changes */
cctx->cParamsChanged = 1;
}
/* only update if parameters are valid */
FORWARD_IF_ERROR(ZSTD_checkCParams(cparams), "");
cctx->requestedParams.cParams = cparams;
return 0;
}
size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize) size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize)
{ {
DEBUGLOG(4, "ZSTD_CCtx_setPledgedSrcSize to %llu bytes", pledgedSrcSize); DEBUGLOG(4, "ZSTD_CCtx_setPledgedSrcSize to %llu bytes", pledgedSrcSize);

View File

@@ -1742,6 +1742,11 @@ ZSTDLIB_STATIC_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params);
* This function never fails (wide contract) */ * This function never fails (wide contract) */
ZSTDLIB_STATIC_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize); ZSTDLIB_STATIC_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize);
/*! ZSTD_CCtx_setCParams() :
* Set all parameters provided within @cparams into the working @cctx
* @return 0 on success, or an error code (can be checked with ZSTD_isError()) */
ZSTDLIB_STATIC_API size_t ZSTD_CCtx_setCParams(ZSTD_CCtx* cctx, ZSTD_compressionParameters cparams);
/*! ZSTD_compress_advanced() : /*! ZSTD_compress_advanced() :
* Note : this function is now DEPRECATED. * Note : this function is now DEPRECATED.
* It can be replaced by ZSTD_compress2(), in combination with ZSTD_CCtx_setParameter() and other parameter setters. * It can be replaced by ZSTD_compress2(), in combination with ZSTD_CCtx_setParameter() and other parameter setters.

View File

@@ -463,9 +463,9 @@ _output_error:
* Unit tests * Unit tests
=============================================*/ =============================================*/
static void test_compressBound(int tnb) static void test_compressBound(unsigned tnb)
{ {
DISPLAYLEVEL(3, "test%3i : compressBound : ", tnb); DISPLAYLEVEL(3, "test%3u : compressBound : ", tnb);
/* check ZSTD_compressBound == ZSTD_COMPRESSBOUND /* check ZSTD_compressBound == ZSTD_COMPRESSBOUND
* for a large range of known valid values */ * for a large range of known valid values */
@@ -485,9 +485,9 @@ static void test_compressBound(int tnb)
DISPLAYLEVEL(3, "OK \n"); DISPLAYLEVEL(3, "OK \n");
} }
static void test_decompressBound(int tnb) static void test_decompressBound(unsigned tnb)
{ {
DISPLAYLEVEL(3, "test%3i : decompressBound : ", tnb); DISPLAYLEVEL(3, "test%3u : decompressBound : ", tnb);
// Simple compression, with size : should provide size; // Simple compression, with size : should provide size;
{ const char example[] = "abcd"; { const char example[] = "abcd";
@@ -538,6 +538,26 @@ static void test_decompressBound(int tnb)
DISPLAYLEVEL(3, "OK \n"); DISPLAYLEVEL(3, "OK \n");
} }
static void test_setCParams(unsigned tnb)
{
ZSTD_CCtx* const cctx = ZSTD_createCCtx();
ZSTD_compressionParameters cparams;
assert(cctx);
DISPLAYLEVEL(3, "test%3u : ZSTD_CCtx_setCParams : ", tnb);
/* valid cparams */
cparams = ZSTD_getCParams(1, 0, 0);
CHECK_Z(ZSTD_CCtx_setCParams(cctx, cparams));
/* invalid cparams (must fail) */
cparams.windowLog = 99;
CHECK(ZSTD_isError(ZSTD_CCtx_setCParams(cctx, cparams)));
free(cctx);
DISPLAYLEVEL(3, "OK \n");
}
static int basicUnitTests(U32 const seed, double compressibility) static int basicUnitTests(U32 const seed, double compressibility)
{ {
size_t const CNBuffSize = 5 MB; size_t const CNBuffSize = 5 MB;
@@ -588,6 +608,8 @@ static int basicUnitTests(U32 const seed, double compressibility)
test_decompressBound(testNb++); test_decompressBound(testNb++);
test_setCParams(testNb++);
DISPLAYLEVEL(3, "test%3u : ZSTD_adjustCParams : ", testNb++); DISPLAYLEVEL(3, "test%3u : ZSTD_adjustCParams : ", testNb++);
{ {
ZSTD_compressionParameters params; ZSTD_compressionParameters params;