feat(compress): move cdict stream init policy into Rust
Move ZSTD_initCStream_usingCDict's reset and CDict-reference ordering into a small Rust projection. Keep the opaque C context and dictionary operation behind callbacks with an explicit layout-checked ABI boundary. Test Plan: - ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/Cargo.toml init_cstream_using_cdict -- --test-threads=1 - ulimit -v 41943040; make -B -C programs -j1 zstd - git diff --cached --check
This commit is contained in:
@@ -949,6 +949,42 @@ pub unsafe extern "C" fn ZSTD_rust_initCStreamUsingCDictAdvanced(
|
||||
0
|
||||
}
|
||||
|
||||
/// Explicit projection for `ZSTD_initCStream_usingCDict`.
|
||||
#[repr(C)]
|
||||
pub struct ZSTD_rust_initCStreamUsingCDictState {
|
||||
callback_context: *mut c_void,
|
||||
reset_session: InitCStreamUsingCDictAdvancedResetFn,
|
||||
ref_cdict: InitCStreamUsingCDictAdvancedRefCDictFn,
|
||||
}
|
||||
|
||||
const _: () = {
|
||||
assert!(offset_of!(ZSTD_rust_initCStreamUsingCDictState, callback_context) == 0);
|
||||
assert!(offset_of!(ZSTD_rust_initCStreamUsingCDictState, reset_session) == size_of::<usize>());
|
||||
assert!(offset_of!(ZSTD_rust_initCStreamUsingCDictState, ref_cdict) == 2 * size_of::<usize>());
|
||||
assert!(size_of::<ZSTD_rust_initCStreamUsingCDictState>() == 3 * size_of::<usize>());
|
||||
};
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn ZSTD_rust_initCStreamUsingCDict(
|
||||
state: *const ZSTD_rust_initCStreamUsingCDictState,
|
||||
cdict: *const c_void,
|
||||
) -> usize {
|
||||
if state.is_null() {
|
||||
return ERROR(ZstdErrorCode::Generic);
|
||||
}
|
||||
let state = unsafe { &*state };
|
||||
|
||||
let result = unsafe { (state.reset_session)(state.callback_context) };
|
||||
if ERR_isError(result) {
|
||||
return result;
|
||||
}
|
||||
let result = unsafe { (state.ref_cdict)(state.callback_context, cdict) };
|
||||
if ERR_isError(result) {
|
||||
return result;
|
||||
}
|
||||
0
|
||||
}
|
||||
|
||||
type CompressStreamBlockFn =
|
||||
unsafe extern "C" fn(*mut c_void, *mut c_void, usize, *const c_void, usize) -> usize;
|
||||
type CompressStreamResetFn = unsafe extern "C" fn(*mut c_void) -> usize;
|
||||
@@ -8922,6 +8958,57 @@ mod tests {
|
||||
assert_eq!(context.frame_params, [1, 2, 3]);
|
||||
}
|
||||
|
||||
fn init_cstream_using_cdict_test_state(
|
||||
context: &mut InitCStreamUsingCDictAdvancedTestContext,
|
||||
) -> ZSTD_rust_initCStreamUsingCDictState {
|
||||
ZSTD_rust_initCStreamUsingCDictState {
|
||||
callback_context: (context as *mut InitCStreamUsingCDictAdvancedTestContext).cast(),
|
||||
reset_session: init_cstream_using_cdict_advanced_test_reset,
|
||||
ref_cdict: init_cstream_using_cdict_advanced_test_ref_cdict,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn init_cstream_using_cdict_preserves_reset_then_reference_order() {
|
||||
let cdict = ptr::dangling::<c_void>();
|
||||
let mut context = InitCStreamUsingCDictAdvancedTestContext::default();
|
||||
let state = init_cstream_using_cdict_test_state(&mut context);
|
||||
|
||||
let result = unsafe { ZSTD_rust_initCStreamUsingCDict(&state, cdict) };
|
||||
|
||||
assert_eq!(result, 0);
|
||||
assert_eq!(context.events, ["reset", "ref-cdict"]);
|
||||
assert_eq!(context.cdict, cdict);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn init_cstream_using_cdict_stops_after_reset_error() {
|
||||
let mut context = InitCStreamUsingCDictAdvancedTestContext {
|
||||
reset_result: ERROR(ZstdErrorCode::MemoryAllocation),
|
||||
..InitCStreamUsingCDictAdvancedTestContext::default()
|
||||
};
|
||||
let state = init_cstream_using_cdict_test_state(&mut context);
|
||||
|
||||
let result = unsafe { ZSTD_rust_initCStreamUsingCDict(&state, ptr::null()) };
|
||||
|
||||
assert_eq!(result, ERROR(ZstdErrorCode::MemoryAllocation));
|
||||
assert_eq!(context.events, ["reset"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn init_cstream_using_cdict_propagates_reference_error() {
|
||||
let mut context = InitCStreamUsingCDictAdvancedTestContext {
|
||||
ref_result: ERROR(ZstdErrorCode::DictionaryCreationFailed),
|
||||
..InitCStreamUsingCDictAdvancedTestContext::default()
|
||||
};
|
||||
let state = init_cstream_using_cdict_test_state(&mut context);
|
||||
|
||||
let result = unsafe { ZSTD_rust_initCStreamUsingCDict(&state, ptr::null()) };
|
||||
|
||||
assert_eq!(result, ERROR(ZstdErrorCode::DictionaryCreationFailed));
|
||||
assert_eq!(context.events, ["reset", "ref-cdict"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pledged_src_size_writes_the_init_stage_value_plus_one() {
|
||||
let mut pledged_src_size_plus_one = 0;
|
||||
|
||||
Reference in New Issue
Block a user