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>,
116 macro_rules! arena_for_type {
120 ([few $(, $attrs:ident)*][$ty:ty]) => {
123 ([$ignore:ident $(, $attrs:ident)*]$args:tt) => {
124 arena_for_type!([$($attrs),*]$args)
128 macro_rules! declare_arena {
129 ([], [$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) => {
131 pub struct Arena<$tcx> {
132 dropless: DroplessArena,
134 $($name: arena_for_type!($a[$ty]),)*
139 macro_rules! which_arena_for_type {
140 ([][$arena:expr]) => {
143 ([few$(, $attrs:ident)*][$arena:expr]) => {
146 ([$ignore:ident$(, $attrs:ident)*]$args:tt) => {
147 which_arena_for_type!([$($attrs),*]$args)
151 macro_rules! impl_arena_allocatable {
152 ([], [$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) => {
154 impl ArenaAllocatable for $ty {}
155 unsafe impl<$tcx> ArenaField<$tcx> for $ty {
157 fn arena<'a>(_arena: &'a Arena<$tcx>) -> Option<&'a TypedArena<Self>> {
158 which_arena_for_type!($a[&_arena.$name])
165 arena_types!(declare_arena, [], 'tcx);
167 arena_types!(impl_arena_allocatable, [], 'tcx);
169 pub trait ArenaAllocatable {}
171 impl<T: Copy> ArenaAllocatable for T {}
173 unsafe trait ArenaField<'tcx>: Sized {
174 /// Returns a specific arena to allocate from.
175 /// If None is returned, the DropArena will be used.
176 fn arena<'a>(arena: &'a Arena<'tcx>) -> Option<&'a TypedArena<Self>>;
179 unsafe impl<'tcx, T> ArenaField<'tcx> for T {
181 default fn arena<'a>(_: &'a Arena<'tcx>) -> Option<&'a TypedArena<Self>> {
186 impl<'tcx> Arena<'tcx> {
188 pub fn alloc<T: ArenaAllocatable>(&self, value: T) -> &mut T {
189 if !mem::needs_drop::<T>() {
190 return self.dropless.alloc(value);
192 match <T as ArenaField<'tcx>>::arena(self) {
193 Some(arena) => arena.alloc(value),
194 None => unsafe { self.drop.alloc(value) },
199 pub fn alloc_slice<T: Copy>(&self, value: &[T]) -> &mut [T] {
200 if value.len() == 0 {
203 self.dropless.alloc_slice(value)
206 pub fn alloc_from_iter<
208 I: IntoIterator<Item = T>
213 if !mem::needs_drop::<T>() {
214 return self.dropless.alloc_from_iter(iter);
216 match <T as ArenaField<'tcx>>::arena(self) {
217 Some(arena) => arena.alloc_from_iter(iter),
218 None => unsafe { self.drop.alloc_from_iter(iter) },
223 /// Calls the destructor for an object when dropped.
225 drop_fn: unsafe fn(*mut u8),
229 unsafe fn drop_for_type<T>(to_drop: *mut u8) {
230 std::ptr::drop_in_place(to_drop as *mut T)
233 impl Drop for DropType {
236 (self.drop_fn)(self.obj)
241 /// An arena which can be used to allocate any type.
242 /// Allocating in this arena is unsafe since the type system
243 /// doesn't know which types it contains. In order to
244 /// allocate safely, you must store a PhantomData<T>
245 /// alongside this arena for each type T you allocate.
248 /// A list of destructors to run when the arena drops.
249 /// Ordered so `destructors` gets dropped before the arena
250 /// since its destructor can reference memory in the arena.
251 destructors: RefCell<Vec<DropType>>,
252 arena: DroplessArena,
257 unsafe fn alloc<T>(&self, object: T) -> &mut T {
258 let mem = self.arena.alloc_raw(
261 ) as *mut _ as *mut T;
262 // Write into uninitialized memory.
263 ptr::write(mem, object);
264 let result = &mut *mem;
265 // Record the destructor after doing the allocation as that may panic
266 // and would cause `object`'s destuctor to run twice if it was recorded before
267 self.destructors.borrow_mut().push(DropType {
268 drop_fn: drop_for_type::<T>,
269 obj: result as *mut T as *mut u8,
275 unsafe fn alloc_from_iter<T, I: IntoIterator<Item = T>>(&self, iter: I) -> &mut [T] {
276 let mut vec: SmallVec<[_; 8]> = iter.into_iter().collect();
282 let start_ptr = self.arena.alloc_raw(
283 len.checked_mul(mem::size_of::<T>()).unwrap(),
285 ) as *mut _ as *mut T;
287 let mut destructors = self.destructors.borrow_mut();
288 // Reserve space for the destructors so we can't panic while adding them
289 destructors.reserve(len);
291 // Move the content to the arena by copying it and then forgetting
292 // the content of the SmallVec
293 vec.as_ptr().copy_to_nonoverlapping(start_ptr, len);
294 mem::forget(vec.drain());
296 // Record the destructors after doing the allocation as that may panic
297 // and would cause `object`'s destuctor to run twice if it was recorded before
299 destructors.push(DropType {
300 drop_fn: drop_for_type::<T>,
301 obj: start_ptr.offset(i as isize) as *mut u8,
305 slice::from_raw_parts_mut(start_ptr, len)