2015-04-16 20:06:57 +08:00
|
|
|
// This file is part of libfringe, a low-level green threading library.
|
2016-03-22 15:25:23 +08:00
|
|
|
// Copyright (c) edef <edef@edef.eu>
|
2015-04-16 18:08:44 +08:00
|
|
|
// See the LICENSE file included in this distribution.
|
2015-04-16 11:37:17 +08:00
|
|
|
|
|
|
|
/// A trait for objects that hold ownership of a stack.
|
2015-01-14 15:31:17 +08:00
|
|
|
pub trait Stack {
|
2016-08-12 07:18:36 +08:00
|
|
|
/// Returns the base of the stack.
|
2015-04-16 11:37:17 +08:00
|
|
|
/// On all modern architectures, the stack grows downwards,
|
|
|
|
/// so this is the highest address.
|
2016-08-12 07:18:36 +08:00
|
|
|
fn base(&self) -> *mut u8;
|
2015-04-16 11:37:17 +08:00
|
|
|
/// Returns the bottom of the stack.
|
|
|
|
/// On all modern architectures, the stack grows downwards,
|
|
|
|
/// so this is the lowest address.
|
2016-07-16 09:22:41 +08:00
|
|
|
fn limit(&self) -> *mut u8;
|
2014-12-23 11:24:40 +08:00
|
|
|
}
|
2016-07-17 11:45:50 +08:00
|
|
|
|
|
|
|
/// A marker trait for `Stack` objects with a guard page.
|
|
|
|
///
|
|
|
|
/// A guarded stack must guarantee that any access of data at addresses `limit()` to
|
|
|
|
/// `limit().offset(4096)` will abnormally terminate the program.
|
|
|
|
pub unsafe trait GuardedStack {}
|
2016-08-19 17:18:46 +08:00
|
|
|
|
|
|
|
/// SliceStack holds a non-guarded stack allocated elsewhere and provided as a mutable
|
|
|
|
/// slice.
|
|
|
|
pub struct SliceStack<'a>(pub &'a mut [u8]);
|
|
|
|
|
|
|
|
impl<'a> Stack for SliceStack<'a> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn base(&self) -> *mut u8 {
|
|
|
|
// The slice cannot wrap around the address space, so the conversion from usize
|
|
|
|
// to isize will not wrap either.
|
|
|
|
let len: isize = self.0.len() as isize;
|
|
|
|
unsafe { self.limit().offset(len) }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn limit(&self) -> *mut u8 {
|
|
|
|
self.0.as_ptr() as *mut u8
|
|
|
|
}
|
|
|
|
}
|