1 //! The arena, a fast but limited type of allocator.
2 //!
3 //! Arenas are a type of allocator that destroy the objects within, all at
4 //! once, once the arena itself is destroyed. They do not support deallocation
5 //! of individual objects while the arena itself is still alive. The benefit
6 //! of an arena is very fast allocation; just a pointer bump.
7 //!
8 //! This crate implements several kinds of arena.
9
10 #![doc(
11 html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/",
12 test(no_crate_inject, attr(deny(warnings)))
13 )]
14 #![feature(dropck_eyepatch)]
15 #![feature(new_uninit)]
16 #![feature(maybe_uninit_slice)]
17 #![feature(min_specialization)]
18 #![feature(decl_macro)]
19 #![feature(rustc_attrs)]
20 #![cfg_attr(test, feature(test))]
21
22 use smallvec::SmallVec;
23
24 use std::alloc::Layout;
25 use std::cell::{Cell, RefCell};
26 use std::cmp;
27 use std::marker::{PhantomData, Send};
28 use std::mem::{self, MaybeUninit};
29 use std::ptr;
30 use std::slice;
31
32 #[inline(never)]
33 #[cold]
cold_path<F: FnOnce() -> R, R>(f: F) -> R34 fn cold_path<F: FnOnce() -> R, R>(f: F) -> R {
35 f()
36 }
37
38 /// An arena that can hold objects of only one type.
39 pub struct TypedArena<T> {
40 /// A pointer to the next object to be allocated.
41 ptr: Cell<*mut T>,
42
43 /// A pointer to the end of the allocated area. When this pointer is
44 /// reached, a new chunk is allocated.
45 end: Cell<*mut T>,
46
47 /// A vector of arena chunks.
48 chunks: RefCell<Vec<TypedArenaChunk<T>>>,
49
50 /// Marker indicating that dropping the arena causes its owned
51 /// instances of `T` to be dropped.
52 _own: PhantomData<T>,
53 }
54
55 struct TypedArenaChunk<T> {
56 /// The raw storage for the arena chunk.
57 storage: Box<[MaybeUninit<T>]>,
58 /// The number of valid entries in the chunk.
59 entries: usize,
60 }
61
62 impl<T> TypedArenaChunk<T> {
63 #[inline]
new(capacity: usize) -> TypedArenaChunk<T>64 unsafe fn new(capacity: usize) -> TypedArenaChunk<T> {
65 TypedArenaChunk { storage: Box::new_uninit_slice(capacity), entries: 0 }
66 }
67
68 /// Destroys this arena chunk.
69 #[inline]
destroy(&mut self, len: usize)70 unsafe fn destroy(&mut self, len: usize) {
71 // The branch on needs_drop() is an -O1 performance optimization.
72 // Without the branch, dropping TypedArena<u8> takes linear time.
73 if mem::needs_drop::<T>() {
74 ptr::drop_in_place(MaybeUninit::slice_assume_init_mut(&mut self.storage[..len]));
75 }
76 }
77
78 // Returns a pointer to the first allocated object.
79 #[inline]
start(&mut self) -> *mut T80 fn start(&mut self) -> *mut T {
81 MaybeUninit::slice_as_mut_ptr(&mut self.storage)
82 }
83
84 // Returns a pointer to the end of the allocated space.
85 #[inline]
end(&mut self) -> *mut T86 fn end(&mut self) -> *mut T {
87 unsafe {
88 if mem::size_of::<T>() == 0 {
89 // A pointer as large as possible for zero-sized elements.
90 !0 as *mut T
91 } else {
92 self.start().add(self.storage.len())
93 }
94 }
95 }
96 }
97
98 // The arenas start with PAGE-sized chunks, and then each new chunk is twice as
99 // big as its predecessor, up until we reach HUGE_PAGE-sized chunks, whereupon
100 // we stop growing. This scales well, from arenas that are barely used up to
101 // arenas that are used for 100s of MiBs. Note also that the chosen sizes match
102 // the usual sizes of pages and huge pages on Linux.
103 const PAGE: usize = 4096;
104 const HUGE_PAGE: usize = 2 * 1024 * 1024;
105
106 impl<T> Default for TypedArena<T> {
107 /// Creates a new `TypedArena`.
default() -> TypedArena<T>108 fn default() -> TypedArena<T> {
109 TypedArena {
110 // We set both `ptr` and `end` to 0 so that the first call to
111 // alloc() will trigger a grow().
112 ptr: Cell::new(ptr::null_mut()),
113 end: Cell::new(ptr::null_mut()),
114 chunks: Default::default(),
115 _own: PhantomData,
116 }
117 }
118 }
119
120 trait IterExt<T> {
alloc_from_iter(self, arena: &TypedArena<T>) -> &mut [T]121 fn alloc_from_iter(self, arena: &TypedArena<T>) -> &mut [T];
122 }
123
124 impl<I, T> IterExt<T> for I
125 where
126 I: IntoIterator<Item = T>,
127 {
128 #[inline]
alloc_from_iter(self, arena: &TypedArena<T>) -> &mut [T]129 default fn alloc_from_iter(self, arena: &TypedArena<T>) -> &mut [T] {
130 let vec: SmallVec<[_; 8]> = self.into_iter().collect();
131 vec.alloc_from_iter(arena)
132 }
133 }
134
135 impl<T, const N: usize> IterExt<T> for std::array::IntoIter<T, N> {
136 #[inline]
alloc_from_iter(self, arena: &TypedArena<T>) -> &mut [T]137 fn alloc_from_iter(self, arena: &TypedArena<T>) -> &mut [T] {
138 let len = self.len();
139 if len == 0 {
140 return &mut [];
141 }
142 // Move the content to the arena by copying and then forgetting it
143 unsafe {
144 let start_ptr = arena.alloc_raw_slice(len);
145 self.as_slice().as_ptr().copy_to_nonoverlapping(start_ptr, len);
146 mem::forget(self);
147 slice::from_raw_parts_mut(start_ptr, len)
148 }
149 }
150 }
151
152 impl<T> IterExt<T> for Vec<T> {
153 #[inline]
alloc_from_iter(mut self, arena: &TypedArena<T>) -> &mut [T]154 fn alloc_from_iter(mut self, arena: &TypedArena<T>) -> &mut [T] {
155 let len = self.len();
156 if len == 0 {
157 return &mut [];
158 }
159 // Move the content to the arena by copying and then forgetting it
160 unsafe {
161 let start_ptr = arena.alloc_raw_slice(len);
162 self.as_ptr().copy_to_nonoverlapping(start_ptr, len);
163 self.set_len(0);
164 slice::from_raw_parts_mut(start_ptr, len)
165 }
166 }
167 }
168
169 impl<A: smallvec::Array> IterExt<A::Item> for SmallVec<A> {
170 #[inline]
alloc_from_iter(mut self, arena: &TypedArena<A::Item>) -> &mut [A::Item]171 fn alloc_from_iter(mut self, arena: &TypedArena<A::Item>) -> &mut [A::Item] {
172 let len = self.len();
173 if len == 0 {
174 return &mut [];
175 }
176 // Move the content to the arena by copying and then forgetting it
177 unsafe {
178 let start_ptr = arena.alloc_raw_slice(len);
179 self.as_ptr().copy_to_nonoverlapping(start_ptr, len);
180 self.set_len(0);
181 slice::from_raw_parts_mut(start_ptr, len)
182 }
183 }
184 }
185
186 impl<T> TypedArena<T> {
187 /// Allocates an object in the `TypedArena`, returning a reference to it.
188 #[inline]
alloc(&self, object: T) -> &mut T189 pub fn alloc(&self, object: T) -> &mut T {
190 if self.ptr == self.end {
191 self.grow(1)
192 }
193
194 unsafe {
195 if mem::size_of::<T>() == 0 {
196 self.ptr.set((self.ptr.get() as *mut u8).wrapping_offset(1) as *mut T);
197 let ptr = mem::align_of::<T>() as *mut T;
198 // Don't drop the object. This `write` is equivalent to `forget`.
199 ptr::write(ptr, object);
200 &mut *ptr
201 } else {
202 let ptr = self.ptr.get();
203 // Advance the pointer.
204 self.ptr.set(self.ptr.get().offset(1));
205 // Write into uninitialized memory.
206 ptr::write(ptr, object);
207 &mut *ptr
208 }
209 }
210 }
211
212 #[inline]
can_allocate(&self, additional: usize) -> bool213 fn can_allocate(&self, additional: usize) -> bool {
214 let available_bytes = self.end.get() as usize - self.ptr.get() as usize;
215 let additional_bytes = additional.checked_mul(mem::size_of::<T>()).unwrap();
216 available_bytes >= additional_bytes
217 }
218
219 /// Ensures there's enough space in the current chunk to fit `len` objects.
220 #[inline]
ensure_capacity(&self, additional: usize)221 fn ensure_capacity(&self, additional: usize) {
222 if !self.can_allocate(additional) {
223 self.grow(additional);
224 debug_assert!(self.can_allocate(additional));
225 }
226 }
227
228 #[inline]
alloc_raw_slice(&self, len: usize) -> *mut T229 unsafe fn alloc_raw_slice(&self, len: usize) -> *mut T {
230 assert!(mem::size_of::<T>() != 0);
231 assert!(len != 0);
232
233 self.ensure_capacity(len);
234
235 let start_ptr = self.ptr.get();
236 self.ptr.set(start_ptr.add(len));
237 start_ptr
238 }
239
240 #[inline]
alloc_from_iter<I: IntoIterator<Item = T>>(&self, iter: I) -> &mut [T]241 pub fn alloc_from_iter<I: IntoIterator<Item = T>>(&self, iter: I) -> &mut [T] {
242 assert!(mem::size_of::<T>() != 0);
243 iter.alloc_from_iter(self)
244 }
245
246 /// Grows the arena.
247 #[inline(never)]
248 #[cold]
grow(&self, additional: usize)249 fn grow(&self, additional: usize) {
250 unsafe {
251 // We need the element size to convert chunk sizes (ranging from
252 // PAGE to HUGE_PAGE bytes) to element counts.
253 let elem_size = cmp::max(1, mem::size_of::<T>());
254 let mut chunks = self.chunks.borrow_mut();
255 let mut new_cap;
256 if let Some(last_chunk) = chunks.last_mut() {
257 // If a type is `!needs_drop`, we don't need to keep track of how many elements
258 // the chunk stores - the field will be ignored anyway.
259 if mem::needs_drop::<T>() {
260 let used_bytes = self.ptr.get() as usize - last_chunk.start() as usize;
261 last_chunk.entries = used_bytes / mem::size_of::<T>();
262 }
263
264 // If the previous chunk's len is less than HUGE_PAGE
265 // bytes, then this chunk will be least double the previous
266 // chunk's size.
267 new_cap = last_chunk.storage.len().min(HUGE_PAGE / elem_size / 2);
268 new_cap *= 2;
269 } else {
270 new_cap = PAGE / elem_size;
271 }
272 // Also ensure that this chunk can fit `additional`.
273 new_cap = cmp::max(additional, new_cap);
274
275 let mut chunk = TypedArenaChunk::<T>::new(new_cap);
276 self.ptr.set(chunk.start());
277 self.end.set(chunk.end());
278 chunks.push(chunk);
279 }
280 }
281
282 // Drops the contents of the last chunk. The last chunk is partially empty, unlike all other
283 // chunks.
clear_last_chunk(&self, last_chunk: &mut TypedArenaChunk<T>)284 fn clear_last_chunk(&self, last_chunk: &mut TypedArenaChunk<T>) {
285 // Determine how much was filled.
286 let start = last_chunk.start() as usize;
287 // We obtain the value of the pointer to the first uninitialized element.
288 let end = self.ptr.get() as usize;
289 // We then calculate the number of elements to be dropped in the last chunk,
290 // which is the filled area's length.
291 let diff = if mem::size_of::<T>() == 0 {
292 // `T` is ZST. It can't have a drop flag, so the value here doesn't matter. We get
293 // the number of zero-sized values in the last and only chunk, just out of caution.
294 // Recall that `end` was incremented for each allocated value.
295 end - start
296 } else {
297 (end - start) / mem::size_of::<T>()
298 };
299 // Pass that to the `destroy` method.
300 unsafe {
301 last_chunk.destroy(diff);
302 }
303 // Reset the chunk.
304 self.ptr.set(last_chunk.start());
305 }
306 }
307
308 unsafe impl<#[may_dangle] T> Drop for TypedArena<T> {
drop(&mut self)309 fn drop(&mut self) {
310 unsafe {
311 // Determine how much was filled.
312 let mut chunks_borrow = self.chunks.borrow_mut();
313 if let Some(mut last_chunk) = chunks_borrow.pop() {
314 // Drop the contents of the last chunk.
315 self.clear_last_chunk(&mut last_chunk);
316 // The last chunk will be dropped. Destroy all other chunks.
317 for chunk in chunks_borrow.iter_mut() {
318 chunk.destroy(chunk.entries);
319 }
320 }
321 // Box handles deallocation of `last_chunk` and `self.chunks`.
322 }
323 }
324 }
325
326 unsafe impl<T: Send> Send for TypedArena<T> {}
327
328 /// An arena that can hold objects of multiple different types that impl `Copy`
329 /// and/or satisfy `!mem::needs_drop`.
330 pub struct DroplessArena {
331 /// A pointer to the start of the free space.
332 start: Cell<*mut u8>,
333
334 /// A pointer to the end of free space.
335 ///
336 /// The allocation proceeds downwards from the end of the chunk towards the
337 /// start. (This is slightly simpler and faster than allocating upwards,
338 /// see <https://fitzgeraldnick.com/2019/11/01/always-bump-downwards.html>.)
339 /// When this pointer crosses the start pointer, a new chunk is allocated.
340 end: Cell<*mut u8>,
341
342 /// A vector of arena chunks.
343 chunks: RefCell<Vec<TypedArenaChunk<u8>>>,
344 }
345
346 unsafe impl Send for DroplessArena {}
347
348 impl Default for DroplessArena {
349 #[inline]
default() -> DroplessArena350 fn default() -> DroplessArena {
351 DroplessArena {
352 start: Cell::new(ptr::null_mut()),
353 end: Cell::new(ptr::null_mut()),
354 chunks: Default::default(),
355 }
356 }
357 }
358
359 impl DroplessArena {
360 #[inline(never)]
361 #[cold]
grow(&self, additional: usize)362 fn grow(&self, additional: usize) {
363 unsafe {
364 let mut chunks = self.chunks.borrow_mut();
365 let mut new_cap;
366 if let Some(last_chunk) = chunks.last_mut() {
367 // There is no need to update `last_chunk.entries` because that
368 // field isn't used by `DroplessArena`.
369
370 // If the previous chunk's len is less than HUGE_PAGE
371 // bytes, then this chunk will be least double the previous
372 // chunk's size.
373 new_cap = last_chunk.storage.len().min(HUGE_PAGE / 2);
374 new_cap *= 2;
375 } else {
376 new_cap = PAGE;
377 }
378 // Also ensure that this chunk can fit `additional`.
379 new_cap = cmp::max(additional, new_cap);
380
381 let mut chunk = TypedArenaChunk::<u8>::new(new_cap);
382 self.start.set(chunk.start());
383 self.end.set(chunk.end());
384 chunks.push(chunk);
385 }
386 }
387
388 /// Allocates a byte slice with specified layout from the current memory
389 /// chunk. Returns `None` if there is no free space left to satisfy the
390 /// request.
391 #[inline]
alloc_raw_without_grow(&self, layout: Layout) -> Option<*mut u8>392 fn alloc_raw_without_grow(&self, layout: Layout) -> Option<*mut u8> {
393 let start = self.start.get() as usize;
394 let end = self.end.get() as usize;
395
396 let align = layout.align();
397 let bytes = layout.size();
398
399 let new_end = end.checked_sub(bytes)? & !(align - 1);
400 if start <= new_end {
401 let new_end = new_end as *mut u8;
402 self.end.set(new_end);
403 Some(new_end)
404 } else {
405 None
406 }
407 }
408
409 #[inline]
alloc_raw(&self, layout: Layout) -> *mut u8410 pub fn alloc_raw(&self, layout: Layout) -> *mut u8 {
411 assert!(layout.size() != 0);
412 loop {
413 if let Some(a) = self.alloc_raw_without_grow(layout) {
414 break a;
415 }
416 // No free space left. Allocate a new chunk to satisfy the request.
417 // On failure the grow will panic or abort.
418 self.grow(layout.size());
419 }
420 }
421
422 #[inline]
alloc<T>(&self, object: T) -> &mut T423 pub fn alloc<T>(&self, object: T) -> &mut T {
424 assert!(!mem::needs_drop::<T>());
425
426 let mem = self.alloc_raw(Layout::for_value::<T>(&object)) as *mut T;
427
428 unsafe {
429 // Write into uninitialized memory.
430 ptr::write(mem, object);
431 &mut *mem
432 }
433 }
434
435 /// Allocates a slice of objects that are copied into the `DroplessArena`, returning a mutable
436 /// reference to it. Will panic if passed a zero-sized type.
437 ///
438 /// Panics:
439 ///
440 /// - Zero-sized types
441 /// - Zero-length slices
442 #[inline]
alloc_slice<T>(&self, slice: &[T]) -> &mut [T] where T: Copy,443 pub fn alloc_slice<T>(&self, slice: &[T]) -> &mut [T]
444 where
445 T: Copy,
446 {
447 assert!(!mem::needs_drop::<T>());
448 assert!(mem::size_of::<T>() != 0);
449 assert!(!slice.is_empty());
450
451 let mem = self.alloc_raw(Layout::for_value::<[T]>(slice)) as *mut T;
452
453 unsafe {
454 mem.copy_from_nonoverlapping(slice.as_ptr(), slice.len());
455 slice::from_raw_parts_mut(mem, slice.len())
456 }
457 }
458
459 #[inline]
write_from_iter<T, I: Iterator<Item = T>>( &self, mut iter: I, len: usize, mem: *mut T, ) -> &mut [T]460 unsafe fn write_from_iter<T, I: Iterator<Item = T>>(
461 &self,
462 mut iter: I,
463 len: usize,
464 mem: *mut T,
465 ) -> &mut [T] {
466 let mut i = 0;
467 // Use a manual loop since LLVM manages to optimize it better for
468 // slice iterators
469 loop {
470 let value = iter.next();
471 if i >= len || value.is_none() {
472 // We only return as many items as the iterator gave us, even
473 // though it was supposed to give us `len`
474 return slice::from_raw_parts_mut(mem, i);
475 }
476 ptr::write(mem.add(i), value.unwrap());
477 i += 1;
478 }
479 }
480
481 #[inline]
alloc_from_iter<T, I: IntoIterator<Item = T>>(&self, iter: I) -> &mut [T]482 pub fn alloc_from_iter<T, I: IntoIterator<Item = T>>(&self, iter: I) -> &mut [T] {
483 let iter = iter.into_iter();
484 assert!(mem::size_of::<T>() != 0);
485 assert!(!mem::needs_drop::<T>());
486
487 let size_hint = iter.size_hint();
488
489 match size_hint {
490 (min, Some(max)) if min == max => {
491 // We know the exact number of elements the iterator will produce here
492 let len = min;
493
494 if len == 0 {
495 return &mut [];
496 }
497
498 let mem = self.alloc_raw(Layout::array::<T>(len).unwrap()) as *mut T;
499 unsafe { self.write_from_iter(iter, len, mem) }
500 }
501 (_, _) => {
502 cold_path(move || -> &mut [T] {
503 let mut vec: SmallVec<[_; 8]> = iter.collect();
504 if vec.is_empty() {
505 return &mut [];
506 }
507 // Move the content to the arena by copying it and then forgetting
508 // the content of the SmallVec
509 unsafe {
510 let len = vec.len();
511 let start_ptr =
512 self.alloc_raw(Layout::for_value::<[T]>(vec.as_slice())) as *mut T;
513 vec.as_ptr().copy_to_nonoverlapping(start_ptr, len);
514 vec.set_len(0);
515 slice::from_raw_parts_mut(start_ptr, len)
516 }
517 })
518 }
519 }
520 }
521 }
522
523 // Declare an `Arena` containing one dropless arena and many typed arenas (the
524 // types of the typed arenas are specified by the arguments). The dropless
525 // arena will be used for any types that impl `Copy`, and also for any of the
526 // specified types that satisfy `!mem::needs_drop`.
527 #[rustc_macro_transparency = "semitransparent"]
528 pub macro declare_arena([$($a:tt $name:ident: $ty:ty,)*]) {
529 #[derive(Default)]
530 pub struct Arena<'tcx> {
531 pub dropless: $crate::DroplessArena,
532 $($name: $crate::TypedArena<$ty>,)*
533 }
534
535 pub trait ArenaAllocatable<'tcx, T = Self>: Sized {
allocate_on<'a>(self, arena: &'a Arena<'tcx>) -> &'a mut Self536 fn allocate_on<'a>(self, arena: &'a Arena<'tcx>) -> &'a mut Self;
allocate_from_iter<'a>( arena: &'a Arena<'tcx>, iter: impl ::std::iter::IntoIterator<Item = Self>, ) -> &'a mut [Self]537 fn allocate_from_iter<'a>(
538 arena: &'a Arena<'tcx>,
539 iter: impl ::std::iter::IntoIterator<Item = Self>,
540 ) -> &'a mut [Self];
541 }
542
543 // Any type that impls `Copy` can be arena-allocated in the `DroplessArena`.
544 impl<'tcx, T: Copy> ArenaAllocatable<'tcx, ()> for T {
545 #[inline]
allocate_on<'a>(self, arena: &'a Arena<'tcx>) -> &'a mut Self546 fn allocate_on<'a>(self, arena: &'a Arena<'tcx>) -> &'a mut Self {
547 arena.dropless.alloc(self)
548 }
549 #[inline]
allocate_from_iter<'a>( arena: &'a Arena<'tcx>, iter: impl ::std::iter::IntoIterator<Item = Self>, ) -> &'a mut [Self]550 fn allocate_from_iter<'a>(
551 arena: &'a Arena<'tcx>,
552 iter: impl ::std::iter::IntoIterator<Item = Self>,
553 ) -> &'a mut [Self] {
554 arena.dropless.alloc_from_iter(iter)
555 }
556 }
557 $(
558 impl<'tcx> ArenaAllocatable<'tcx, $ty> for $ty {
559 #[inline]
allocate_on<'a>(self, arena: &'a Arena<'tcx>) -> &'a mut Self560 fn allocate_on<'a>(self, arena: &'a Arena<'tcx>) -> &'a mut Self {
561 if !::std::mem::needs_drop::<Self>() {
562 arena.dropless.alloc(self)
563 } else {
564 arena.$name.alloc(self)
565 }
566 }
567
568 #[inline]
allocate_from_iter<'a>( arena: &'a Arena<'tcx>, iter: impl ::std::iter::IntoIterator<Item = Self>, ) -> &'a mut [Self]569 fn allocate_from_iter<'a>(
570 arena: &'a Arena<'tcx>,
571 iter: impl ::std::iter::IntoIterator<Item = Self>,
572 ) -> &'a mut [Self] {
573 if !::std::mem::needs_drop::<Self>() {
574 arena.dropless.alloc_from_iter(iter)
575 } else {
576 arena.$name.alloc_from_iter(iter)
577 }
578 }
579 }
580 )*
581
582 impl<'tcx> Arena<'tcx> {
583 #[inline]
alloc<T: ArenaAllocatable<'tcx, U>, U>(&self, value: T) -> &mut T584 pub fn alloc<T: ArenaAllocatable<'tcx, U>, U>(&self, value: T) -> &mut T {
585 value.allocate_on(self)
586 }
587
588 // Any type that impls `Copy` can have slices be arena-allocated in the `DroplessArena`.
589 #[inline]
alloc_slice<T: ::std::marker::Copy>(&self, value: &[T]) -> &mut [T]590 pub fn alloc_slice<T: ::std::marker::Copy>(&self, value: &[T]) -> &mut [T] {
591 if value.is_empty() {
592 return &mut [];
593 }
594 self.dropless.alloc_slice(value)
595 }
596
alloc_from_iter<'a, T: ArenaAllocatable<'tcx, U>, U>( &'a self, iter: impl ::std::iter::IntoIterator<Item = T>, ) -> &'a mut [T]597 pub fn alloc_from_iter<'a, T: ArenaAllocatable<'tcx, U>, U>(
598 &'a self,
599 iter: impl ::std::iter::IntoIterator<Item = T>,
600 ) -> &'a mut [T] {
601 T::allocate_from_iter(self, iter)
602 }
603 }
604 }
605
606 #[cfg(test)]
607 mod tests;
608