feat(compress): move CCtx creation policy into Rust
Move public CCtx creation validation, allocation failure handling, and allocate-before-init ordering behind a Rust-owned boundary. Keep the private ZSTD_CCtx initialization and custom allocator invocation in C callbacks. Test Plan: - ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/Cargo.toml --release create_cctx -- --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:
@@ -322,6 +322,28 @@ typedef char ZSTD_rust_free_cctx_state_layout[
|
||||
== 4 * sizeof(void*)
|
||||
&& sizeof(ZSTD_rust_freeCCtxState) == 5 * sizeof(void*))
|
||||
? 1 : -1];
|
||||
typedef int (*ZSTD_rust_createCCtxValidateCustomMem_f)(void* context);
|
||||
typedef void* (*ZSTD_rust_createCCtxAllocate_f)(void* context, size_t size);
|
||||
typedef void (*ZSTD_rust_createCCtxInit_f)(void* context, void* cctx);
|
||||
typedef struct {
|
||||
void* callbackContext;
|
||||
size_t cctxSize;
|
||||
ZSTD_rust_createCCtxValidateCustomMem_f validateCustomMem;
|
||||
ZSTD_rust_createCCtxAllocate_f allocate;
|
||||
ZSTD_rust_createCCtxInit_f init;
|
||||
} ZSTD_rust_createCCtxState;
|
||||
void* ZSTD_rust_createCCtx(const ZSTD_rust_createCCtxState* state);
|
||||
typedef char ZSTD_rust_create_cctx_state_layout[
|
||||
(offsetof(ZSTD_rust_createCCtxState, callbackContext) == 0
|
||||
&& offsetof(ZSTD_rust_createCCtxState, cctxSize) == sizeof(void*)
|
||||
&& offsetof(ZSTD_rust_createCCtxState, validateCustomMem)
|
||||
== 2 * sizeof(void*)
|
||||
&& offsetof(ZSTD_rust_createCCtxState, allocate)
|
||||
== 3 * sizeof(void*)
|
||||
&& offsetof(ZSTD_rust_createCCtxState, init)
|
||||
== 4 * sizeof(void*)
|
||||
&& sizeof(ZSTD_rust_createCCtxState) == 5 * sizeof(void*))
|
||||
? 1 : -1];
|
||||
typedef void (*ZSTD_rust_resetCCtxClearAllDicts_f)(void* context);
|
||||
typedef size_t (*ZSTD_rust_resetCCtxResetParams_f)(void* context);
|
||||
typedef struct {
|
||||
@@ -2053,16 +2075,33 @@ static void ZSTD_initCCtx(ZSTD_CCtx* cctx, ZSTD_customMem memManager)
|
||||
}
|
||||
}
|
||||
|
||||
static int ZSTD_rust_createCCtx_validateCustomMem(void* context)
|
||||
{
|
||||
ZSTD_customMem const* const customMem = (const ZSTD_customMem*)context;
|
||||
return ((!customMem->customAlloc) ^ (!customMem->customFree)) == 0;
|
||||
}
|
||||
|
||||
static void* ZSTD_rust_createCCtx_allocate(void* context, size_t size)
|
||||
{
|
||||
return ZSTD_customMalloc(size, *(const ZSTD_customMem*)context);
|
||||
}
|
||||
|
||||
static void ZSTD_rust_createCCtx_init(void* context, void* cctx)
|
||||
{
|
||||
ZSTD_initCCtx((ZSTD_CCtx*)cctx, *(const ZSTD_customMem*)context);
|
||||
}
|
||||
|
||||
ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem)
|
||||
{
|
||||
ZSTD_rust_createCCtxState state;
|
||||
ZSTD_STATIC_ASSERT(zcss_init==0);
|
||||
ZSTD_STATIC_ASSERT(ZSTD_CONTENTSIZE_UNKNOWN==(0ULL - 1));
|
||||
if ((!customMem.customAlloc) ^ (!customMem.customFree)) return NULL;
|
||||
{ ZSTD_CCtx* const cctx = (ZSTD_CCtx*)ZSTD_customMalloc(sizeof(ZSTD_CCtx), customMem);
|
||||
if (!cctx) return NULL;
|
||||
ZSTD_initCCtx(cctx, customMem);
|
||||
return cctx;
|
||||
}
|
||||
state.callbackContext = &customMem;
|
||||
state.cctxSize = sizeof(ZSTD_CCtx);
|
||||
state.validateCustomMem = ZSTD_rust_createCCtx_validateCustomMem;
|
||||
state.allocate = ZSTD_rust_createCCtx_allocate;
|
||||
state.init = ZSTD_rust_createCCtx_init;
|
||||
return (ZSTD_CCtx*)ZSTD_rust_createCCtx(&state);
|
||||
}
|
||||
|
||||
ZSTD_CCtx* ZSTD_initStaticCCtx(void* workspace, size_t workspaceSize)
|
||||
|
||||
Reference in New Issue
Block a user