2015-04-16 20:06:57 +08:00
|
|
|
// This file is part of libfringe, a low-level green threading library.
|
2015-04-16 18:08:44 +08:00
|
|
|
// Copyright (c) 2015, edef <edef@edef.eu>
|
|
|
|
// See the LICENSE file included in this distribution.
|
2015-04-16 19:30:53 +08:00
|
|
|
|
2014-12-24 14:28:21 +08:00
|
|
|
extern crate std;
|
2015-03-27 14:52:25 +08:00
|
|
|
use self::std::io::Error as IoError;
|
2015-01-14 15:31:17 +08:00
|
|
|
use stack;
|
2015-04-16 18:42:31 +08:00
|
|
|
mod sys;
|
2015-04-16 14:04:06 +08:00
|
|
|
|
2015-04-16 19:47:23 +08:00
|
|
|
/// This object represents a stack from the operating system's
|
|
|
|
/// anonymous memory mapping facility, usually `mmap`.
|
|
|
|
/// The stack it provides comes with a guard page, which is not included
|
|
|
|
/// in the stack limit.
|
2015-03-27 14:52:25 +08:00
|
|
|
#[derive(Debug)]
|
2014-12-24 14:07:25 +08:00
|
|
|
pub struct Stack {
|
2015-03-27 14:52:25 +08:00
|
|
|
ptr: *mut u8,
|
2015-04-16 14:30:10 +08:00
|
|
|
len: usize
|
2014-12-24 14:07:25 +08:00
|
|
|
}
|
|
|
|
|
2015-04-16 19:06:30 +08:00
|
|
|
unsafe impl Send for Stack {}
|
|
|
|
|
2015-04-16 19:39:28 +08:00
|
|
|
impl Stack {
|
|
|
|
pub fn new(size: usize) -> Result<Stack, IoError> {
|
2015-04-16 14:04:06 +08:00
|
|
|
let page_size = sys::page_size();
|
2014-12-24 14:07:25 +08:00
|
|
|
|
2015-03-27 14:52:25 +08:00
|
|
|
// round the page size up,
|
|
|
|
// using the fact that it is a power of two
|
|
|
|
let len = (size + page_size - 1) & !(page_size - 1);
|
|
|
|
|
|
|
|
let stack = unsafe {
|
2015-04-16 16:14:18 +08:00
|
|
|
let ptr = try!(match sys::map_stack(size) {
|
|
|
|
None => Err(IoError::last_os_error()),
|
|
|
|
Some(ptr) => Ok(ptr)
|
|
|
|
});
|
2015-03-27 14:52:25 +08:00
|
|
|
|
2015-04-16 14:30:10 +08:00
|
|
|
Stack { ptr: ptr as *mut u8, len: len }
|
2014-12-24 14:07:25 +08:00
|
|
|
};
|
|
|
|
|
2016-03-16 07:20:45 +08:00
|
|
|
try!(if unsafe { sys::protect_stack(stack.ptr) } {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(IoError::last_os_error())
|
2015-04-16 16:14:18 +08:00
|
|
|
});
|
2015-04-16 14:04:06 +08:00
|
|
|
|
2015-04-16 16:14:18 +08:00
|
|
|
Ok(stack)
|
2014-12-24 14:07:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 17:24:18 +08:00
|
|
|
impl stack::Stack for Stack {
|
|
|
|
fn top(&mut self) -> *mut u8 {
|
|
|
|
unsafe {
|
|
|
|
self.ptr.offset(self.len as isize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn limit(&self) -> *const u8 {
|
|
|
|
unsafe {
|
|
|
|
self.ptr.offset(sys::page_size() as isize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-27 14:52:25 +08:00
|
|
|
impl Drop for Stack {
|
|
|
|
fn drop(&mut self) {
|
2014-12-24 14:07:25 +08:00
|
|
|
unsafe {
|
2015-04-16 14:04:06 +08:00
|
|
|
if !sys::unmap_stack(self.ptr, self.len) {
|
2015-04-16 14:08:41 +08:00
|
|
|
panic!("munmap for stack {:p} of size {} failed: {}",
|
2015-03-27 14:52:25 +08:00
|
|
|
self.ptr, self.len, IoError::last_os_error())
|
|
|
|
}
|
2014-12-24 14:07:25 +08:00
|
|
|
}
|
|
|
|
}
|
2014-12-24 13:12:39 +08:00
|
|
|
}
|