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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use std::{marker, mem, ops, ptr};
pub struct StackA<T: ?Sized, D: ::DataBuf> {
_pd: marker::PhantomData<*const T>,
next_ofs: usize,
data: D,
}
impl<T: ?Sized, D: ::DataBuf> ops::Drop for StackA<T, D> {
fn drop(&mut self) {
while !self.is_empty() {
self.pop();
}
}
}
impl<T: ?Sized, D: ::DataBuf> Default for StackA<T, D> {
fn default() -> Self {
StackA::new()
}
}
impl<T: ?Sized, D: ::DataBuf> StackA<T, D> {
pub fn new() -> StackA<T, D> {
StackA {
_pd: marker::PhantomData,
next_ofs: 0,
data: Default::default(),
}
}
pub fn is_empty(&self) -> bool {
self.next_ofs == 0
}
fn meta_words() -> usize {
mem::size_of::<&T>() / mem::size_of::<usize>() - 1
}
fn push_inner(&mut self, fat_ptr: &T) -> Result<(&mut [usize],&mut [usize]), ()> {
let bytes = mem::size_of_val(fat_ptr);
let words = super::round_to_words(bytes) + Self::meta_words();
if self.next_ofs + words <= self.data.as_ref().len() {
self.next_ofs += words;
let len = self.data.as_ref().len();
let slot = &mut self.data.as_mut()[len - self.next_ofs..][..words];
let (meta, rv) = slot.split_at_mut(Self::meta_words());
let mut ptr_raw: *const T = fat_ptr;
let ptr_words = ::ptr_as_slice(&mut ptr_raw);
assert_eq!(ptr_words.len(), 1 + Self::meta_words());
meta.clone_from_slice(&ptr_words[1..]);
Ok( (meta, rv) )
} else {
Err(())
}
}
#[cfg(feature = "unsize")]
pub fn push<U: marker::Unsize<T>>(&mut self, v: U) -> Result<(), U> {
self.push_stable(v, |p| p)
}
pub fn push_stable<U, F: FnOnce(&U) -> &T>(&mut self, v: U, f: F) -> Result<(), U> {
assert!(
mem::align_of::<U>() <= mem::align_of::<Self>(),
"TODO: Enforce alignment >{} (requires {})",
mem::align_of::<Self>(),
mem::align_of::<U>()
);
match self.push_inner(f(&v)) {
Ok((_,d)) => {
unsafe {
ptr::write(d.as_mut_ptr() as *mut U, v);
}
Ok(())
}
Err(_) => Err(v),
}
}
fn top_raw(&self) -> Option<*mut T> {
if self.next_ofs == 0 {
None
} else {
let dar = self.data.as_ref();
let meta = &dar[dar.len() - self.next_ofs..];
let mw = Self::meta_words();
Some(unsafe { super::make_fat_ptr(meta[mw..].as_ptr() as usize, &meta[..mw]) })
}
}
pub fn top(&self) -> Option<&T> {
self.top_raw().map(|x| unsafe { &*x })
}
pub fn top_mut(&mut self) -> Option<&mut T> {
self.top_raw().map(|x| unsafe { &mut *x })
}
pub fn pop(&mut self) {
if let Some(ptr) = self.top_raw() {
assert!(self.next_ofs > 0);
let words = unsafe {
let size = mem::size_of_val(&*ptr);
ptr::drop_in_place(ptr);
super::round_to_words(size)
};
self.next_ofs -= words + 1;
}
}
}
impl<D: ::DataBuf> StackA<str, D> {
pub fn push_str(&mut self, v: &str) -> Result<(), ()> {
self.push_inner(v).map(|(_,d)| unsafe {
ptr::copy(v.as_bytes().as_ptr(), d.as_mut_ptr() as *mut u8, v.len());
})
}
}
impl<D: ::DataBuf, T: Clone> StackA<[T], D> {
pub fn push_cloned(&mut self, v: &[T]) -> Result<(), ()> {
let (meta,d) = self.push_inner(&v)?;
meta[0] = 0;
for v in d.iter_mut() {
*v = 0;
}
unsafe {
let mut ptr = d.as_mut_ptr() as *mut T;
for val in v {
ptr::write(ptr, val.clone());
meta[0] += 1;
ptr = ptr.offset(1);
}
}
Ok( () )
}
}