1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! The arena, a fast but limited type of allocator.
13 //! Arenas are a type of allocator that destroy the objects within, all at
14 //! once, once the arena itself is destroyed. They do not support deallocation
15 //! of individual objects while the arena itself is still alive. The benefit
16 //! of an arena is very fast allocation; just a pointer bump.
18 //! This crate implements `TypedArena`, a simple arena that can only hold
19 //! objects of a single type.
21 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
22 html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
23 html_root_url = "https://doc.rust-lang.org/nightly/",
24 test(no_crate_inject, attr(deny(warnings))))]
28 #![feature(core_intrinsics)]
29 #![feature(dropck_eyepatch)]
30 #![feature(generic_param_attrs)]
31 #![feature(needs_drop)]
32 #![cfg_attr(test, feature(test))]
38 use std::cell::{Cell, RefCell};
41 use std::marker::{PhantomData, Send};
46 use alloc::raw_vec::RawVec;
48 /// An arena that can hold objects of only one type.
49 pub struct TypedArena<T> {
50 /// A pointer to the next object to be allocated.
53 /// A pointer to the end of the allocated area. When this pointer is
54 /// reached, a new chunk is allocated.
57 /// A vector of arena chunks.
58 chunks: RefCell<Vec<TypedArenaChunk<T>>>,
60 /// Marker indicating that dropping the arena causes its owned
61 /// instances of `T` to be dropped.
65 struct TypedArenaChunk<T> {
66 /// The raw storage for the arena chunk.
70 impl<T> TypedArenaChunk<T> {
72 unsafe fn new(capacity: usize) -> TypedArenaChunk<T> {
73 TypedArenaChunk { storage: RawVec::with_capacity(capacity) }
76 /// Destroys this arena chunk.
78 unsafe fn destroy(&mut self, len: usize) {
79 // The branch on needs_drop() is an -O1 performance optimization.
80 // Without the branch, dropping TypedArena<u8> takes linear time.
81 if mem::needs_drop::<T>() {
82 let mut start = self.start();
83 // Destroy all allocated objects.
85 ptr::drop_in_place(start);
86 start = start.offset(1);
91 // Returns a pointer to the first allocated object.
93 fn start(&self) -> *mut T {
97 // Returns a pointer to the end of the allocated space.
99 fn end(&self) -> *mut T {
101 if mem::size_of::<T>() == 0 {
102 // A pointer as large as possible for zero-sized elements.
105 self.start().offset(self.storage.cap() as isize)
111 const PAGE: usize = 4096;
113 impl<T> TypedArena<T> {
114 /// Creates a new `TypedArena`.
116 pub fn new() -> TypedArena<T> {
118 // We set both `ptr` and `end` to 0 so that the first call to
119 // alloc() will trigger a grow().
120 ptr: Cell::new(0 as *mut T),
121 end: Cell::new(0 as *mut T),
122 chunks: RefCell::new(vec![]),
127 /// Allocates an object in the `TypedArena`, returning a reference to it.
129 pub fn alloc(&self, object: T) -> &mut T {
130 if self.ptr == self.end {
135 if mem::size_of::<T>() == 0 {
136 self.ptr.set(intrinsics::arith_offset(self.ptr.get() as *mut u8, 1) as *mut T);
137 let ptr = mem::align_of::<T>() as *mut T;
138 // Don't drop the object. This `write` is equivalent to `forget`.
139 ptr::write(ptr, object);
142 let ptr = self.ptr.get();
143 // Advance the pointer.
144 self.ptr.set(self.ptr.get().offset(1));
145 // Write into uninitialized memory.
146 ptr::write(ptr, object);
152 /// Allocates a slice of objects that are copy into the `TypedArena`, returning a mutable
153 /// reference to it. Will panic if passed a zero-sized types.
156 /// - Zero-sized types
157 /// - Zero-length slices
159 pub fn alloc_slice(&self, slice: &[T]) -> &mut [T]
161 assert!(mem::size_of::<T>() != 0);
162 assert!(slice.len() != 0);
164 let available_capacity_bytes = self.end.get() as usize - self.ptr.get() as usize;
165 let at_least_bytes = slice.len() * mem::size_of::<T>();
166 if available_capacity_bytes < at_least_bytes {
167 self.grow(slice.len());
171 let start_ptr = self.ptr.get();
172 let arena_slice = slice::from_raw_parts_mut(start_ptr, slice.len());
173 self.ptr.set(start_ptr.offset(arena_slice.len() as isize));
174 arena_slice.copy_from_slice(slice);
182 fn grow(&self, n: usize) {
184 let mut chunks = self.chunks.borrow_mut();
185 let (chunk, mut new_capacity);
186 if let Some(last_chunk) = chunks.last_mut() {
187 let used_bytes = self.ptr.get() as usize - last_chunk.start() as usize;
188 let currently_used_cap = used_bytes / mem::size_of::<T>();
189 if last_chunk.storage.reserve_in_place(currently_used_cap, n) {
190 self.end.set(last_chunk.end());
193 new_capacity = last_chunk.storage.cap();
195 new_capacity = new_capacity.checked_mul(2).unwrap();
196 if new_capacity >= currently_used_cap + n {
202 let elem_size = cmp::max(1, mem::size_of::<T>());
203 new_capacity = cmp::max(n, PAGE / elem_size);
205 chunk = TypedArenaChunk::<T>::new(new_capacity);
206 self.ptr.set(chunk.start());
207 self.end.set(chunk.end());
212 /// Clears the arena. Deallocates all but the longest chunk which may be reused.
213 pub fn clear(&mut self) {
215 // Clear the last chunk, which is partially filled.
216 let mut chunks_borrow = self.chunks.borrow_mut();
217 if let Some(mut last_chunk) = chunks_borrow.pop() {
218 self.clear_last_chunk(&mut last_chunk);
219 // If `T` is ZST, code below has no effect.
220 for mut chunk in chunks_borrow.drain(..) {
221 let cap = chunk.storage.cap();
224 chunks_borrow.push(last_chunk);
229 // Drops the contents of the last chunk. The last chunk is partially empty, unlike all other
231 fn clear_last_chunk(&self, last_chunk: &mut TypedArenaChunk<T>) {
232 // Determine how much was filled.
233 let start = last_chunk.start() as usize;
234 // We obtain the value of the pointer to the first uninitialized element.
235 let end = self.ptr.get() as usize;
236 // We then calculate the number of elements to be dropped in the last chunk,
237 // which is the filled area's length.
238 let diff = if mem::size_of::<T>() == 0 {
239 // `T` is ZST. It can't have a drop flag, so the value here doesn't matter. We get
240 // the number of zero-sized values in the last and only chunk, just out of caution.
241 // Recall that `end` was incremented for each allocated value.
244 (end - start) / mem::size_of::<T>()
246 // Pass that to the `destroy` method.
248 last_chunk.destroy(diff);
251 self.ptr.set(last_chunk.start());
255 unsafe impl<#[may_dangle] T> Drop for TypedArena<T> {
258 // Determine how much was filled.
259 let mut chunks_borrow = self.chunks.borrow_mut();
260 if let Some(mut last_chunk) = chunks_borrow.pop() {
261 // Drop the contents of the last chunk.
262 self.clear_last_chunk(&mut last_chunk);
263 // The last chunk will be dropped. Destroy all other chunks.
264 for chunk in chunks_borrow.iter_mut() {
265 let cap = chunk.storage.cap();
269 // RawVec handles deallocation of `last_chunk` and `self.chunks`.
274 unsafe impl<T: Send> Send for TypedArena<T> {}
276 pub struct DroplessArena {
277 /// A pointer to the next object to be allocated.
280 /// A pointer to the end of the allocated area. When this pointer is
281 /// reached, a new chunk is allocated.
284 /// A vector of arena chunks.
285 chunks: RefCell<Vec<TypedArenaChunk<u8>>>,
289 pub fn new() -> DroplessArena {
291 ptr: Cell::new(0 as *mut u8),
292 end: Cell::new(0 as *mut u8),
293 chunks: RefCell::new(vec![]),
297 pub fn in_arena<T: ?Sized>(&self, ptr: *const T) -> bool {
298 let ptr = ptr as *const u8 as *mut u8;
299 for chunk in &*self.chunks.borrow() {
300 if chunk.start() <= ptr && ptr < chunk.end() {
308 fn align_for<T>(&self) {
309 let align = mem::align_of::<T>();
310 let final_address = ((self.ptr.get() as usize) + align - 1) & !(align - 1);
311 self.ptr.set(final_address as *mut u8);
312 assert!(self.ptr <= self.end);
317 fn grow<T>(&self, n: usize) {
318 let needed_bytes = n * mem::size_of::<T>();
320 let mut chunks = self.chunks.borrow_mut();
321 let (chunk, mut new_capacity);
322 if let Some(last_chunk) = chunks.last_mut() {
323 let used_bytes = self.ptr.get() as usize - last_chunk.start() as usize;
324 if last_chunk.storage.reserve_in_place(used_bytes, needed_bytes) {
325 self.end.set(last_chunk.end());
328 new_capacity = last_chunk.storage.cap();
330 new_capacity = new_capacity.checked_mul(2).unwrap();
331 if new_capacity >= used_bytes + needed_bytes {
337 new_capacity = cmp::max(needed_bytes, PAGE);
339 chunk = TypedArenaChunk::<u8>::new(new_capacity);
340 self.ptr.set(chunk.start());
341 self.end.set(chunk.end());
347 pub fn alloc<T>(&self, object: T) -> &mut T {
349 assert!(!mem::needs_drop::<T>());
350 assert!(mem::size_of::<T>() != 0);
352 self.align_for::<T>();
353 let future_end = intrinsics::arith_offset(self.ptr.get(), mem::size_of::<T>() as isize);
354 if (future_end as *mut u8) >= self.end.get() {
358 let ptr = self.ptr.get();
359 // Set the pointer past ourselves
360 self.ptr.set(intrinsics::arith_offset(
361 self.ptr.get(), mem::size_of::<T>() as isize
363 // Write into uninitialized memory.
364 ptr::write(ptr as *mut T, object);
365 &mut *(ptr as *mut T)
369 /// Allocates a slice of objects that are copied into the `DroplessArena`, returning a mutable
370 /// reference to it. Will panic if passed a zero-sized type.
373 /// - Zero-sized types
374 /// - Zero-length slices
376 pub fn alloc_slice<T>(&self, slice: &[T]) -> &mut [T]
378 assert!(!mem::needs_drop::<T>());
379 assert!(mem::size_of::<T>() != 0);
380 assert!(slice.len() != 0);
381 self.align_for::<T>();
383 let future_end = unsafe {
384 intrinsics::arith_offset(self.ptr.get(), (slice.len() * mem::size_of::<T>()) as isize)
386 if (future_end as *mut u8) >= self.end.get() {
387 self.grow::<T>(slice.len());
391 let arena_slice = slice::from_raw_parts_mut(self.ptr.get() as *mut T, slice.len());
392 self.ptr.set(intrinsics::arith_offset(
393 self.ptr.get(), (slice.len() * mem::size_of::<T>()) as isize
395 arena_slice.copy_from_slice(slice);
404 use self::test::Bencher;
405 use super::TypedArena;
409 #[derive(Debug, Eq, PartialEq)]
417 pub fn test_unused() {
418 let arena: TypedArena<Point> = TypedArena::new();
419 assert!(arena.chunks.borrow().is_empty());
423 fn test_arena_alloc_nested() {
435 struct Wrap<'a>(TypedArena<EI<'a>>);
438 fn alloc_inner<F: Fn() -> Inner>(&self, f: F) -> &Inner {
439 let r: &EI = self.0.alloc(EI::I(f()));
440 if let &EI::I(ref i) = r {
446 fn alloc_outer<F: Fn() -> Outer<'a>>(&self, f: F) -> &Outer {
447 let r: &EI = self.0.alloc(EI::O(f()));
448 if let &EI::O(ref o) = r {
456 let arena = Wrap(TypedArena::new());
459 arena.alloc_outer(|| Outer { inner: arena.alloc_inner(|| Inner { value: 10 }) });
461 assert_eq!(result.inner.value, 10);
466 let arena = TypedArena::new();
468 arena.alloc(Point { x: 1, y: 2, z: 3 });
473 pub fn bench_copy(b: &mut Bencher) {
474 let arena = TypedArena::new();
475 b.iter(|| arena.alloc(Point { x: 1, y: 2, z: 3 }))
479 pub fn bench_copy_nonarena(b: &mut Bencher) {
481 let _: Box<_> = Box::new(Point { x: 1, y: 2, z: 3 });
492 pub fn test_noncopy() {
493 let arena = TypedArena::new();
495 arena.alloc(Noncopy {
496 string: "hello world".to_string(),
497 array: vec![1, 2, 3, 4, 5],
503 pub fn test_typed_arena_zero_sized() {
504 let arena = TypedArena::new();
511 pub fn test_typed_arena_clear() {
512 let mut arena = TypedArena::new();
516 arena.alloc(Point { x: 1, y: 2, z: 3 });
523 struct DropCounter<'a> {
524 count: &'a Cell<u32>,
527 impl<'a> Drop for DropCounter<'a> {
529 self.count.set(self.count.get() + 1);
534 fn test_typed_arena_drop_count() {
535 let counter = Cell::new(0);
537 let arena: TypedArena<DropCounter> = TypedArena::new();
539 // Allocate something with drop glue to make sure it doesn't leak.
540 arena.alloc(DropCounter { count: &counter });
543 assert_eq!(counter.get(), 100);
547 fn test_typed_arena_drop_on_clear() {
548 let counter = Cell::new(0);
549 let mut arena: TypedArena<DropCounter> = TypedArena::new();
552 // Allocate something with drop glue to make sure it doesn't leak.
553 arena.alloc(DropCounter { count: &counter });
556 assert_eq!(counter.get(), i * 100 + 100);
561 static DROP_COUNTER: Cell<u32> = Cell::new(0)
564 struct SmallDroppable;
566 impl Drop for SmallDroppable {
568 DROP_COUNTER.with(|c| c.set(c.get() + 1));
573 fn test_typed_arena_drop_small_count() {
574 DROP_COUNTER.with(|c| c.set(0));
576 let arena: TypedArena<SmallDroppable> = TypedArena::new();
578 // Allocate something with drop glue to make sure it doesn't leak.
579 arena.alloc(SmallDroppable);
583 assert_eq!(DROP_COUNTER.with(|c| c.get()), 100);
587 pub fn bench_noncopy(b: &mut Bencher) {
588 let arena = TypedArena::new();
590 arena.alloc(Noncopy {
591 string: "hello world".to_string(),
592 array: vec![1, 2, 3, 4, 5],
598 pub fn bench_noncopy_nonarena(b: &mut Bencher) {
600 let _: Box<_> = Box::new(Noncopy {
601 string: "hello world".to_string(),
602 array: vec![1, 2, 3, 4, 5],