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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! # generator stack
//!
//!
use std::ptr;
use alloc::raw_vec::RawVec;
/// generator stack
pub struct Stack {
buf: RawVec<usize>,
}
impl Stack {
/// Allocate a new stack of `size`. If size = 0, this will fail. Use
/// `dummy_stack` if you want a zero-sized stack.
pub fn new(size: usize) -> Stack {
let mut size = size;
// the minimal size
if size != 0 && size < 8 {
size = 8;
}
let stk = Stack { buf: RawVec::with_capacity(size) };
// if size is not even we do the full foot print test
if (size & 1) == 0 && (size > 8) {
// we only check the last few words
size = 8;
}
unsafe {
let buf = stk.buf.ptr();
ptr::write_bytes(buf, 0xEE, size);
}
stk
}
/// get used stack size
pub fn get_used_size(&self) -> usize {
let mut offset: usize = 0;
unsafe {
let mut magic: usize = 0xEE;
ptr::write_bytes(&mut magic, 0xEE, 1);
let mut ptr = self.buf.ptr();
while *ptr == magic {
offset += 1;
ptr = ptr.offset(1);
}
}
self.buf.cap() - offset
}
/// get the stack cap
pub fn size(&self) -> usize {
self.buf.cap()
}
/// Point to the high end of the allocated stack
pub fn end(&self) -> *mut usize {
unsafe { self.buf.ptr().offset(self.buf.cap() as isize) as *mut usize }
}
}