feat(compress): move CCtx copy policy into Rust

Move public ZSTD_copyCCtx pledge normalization and frame-parameter policy into
Rust while retaining the private workspace and match-table copy implementation
behind a C callback.

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
- make -C tests -j1 test-zstream ZSTREAM_TESTTIME=-T1s
- focused copy_cctx unit tests
This commit is contained in:
2026-07-19 15:30:07 +02:00
parent c9bcec362c
commit 41255c8dcc
2 changed files with 216 additions and 8 deletions
+45 -8
View File
@@ -350,6 +350,30 @@ typedef char ZSTD_rust_reset_cctx_state_layout[
== 6 * sizeof(void*)
&& sizeof(ZSTD_rust_resetCCtxState) == 7 * sizeof(void*))
? 1 : -1];
typedef size_t (*ZSTD_rust_copyCCtxInternal_f)(
void* context, const void* srcCCtx,
const ZSTD_frameParameters* fParams,
U64 pledgedSrcSize, int zbuff);
typedef struct {
void* callbackContext;
const void* srcCCtx;
const ZSTD_frameParameters* fParams;
const U64* pledgedSrcSize;
const int* zbuff;
ZSTD_rust_copyCCtxInternal_f copyInternal;
} ZSTD_rust_copyCCtxState;
size_t ZSTD_rust_copyCCtx(const ZSTD_rust_copyCCtxState* state);
typedef char ZSTD_rust_copy_cctx_state_layout[
(offsetof(ZSTD_rust_copyCCtxState, callbackContext) == 0
&& offsetof(ZSTD_rust_copyCCtxState, srcCCtx) == sizeof(void*)
&& offsetof(ZSTD_rust_copyCCtxState, fParams) == 2 * sizeof(void*)
&& offsetof(ZSTD_rust_copyCCtxState, pledgedSrcSize)
== 3 * sizeof(void*)
&& offsetof(ZSTD_rust_copyCCtxState, zbuff) == 4 * sizeof(void*)
&& offsetof(ZSTD_rust_copyCCtxState, copyInternal)
== 5 * sizeof(void*)
&& sizeof(ZSTD_rust_copyCCtxState) == 6 * sizeof(void*))
? 1 : -1];
ZSTD_frameProgression ZSTD_rust_frameProgression(U64 consumedSrcSize,
size_t buffered,
U64 producedCSize);
@@ -3495,6 +3519,16 @@ static size_t ZSTD_copyCCtx_internal(ZSTD_CCtx* dstCCtx,
return 0;
}
static size_t ZSTD_rust_copyCCtx_internal_callback(
void* context, const void* srcCCtx,
const ZSTD_frameParameters* fParams,
U64 pledgedSrcSize, int zbuff)
{
return ZSTD_copyCCtx_internal(
(ZSTD_CCtx*)context, (const ZSTD_CCtx*)srcCCtx,
*fParams, pledgedSrcSize, (ZSTD_buffered_policy_e)zbuff);
}
/*! ZSTD_copyCCtx() :
* Duplicate an existing context `srcCCtx` into another one `dstCCtx`.
* Only works during stage ZSTDcs_init (i.e. after creation, but before first call to ZSTD_compressContinue()).
@@ -3502,15 +3536,18 @@ static size_t ZSTD_copyCCtx_internal(ZSTD_CCtx* dstCCtx,
* @return : 0, or an error code */
size_t ZSTD_copyCCtx(ZSTD_CCtx* dstCCtx, const ZSTD_CCtx* srcCCtx, unsigned long long pledgedSrcSize)
{
ZSTD_frameParameters fParams = { 1 /*content*/, 0 /*checksum*/, 0 /*noDictID*/ };
ZSTD_buffered_policy_e const zbuff = srcCCtx->bufferedPolicy;
ZSTD_rust_copyCCtxState state;
ZSTD_frameParameters const fParams = { 1 /*content*/, 0 /*checksum*/, 0 /*noDictID*/ };
U64 const pledgedSrcSizeValue = pledgedSrcSize;
int const zbuffValue = (int)srcCCtx->bufferedPolicy;
ZSTD_STATIC_ASSERT((U32)ZSTDb_buffered==1);
if (pledgedSrcSize==0) pledgedSrcSize = ZSTD_CONTENTSIZE_UNKNOWN;
fParams.contentSizeFlag = (pledgedSrcSize != ZSTD_CONTENTSIZE_UNKNOWN);
return ZSTD_copyCCtx_internal(dstCCtx, srcCCtx,
fParams, pledgedSrcSize,
zbuff);
state.callbackContext = dstCCtx;
state.srcCCtx = srcCCtx;
state.fParams = &fParams;
state.pledgedSrcSize = &pledgedSrcSizeValue;
state.zbuff = &zbuffValue;
state.copyInternal = ZSTD_rust_copyCCtx_internal_callback;
return ZSTD_rust_copyCCtx(&state);
}