1
0
Fork 0
libfringe/src/stack.rs

95 lines
2.1 KiB
Rust
Raw Normal View History

2014-12-23 11:24:40 +08:00
use libc;
2014-12-24 13:12:39 +08:00
use platform;
2014-12-23 11:24:40 +08:00
use std::ptr;
use std::os::{errno, page_size, MemoryMap, MapReadable, MapWritable,
MapNonStandardFlags};
const STACK_FLAGS: libc::c_int = libc::MAP_STACK
| libc::MAP_PRIVATE
| libc::MAP_ANON;
pub enum Stack {
Native {
sp_limit: *const u8
},
Managed {
buf: MemoryMap,
valgrind_id: libc::c_uint
}
}
impl Stack {
pub fn new(size: uint) -> Stack {
let buf = match MemoryMap::new(size, &[MapReadable, MapWritable,
MapNonStandardFlags(STACK_FLAGS)]) {
Ok(map) => map,
Err(e) => panic!("mmap for stack of size {} failed: {}", size, e)
};
if !protect_last_page(&buf) {
panic!("Could not memory-protect guard page. stack={}, errno={}",
buf.data(), errno());
}
let valgrind_id = unsafe {
2014-12-24 13:12:39 +08:00
platform::stack_register(buf.data().offset(buf.len() as int) as *const _,
buf.data() as *const _)
2014-12-23 11:24:40 +08:00
};
let stk = Stack::Managed {
buf: buf,
valgrind_id: valgrind_id
};
stk
}
pub unsafe fn native(limit: *const u8) -> Stack {
Stack::Native {
sp_limit: limit
}
}
pub fn top(&mut self) -> *mut u8 {
2014-12-23 11:24:40 +08:00
unsafe {
match *self {
2014-12-24 13:30:54 +08:00
Stack::Native { .. } => ptr::null_mut(),
2014-12-23 11:24:40 +08:00
Stack::Managed { ref buf, .. } => {
2014-12-24 13:30:54 +08:00
buf.data().offset(buf.len() as int)
2014-12-23 11:24:40 +08:00
}
}
}
}
pub fn limit(&self) -> *const u8 {
unsafe {
match *self {
Stack::Native { sp_limit, .. } => sp_limit,
Stack::Managed { ref buf, .. } => {
buf.data().offset(page_size() as int) as *const _
}
}
}
}
}
fn protect_last_page(stack: &MemoryMap) -> bool {
unsafe {
let last_page = stack.data() as *mut libc::c_void;
libc::mprotect(last_page, page_size() as libc::size_t,
libc::PROT_NONE) != -1
}
}
impl Drop for Stack {
fn drop(&mut self) {
match *self {
Stack::Native { .. } => {},
Stack::Managed { valgrind_id, .. } => unsafe {
2014-12-24 13:12:39 +08:00
platform::stack_deregister(valgrind_id);
2014-12-23 11:24:40 +08:00
}
}
}
}