72 lines
1.9 KiB
Rust
72 lines
1.9 KiB
Rust
|
|
use std::mem::MaybeUninit;
|
|
|
|
/// Safe wrapper for ucontext_t struct of linux-gnu libc
|
|
///
|
|
/// setcontext and getcontext are unsafe function, this wrap unsafe libc functions
|
|
///
|
|
/// This struct doesn't work on windows, because this struct is unavailable
|
|
///
|
|
/// todo ucontext_t is not thread-safe (doesn't implements Send and Sync trait), and cannot be use in Threads as rust require var in Mutex (see system.rs) to have everything inside to implements thread-safe traits
|
|
#[derive(PartialEq)]
|
|
pub struct UContextT {
|
|
#[cfg(not(target_os = "windows"))] // struct non disponible sur la libc sur windows
|
|
pub buf: lib::ucontext_t,
|
|
pub stackBottom: Vec<i8>
|
|
}
|
|
|
|
#[cfg(not(target_os = "windows"))]
|
|
impl UContextT {
|
|
|
|
pub fn new() -> Self {
|
|
let mut context = MaybeUninit::<ucontext_t>::uninit();
|
|
unsafe { lib::getcontext(context.as_mut_ptr()) };
|
|
Self {
|
|
buf: unsafe { context.assume_init() },
|
|
stackBottom: Vec::default(),
|
|
}
|
|
}
|
|
|
|
/// Get user context and store it in variable pointed to by UCP.
|
|
///
|
|
/// Use `man getcontext` for more informations
|
|
pub fn get_context(&mut self) -> i32 {
|
|
unsafe {
|
|
lib::getcontext(&mut self.buf)
|
|
}
|
|
}
|
|
|
|
/// Set user context from information of variable pointed to by UCP.
|
|
///
|
|
/// Use `man setcontext` for more informations
|
|
pub fn set_context(&mut self) -> i32 {
|
|
unsafe {
|
|
lib::setcontext(&self.buf)
|
|
}
|
|
}
|
|
|
|
pub fn make_context(&mut self, func: extern "C" fn(), args: i32) {
|
|
unsafe {
|
|
lib::makecontext(&mut self.buf, func, args)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(target_os = "windows")]
|
|
impl UContextT {
|
|
|
|
pub fn new() -> Self {
|
|
Self {
|
|
stackBottom: Vec::default()
|
|
}
|
|
}
|
|
|
|
pub fn get_context(&mut self) {
|
|
// no op
|
|
}
|
|
|
|
pub fn set_context(&mut self) {
|
|
// no op
|
|
}
|
|
} |