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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#![feature(maybe_uninit_slice, maybe_uninit_extra)]
#![no_std]
use core::{
mem::{ManuallyDrop, MaybeUninit},
ops::{Deref, DerefMut},
pin::Pin,
};
use alloc::{boxed::Box, vec::Vec};
use spin::Mutex;
extern crate alloc;
pub use bumpalo::{
boxed, collections, format, vec, AllocOrInitError, Bump, ChunkIter, ChunkRawIter,
};
pub struct BumpPool {
ready: Mutex<Vec<Pin<Box<Bump>>>>,
shunned: Mutex<Vec<Pin<Box<Bump>>>>,
}
impl BumpPool {
pub const fn new() -> Self {
Self {
ready: Mutex::new(Vec::new()),
shunned: Mutex::new(Vec::new()),
}
}
pub fn reset(&mut self) {
for pool in self.shunned.get_mut().drain(..) {
self.ready.get_mut().push(pool);
}
self.ready.get_mut().iter_mut().for_each(|b| b.reset());
}
pub fn get(&self) -> PooledBump {
let next = self
.ready
.lock()
.pop()
.unwrap_or_else(|| Box::pin(Bump::new()));
PooledBump {
stampede: self,
bump: ManuallyDrop::new(next),
}
}
}
pub struct PooledBump<'s> {
stampede: &'s BumpPool,
bump: ManuallyDrop<Pin<Box<Bump>>>,
}
impl<'s> PooledBump<'s> {
pub fn alloc<T>(&self, val: T) -> &'s mut T {
unsafe { self.as_bump_unbound().alloc(val) }
}
pub fn alloc_boxed<T>(&self, val: T) -> boxed::Box<'s, T> {
boxed::Box::new_in(val, unsafe { self.as_bump_unbound() })
}
pub fn chunk<T>(&self, size: usize) -> Chunk<'s, T> {
Chunk::new(unsafe {
self.as_bump_unbound()
.alloc_slice_fill_with(size, |_| MaybeUninit::uninit())
})
}
pub unsafe fn as_bump_unbound(&self) -> &'s Bump {
let bump_ref = self.bump.as_ref().get_ref();
core::mem::transmute::<&Bump, &'s Bump>(bump_ref)
}
pub fn as_bump(&self) -> &Bump {
self.bump.as_ref().get_ref()
}
pub fn detach(mut self) -> &'s Bump {
let bump = unsafe { self.as_bump_unbound() };
let mut shunned = self.stampede.shunned.lock();
shunned.push(unsafe { ManuallyDrop::take(&mut self.bump) });
core::mem::forget(self);
bump
}
}
impl<'s> Drop for PooledBump<'s> {
fn drop(&mut self) {
let mut herd = self.stampede.ready.lock();
herd.push(unsafe { ManuallyDrop::take(&mut self.bump) });
}
}
pub struct Chunk<'s, T> {
len: usize,
storage: &'s mut [MaybeUninit<T>],
}
impl<'s, T> Deref for Chunk<'s, T> {
type Target = [T];
fn deref(&self) -> &Self::Target {
unsafe { MaybeUninit::slice_assume_init_ref(&self.storage[..self.len]) }
}
}
impl<'s, T> DerefMut for Chunk<'s, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { MaybeUninit::slice_assume_init_mut(&mut self.storage[..self.len]) }
}
}
impl<'s, T> Chunk<'s, T> {
pub fn new(storage: &'s mut [MaybeUninit<T>]) -> Self {
Self { len: 0, storage }
}
pub fn push(&mut self, val: T) -> Result<(), T> {
if self.len < self.capacity() {
self.storage[self.len].write(val);
Ok(())
} else {
Err(val)
}
}
pub fn len(&self) -> usize {
self.len
}
pub fn capacity(&self) -> usize {
self.storage.len()
}
pub fn is_empty(&self) -> bool {
self.len == 0
}
}
impl<'s, T> Drop for Chunk<'s, T> {
fn drop(&mut self) {
unsafe {
for i in 0..self.len {
self.storage[i].assume_init_drop();
}
}
}
}
pub struct ChunkIntoIter<'s, T> {
storage: &'s mut [MaybeUninit<T>],
}
impl<'s, T> Iterator for ChunkIntoIter<'s, T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
let storage = core::mem::take(&mut self.storage);
let (head, new_tail) = storage.split_first_mut()?;
self.storage = new_tail;
Some(unsafe { head.assume_init_read() })
}
}
impl<'s, T> Drop for ChunkIntoIter<'s, T> {
fn drop(&mut self) {
self.for_each(drop);
}
}
impl<'s, T> IntoIterator for Chunk<'s, T> {
type Item = T;
type IntoIter = ChunkIntoIter<'s, T>;
fn into_iter(mut self) -> Self::IntoIter {
let storage = core::mem::take(&mut self.storage);
self.len = 0;
ChunkIntoIter { storage }
}
}