refactor(compress): move heap CCtx release order to Rust

ZSTD_freeCCtx previously passed one C callback that bundled dictionary,
multithreaded context, workspace, and heap-object teardown. That left the
lifetime order inside private C control flow and made the public destruction
policy harder to audit. Project the four private operations separately and
let Rust enforce the original dictionary, MT-context, workspace, and
object order while preserving the static-context and workspace-embedded
object guards. The callbacks still own the C layouts and custom allocator
operations.

Test Plan:
- `ulimit -v 41943040; cargo +nightly fmt --manifest-path rust/Cargo.toml --all -- --check` -- passed
- `ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo clippy --manifest-path rust/Cargo.toml --all-targets -- -D warnings` -- passed
- Broader native and original-test verification remains pending for the complete batch.
This commit is contained in:
2026-07-20 19:41:52 +02:00
parent a152fe6498
commit d714aeac23
2 changed files with 64 additions and 34 deletions
+24 -15
View File
@@ -297,14 +297,15 @@ 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 void (*ZSTD_rust_freeCCtxCallback_f)(void* context);
typedef struct {
void* callbackContext;
size_t staticSize;
int cctxInWorkspace;
ZSTD_rust_freeCCtxContent_f freeContent;
ZSTD_rust_freeCCtxObject_f freeObject;
ZSTD_rust_freeCCtxCallback_f freeDictionaries;
ZSTD_rust_freeCCtxCallback_f freeMTContext;
ZSTD_rust_freeCCtxCallback_f freeWorkspace;
ZSTD_rust_freeCCtxCallback_f freeObject;
} ZSTD_rust_freeCCtxState;
size_t ZSTD_rust_freeCCtx(const ZSTD_rust_freeCCtxState* state);
typedef char ZSTD_rust_free_cctx_state_layout[
@@ -312,11 +313,15 @@ typedef char ZSTD_rust_free_cctx_state_layout[
&& offsetof(ZSTD_rust_freeCCtxState, staticSize) == sizeof(void*)
&& offsetof(ZSTD_rust_freeCCtxState, cctxInWorkspace)
== 2 * sizeof(void*)
&& offsetof(ZSTD_rust_freeCCtxState, freeContent)
&& offsetof(ZSTD_rust_freeCCtxState, freeDictionaries)
== 3 * sizeof(void*)
&& offsetof(ZSTD_rust_freeCCtxState, freeObject)
&& offsetof(ZSTD_rust_freeCCtxState, freeMTContext)
== 4 * sizeof(void*)
&& sizeof(ZSTD_rust_freeCCtxState) == 5 * sizeof(void*))
&& offsetof(ZSTD_rust_freeCCtxState, freeWorkspace)
== 5 * sizeof(void*)
&& offsetof(ZSTD_rust_freeCCtxState, freeObject)
== 6 * sizeof(void*)
&& sizeof(ZSTD_rust_freeCCtxState) == 7 * sizeof(void*))
? 1 : -1];
typedef int (*ZSTD_rust_createCCtxValidateCustomMem_f)(void* context);
typedef void* (*ZSTD_rust_createCCtxAllocate_f)(void* context, size_t size);
@@ -3635,20 +3640,20 @@ static size_t ZSTD_sizeof_localDict(ZSTD_localDict dict)
cdictSize);
}
static void ZSTD_freeCCtxContent(ZSTD_CCtx* cctx)
static void ZSTD_rust_freeCCtx_freeMTContext(void* context)
{
assert(cctx != NULL);
assert(cctx->staticSize == 0);
ZSTD_clearAllDicts(cctx);
#ifdef ZSTD_MULTITHREAD
ZSTD_CCtx* const cctx = (ZSTD_CCtx*)context;
ZSTDMT_freeCCtx(cctx->mtctx); cctx->mtctx = NULL;
#else
(void)context;
#endif
ZSTD_cwksp_free(&cctx->workspace, cctx->customMem);
}
static void ZSTD_rust_freeCCtx_freeContent(void* context)
static void ZSTD_rust_freeCCtx_freeWorkspace(void* context)
{
ZSTD_freeCCtxContent((ZSTD_CCtx*)context);
ZSTD_CCtx* const cctx = (ZSTD_CCtx*)context;
ZSTD_cwksp_free(&cctx->workspace, cctx->customMem);
}
static void ZSTD_rust_freeCCtx_freeObject(void* context)
@@ -3666,7 +3671,11 @@ size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx)
state.cctxInWorkspace = cctx != NULL && cctx->staticSize == 0
? ZSTD_cwksp_owns_buffer(&cctx->workspace, cctx)
: 0;
state.freeContent = ZSTD_rust_freeCCtx_freeContent;
/* Rust owns the release order; these callbacks retain only private C
* dictionary, MT-context, workspace, and object operations. */
state.freeDictionaries = ZSTD_clearAllDicts_callback;
state.freeMTContext = ZSTD_rust_freeCCtx_freeMTContext;
state.freeWorkspace = ZSTD_rust_freeCCtx_freeWorkspace;
state.freeObject = ZSTD_rust_freeCCtx_freeObject;
return ZSTD_rust_freeCCtx(&state);
}