1 use arena::{TypedArena, DroplessArena};
5 use std::cell::RefCell;
6 use std::marker::PhantomData;
7 use smallvec::SmallVec;
10 macro_rules! arena_types {
11 ($macro:path, $args:tt, $tcx:lifetime) => (
13 [] vtable_method: Option<(
14 rustc::hir::def_id::DefId,
15 rustc::ty::subst::SubstsRef<$tcx>
17 [few] mir_keys: rustc::util::nodemap::DefIdSet,
18 [decode] specialization_graph: rustc::traits::specialization_graph::Graph,
23 macro_rules! arena_for_type {
27 ([few $(, $attrs:ident)*][$ty:ty]) => {
30 ([$ignore:ident $(, $attrs:ident)*]$args:tt) => {
31 arena_for_type!([$($attrs),*]$args)
35 macro_rules! declare_arena {
36 ([], [$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) => {
38 pub struct Arena<$tcx> {
39 dropless: DroplessArena,
41 $($name: arena_for_type!($a[$ty]),)*
46 macro_rules! which_arena_for_type {
47 ([][$arena:expr]) => {
50 ([few$(, $attrs:ident)*][$arena:expr]) => {
53 ([$ignore:ident$(, $attrs:ident)*]$args:tt) => {
54 which_arena_for_type!([$($attrs),*]$args)
58 macro_rules! impl_arena_allocatable {
59 ([], [$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) => {
61 impl ArenaAllocatable for $ty {}
62 unsafe impl<$tcx> ArenaField<$tcx> for $ty {
64 fn arena<'a>(_arena: &'a Arena<$tcx>) -> Option<&'a TypedArena<Self>> {
65 which_arena_for_type!($a[&_arena.$name])
72 arena_types!(declare_arena, [], 'tcx);
74 arena_types!(impl_arena_allocatable, [], 'tcx);
76 pub trait ArenaAllocatable {}
78 impl<T: Copy> ArenaAllocatable for T {}
80 pub unsafe trait ArenaField<'tcx>: Sized {
81 /// Returns a specific arena to allocate from.
82 /// If None is returned, the DropArena will be used.
83 fn arena<'a>(arena: &'a Arena<'tcx>) -> Option<&'a TypedArena<Self>>;
86 unsafe impl<'tcx, T> ArenaField<'tcx> for T {
88 default fn arena<'a>(_: &'a Arena<'tcx>) -> Option<&'a TypedArena<Self>> {
93 impl<'tcx> Arena<'tcx> {
95 pub fn alloc<T: ArenaAllocatable>(&self, value: T) -> &mut T {
96 if !mem::needs_drop::<T>() {
97 return self.dropless.alloc(value);
99 match <T as ArenaField<'tcx>>::arena(self) {
100 Some(arena) => arena.alloc(value),
101 None => unsafe { self.drop.alloc(value) },
105 pub fn alloc_from_iter<
107 I: IntoIterator<Item = T>
112 if !mem::needs_drop::<T>() {
113 return self.dropless.alloc_from_iter(iter);
115 match <T as ArenaField<'tcx>>::arena(self) {
116 Some(arena) => arena.alloc_from_iter(iter),
117 None => unsafe { self.drop.alloc_from_iter(iter) },
122 /// Calls the destructor for an object when dropped.
124 drop_fn: unsafe fn(*mut u8),
128 unsafe fn drop_for_type<T>(to_drop: *mut u8) {
129 std::ptr::drop_in_place(to_drop as *mut T)
132 impl Drop for DropType {
135 (self.drop_fn)(self.obj)
140 /// An arena which can be used to allocate any type.
141 /// Allocating in this arena is unsafe since the type system
142 /// doesn't know which types it contains. In order to
143 /// allocate safely, you must store a PhantomData<T>
144 /// alongside this arena for each type T you allocate.
147 /// A list of destructors to run when the arena drops.
148 /// Ordered so `destructors` gets dropped before the arena
149 /// since its destructor can reference memory in the arena.
150 destructors: RefCell<Vec<DropType>>,
151 arena: DroplessArena,
156 unsafe fn alloc<T>(&self, object: T) -> &mut T {
157 let mem = self.arena.alloc_raw(
160 ) as *mut _ as *mut T;
161 // Write into uninitialized memory.
162 ptr::write(mem, object);
163 let result = &mut *mem;
164 // Record the destructor after doing the allocation as that may panic
165 // and would cause `object`'s destuctor to run twice if it was recorded before
166 self.destructors.borrow_mut().push(DropType {
167 drop_fn: drop_for_type::<T>,
168 obj: result as *mut T as *mut u8,
174 unsafe fn alloc_from_iter<T, I: IntoIterator<Item = T>>(&self, iter: I) -> &mut [T] {
175 let mut vec: SmallVec<[_; 8]> = iter.into_iter().collect();
181 let start_ptr = self.arena.alloc_raw(
182 len.checked_mul(mem::size_of::<T>()).unwrap(),
184 ) as *mut _ as *mut T;
186 let mut destructors = self.destructors.borrow_mut();
187 // Reserve space for the destructors so we can't panic while adding them
188 destructors.reserve(len);
190 // Move the content to the arena by copying it and then forgetting
191 // the content of the SmallVec
192 vec.as_ptr().copy_to_nonoverlapping(start_ptr, len);
193 mem::forget(vec.drain());
195 // Record the destructors after doing the allocation as that may panic
196 // and would cause `object`'s destuctor to run twice if it was recorded before
198 destructors.push(DropType {
199 drop_fn: drop_for_type::<T>,
200 obj: start_ptr.offset(i as isize) as *mut u8,
204 slice::from_raw_parts_mut(start_ptr, len)