Skip to content

Commit

Permalink
Update IPASIR callback wrappers to use Box::leak instead of mem::forget
Browse files Browse the repository at this point in the history
  • Loading branch information
Dekker1 committed May 23, 2024
1 parent 8fb3c6d commit 420d429
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions crates/pindakaas-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,16 @@ pub fn ipasir_solver_derive(input: TokenStream) -> TokenStream {
crate::solver::SlvTermSignal::Terminate => std::ffi::c_int::from(1),
}
};
let data = &mut wrapped_cb as *mut _ as *mut std::ffi::c_void;
let trampoline = crate::solver::libloading::get_trampoline0(&wrapped_cb);
// WARNING: Any data in the callback now exists forever
let data = Box::leak(Box::new(wrapped_cb)) as *mut _ as *mut std::ffi::c_void;
unsafe {
#krate::ipasir_set_terminate(
#ptr,
data,
Some(crate::solver::libloading::get_trampoline0(&wrapped_cb)),
Some(trampoline),
)
}
// WARNING: Any data in the callback now exists forever
std::mem::forget(wrapped_cb);
} else {
unsafe { #krate::ipasir_set_terminate(#ptr, std::ptr::null_mut(), None) }
}
Expand All @@ -135,17 +135,17 @@ pub fn ipasir_solver_derive(input: TokenStream) -> TokenStream {
.map(|i: i32| crate::Lit(std::num::NonZeroI32::new(i).unwrap()));
cb(&mut iter)
};
let data = &mut wrapped_cb as *mut _ as *mut std::ffi::c_void;
let trampoline = crate::solver::libloading::get_trampoline1(&wrapped_cb);
// WARNING: Any data in the callback now exists forever
let data = Box::leak(Box::new(wrapped_cb)) as *mut _ as *mut std::ffi::c_void;
unsafe {
#krate::ipasir_set_learn(
#ptr,
data,
MAX_LEN,
Some(crate::solver::libloading::get_trampoline1(&wrapped_cb)),
Some(trampoline),
)
}
// WARNING: Any data in the callback now exists forever
std::mem::forget(wrapped_cb);
} else {
unsafe { #krate::ipasir_set_learn(#ptr, std::ptr::null_mut(), MAX_LEN, None) }
}
Expand Down

0 comments on commit 420d429

Please sign in to comment.