refactor(compress): move CCtx reset orchestration to Rust

Move the residual ZSTD_resetCCtx_internal sequencing into a Rust policy
entrypoint. The Rust side now owns the plan-to-workspace-to-tail order and
scalar hand-off, while C retains private workspace checks, context
initialization, match-state reset, and pointer publication callbacks. Preserve
error short-circuiting and the existing static, dynamic, LDM, and index-reset
behavior.

Test Plan:
- cargo fmt --manifest-path rust/Cargo.toml --all -- --check
- git diff --check
- Full capped Rust, native, CLI, and upstream test suites to follow
This commit is contained in:
2026-07-20 03:32:56 +02:00
parent 4b96b32118
commit 6c060c901f
2 changed files with 323 additions and 59 deletions
+210
View File
@@ -7619,6 +7619,155 @@ const _: () = {
);
};
type ResetCCtxWorkspacePrepare =
unsafe extern "C" fn(*mut c_void, usize, usize, usize, *mut c_int, *mut c_int);
type ResetCCtxTailPrepare = unsafe extern "C" fn(*mut c_void, usize, c_int);
/// State for composing the already-projected CCtx reset policies.
///
/// Rust owns the plan/workspace/tail order and the scalar hand-off between
/// those helpers. C retains the workspace checks, context initialization,
/// match-state reset, and private pointer publication behind callbacks.
#[repr(C)]
pub struct ZSTD_rust_resetCCtxInternalState {
pub callbackContext: *mut c_void,
pub resetState: *const ZSTD_rustCCtxResetState,
pub storageState: *mut ZSTD_rust_resetCCtxStorageState,
pub workspaceState: *mut ZSTD_rust_resetCCtxWorkspaceState,
pub tailState: *mut ZSTD_rust_resetCCtxTailState,
pub prepareWorkspace: Option<ResetCCtxWorkspacePrepare>,
pub prepareTail: Option<ResetCCtxTailPrepare>,
}
const _: () = {
assert!(size_of::<ResetCCtxWorkspacePrepare>() == size_of::<usize>());
assert!(size_of::<ResetCCtxTailPrepare>() == size_of::<usize>());
assert!(offset_of!(ZSTD_rust_resetCCtxInternalState, callbackContext) == 0);
assert!(offset_of!(ZSTD_rust_resetCCtxInternalState, resetState) == size_of::<usize>());
assert!(offset_of!(ZSTD_rust_resetCCtxInternalState, storageState) == 2 * size_of::<usize>());
assert!(offset_of!(ZSTD_rust_resetCCtxInternalState, workspaceState) == 3 * size_of::<usize>());
assert!(offset_of!(ZSTD_rust_resetCCtxInternalState, tailState) == 4 * size_of::<usize>());
assert!(
offset_of!(ZSTD_rust_resetCCtxInternalState, prepareWorkspace) == 5 * size_of::<usize>()
);
assert!(offset_of!(ZSTD_rust_resetCCtxInternalState, prepareTail) == 6 * size_of::<usize>());
assert!(size_of::<ZSTD_rust_resetCCtxInternalState>() == 7 * size_of::<usize>());
};
#[inline]
fn project_cctx_reset_plan(
plan: &ZSTD_rustCCtxResetPlan,
storage_state: &mut ZSTD_rust_resetCCtxStorageState,
workspace_state: &mut ZSTD_rust_resetCCtxWorkspaceState,
) {
storage_state.blockSize = plan.blockSize;
storage_state.maxNbSeq = plan.maxNbSeq;
storage_state.maxNbLdmSeq = plan.maxNbLdmSeq;
storage_state.maxNbExternalSeq = plan.maxNbExternalSeq;
storage_state.buffInSize = plan.buffInSize;
storage_state.buffOutSize = plan.buffOutSize;
workspace_state.neededSpace = plan.neededSpace;
}
#[inline]
fn run_cctx_reset_policy(
plan: impl FnOnce() -> usize,
prepare_workspace: impl FnOnce(),
reset_workspace: impl FnOnce() -> usize,
prepare_tail: impl FnOnce(),
reset_tail: impl FnOnce() -> usize,
) -> usize {
let result = plan();
if ERR_isError(result) {
return result;
}
prepare_workspace();
let result = reset_workspace();
if ERR_isError(result) {
return result;
}
prepare_tail();
reset_tail()
}
/// Compose the private CCtx reset without moving C-only layout operations.
#[no_mangle]
pub unsafe extern "C" fn ZSTD_rust_resetCCtxInternal(
state: *const ZSTD_rust_resetCCtxInternalState,
) -> usize {
if state.is_null() {
return ERROR(ZstdErrorCode::Generic);
}
let state = unsafe { &*state };
let Some(prepare_workspace) = state.prepareWorkspace else {
return ERROR(ZstdErrorCode::Generic);
};
let Some(prepare_tail) = state.prepareTail else {
return ERROR(ZstdErrorCode::Generic);
};
if state.callbackContext.is_null()
|| state.resetState.is_null()
|| state.storageState.is_null()
|| state.workspaceState.is_null()
|| state.tailState.is_null()
{
return ERROR(ZstdErrorCode::Generic);
}
let reset_state = unsafe { &*state.resetState };
if reset_state.plan.is_null() {
return ERROR(ZstdErrorCode::Generic);
}
let workspace_state = unsafe { &*state.workspaceState };
if workspace_state.needsIndexReset.is_null() {
return ERROR(ZstdErrorCode::Generic);
}
let plan = reset_state.plan;
let reset_state_ptr = state.resetState;
let storage_state_ptr = state.storageState;
let workspace_state_ptr = state.workspaceState;
let tail_state_ptr = state.tailState;
let callback_context = state.callbackContext;
run_cctx_reset_policy(
|| unsafe {
let result = ZSTD_rust_planCCtxReset(reset_state_ptr);
if ERR_isError(result) {
return result;
}
let plan = &*plan;
let storage_state = &mut *storage_state_ptr;
let workspace_state = &mut *workspace_state_ptr;
project_cctx_reset_plan(plan, storage_state, workspace_state);
*workspace_state.needsIndexReset = plan.needsIndexReset;
0
},
|| unsafe {
let plan = &*plan;
let workspace_state = &mut *workspace_state_ptr;
prepare_workspace(
callback_context,
plan.neededSpace,
plan.windowSize,
plan.blockSize,
&mut workspace_state.workspaceTooSmall,
&mut workspace_state.workspaceWasteful,
);
},
|| unsafe { ZSTD_rust_resetCCtxWorkspace(workspace_state_ptr) },
|| unsafe {
let workspace_state = &*workspace_state_ptr;
prepare_tail(
callback_context,
(&*plan).blockSize,
*workspace_state.needsIndexReset,
);
},
|| unsafe { ZSTD_rust_resetCCtxTail(tail_state_ptr) },
)
}
/// Reserve and publish the private CCtx storage that follows match-state reset.
#[no_mangle]
pub unsafe extern "C" fn ZSTD_rust_resetCCtxStorage(
@@ -17882,6 +18031,67 @@ mod tests {
);
}
#[test]
fn cctx_reset_policy_preserves_plan_workspace_tail_order() {
let events = std::cell::RefCell::new(Vec::new());
let result = run_cctx_reset_policy(
|| {
events.borrow_mut().push("plan");
0
},
|| events.borrow_mut().push("prepare-workspace"),
|| {
events.borrow_mut().push("workspace");
0
},
|| events.borrow_mut().push("prepare-tail"),
|| {
events.borrow_mut().push("tail");
0
},
);
assert_eq!(result, 0);
assert_eq!(
events.into_inner(),
[
"plan",
"prepare-workspace",
"workspace",
"prepare-tail",
"tail"
]
);
}
#[test]
fn cctx_reset_policy_propagates_workspace_error_before_tail() {
let events = std::cell::RefCell::new(Vec::new());
let workspace_error = ERROR(ZstdErrorCode::MemoryAllocation);
let result = run_cctx_reset_policy(
|| {
events.borrow_mut().push("plan");
0
},
|| events.borrow_mut().push("prepare-workspace"),
|| {
events.borrow_mut().push("workspace");
workspace_error
},
|| events.borrow_mut().push("prepare-tail"),
|| {
events.borrow_mut().push("tail");
0
},
);
assert_eq!(result, workspace_error);
assert_eq!(
events.into_inner(),
["plan", "prepare-workspace", "workspace"]
);
}
#[derive(Debug, PartialEq)]
enum ResetCCtxStorageTestEvent {
Reserve(c_int, usize),