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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use std::cell::{Cell, RefCell};
use std::cmp;
use std::intrinsics;
use std::marker::PhantomData;
use std::mem;
use std::ptr;
use std::slice;
use alloc::heap;
use alloc::raw_vec::RawVec;
pub struct AnyArena<'longer_than_self> {
head: RefCell<Chunk>,
copy_head: RefCell<Chunk>,
chunks: RefCell<Vec<Chunk>>,
_marker: PhantomData<*mut &'longer_than_self ()>,
}
impl<'longer_than_self> AnyArena<'longer_than_self> {
pub fn new() -> AnyArena<'longer_than_self> {
AnyArena::new_with_size(32)
}
pub fn new_with_size(initial_size: usize) -> AnyArena<'longer_than_self> {
AnyArena {
head: RefCell::new(Chunk::new(initial_size, false)),
copy_head: RefCell::new(Chunk::new(initial_size, true)),
chunks: RefCell::new(Vec::new()),
_marker: PhantomData,
}
}
#[inline]
pub fn alloc<T: 'longer_than_self, F>(&self, op: F) -> &mut T
where F: FnOnce() -> T
{
unsafe {
if intrinsics::needs_drop::<T>() {
self.alloc_noncopy(op)
} else {
self.alloc_copy(op)
}
}
}
#[inline]
fn alloc_copy<T, F>(&self, op: F) -> &mut T
where F: FnOnce() -> T
{
unsafe {
let ptr = self.alloc_copy_inner(mem::size_of::<T>(), mem::align_of::<T>());
let ptr = ptr as *mut T;
ptr::write(&mut (*ptr), op());
&mut *ptr
}
}
#[inline]
fn alloc_copy_inner(&self, n_bytes: usize, align: usize) -> *const u8 {
let mut copy_head = self.copy_head.borrow_mut();
let fill = copy_head.fill.get();
let mut start = round_up(fill, align);
let mut end = start + n_bytes;
if end > copy_head.capacity() {
if self.alloc_grow(&mut *copy_head, fill, end - fill) {
start = 0;
end = n_bytes;
copy_head.is_copy.set(true);
}
}
copy_head.fill.set(end);
unsafe { copy_head.as_ptr().offset(start as isize) }
}
#[inline]
fn alloc_noncopy<T, F>(&self, op: F) -> &mut T
where F: FnOnce() -> T
{
unsafe {
let tydesc = get_tydesc::<T>();
let (ty_ptr, ptr) = self.alloc_noncopy_inner(mem::size_of::<T>(), mem::align_of::<T>());
let ty_ptr = ty_ptr as *mut usize;
let ptr = ptr as *mut T;
*ty_ptr = bitpack_tydesc_ptr(tydesc, false);
ptr::write(&mut (*ptr), op());
*ty_ptr = bitpack_tydesc_ptr(tydesc, true);
&mut *ptr
}
}
#[inline]
fn alloc_noncopy_inner(&self, n_bytes: usize, align: usize) -> (*const u8, *const u8) {
let mut head = self.head.borrow_mut();
let fill = head.fill.get();
let mut tydesc_start = fill;
let after_tydesc = fill + mem::size_of::<*const TyDesc>();
let mut start = round_up(after_tydesc, align);
let mut end = round_up(start + n_bytes, mem::align_of::<*const TyDesc>());
if end > head.capacity() {
if self.alloc_grow(&mut *head, tydesc_start, end - tydesc_start) {
tydesc_start = 0;
start = round_up(mem::size_of::<*const TyDesc>(), align);
end = round_up(start + n_bytes, mem::align_of::<*const TyDesc>());
}
}
head.fill.set(end);
unsafe {
let buf = head.as_ptr();
(buf.offset(tydesc_start as isize),
buf.offset(start as isize))
}
}
#[cold]
fn alloc_grow(&self, head: &mut Chunk, used_cap: usize, n_bytes: usize) -> bool {
if head.data.reserve_in_place(used_cap, n_bytes) {
false
} else {
let new_min_chunk_size = cmp::max(n_bytes, head.capacity());
let new_chunk = Chunk::new((new_min_chunk_size + 1).next_power_of_two(), false);
let old_chunk = mem::replace(head, new_chunk);
if old_chunk.fill.get() != 0 {
self.chunks.borrow_mut().push(old_chunk);
}
true
}
}
pub fn alloc_bytes(&self, len: usize) -> &mut [u8] {
unsafe {
self.copy_head.borrow().fill.get().checked_add(len).expect("length overflow");
let ptr = self.alloc_copy_inner(len, 1);
intrinsics::assume(!ptr.is_null());
slice::from_raw_parts_mut(ptr as *mut _, len)
}
}
pub fn clear(&mut self) {
unsafe {
self.head.borrow().destroy();
self.head.borrow().fill.set(0);
self.copy_head.borrow().fill.set(0);
for chunk in self.chunks.borrow().iter() {
if !chunk.is_copy.get() {
chunk.destroy();
}
}
self.chunks.borrow_mut().clear();
}
}
}
impl<'longer_than_self> Drop for AnyArena<'longer_than_self> {
fn drop(&mut self) {
unsafe {
self.head.borrow().destroy();
for chunk in self.chunks.borrow().iter() {
if !chunk.is_copy.get() {
chunk.destroy();
}
}
}
}
}
struct Chunk {
data: RawVec<u8>,
fill: Cell<usize>,
is_copy: Cell<bool>,
}
impl Chunk {
fn new(size: usize, is_copy: bool) -> Chunk {
Chunk {
data: RawVec::with_capacity(size),
fill: Cell::new(0),
is_copy: Cell::new(is_copy),
}
}
fn capacity(&self) -> usize {
self.data.cap()
}
unsafe fn as_ptr(&self) -> *const u8 {
self.data.ptr()
}
unsafe fn destroy(&self) {
let mut idx = 0;
let buf = self.as_ptr();
let fill = self.fill.get();
while idx < fill {
let tydesc_data = buf.offset(idx as isize) as *const usize;
let (tydesc, is_done) = un_bitpack_tydesc_ptr(*tydesc_data);
let (size, align) = ((*tydesc).size, (*tydesc).align);
let after_tydesc = idx + mem::size_of::<*const TyDesc>();
let start = round_up(after_tydesc, align);
if is_done {
((*tydesc).drop_glue)(buf.offset(start as isize) as *const i8);
}
idx = round_up(start + size, mem::align_of::<*const TyDesc>());
}
}
}
#[inline]
fn round_up(base: usize, align: usize) -> usize {
(base.checked_add(align - 1)).unwrap() & !(align - 1)
}
struct TyDesc {
drop_glue: fn(*const i8),
size: usize,
align: usize,
}
unsafe fn get_tydesc<T>() -> *const TyDesc {
use std::raw::TraitObject;
let ptr = &*(heap::EMPTY as *const T);
let obj = mem::transmute::<&AllTypes, TraitObject>(ptr);
obj.vtable as *const TyDesc
}
#[inline]
fn bitpack_tydesc_ptr(p: *const TyDesc, is_done: bool) -> usize {
p as usize | (is_done as usize)
}
#[inline]
fn un_bitpack_tydesc_ptr(p: usize) -> (*const TyDesc, bool) {
((p & !1) as *const TyDesc, p & 1 == 1)
}
trait AllTypes {
fn dummy(&self) {}
}
impl<T: ?Sized> AllTypes for T {}