refactor(fileio): move decompression teardown order to Rust

Project the decompression-resource cleanup callbacks into Rust so the stable
teardown order remains dictionary, dstream, write pool, and read pool. C
retains ownership of dRess_t and each private resource implementation, with
its existing dstream CHECK behavior preserved. Focused tests cover order and
null or incomplete callback states.

All heavy verification was run serially with a 40 GiB virtual-memory cap and
one build job.

Test Plan:
- git diff --cached --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 (788 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 (181 passed)
- ulimit -v 41943040; cargo +nightly fmt --manifest-path rust/Cargo.toml --all -- --check
- ulimit -v 41943040; make -j1
- ulimit -v 41943040; make -j1 -C tests test
This commit is contained in:
2026-07-20 07:56:27 +02:00
parent 9c46e18172
commit 22cb347f6e
2 changed files with 164 additions and 4 deletions
+111
View File
@@ -173,6 +173,56 @@ pub unsafe extern "C" fn FIO_rust_freeCResources(
}
}
type FIO_freeDResourcesCallbackFn = unsafe extern "C" fn(*mut c_void);
/// C-owned decompression-resource teardown projection.
///
/// Rust owns the stable cleanup order while C keeps `dRess_t`, dictionary,
/// asynchronous-pool, and stream layouts private behind callbacks.
#[repr(C)]
pub struct FIO_rust_freeDResourcesState {
callback_context: *mut c_void,
free_dict: Option<FIO_freeDResourcesCallbackFn>,
free_dctx: Option<FIO_freeDResourcesCallbackFn>,
free_write_pool: Option<FIO_freeDResourcesCallbackFn>,
free_read_pool: Option<FIO_freeDResourcesCallbackFn>,
}
const _: () = {
assert!(offset_of!(FIO_rust_freeDResourcesState, callback_context) == 0);
assert!(offset_of!(FIO_rust_freeDResourcesState, free_dict) == size_of::<usize>());
assert!(offset_of!(FIO_rust_freeDResourcesState, free_dctx) == 2 * size_of::<usize>());
assert!(offset_of!(FIO_rust_freeDResourcesState, free_write_pool) == 3 * size_of::<usize>());
assert!(offset_of!(FIO_rust_freeDResourcesState, free_read_pool) == 4 * size_of::<usize>());
assert!(size_of::<Option<FIO_freeDResourcesCallbackFn>>() == size_of::<usize>());
assert!(size_of::<FIO_rust_freeDResourcesState>() == 5 * size_of::<usize>());
};
#[no_mangle]
pub unsafe extern "C" fn FIO_rust_freeDResources(state: *const FIO_rust_freeDResourcesState) {
let Some(state) = (unsafe { state.as_ref() }) else {
return;
};
if state.callback_context.is_null() {
return;
}
let (Some(free_dict), Some(free_dctx), Some(free_write_pool), Some(free_read_pool)) = (
state.free_dict,
state.free_dctx,
state.free_write_pool,
state.free_read_pool,
) else {
return;
};
unsafe {
free_dict(state.callback_context);
free_dctx(state.callback_context);
free_write_pool(state.callback_context);
free_read_pool(state.callback_context);
}
}
pub type FIO_createCResourcesSetParameterFn =
unsafe extern "C" fn(*mut c_void, c_int, c_int) -> usize;
pub type FIO_createCResourcesIsErrorFn = unsafe extern "C" fn(usize) -> c_int;
@@ -2169,6 +2219,67 @@ mod tests {
assert_eq!(context.events, ["dict", "write", "read", "cctx"]);
}
#[derive(Default)]
struct FreeDResourcesTestState {
events: Vec<&'static str>,
}
unsafe extern "C" fn free_d_resources_dict_test(context: *mut c_void) {
let state = unsafe { &mut *context.cast::<FreeDResourcesTestState>() };
state.events.push("dict");
}
unsafe extern "C" fn free_d_resources_dctx_test(context: *mut c_void) {
let state = unsafe { &mut *context.cast::<FreeDResourcesTestState>() };
state.events.push("dctx");
}
unsafe extern "C" fn free_d_resources_write_pool_test(context: *mut c_void) {
let state = unsafe { &mut *context.cast::<FreeDResourcesTestState>() };
state.events.push("write");
}
unsafe extern "C" fn free_d_resources_read_pool_test(context: *mut c_void) {
let state = unsafe { &mut *context.cast::<FreeDResourcesTestState>() };
state.events.push("read");
}
fn free_d_resources_test_state(
context: &mut FreeDResourcesTestState,
) -> FIO_rust_freeDResourcesState {
FIO_rust_freeDResourcesState {
callback_context: context as *mut FreeDResourcesTestState as *mut c_void,
free_dict: Some(free_d_resources_dict_test),
free_dctx: Some(free_d_resources_dctx_test),
free_write_pool: Some(free_d_resources_write_pool_test),
free_read_pool: Some(free_d_resources_read_pool_test),
}
}
#[test]
fn free_d_resources_preserves_teardown_order() {
let mut context = FreeDResourcesTestState::default();
let state = free_d_resources_test_state(&mut context);
unsafe { FIO_rust_freeDResources(&state) };
assert_eq!(context.events, ["dict", "dctx", "write", "read"]);
}
#[test]
fn free_d_resources_rejects_null_or_incomplete_state_without_callbacks() {
let mut context = FreeDResourcesTestState::default();
let mut incomplete = free_d_resources_test_state(&mut context);
incomplete.free_dctx = None;
unsafe {
FIO_rust_freeDResources(ptr::null());
FIO_rust_freeDResources(&incomplete);
}
assert!(context.events.is_empty());
}
#[derive(Default)]
struct CreateCResourcesTestState {
events: Vec<(c_int, c_int)>,