refactor(compress): move heap CCtx initialization to Rust

Move the ordering-sensitive setup of a privately allocated compression
context behind a Rust callback coordinator while keeping allocation and
context storage in C.  The explicit callback ABI preserves zeroing, custom
allocator publication, BMI2 setup, and parameter-reset order, including
reset-error propagation.

Test Plan:
- ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo +nightly fmt --manifest-path rust/Cargo.toml -- --check
- ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo clippy --manifest-path rust/Cargo.toml --all-targets -- -D warnings
- ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/Cargo.toml --all-targets (775 passed)
- ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo clippy --manifest-path rust/cli/Cargo.toml --all-targets -- -D warnings
- ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/cli/Cargo.toml --all-targets (179 passed)
- ulimit -v 41943040; make -j1
- ulimit -v 41943040; make -j1 -C tests test (all tests completed successfully)
This commit is contained in:
2026-07-20 06:21:05 +02:00
parent a67e6a59df
commit 0b56f667db
2 changed files with 186 additions and 7 deletions
+52 -7
View File
@@ -340,6 +340,29 @@ typedef char ZSTD_rust_create_cctx_state_layout[
== 4 * sizeof(void*)
&& sizeof(ZSTD_rust_createCCtxState) == 5 * sizeof(void*))
? 1 : -1];
typedef void (*ZSTD_rust_initCCtxCallback_f)(void* context);
typedef void (*ZSTD_rust_initCCtxSetCustomMem_f)(
void* context, const void* customMem);
typedef size_t (*ZSTD_rust_initCCtxReset_f)(void* context);
typedef struct {
void* callbackContext;
const void* customMem;
ZSTD_rust_initCCtxCallback_f zero;
ZSTD_rust_initCCtxSetCustomMem_f setCustomMem;
ZSTD_rust_initCCtxCallback_f setBmi2;
ZSTD_rust_initCCtxReset_f reset;
} ZSTD_rust_initCCtxState;
size_t ZSTD_rust_initCCtx(const ZSTD_rust_initCCtxState* state);
typedef char ZSTD_rust_init_cctx_state_layout[
(offsetof(ZSTD_rust_initCCtxState, callbackContext) == 0
&& offsetof(ZSTD_rust_initCCtxState, customMem) == sizeof(void*)
&& offsetof(ZSTD_rust_initCCtxState, zero) == 2 * sizeof(void*)
&& offsetof(ZSTD_rust_initCCtxState, setCustomMem)
== 3 * sizeof(void*)
&& offsetof(ZSTD_rust_initCCtxState, setBmi2) == 4 * sizeof(void*)
&& offsetof(ZSTD_rust_initCCtxState, reset) == 5 * sizeof(void*)
&& sizeof(ZSTD_rust_initCCtxState) == 6 * sizeof(void*))
? 1 : -1];
typedef void* (*ZSTD_rust_initStaticCCtxInit_f)(void* context);
typedef struct {
void* callbackContext;
@@ -3149,16 +3172,28 @@ ZSTD_CCtx* ZSTD_createCCtx(void)
return ZSTD_createCCtx_advanced(ZSTD_defaultCMem);
}
static void ZSTD_initCCtx(ZSTD_CCtx* cctx, ZSTD_customMem memManager)
static void ZSTD_initCCtx_zero(void* context)
{
ZSTD_CCtx* const cctx = (ZSTD_CCtx*)context;
assert(cctx != NULL);
ZSTD_memset(cctx, 0, sizeof(*cctx));
cctx->customMem = memManager;
}
static void ZSTD_initCCtx_setCustomMem(void* context, const void* customMem)
{
ZSTD_CCtx* const cctx = (ZSTD_CCtx*)context;
cctx->customMem = *(const ZSTD_customMem*)customMem;
}
static void ZSTD_initCCtx_setBmi2(void* context)
{
ZSTD_CCtx* const cctx = (ZSTD_CCtx*)context;
cctx->bmi2 = ZSTD_cpuSupportsBmi2();
{ size_t const err = ZSTD_CCtx_reset(cctx, ZSTD_reset_parameters);
assert(!ZSTD_isError(err));
(void)err;
}
}
static size_t ZSTD_initCCtx_reset(void* context)
{
return ZSTD_CCtx_reset((ZSTD_CCtx*)context, ZSTD_reset_parameters);
}
static int ZSTD_rust_createCCtx_validateCustomMem(void* context)
@@ -3174,7 +3209,17 @@ static void* ZSTD_rust_createCCtx_allocate(void* context, size_t size)
static void ZSTD_rust_createCCtx_init(void* context, void* cctx)
{
ZSTD_initCCtx((ZSTD_CCtx*)cctx, *(const ZSTD_customMem*)context);
ZSTD_rust_initCCtxState state;
state.callbackContext = cctx;
state.customMem = context;
state.zero = ZSTD_initCCtx_zero;
state.setCustomMem = ZSTD_initCCtx_setCustomMem;
state.setBmi2 = ZSTD_initCCtx_setBmi2;
state.reset = ZSTD_initCCtx_reset;
{ size_t const err = ZSTD_rust_initCCtx(&state);
assert(!ZSTD_isError(err));
(void)err;
}
}
ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem)