feat(decompress): move DCtx prefix copy into Rust
Move the `ZSTD_copyDCtx` prefix-copy leaf into the Rust decompression module. Rust uses the C-projected address of `inBuff` to preserve the exact private context cutoff, while C retains the configuration-dependent context layout and shallow-pointer ownership semantics. Add a focused boundary test and remove the now-unused C copy helper. Test Plan: - ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo fmt --manifest-path rust/Cargo.toml -- --check - ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/Cargo.toml copy_prefix -- --nocapture - ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/Cargo.toml - ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo clippy --manifest-path rust/Cargo.toml --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:
@@ -326,7 +326,6 @@ unsafe extern "C" {
|
||||
streaming: c_int,
|
||||
);
|
||||
fn ZSTD_rust_dctx_trace_begin(dctx: *mut ZSTD_DCtx);
|
||||
fn ZSTD_rust_dctx_copy_prefix(dst: *mut ZSTD_DCtx, src: *const ZSTD_DCtx);
|
||||
#[cfg(all(
|
||||
not(test),
|
||||
any(
|
||||
@@ -367,6 +366,12 @@ unsafe fn dctx_view(dctx: *mut ZSTD_DCtx) -> ZSTD_rustDctxView {
|
||||
unsafe { view.assume_init() }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn copy_dctx_prefix(dst: *mut u8, src: *const u8, in_buff: *const c_void) {
|
||||
let to_copy = (in_buff as usize).wrapping_sub(dst as usize);
|
||||
unsafe { ptr::copy_nonoverlapping(src, dst, to_copy) };
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn legacy_empty_const_ptr() -> *const c_void {
|
||||
ptr::addr_of!(LEGACY_EMPTY_BYTE).cast()
|
||||
@@ -2574,7 +2579,32 @@ pub unsafe extern "C" fn ZSTD_freeDCtx(dctx: *mut ZSTD_DCtx) -> usize {
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn ZSTD_copyDCtx(dst: *mut ZSTD_DCtx, src: *const ZSTD_DCtx) {
|
||||
unsafe { ZSTD_rust_dctx_copy_prefix(dst, src) }
|
||||
if dst.is_null() || src.is_null() {
|
||||
return;
|
||||
}
|
||||
let view = unsafe { dctx_view(dst) };
|
||||
unsafe {
|
||||
copy_dctx_prefix(dst.cast(), src.cast(), view.in_buff);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod dctx_copy_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn copy_prefix_stops_before_the_input_buffer_field() {
|
||||
let source = [1u8, 2, 3, 4, 5];
|
||||
let mut destination = [0xAAu8; 8];
|
||||
let in_buff = unsafe { destination.as_mut_ptr().add(5).cast::<c_void>() };
|
||||
|
||||
unsafe {
|
||||
copy_dctx_prefix(destination.as_mut_ptr(), source.as_ptr(), in_buff);
|
||||
}
|
||||
|
||||
assert_eq!(&destination[..5], &source);
|
||||
assert_eq!(&destination[5..], &[0xAA; 3]);
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn select_frame_ddict(view: &ZSTD_rustDctxView) {
|
||||
|
||||
Reference in New Issue
Block a user