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
176
177
178
179
180
181
182
183
184
185
186
187
#![feature(alloc)]
#![feature(heap_api)]
#![feature(thread_local)]
#![feature(const_fn)]
#![no_std]
extern crate alloc;
use core::cell::Cell;
use core::{mem, slice, ptr};
#[thread_local]
static STASH: Stash = Stash {
top: Cell::new(0),
cap: Cell::new(0)
};
const STASH_SIZE: usize = 1 << 20;
struct Stash {
top: Cell<usize>,
cap: Cell<usize>
}
unsafe impl Sync for Stash {}
impl Stash {
#[inline(always)]
fn top(&self) -> usize {
let top = self.top.get();
if top != 0 {
top
} else {
self.init();
self.top.get()
}
}
#[inline(always)]
fn set_top(&self, top: usize) {
self.top.set(top);
}
fn init_size(&self, size: usize) {
use alloc;
assert_eq!(STASH.top.get(), 0);
let base = unsafe { alloc::heap::allocate(size, 64) } as usize;
self.top.set(base);
self.cap.set(base + size);
}
#[cold]
fn init(&self) {
self.init_size(STASH_SIZE);
}
}
#[derive(Debug)]
pub enum InitError {
AlreadyInitialized
}
pub fn init_with_capacity(size: usize) -> Result<(), InitError> {
if STASH.top.get() != 0 {
Ok(STASH.init_size(size))
} else {
Err(InitError::AlreadyInitialized)
}
}
#[derive(Debug)]
pub enum PlaceError {
OutOfMemory
}
#[inline(always)]
fn padding_for<T>(ptr: usize) -> usize {
let align = mem::align_of::<T>();
align - (ptr - 1) % align - 1
}
#[inline(always)]
pub unsafe fn place_raw<F, O, T>(count: usize, f: F) -> Result<O, PlaceError>
where F: FnOnce(*mut T) -> O
{
let old_top = STASH.top();
let size = count * mem::size_of::<T>();
let pad = padding_for::<T>(old_top);
let ptr = old_top + pad;
let new_top = ptr + size;
if new_top < STASH.cap.get() {
STASH.set_top(new_top);
let r = f(ptr as *mut T);
STASH.set_top(old_top);
Ok(r)
} else {
Err(PlaceError::OutOfMemory)
}
}
#[inline]
pub fn place<F, O, T>(count: usize, f: F) -> Result<O, PlaceError>
where F: FnOnce(&mut [T]) -> O, T: Default
{
unsafe {
place_raw(count, |ptr| {
for i in 0 .. count {
ptr::write(ptr.offset(i as isize), T::default());
}
let slice = slice::from_raw_parts_mut(ptr, count);
let r = f(slice);
for i in 0 .. count {
ptr::drop_in_place(ptr.offset(i as isize));
}
r
})
}
}
#[inline]
pub fn place_iter<I, F, T, O>(mut iter: I, f: F) -> Result<O, PlaceError>
where I: Iterator<Item=T>, F: FnOnce(&mut [I::Item]) -> O
{
let old_top = STASH.top();
let cap = STASH.cap.get();
let pad = padding_for::<T>(old_top);
let ptr_base = old_top + pad;
let mut p = ptr_base;
let mut len = 0;
while let Some(e) = iter.next() {
if p + mem::size_of::<T>() > cap {
while p > ptr_base {
p -= mem::size_of::<T>();
unsafe { ptr::drop_in_place(p as *mut T) };
}
#[cold]
return Err(PlaceError::OutOfMemory);
}
unsafe {
ptr::write(p as *mut T, e);
}
p += mem::size_of::<T>();
len += 1;
STASH.set_top(p as usize);
}
let slice = unsafe {
slice::from_raw_parts_mut(p as *mut T, len)
};
let r = f(slice);
while p > ptr_base {
p -= mem::size_of::<T>();
unsafe {
ptr::drop_in_place(p as *mut T);
}
}
STASH.set_top(old_top);
Ok(r)
}