feat(compress): move CDict free policy into Rust

Move public CDict null handling, workspace/object teardown ordering, and
embedded-object selection behind a Rust-owned boundary. Keep private workspace
and allocator teardown in C callbacks.

Test Plan:
- ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/Cargo.toml --release free_cdict -- --nocapture
- ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/Cargo.toml --release
- ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo clippy --manifest-path rust/Cargo.toml --release --all-targets -- -D warnings
- ulimit -v 41943040; make -B -C programs -j1 zstd
- ulimit -v 41943040; make -C tests -j1 test-zstream ZSTREAM_TESTTIME=-T1s
This commit is contained in:
2026-07-19 16:19:26 +02:00
parent 7cacbcee03
commit 14157558f7
2 changed files with 143 additions and 9 deletions
+38 -9
View File
@@ -368,6 +368,25 @@ typedef char ZSTD_rust_create_cdict_state_layout[
== 3 * sizeof(void*)
&& sizeof(ZSTD_rust_createCDictState) == 4 * sizeof(void*))
? 1 : -1];
typedef void (*ZSTD_rust_freeCDictWorkspace_f)(void* context);
typedef void (*ZSTD_rust_freeCDictObject_f)(void* context);
typedef struct {
void* callbackContext;
int cdictInWorkspace;
ZSTD_rust_freeCDictWorkspace_f freeWorkspace;
ZSTD_rust_freeCDictObject_f freeObject;
} ZSTD_rust_freeCDictState;
size_t ZSTD_rust_freeCDict(const ZSTD_rust_freeCDictState* state);
typedef char ZSTD_rust_free_cdict_state_layout[
(offsetof(ZSTD_rust_freeCDictState, callbackContext) == 0
&& offsetof(ZSTD_rust_freeCDictState, cdictInWorkspace)
== sizeof(void*)
&& offsetof(ZSTD_rust_freeCDictState, freeWorkspace)
== 2 * sizeof(void*)
&& offsetof(ZSTD_rust_freeCDictState, freeObject)
== 3 * sizeof(void*)
&& sizeof(ZSTD_rust_freeCDictState) == 4 * sizeof(void*))
? 1 : -1];
typedef void (*ZSTD_rust_resetCCtxClearAllDicts_f)(void* context);
typedef size_t (*ZSTD_rust_resetCCtxResetParams_f)(void* context);
typedef struct {
@@ -5547,17 +5566,27 @@ ZSTD_CDict* ZSTD_createCDict_byReference(const void* dict, size_t dictSize, int
&state, dict, dictSize, compressionLevel, ZSTD_dlm_byRef);
}
static void ZSTD_rust_freeCDict_freeWorkspace(void* context)
{
ZSTD_CDict* const cdict = (ZSTD_CDict*)context;
ZSTD_cwksp_free(&cdict->workspace, cdict->customMem);
}
static void ZSTD_rust_freeCDict_freeObject(void* context)
{
ZSTD_CDict* const cdict = (ZSTD_CDict*)context;
ZSTD_customFree(cdict, cdict->customMem);
}
size_t ZSTD_freeCDict(ZSTD_CDict* cdict)
{
if (cdict==NULL) return 0; /* support free on NULL */
{ ZSTD_customMem const cMem = cdict->customMem;
int cdictInWorkspace = ZSTD_cwksp_owns_buffer(&cdict->workspace, cdict);
ZSTD_cwksp_free(&cdict->workspace, cMem);
if (!cdictInWorkspace) {
ZSTD_customFree(cdict, cMem);
}
return 0;
}
ZSTD_rust_freeCDictState state;
state.callbackContext = cdict;
state.cdictInWorkspace = cdict == NULL ? 0
: ZSTD_cwksp_owns_buffer(&cdict->workspace, cdict);
state.freeWorkspace = ZSTD_rust_freeCDict_freeWorkspace;
state.freeObject = ZSTD_rust_freeCDict_freeObject;
return ZSTD_rust_freeCDict(&state);
}
/*! ZSTD_initStaticCDict_advanced() :