feat(compress): move CCtx free policy into Rust

Move ZSTD_freeCCtx null handling, static-context rejection, content/object
free ordering, and embedded-workspace policy behind a Rust-owned ABI bridge.
C retains dictionary, multithread, workspace, and custom allocator teardown
through callbacks, and ZSTD_freeCStream continues to use the CCtx path.

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 free_cctx unit tests
This commit is contained in:
2026-07-19 14:00:21 +02:00
parent 55f28cdd71
commit 148af6d884
2 changed files with 161 additions and 8 deletions
+41 -8
View File
@@ -301,6 +301,27 @@ typedef char ZSTD_rust_ref_thread_pool_state_layout[
&& offsetof(ZSTD_rust_refThreadPoolState, streamStage) == 2 * sizeof(void*)
&& sizeof(ZSTD_rust_refThreadPoolState) == 3 * sizeof(void*))
? 1 : -1];
typedef void (*ZSTD_rust_freeCCtxContent_f)(void* context);
typedef void (*ZSTD_rust_freeCCtxObject_f)(void* context);
typedef struct {
void* callbackContext;
size_t staticSize;
int cctxInWorkspace;
ZSTD_rust_freeCCtxContent_f freeContent;
ZSTD_rust_freeCCtxObject_f freeObject;
} ZSTD_rust_freeCCtxState;
size_t ZSTD_rust_freeCCtx(const ZSTD_rust_freeCCtxState* state);
typedef char ZSTD_rust_free_cctx_state_layout[
(offsetof(ZSTD_rust_freeCCtxState, callbackContext) == 0
&& offsetof(ZSTD_rust_freeCCtxState, staticSize) == sizeof(void*)
&& offsetof(ZSTD_rust_freeCCtxState, cctxInWorkspace)
== 2 * sizeof(void*)
&& offsetof(ZSTD_rust_freeCCtxState, freeContent)
== 3 * sizeof(void*)
&& offsetof(ZSTD_rust_freeCCtxState, freeObject)
== 4 * sizeof(void*)
&& sizeof(ZSTD_rust_freeCCtxState) == 5 * sizeof(void*))
? 1 : -1];
typedef void (*ZSTD_rust_resetCCtxClearAllDicts_f)(void* context);
typedef size_t (*ZSTD_rust_resetCCtxResetParams_f)(void* context);
typedef struct {
@@ -1660,17 +1681,29 @@ static void ZSTD_freeCCtxContent(ZSTD_CCtx* cctx)
ZSTD_cwksp_free(&cctx->workspace, cctx->customMem);
}
static void ZSTD_rust_freeCCtx_freeContent(void* context)
{
ZSTD_freeCCtxContent((ZSTD_CCtx*)context);
}
static void ZSTD_rust_freeCCtx_freeObject(void* context)
{
ZSTD_CCtx* const cctx = (ZSTD_CCtx*)context;
ZSTD_customFree(cctx, cctx->customMem);
}
size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx)
{
ZSTD_rust_freeCCtxState state;
DEBUGLOG(3, "ZSTD_freeCCtx (address: %p)", (void*)cctx);
if (cctx==NULL) return 0; /* support free on NULL */
RETURN_ERROR_IF(cctx->staticSize, memory_allocation,
"not compatible with static CCtx");
{ int cctxInWorkspace = ZSTD_cwksp_owns_buffer(&cctx->workspace, cctx);
ZSTD_freeCCtxContent(cctx);
if (!cctxInWorkspace) ZSTD_customFree(cctx, cctx->customMem);
}
return 0;
state.callbackContext = cctx;
state.staticSize = cctx == NULL ? 0 : cctx->staticSize;
state.cctxInWorkspace = cctx != NULL && cctx->staticSize == 0
? ZSTD_cwksp_owns_buffer(&cctx->workspace, cctx)
: 0;
state.freeContent = ZSTD_rust_freeCCtx_freeContent;
state.freeObject = ZSTD_rust_freeCCtx_freeObject;
return ZSTD_rust_freeCCtx(&state);
}