refactor(compress): move heap CCtx release order to Rust
ZSTD_freeCCtx previously passed one C callback that bundled dictionary, multithreaded context, workspace, and heap-object teardown. That left the lifetime order inside private C control flow and made the public destruction policy harder to audit. Project the four private operations separately and let Rust enforce the original dictionary, MT-context, workspace, and object order while preserving the static-context and workspace-embedded object guards. The callbacks still own the C layouts and custom allocator operations. Test Plan: - `ulimit -v 41943040; cargo +nightly fmt --manifest-path rust/Cargo.toml --all -- --check` -- passed - `ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo clippy --manifest-path rust/Cargo.toml --all-targets -- -D warnings` -- passed - Broader native and original-test verification remains pending for the complete batch.
This commit is contained in:
+40
-19
@@ -2550,34 +2550,38 @@ pub unsafe extern "C" fn ZSTD_rust_initStaticCCtx(
|
||||
}
|
||||
}
|
||||
|
||||
type FreeCCtxContentFn = unsafe extern "C" fn(*mut c_void);
|
||||
type FreeCCtxObjectFn = unsafe extern "C" fn(*mut c_void);
|
||||
type FreeCCtxCallbackFn = 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.
|
||||
/// Rust owns the null/static/embedded-object policy and the release order.
|
||||
/// C retains only the private dictionary, MT-context, workspace, and custom
|
||||
/// allocator operations behind narrow 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,
|
||||
free_dictionaries: FreeCCtxCallbackFn,
|
||||
free_mt_context: FreeCCtxCallbackFn,
|
||||
free_workspace: FreeCCtxCallbackFn,
|
||||
free_object: FreeCCtxCallbackFn,
|
||||
}
|
||||
|
||||
const _: () = {
|
||||
assert!(size_of::<FreeCCtxContentFn>() == size_of::<usize>());
|
||||
assert!(size_of::<FreeCCtxObjectFn>() == size_of::<usize>());
|
||||
assert!(size_of::<FreeCCtxCallbackFn>() == 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>());
|
||||
assert!(offset_of!(ZSTD_rust_freeCCtxState, free_dictionaries) == 3 * size_of::<usize>());
|
||||
assert!(offset_of!(ZSTD_rust_freeCCtxState, free_mt_context) == 4 * size_of::<usize>());
|
||||
assert!(offset_of!(ZSTD_rust_freeCCtxState, free_workspace) == 5 * size_of::<usize>());
|
||||
assert!(offset_of!(ZSTD_rust_freeCCtxState, free_object) == 6 * size_of::<usize>());
|
||||
assert!(size_of::<ZSTD_rust_freeCCtxState>() == 7 * size_of::<usize>());
|
||||
};
|
||||
|
||||
/// Free the C-owned context content and, when applicable, its outer object.
|
||||
/// Release a heap CCtx in the original dictionary, MT-context, workspace, and
|
||||
/// outer-object order.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn ZSTD_rust_freeCCtx(state: *const ZSTD_rust_freeCCtxState) -> usize {
|
||||
if state.is_null() {
|
||||
@@ -2591,7 +2595,9 @@ pub unsafe extern "C" fn ZSTD_rust_freeCCtx(state: *const ZSTD_rust_freeCCtxStat
|
||||
return ERROR(ZstdErrorCode::MemoryAllocation);
|
||||
}
|
||||
unsafe {
|
||||
(state.free_content)(state.callback_context);
|
||||
(state.free_dictionaries)(state.callback_context);
|
||||
(state.free_mt_context)(state.callback_context);
|
||||
(state.free_workspace)(state.callback_context);
|
||||
if state.cctx_in_workspace == 0 {
|
||||
(state.free_object)(state.callback_context);
|
||||
}
|
||||
@@ -19025,9 +19031,19 @@ mod tests {
|
||||
events: Vec<&'static str>,
|
||||
}
|
||||
|
||||
unsafe extern "C" fn free_cctx_test_content(context: *mut c_void) {
|
||||
unsafe extern "C" fn free_cctx_test_dictionaries(context: *mut c_void) {
|
||||
let context = unsafe { &mut *context.cast::<FreeCCtxTestContext>() };
|
||||
context.events.push("content");
|
||||
context.events.push("dictionaries");
|
||||
}
|
||||
|
||||
unsafe extern "C" fn free_cctx_test_mt_context(context: *mut c_void) {
|
||||
let context = unsafe { &mut *context.cast::<FreeCCtxTestContext>() };
|
||||
context.events.push("mt-context");
|
||||
}
|
||||
|
||||
unsafe extern "C" fn free_cctx_test_workspace(context: *mut c_void) {
|
||||
let context = unsafe { &mut *context.cast::<FreeCCtxTestContext>() };
|
||||
context.events.push("workspace");
|
||||
}
|
||||
|
||||
unsafe extern "C" fn free_cctx_test_object(context: *mut c_void) {
|
||||
@@ -19044,20 +19060,25 @@ mod tests {
|
||||
callback_context: context,
|
||||
static_size,
|
||||
cctx_in_workspace,
|
||||
free_content: free_cctx_test_content,
|
||||
free_dictionaries: free_cctx_test_dictionaries,
|
||||
free_mt_context: free_cctx_test_mt_context,
|
||||
free_workspace: free_cctx_test_workspace,
|
||||
free_object: free_cctx_test_object,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn free_cctx_frees_content_before_an_external_object() {
|
||||
fn free_cctx_releases_dictionaries_mt_context_and_workspace_before_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"]);
|
||||
assert_eq!(
|
||||
context.events,
|
||||
["dictionaries", "mt-context", "workspace", "object"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -19068,7 +19089,7 @@ mod tests {
|
||||
let result = unsafe { ZSTD_rust_freeCCtx(&state) };
|
||||
|
||||
assert_eq!(result, 0);
|
||||
assert_eq!(context.events, ["content"]);
|
||||
assert_eq!(context.events, ["dictionaries", "mt-context", "workspace"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user