feat(compress): move CCtx reset policy into Rust

Move ZSTD_CCtx_reset directive handling into a Rust-owned state machine while keeping dictionary cleanup and parameter reset behind C callbacks. Preserve the original session ordering, stage error boundary, completion/max-block flag updates, and reset error propagation across the ABI bridge.

Test Plan:
- cargo test --manifest-path rust/Cargo.toml --lib
- cargo clippy --manifest-path rust/Cargo.toml --all-targets -- -D warnings
- make -B -C programs -j1 zstd
- focused reset_cctx unit tests
This commit is contained in:
2026-07-19 13:22:25 +02:00
parent 878e18a966
commit 1cf5a92e02
2 changed files with 291 additions and 15 deletions
+49 -15
View File
@@ -247,6 +247,34 @@ typedef char ZSTD_rust_set_params_state_layout[
&& offsetof(ZSTD_rust_setParamsState, setCParams) == 3 * sizeof(void*)
&& sizeof(ZSTD_rust_setParamsState) == 4 * sizeof(void*))
? 1 : -1];
typedef void (*ZSTD_rust_resetCCtxClearAllDicts_f)(void* context);
typedef size_t (*ZSTD_rust_resetCCtxResetParams_f)(void* context);
typedef struct {
void* callbackContext;
unsigned* rustSimpleCompress2Completed;
ZSTD_cStreamStage* streamStage;
unsigned long long* pledgedSrcSizePlusOne;
unsigned* rustSimpleCompress2MaxBlockSizeSet;
ZSTD_rust_resetCCtxClearAllDicts_f clearAllDicts;
ZSTD_rust_resetCCtxResetParams_f resetParams;
} ZSTD_rust_resetCCtxState;
size_t ZSTD_rust_resetCCtx(const ZSTD_rust_resetCCtxState* state, int reset);
typedef char ZSTD_rust_reset_cctx_state_layout[
(offsetof(ZSTD_rust_resetCCtxState, callbackContext) == 0
&& offsetof(ZSTD_rust_resetCCtxState, rustSimpleCompress2Completed)
== sizeof(void*)
&& offsetof(ZSTD_rust_resetCCtxState, streamStage)
== 2 * sizeof(void*)
&& offsetof(ZSTD_rust_resetCCtxState, pledgedSrcSizePlusOne)
== 3 * sizeof(void*)
&& offsetof(ZSTD_rust_resetCCtxState, rustSimpleCompress2MaxBlockSizeSet)
== 4 * sizeof(void*)
&& offsetof(ZSTD_rust_resetCCtxState, clearAllDicts)
== 5 * sizeof(void*)
&& offsetof(ZSTD_rust_resetCCtxState, resetParams)
== 6 * sizeof(void*)
&& sizeof(ZSTD_rust_resetCCtxState) == 7 * sizeof(void*))
? 1 : -1];
ZSTD_frameProgression ZSTD_rust_frameProgression(U64 consumedSrcSize,
size_t buffered,
U64 producedCSize);
@@ -2099,25 +2127,31 @@ size_t ZSTD_CCtx_refPrefix_advanced(
ZSTD_clearAllDicts_callback, ZSTD_assignPrefixDict_callback);
}
static void ZSTD_rust_resetCCtx_clearAllDicts(void* context)
{
ZSTD_clearAllDicts((ZSTD_CCtx*)context);
}
static size_t ZSTD_rust_resetCCtx_resetParams(void* context)
{
ZSTD_CCtx* const cctx = (ZSTD_CCtx*)context;
return ZSTD_CCtxParams_reset(&cctx->requestedParams);
}
/*! ZSTD_CCtx_reset() :
* Also dumps dictionary */
size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_ResetDirective reset)
{
cctx->rustSimpleCompress2Completed = 0;
if ( (reset == ZSTD_reset_session_only)
|| (reset == ZSTD_reset_session_and_parameters) ) {
cctx->streamStage = zcss_init;
cctx->pledgedSrcSizePlusOne = 0;
}
if ( (reset == ZSTD_reset_parameters)
|| (reset == ZSTD_reset_session_and_parameters) ) {
RETURN_ERROR_IF(cctx->streamStage != zcss_init, stage_wrong,
"Reset parameters is only possible during init stage.");
ZSTD_clearAllDicts(cctx);
cctx->rustSimpleCompress2MaxBlockSizeSet = 0;
return ZSTD_CCtxParams_reset(&cctx->requestedParams);
}
return 0;
ZSTD_rust_resetCCtxState state;
state.callbackContext = cctx;
state.rustSimpleCompress2Completed = &cctx->rustSimpleCompress2Completed;
state.streamStage = &cctx->streamStage;
state.pledgedSrcSizePlusOne = &cctx->pledgedSrcSizePlusOne;
state.rustSimpleCompress2MaxBlockSizeSet =
&cctx->rustSimpleCompress2MaxBlockSizeSet;
state.clearAllDicts = ZSTD_rust_resetCCtx_clearAllDicts;
state.resetParams = ZSTD_rust_resetCCtx_resetParams;
return ZSTD_rust_resetCCtx(&state, (int)reset);
}