2023-03-06 16:31:35 +01:00
2023-03-09 12:08:33 +01:00
use std ::mem ::MaybeUninit ;
2023-03-06 16:31:35 +01:00
/// 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
2023-03-08 14:09:07 +01:00
pub buf : libc ::ucontext_t ,
2023-03-09 12:08:33 +01:00
pub stack_bottom : Vec < i8 >
2023-03-06 16:31:35 +01:00
}
#[ cfg(not(target_os = " windows " )) ]
2023-03-09 12:08:33 +01:00
#[ allow(unused) ] // Temporary as we currently doesn't use this structure (this structure may disapear in a near future)
2023-03-06 16:31:35 +01:00
impl UContextT {
pub fn new ( ) -> Self {
2023-03-08 14:09:07 +01:00
let mut context = MaybeUninit ::< libc ::ucontext_t > ::uninit ( ) ;
unsafe { libc ::getcontext ( context . as_mut_ptr ( ) ) } ;
2023-03-06 16:31:35 +01:00
Self {
buf : unsafe { context . assume_init ( ) } ,
2023-03-09 12:08:33 +01:00
stack_bottom : Vec ::default ( ) ,
2023-03-06 16:31:35 +01:00
}
}
/// 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 {
2023-03-08 14:09:07 +01:00
libc ::getcontext ( & mut self . buf )
2023-03-06 16:31:35 +01:00
}
}
/// 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 {
2023-03-08 14:09:07 +01:00
libc ::setcontext ( & self . buf )
2023-03-06 16:31:35 +01:00
}
}
pub fn make_context ( & mut self , func : extern " C " fn ( ) , args : i32 ) {
unsafe {
2023-03-08 14:09:07 +01:00
libc ::makecontext ( & mut self . buf , func , args )
2023-03-06 16:31:35 +01:00
}
}
}
#[ cfg(target_os = " windows " ) ]
impl UContextT {
pub fn new ( ) -> Self {
2023-03-08 13:21:08 +01:00
Self {
2023-03-09 14:00:42 +01:00
stack_bottom : Vec ::default ( )
2023-03-08 13:21:08 +01:00
}
2023-03-06 16:31:35 +01:00
}
pub fn get_context ( & mut self ) {
// no op
}
pub fn set_context ( & mut self ) {
// no op
}
}