Debian Codegraph @

src/cee_based.rs

raw symbols SCIP
1 use crate::{JmpBuf, JmpBufFields};
2 use crate::{SigJmpBuf, SigJmpBufFields};
3 use libc::{c_int, c_void};
4
5 #[link(name = "interop_via_c", kind="static")]
6 extern "C" {
7 fn call_closure_with_setjmp(closure_env_ptr: *mut c_void, closure_code: extern "C" fn(jbuf: *const JmpBufFields, env_ptr: *mut c_void) -> c_int) -> c_int;
8
9 fn call_closure_with_sigsetjmp(savemask: c_int, closure_env_ptr: *mut c_void, closure_code: extern "C" fn(jbuf: *const SigJmpBufFields, env_ptr: *mut c_void) -> c_int) -> c_int;
10 }
11
12 /// Covers the usual use case for setjmp: it invokes the callback, and the code
13 /// of the callback can use longjmp to exit early from the call_with_setjmp.
14 pub fn call_with_setjmp<F>(mut callback: F) -> c_int
15 where
16 F: for<'a> FnOnce(&'a JmpBufFields) -> c_int,
17 {
18 extern "C" fn call_from_c_to_rust<F>(jbuf: JmpBuf, closure_env_ptr: *mut c_void) -> c_int
19 where
20 F: for<'a> FnOnce(&'a JmpBufFields) -> c_int,
21 {
22 let closure_env_ptr: *mut F = closure_env_ptr as *mut F;
23
24 // Dereference `closure_env_ptr` with .read() to acquire ownership of
25 // the FnOnce object, then call it. (See also the forget note below.)
26 //
27 // Note that `closure_env_ptr` is not a raw function pointer, it's a
28 // pointer to a FnOnce; the code we call comes from the generic `F`.
29 unsafe { (closure_env_ptr.read())(&*jbuf) }
30 }
31
32 unsafe {
33 let closure_env_ptr = std::ptr::addr_of_mut!(callback);
34
35 // The callback is now effectively owned by `closure_env_ptr` (i.e., the
36 // `closure_env_ptr.read()` call in `call_from_c_to_rust` will take a
37 // direct bitwise copy of its state, and pass that ownership into the
38 // FnOnce::call_once invocation.)
39 //
40 // Therefore, we need to forget about our own ownership of the callback now.
41 std::mem::forget(callback);
42
43 call_closure_with_setjmp(closure_env_ptr as *mut c_void, call_from_c_to_rust::<F>)
44 }
45 }
46
47 /// TODO
48 pub fn call_with_sigsetjmp<F>(savemask: bool, mut callback: F) -> c_int
49 where
50 F: for<'a> FnOnce(&'a SigJmpBufFields) -> c_int,
51 {
52 extern "C" fn call_from_c_to_rust<F>(jbuf: SigJmpBuf, closure_env_ptr: *mut c_void) -> c_int
53 where
54 F: for<'a> FnOnce(&'a SigJmpBufFields) -> c_int,
55 {
56 let closure_env_ptr: *mut F = closure_env_ptr as *mut F;
57
58 // Dereference `closure_env_ptr` with .read() to acquire ownership of
59 // the FnOnce object, then call it. (See also the forget note below.)
60 //
61 // Note that `closure_env_ptr` is not a raw function pointer, it's a
62 // pointer to a FnOnce; the code we call comes from the generic `F`.
63 unsafe { (closure_env_ptr.read())(&*jbuf) }
64 }
65
66 let savemask: c_int = if savemask { 1 } else { 0 };
67
68 unsafe {
69 let closure_env_ptr = std::ptr::addr_of_mut!(callback);
70
71 // The callback is now effectively owned by `closure_env_ptr` (i.e., the
72 // `closure_env_ptr.read()` call in `call_from_c_to_rust` will take a
73 // direct bitwise copy of its state, and pass that ownership into the
74 // FnOnce::call_once invocation.)
75 //
76 // Therefore, we need to forget about our own ownership of the callback now.
77 std::mem::forget(callback);
78
79 call_closure_with_sigsetjmp(savemask, closure_env_ptr as *mut c_void, call_from_c_to_rust::<F>)
80 }
81 }