feat(compress): move block policy predicates to Rust

Move target block-size and block-splitter policy predicates behind narrow Rust ABI shims while retaining the C debug logging, finalized-mode assertion, and surrounding control flow.

Test Plan:

- cargo test --manifest-path rust/Cargo.toml --no-default-features --features compression

- cargo clippy --manifest-path rust/Cargo.toml and native library build

- make -B -C tests -j2 test-zstream (84 deterministic tests; fuzzers 6778 and 8299 cases)
This commit is contained in:
2026-07-18 04:54:15 +02:00
parent 49f03de3e2
commit 59eda8deac
2 changed files with 67 additions and 3 deletions
+4 -3
View File
@@ -107,6 +107,8 @@ ZSTD_CCtx_params* ZSTD_rust_createCCtxParams(ZSTD_customMem customMem);
size_t ZSTD_rust_freeCCtxParams(ZSTD_CCtx_params* params);
size_t ZSTD_rust_CCtxParams_init_advanced(ZSTD_CCtx_params* cctxParams,
ZSTD_parameters params);
int ZSTD_rust_useTargetCBlockSize(const ZSTD_CCtx_params* cctxParams);
int ZSTD_rust_blockSplitterEnabled(ZSTD_CCtx_params* cctxParams);
#define ZSTD_RUST_CCTX_PARAMS_ASSERT(name, condition) \
typedef char name[(condition) ? 1 : -1]
@@ -2132,7 +2134,7 @@ static void ZSTD_reduceIndex (ZSTD_MatchState_t* ms, ZSTD_CCtx_params const* par
static int ZSTD_useTargetCBlockSize(const ZSTD_CCtx_params* cctxParams)
{
DEBUGLOG(5, "ZSTD_useTargetCBlockSize (targetCBlockSize=%zu)", cctxParams->targetCBlockSize);
return (cctxParams->targetCBlockSize != 0);
return ZSTD_rust_useTargetCBlockSize(cctxParams);
}
/* ZSTD_blockSplitterEnabled():
@@ -2143,8 +2145,7 @@ static int ZSTD_useTargetCBlockSize(const ZSTD_CCtx_params* cctxParams)
static int ZSTD_blockSplitterEnabled(ZSTD_CCtx_params* cctxParams)
{
DEBUGLOG(5, "ZSTD_blockSplitterEnabled (postBlockSplitter=%d)", cctxParams->postBlockSplitter);
assert(cctxParams->postBlockSplitter != ZSTD_ps_auto);
return (cctxParams->postBlockSplitter == ZSTD_ps_enable);
return ZSTD_rust_blockSplitterEnabled(cctxParams);
}
/* ZSTD_buildSequencesStatistics() and the ZSTD_entropyCompressSeqStore*()
+63
View File
@@ -331,6 +331,32 @@ pub unsafe extern "C" fn ZSTD_rust_cctx_params_within_bounds(param: c_int, value
unsafe { within_bounds(param, value) as c_int }
}
#[inline]
unsafe fn use_target_c_block_size(cctx_params: *const ZSTD_CCtx_params) -> c_int {
unsafe { ((*cctx_params).targetCBlockSize != 0) as c_int }
}
#[no_mangle]
pub unsafe extern "C" fn ZSTD_rust_useTargetCBlockSize(
cctx_params: *const ZSTD_CCtx_params,
) -> c_int {
unsafe { use_target_c_block_size(cctx_params) }
}
#[inline]
unsafe fn block_splitter_enabled(cctx_params: *mut ZSTD_CCtx_params) -> c_int {
let post_block_splitter = unsafe { (*cctx_params).postBlockSplitter };
assert_ne!(post_block_splitter, PS_AUTO);
(post_block_splitter == ZSTD_RUST_PS_ENABLE) as c_int
}
#[no_mangle]
pub unsafe extern "C" fn ZSTD_rust_blockSplitterEnabled(
cctx_params: *mut ZSTD_CCtx_params,
) -> c_int {
unsafe { block_splitter_enabled(cctx_params) }
}
#[inline]
unsafe fn multithreading_enabled() -> bool {
#[cfg(not(test))]
@@ -1070,6 +1096,43 @@ mod tests {
);
}
#[test]
fn target_c_block_size_predicate_uses_zero_as_the_disable_value() {
let mut storage = MaybeUninit::<ZSTD_CCtx_params>::zeroed();
let params = storage.as_mut_ptr();
unsafe {
(*params).targetCBlockSize = 0;
assert_eq!(ZSTD_rust_useTargetCBlockSize(params), 0);
(*params).targetCBlockSize = 1;
assert_eq!(ZSTD_rust_useTargetCBlockSize(params), 1);
}
}
#[test]
fn block_splitter_predicate_only_enables_the_enable_mode() {
let mut storage = MaybeUninit::<ZSTD_CCtx_params>::zeroed();
let params = storage.as_mut_ptr();
unsafe {
(*params).postBlockSplitter = ZSTD_RUST_PS_ENABLE;
assert_eq!(ZSTD_rust_blockSplitterEnabled(params), 1);
(*params).postBlockSplitter = PS_DISABLE;
assert_eq!(ZSTD_rust_blockSplitterEnabled(params), 0);
}
}
#[test]
#[should_panic]
fn block_splitter_predicate_rejects_auto_mode() {
let mut storage = MaybeUninit::<ZSTD_CCtx_params>::zeroed();
let params = storage.as_mut_ptr();
unsafe {
(*params).postBlockSplitter = PS_AUTO;
let _ = block_splitter_enabled(params);
}
}
#[test]
fn migrated_non_core_bounds_preserve_pointer_and_thread_configuration() {
let single_threaded = BoundsConfig {