1 use arena::{TypedArena, DroplessArena};
5 use std::cell::RefCell;
6 use std::marker::PhantomData;
7 use smallvec::SmallVec;
9 /// This declares a list of types which can be allocated by `Arena`.
11 /// The `few` modifier will cause allocation to use the shared arena and recording the destructor.
12 /// This is faster and more memory efficient if there's only a few allocations of the type.
13 /// Leaving `few` out will cause the type to get its own dedicated `TypedArena` which is
14 /// faster and more memory efficient if there is lots of allocations.
16 /// Specifying the `decode` modifier will add decode impls for &T and &[T] where T is the type
17 /// listed. These impls will appear in the implement_ty_decoder! macro.
19 macro_rules! arena_types {
20 ($macro:path, $args:tt, $tcx:lifetime) => (
22 [] layouts: rustc::ty::layout::LayoutDetails,
23 [] generics: rustc::ty::Generics,
24 [] trait_def: rustc::ty::TraitDef,
25 [] adt_def: rustc::ty::AdtDef,
26 [] steal_mir: rustc::ty::steal::Steal<rustc::mir::Body<$tcx>>,
27 [] mir: rustc::mir::Body<$tcx>,
28 [] tables: rustc::ty::TypeckTables<$tcx>,
29 [] const_allocs: rustc::mir::interpret::Allocation,
30 [] vtable_method: Option<(
31 rustc::hir::def_id::DefId,
32 rustc::ty::subst::SubstsRef<$tcx>
34 [few, decode] mir_keys: rustc::util::nodemap::DefIdSet,
35 [decode] specialization_graph: rustc::traits::specialization_graph::Graph,
36 [] region_scope_tree: rustc::middle::region::ScopeTree,
37 [] item_local_set: rustc::util::nodemap::ItemLocalSet,
38 [decode] mir_const_qualif: rustc_data_structures::bit_set::BitSet<rustc::mir::Local>,
39 [] trait_impls_of: rustc::ty::trait_def::TraitImpls,
41 rustc::infer::canonical::Canonical<'tcx,
42 rustc::infer::canonical::QueryResponse<'tcx,
43 rustc::traits::query::dropck_outlives::DropckOutlivesResult<'tcx>
46 [] normalize_projection_ty:
47 rustc::infer::canonical::Canonical<'tcx,
48 rustc::infer::canonical::QueryResponse<'tcx,
49 rustc::traits::query::normalize::NormalizationResult<'tcx>
52 [] implied_outlives_bounds:
53 rustc::infer::canonical::Canonical<'tcx,
54 rustc::infer::canonical::QueryResponse<'tcx,
55 Vec<rustc::traits::query::outlives_bounds::OutlivesBound<'tcx>>
59 rustc::infer::canonical::Canonical<'tcx,
60 rustc::infer::canonical::QueryResponse<'tcx, ()>
62 [] type_op_normalize_poly_fn_sig:
63 rustc::infer::canonical::Canonical<'tcx,
64 rustc::infer::canonical::QueryResponse<'tcx, rustc::ty::PolyFnSig<'tcx>>
66 [] type_op_normalize_fn_sig:
67 rustc::infer::canonical::Canonical<'tcx,
68 rustc::infer::canonical::QueryResponse<'tcx, rustc::ty::FnSig<'tcx>>
70 [] type_op_normalize_predicate:
71 rustc::infer::canonical::Canonical<'tcx,
72 rustc::infer::canonical::QueryResponse<'tcx, rustc::ty::Predicate<'tcx>>
74 [] type_op_normalize_ty:
75 rustc::infer::canonical::Canonical<'tcx,
76 rustc::infer::canonical::QueryResponse<'tcx, rustc::ty::Ty<'tcx>>
78 [few] crate_inherent_impls: rustc::ty::CrateInherentImpls,
79 [decode] borrowck: rustc::middle::borrowck::BorrowCheckResult,
80 [few] upstream_monomorphizations:
81 rustc::util::nodemap::DefIdMap<
82 rustc_data_structures::fx::FxHashMap<
83 rustc::ty::subst::SubstsRef<'tcx>,
84 rustc::hir::def_id::CrateNum
87 [few] resolve_lifetimes: rustc::middle::resolve_lifetime::ResolveLifetimes,
88 [decode] generic_predicates: rustc::ty::GenericPredicates<'tcx>,
89 [few] lint_levels: rustc::lint::LintLevelMap,
90 [few] stability_index: rustc::middle::stability::Index<'tcx>,
91 [few] features: syntax::feature_gate::Features,
92 [few] all_traits: Vec<rustc::hir::def_id::DefId>,
93 [few] privacy_access_levels: rustc::middle::privacy::AccessLevels,
94 [few] target_features_whitelist: rustc_data_structures::fx::FxHashMap<
96 Option<syntax::symbol::Symbol>
98 [few] wasm_import_module_map: rustc_data_structures::fx::FxHashMap<
99 rustc::hir::def_id::DefId,
102 [few] get_lib_features: rustc::middle::lib_features::LibFeatures,
103 [few] defined_lib_features: rustc::middle::lang_items::LanguageItems,
104 [few] visible_parent_map: rustc::util::nodemap::DefIdMap<rustc::hir::def_id::DefId>,
105 [few] foreign_module: rustc::middle::cstore::ForeignModule,
106 [few] foreign_modules: Vec<rustc::middle::cstore::ForeignModule>,
107 [few] reachable_non_generics: rustc::util::nodemap::DefIdMap<
108 rustc::middle::exported_symbols::SymbolExportLevel
110 [few] crate_variances: rustc::ty::CrateVariancesMap<'tcx>,
111 [few] inferred_outlives_crate: rustc::ty::CratePredicatesMap<'tcx>,
112 [] upvars: rustc_data_structures::fx::FxIndexMap<rustc::hir::HirId, rustc::hir::Upvar>,
117 macro_rules! arena_for_type {
121 ([few $(, $attrs:ident)*][$ty:ty]) => {
124 ([$ignore:ident $(, $attrs:ident)*]$args:tt) => {
125 arena_for_type!([$($attrs),*]$args)
129 macro_rules! declare_arena {
130 ([], [$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) => {
132 pub struct Arena<$tcx> {
133 dropless: DroplessArena,
135 $($name: arena_for_type!($a[$ty]),)*
140 macro_rules! which_arena_for_type {
141 ([][$arena:expr]) => {
144 ([few$(, $attrs:ident)*][$arena:expr]) => {
147 ([$ignore:ident$(, $attrs:ident)*]$args:tt) => {
148 which_arena_for_type!([$($attrs),*]$args)
152 macro_rules! impl_arena_allocatable {
153 ([], [$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) => {
155 impl ArenaAllocatable for $ty {}
156 unsafe impl<$tcx> ArenaField<$tcx> for $ty {
158 fn arena<'a>(_arena: &'a Arena<$tcx>) -> Option<&'a TypedArena<Self>> {
159 which_arena_for_type!($a[&_arena.$name])
166 arena_types!(declare_arena, [], 'tcx);
168 arena_types!(impl_arena_allocatable, [], 'tcx);
170 pub trait ArenaAllocatable {}
172 impl<T: Copy> ArenaAllocatable for T {}
174 unsafe trait ArenaField<'tcx>: Sized {
175 /// Returns a specific arena to allocate from.
176 /// If None is returned, the DropArena will be used.
177 fn arena<'a>(arena: &'a Arena<'tcx>) -> Option<&'a TypedArena<Self>>;
180 unsafe impl<'tcx, T> ArenaField<'tcx> for T {
182 default fn arena<'a>(_: &'a Arena<'tcx>) -> Option<&'a TypedArena<Self>> {
187 impl<'tcx> Arena<'tcx> {
189 pub fn alloc<T: ArenaAllocatable>(&self, value: T) -> &mut T {
190 if !mem::needs_drop::<T>() {
191 return self.dropless.alloc(value);
193 match <T as ArenaField<'tcx>>::arena(self) {
194 Some(arena) => arena.alloc(value),
195 None => unsafe { self.drop.alloc(value) },
200 pub fn alloc_slice<T: Copy>(&self, value: &[T]) -> &mut [T] {
201 if value.len() == 0 {
204 self.dropless.alloc_slice(value)
207 pub fn alloc_from_iter<
209 I: IntoIterator<Item = T>
214 if !mem::needs_drop::<T>() {
215 return self.dropless.alloc_from_iter(iter);
217 match <T as ArenaField<'tcx>>::arena(self) {
218 Some(arena) => arena.alloc_from_iter(iter),
219 None => unsafe { self.drop.alloc_from_iter(iter) },
224 /// Calls the destructor for an object when dropped.
226 drop_fn: unsafe fn(*mut u8),
230 unsafe fn drop_for_type<T>(to_drop: *mut u8) {
231 std::ptr::drop_in_place(to_drop as *mut T)
234 impl Drop for DropType {
237 (self.drop_fn)(self.obj)
242 /// An arena which can be used to allocate any type.
243 /// Allocating in this arena is unsafe since the type system
244 /// doesn't know which types it contains. In order to
245 /// allocate safely, you must store a PhantomData<T>
246 /// alongside this arena for each type T you allocate.
249 /// A list of destructors to run when the arena drops.
250 /// Ordered so `destructors` gets dropped before the arena
251 /// since its destructor can reference memory in the arena.
252 destructors: RefCell<Vec<DropType>>,
253 arena: DroplessArena,
258 unsafe fn alloc<T>(&self, object: T) -> &mut T {
259 let mem = self.arena.alloc_raw(
262 ) as *mut _ as *mut T;
263 // Write into uninitialized memory.
264 ptr::write(mem, object);
265 let result = &mut *mem;
266 // Record the destructor after doing the allocation as that may panic
267 // and would cause `object`'s destuctor to run twice if it was recorded before
268 self.destructors.borrow_mut().push(DropType {
269 drop_fn: drop_for_type::<T>,
270 obj: result as *mut T as *mut u8,
276 unsafe fn alloc_from_iter<T, I: IntoIterator<Item = T>>(&self, iter: I) -> &mut [T] {
277 let mut vec: SmallVec<[_; 8]> = iter.into_iter().collect();
283 let start_ptr = self.arena.alloc_raw(
284 len.checked_mul(mem::size_of::<T>()).unwrap(),
286 ) as *mut _ as *mut T;
288 let mut destructors = self.destructors.borrow_mut();
289 // Reserve space for the destructors so we can't panic while adding them
290 destructors.reserve(len);
292 // Move the content to the arena by copying it and then forgetting
293 // the content of the SmallVec
294 vec.as_ptr().copy_to_nonoverlapping(start_ptr, len);
295 mem::forget(vec.drain());
297 // Record the destructors after doing the allocation as that may panic
298 // and would cause `object`'s destuctor to run twice if it was recorded before
300 destructors.push(DropType {
301 drop_fn: drop_for_type::<T>,
302 obj: start_ptr.offset(i as isize) as *mut u8,
306 slice::from_raw_parts_mut(start_ptr, len)