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
+120
View File
@@ -1049,6 +1049,55 @@ pub unsafe extern "C" fn ZSTD_rust_refThreadPool(
0
}
type FreeCCtxContentFn = unsafe extern "C" fn(*mut c_void);
type FreeCCtxObjectFn = unsafe extern "C" fn(*mut c_void);
/// Explicit projection for the public `ZSTD_freeCCtx` wrapper.
///
/// Rust owns the null/static/embedded-object policy and callback ordering.
/// C retains the private content teardown and custom allocator callbacks.
#[repr(C)]
pub struct ZSTD_rust_freeCCtxState {
callback_context: *mut c_void,
static_size: usize,
cctx_in_workspace: c_int,
free_content: FreeCCtxContentFn,
free_object: FreeCCtxObjectFn,
}
const _: () = {
assert!(size_of::<FreeCCtxContentFn>() == size_of::<usize>());
assert!(size_of::<FreeCCtxObjectFn>() == size_of::<usize>());
assert!(offset_of!(ZSTD_rust_freeCCtxState, callback_context) == 0);
assert!(offset_of!(ZSTD_rust_freeCCtxState, static_size) == size_of::<usize>());
assert!(offset_of!(ZSTD_rust_freeCCtxState, cctx_in_workspace) == 2 * size_of::<usize>());
assert!(offset_of!(ZSTD_rust_freeCCtxState, free_content) == 3 * size_of::<usize>());
assert!(offset_of!(ZSTD_rust_freeCCtxState, free_object) == 4 * size_of::<usize>());
assert!(size_of::<ZSTD_rust_freeCCtxState>() == 5 * size_of::<usize>());
};
/// Free the C-owned context content and, when applicable, its outer object.
#[no_mangle]
pub unsafe extern "C" fn ZSTD_rust_freeCCtx(state: *const ZSTD_rust_freeCCtxState) -> usize {
if state.is_null() {
return ERROR(ZstdErrorCode::Generic);
}
let state = unsafe { &*state };
if state.callback_context.is_null() {
return 0;
}
if state.static_size != 0 {
return ERROR(ZstdErrorCode::MemoryAllocation);
}
unsafe {
(state.free_content)(state.callback_context);
if state.cctx_in_workspace == 0 {
(state.free_object)(state.callback_context);
}
}
0
}
type ResetCCtxClearAllDictsFn = unsafe extern "C" fn(*mut c_void);
type ResetCCtxResetParamsFn = unsafe extern "C" fn(*mut c_void) -> usize;
@@ -10558,6 +10607,77 @@ mod tests {
assert_eq!(result, ERROR(ZstdErrorCode::Generic));
}
#[derive(Default)]
struct FreeCCtxTestContext {
events: Vec<&'static str>,
}
unsafe extern "C" fn free_cctx_test_content(context: *mut c_void) {
let context = unsafe { &mut *context.cast::<FreeCCtxTestContext>() };
context.events.push("content");
}
unsafe extern "C" fn free_cctx_test_object(context: *mut c_void) {
let context = unsafe { &mut *context.cast::<FreeCCtxTestContext>() };
context.events.push("object");
}
fn free_cctx_test_state(
context: *mut c_void,
static_size: usize,
cctx_in_workspace: c_int,
) -> ZSTD_rust_freeCCtxState {
ZSTD_rust_freeCCtxState {
callback_context: context,
static_size,
cctx_in_workspace,
free_content: free_cctx_test_content,
free_object: free_cctx_test_object,
}
}
#[test]
fn free_cctx_frees_content_before_an_external_object() {
let mut context = FreeCCtxTestContext::default();
let state = free_cctx_test_state((&mut context as *mut FreeCCtxTestContext).cast(), 0, 0);
let result = unsafe { ZSTD_rust_freeCCtx(&state) };
assert_eq!(result, 0);
assert_eq!(context.events, ["content", "object"]);
}
#[test]
fn free_cctx_skips_the_external_object_when_embedded_in_workspace() {
let mut context = FreeCCtxTestContext::default();
let state = free_cctx_test_state((&mut context as *mut FreeCCtxTestContext).cast(), 0, 1);
let result = unsafe { ZSTD_rust_freeCCtx(&state) };
assert_eq!(result, 0);
assert_eq!(context.events, ["content"]);
}
#[test]
fn free_cctx_rejects_static_contexts_before_any_callback() {
let mut context = FreeCCtxTestContext::default();
let state = free_cctx_test_state((&mut context as *mut FreeCCtxTestContext).cast(), 1, 0);
let result = unsafe { ZSTD_rust_freeCCtx(&state) };
assert_eq!(result, ERROR(ZstdErrorCode::MemoryAllocation));
assert!(context.events.is_empty());
}
#[test]
fn free_cctx_accepts_a_null_context() {
let state = free_cctx_test_state(ptr::null_mut(), 0, 0);
let result = unsafe { ZSTD_rust_freeCCtx(&state) };
assert_eq!(result, 0);
}
#[derive(Default)]
struct SetCParamsTestContext {
events: Vec<&'static str>,