1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use stack::Stack;
#[derive(Debug)]
pub struct SliceStack<'a>(&'a mut [u8]);
impl<'a> SliceStack<'a> {
pub fn new(slice: &'a mut [u8]) -> SliceStack<'a> {
let ptr = slice.as_ptr() as usize;
let adjusted_ptr = (ptr + ::STACK_ALIGNMENT - 1) & !(::STACK_ALIGNMENT - 1);
let offset = adjusted_ptr - ptr;
if offset > slice.len() {
panic!("SliceStack too small");
}
let adjusted_len = (slice.len() - offset) & !(::STACK_ALIGNMENT - 1);
SliceStack(&mut slice[offset..(offset + adjusted_len)])
}
}
unsafe impl<'a> Stack for SliceStack<'a> {
#[inline(always)]
fn base(&self) -> *mut u8 {
let len = self.0.len() as isize;
unsafe { self.limit().offset(len) }
}
#[inline(always)]
fn limit(&self) -> *mut u8 {
self.0.as_ptr() as *mut u8
}
}