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:
@@ -437,6 +437,48 @@ pub unsafe extern "C" fn ZSTD_rust_createCDict(
|
||||
cdict
|
||||
}
|
||||
|
||||
type FreeCDictWorkspaceFn = unsafe extern "C" fn(*mut c_void);
|
||||
type FreeCDictObjectFn = unsafe extern "C" fn(*mut c_void);
|
||||
|
||||
/// Explicit projection for the public `ZSTD_freeCDict` wrapper.
|
||||
///
|
||||
/// Rust owns null handling, workspace/object ordering, and the embedded-object
|
||||
/// decision. C retains private workspace and allocator teardown callbacks.
|
||||
#[repr(C)]
|
||||
pub struct ZSTD_rust_freeCDictState {
|
||||
callback_context: *mut c_void,
|
||||
cdict_in_workspace: c_int,
|
||||
free_workspace: FreeCDictWorkspaceFn,
|
||||
free_object: FreeCDictObjectFn,
|
||||
}
|
||||
|
||||
const _: () = {
|
||||
assert!(offset_of!(ZSTD_rust_freeCDictState, callback_context) == 0);
|
||||
assert!(offset_of!(ZSTD_rust_freeCDictState, cdict_in_workspace) == size_of::<usize>());
|
||||
assert!(offset_of!(ZSTD_rust_freeCDictState, free_workspace) == 2 * size_of::<usize>());
|
||||
assert!(offset_of!(ZSTD_rust_freeCDictState, free_object) == 3 * size_of::<usize>());
|
||||
assert!(size_of::<ZSTD_rust_freeCDictState>() == size_of::<[usize; 4]>());
|
||||
};
|
||||
|
||||
/// Free private CDict storage and, when applicable, its outer object.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn ZSTD_rust_freeCDict(state: *const ZSTD_rust_freeCDictState) -> usize {
|
||||
if state.is_null() {
|
||||
return ERROR(ZstdErrorCode::Generic);
|
||||
}
|
||||
let state = unsafe { &*state };
|
||||
if state.callback_context.is_null() {
|
||||
return 0;
|
||||
}
|
||||
unsafe {
|
||||
(state.free_workspace)(state.callback_context);
|
||||
if state.cdict_in_workspace == 0 {
|
||||
(state.free_object)(state.callback_context);
|
||||
}
|
||||
}
|
||||
0
|
||||
}
|
||||
|
||||
type CompressBeginUsingDictBeginFn =
|
||||
unsafe extern "C" fn(*mut c_void, *const c_void, usize, *const c_void, u64) -> usize;
|
||||
|
||||
@@ -2834,6 +2876,69 @@ mod tests {
|
||||
assert!(probe.published_cdict.is_null());
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct FreeCDictProbe {
|
||||
events: Vec<&'static str>,
|
||||
}
|
||||
|
||||
unsafe extern "C" fn free_cdict_test_workspace(context: *mut c_void) {
|
||||
let probe = unsafe { &mut *context.cast::<FreeCDictProbe>() };
|
||||
probe.events.push("workspace");
|
||||
}
|
||||
|
||||
unsafe extern "C" fn free_cdict_test_object(context: *mut c_void) {
|
||||
let probe = unsafe { &mut *context.cast::<FreeCDictProbe>() };
|
||||
probe.events.push("object");
|
||||
}
|
||||
|
||||
fn free_cdict_test_state(
|
||||
probe: &mut FreeCDictProbe,
|
||||
cdict_in_workspace: c_int,
|
||||
) -> ZSTD_rust_freeCDictState {
|
||||
ZSTD_rust_freeCDictState {
|
||||
callback_context: (probe as *mut FreeCDictProbe).cast(),
|
||||
cdict_in_workspace,
|
||||
free_workspace: free_cdict_test_workspace,
|
||||
free_object: free_cdict_test_object,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn free_cdict_frees_workspace_before_an_external_object() {
|
||||
let mut probe = FreeCDictProbe::default();
|
||||
let state = free_cdict_test_state(&mut probe, 0);
|
||||
|
||||
let result = unsafe { ZSTD_rust_freeCDict(&state) };
|
||||
|
||||
assert_eq!(result, 0);
|
||||
assert_eq!(probe.events, ["workspace", "object"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn free_cdict_skips_the_external_object_when_embedded_in_workspace() {
|
||||
let mut probe = FreeCDictProbe::default();
|
||||
let state = free_cdict_test_state(&mut probe, 1);
|
||||
|
||||
let result = unsafe { ZSTD_rust_freeCDict(&state) };
|
||||
|
||||
assert_eq!(result, 0);
|
||||
assert_eq!(probe.events, ["workspace"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn free_cdict_accepts_a_null_context() {
|
||||
let state = ZSTD_rust_freeCDictState {
|
||||
callback_context: ptr::null_mut(),
|
||||
cdict_in_workspace: 0,
|
||||
free_workspace: free_cdict_test_workspace,
|
||||
free_object: free_cdict_test_object,
|
||||
};
|
||||
|
||||
let result = unsafe { ZSTD_rust_freeCDict(&state) };
|
||||
|
||||
assert_eq!(result, 0);
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct CompressBeginUsingDictProbe {
|
||||
events: Vec<&'static str>,
|
||||
|
||||
Reference in New Issue
Block a user