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 has two arenas implemented: `TypedArena`, which is a simpler
19 //! arena but can only hold objects of a single type, and `Arena`, which is a
20 //! more complex, slower arena which can hold objects of any type.
22 #![crate_name = "arena"]
24 #![crate_type = "rlib"]
25 #![crate_type = "dylib"]
26 #![license = "MIT/ASL2"]
27 #![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
28 html_favicon_url = "http://www.rust-lang.org/favicon.ico",
29 html_root_url = "http://doc.rust-lang.org/master/")]
31 #![feature(unsafe_destructor)]
32 #![allow(missing_doc)]
34 use std::cell::{Cell, RefCell};
36 use std::intrinsics::{TyDesc, get_tydesc};
42 use std::rt::heap::{allocate, deallocate};
44 // The way arena uses arrays is really deeply awful. The arrays are
45 // allocated, and have capacities reserved, but the fill for the array
46 // will always stay at 0.
47 #[deriving(Clone, PartialEq)]
49 data: Rc<RefCell<Vec<u8>>>,
55 fn capacity(&self) -> uint {
56 self.data.borrow().capacity()
59 unsafe fn as_ptr(&self) -> *const u8 {
60 self.data.borrow().as_ptr()
64 /// A slower reflection-based arena that can allocate objects of any type.
66 /// This arena uses `Vec<u8>` as a backing store to allocate objects from. For
67 /// each allocated object, the arena stores a pointer to the type descriptor
68 /// followed by the object (potentially with alignment padding after each
69 /// element). When the arena is destroyed, it iterates through all of its
70 /// chunks, and uses the tydesc information to trace through the objects,
71 /// calling the destructors on them. One subtle point that needs to be
72 /// addressed is how to handle failures while running the user provided
73 /// initializer function. It is important to not run the destructor on
74 /// uninitialized objects, but how to detect them is somewhat subtle. Since
75 /// `alloc()` can be invoked recursively, it is not sufficient to simply exclude
76 /// the most recent object. To solve this without requiring extra space, we
77 /// use the low order bit of the tydesc pointer to encode whether the object
78 /// it describes has been fully initialized.
80 /// As an optimization, objects with destructors are stored in different chunks
81 /// than objects without destructors. This reduces overhead when initializing
82 /// plain-old-data (`Copy` types) and means we don't need to waste time running
83 /// their destructors.
85 // The head is separated out from the list as a unbenchmarked
86 // microoptimization, to avoid needing to case on the list to access the
89 copy_head: RefCell<Chunk>,
90 chunks: RefCell<Vec<Chunk>>,
94 /// Allocates a new Arena with 32 bytes preallocated.
95 pub fn new() -> Arena {
96 Arena::new_with_size(32u)
99 /// Allocates a new Arena with `initial_size` bytes preallocated.
100 pub fn new_with_size(initial_size: uint) -> Arena {
102 head: RefCell::new(chunk(initial_size, false)),
103 copy_head: RefCell::new(chunk(initial_size, true)),
104 chunks: RefCell::new(Vec::new()),
109 fn chunk(size: uint, is_copy: bool) -> Chunk {
111 data: Rc::new(RefCell::new(Vec::with_capacity(size))),
113 is_copy: Cell::new(is_copy),
118 impl Drop for Arena {
121 destroy_chunk(&*self.head.borrow());
122 for chunk in self.chunks.borrow().iter() {
123 if !chunk.is_copy.get() {
124 destroy_chunk(chunk);
132 fn round_up(base: uint, align: uint) -> uint {
133 (base.checked_add(&(align - 1))).unwrap() & !(&(align - 1))
136 // Walk down a chunk, running the destructors for any objects stored
138 unsafe fn destroy_chunk(chunk: &Chunk) {
140 let buf = chunk.as_ptr();
141 let fill = chunk.fill.get();
144 let tydesc_data: *const uint = mem::transmute(buf.offset(idx as int));
145 let (tydesc, is_done) = un_bitpack_tydesc_ptr(*tydesc_data);
146 let (size, align) = ((*tydesc).size, (*tydesc).align);
148 let after_tydesc = idx + mem::size_of::<*const TyDesc>();
150 let start = round_up(after_tydesc, align);
152 //debug!("freeing object: idx = {}, size = {}, align = {}, done = {}",
153 // start, size, align, is_done);
155 ((*tydesc).drop_glue)(buf.offset(start as int) as *const i8);
158 // Find where the next tydesc lives
159 idx = round_up(start + size, mem::align_of::<*const TyDesc>());
163 // We encode whether the object a tydesc describes has been
164 // initialized in the arena in the low bit of the tydesc pointer. This
165 // is necessary in order to properly do cleanup if a failure occurs
166 // during an initializer.
168 fn bitpack_tydesc_ptr(p: *const TyDesc, is_done: bool) -> uint {
169 p as uint | (is_done as uint)
172 fn un_bitpack_tydesc_ptr(p: uint) -> (*const TyDesc, bool) {
173 ((p & !1) as *const TyDesc, p & 1 == 1)
177 fn chunk_size(&self) -> uint {
178 self.copy_head.borrow().capacity()
181 // Functions for the POD part of the arena
182 fn alloc_copy_grow(&self, n_bytes: uint, align: uint) -> *const u8 {
183 // Allocate a new chunk.
184 let new_min_chunk_size = cmp::max(n_bytes, self.chunk_size());
185 self.chunks.borrow_mut().push(self.copy_head.borrow().clone());
187 *self.copy_head.borrow_mut() =
188 chunk(num::next_power_of_two(new_min_chunk_size + 1u), true);
190 return self.alloc_copy_inner(n_bytes, align);
194 fn alloc_copy_inner(&self, n_bytes: uint, align: uint) -> *const u8 {
195 let start = round_up(self.copy_head.borrow().fill.get(), align);
197 let end = start + n_bytes;
198 if end > self.chunk_size() {
199 return self.alloc_copy_grow(n_bytes, align);
202 let copy_head = self.copy_head.borrow();
203 copy_head.fill.set(end);
206 copy_head.as_ptr().offset(start as int)
211 fn alloc_copy<T>(&self, op: || -> T) -> &T {
213 let ptr = self.alloc_copy_inner(mem::size_of::<T>(),
214 mem::min_align_of::<T>());
215 let ptr = ptr as *mut T;
216 ptr::write(&mut (*ptr), op());
221 // Functions for the non-POD part of the arena
222 fn alloc_noncopy_grow(&self, n_bytes: uint,
223 align: uint) -> (*const u8, *const u8) {
224 // Allocate a new chunk.
225 let new_min_chunk_size = cmp::max(n_bytes, self.chunk_size());
226 self.chunks.borrow_mut().push(self.head.borrow().clone());
228 *self.head.borrow_mut() =
229 chunk(num::next_power_of_two(new_min_chunk_size + 1u), false);
231 return self.alloc_noncopy_inner(n_bytes, align);
235 fn alloc_noncopy_inner(&self, n_bytes: uint,
236 align: uint) -> (*const u8, *const u8) {
237 // Be careful to not maintain any `head` borrows active, because
238 // `alloc_noncopy_grow` borrows it mutably.
239 let (start, end, tydesc_start, head_capacity) = {
240 let head = self.head.borrow();
241 let fill = head.fill.get();
243 let tydesc_start = fill;
244 let after_tydesc = fill + mem::size_of::<*const TyDesc>();
245 let start = round_up(after_tydesc, align);
246 let end = start + n_bytes;
248 (start, end, tydesc_start, head.capacity())
251 if end > head_capacity {
252 return self.alloc_noncopy_grow(n_bytes, align);
255 let head = self.head.borrow();
256 head.fill.set(round_up(end, mem::align_of::<*const TyDesc>()));
259 let buf = head.as_ptr();
260 return (buf.offset(tydesc_start as int), buf.offset(start as int));
265 fn alloc_noncopy<T>(&self, op: || -> T) -> &T {
267 let tydesc = get_tydesc::<T>();
269 self.alloc_noncopy_inner(mem::size_of::<T>(),
270 mem::min_align_of::<T>());
271 let ty_ptr = ty_ptr as *mut uint;
272 let ptr = ptr as *mut T;
273 // Write in our tydesc along with a bit indicating that it
274 // has *not* been initialized yet.
275 *ty_ptr = mem::transmute(tydesc);
276 // Actually initialize it
277 ptr::write(&mut(*ptr), op());
278 // Now that we are done, update the tydesc to indicate that
279 // the object is there.
280 *ty_ptr = bitpack_tydesc_ptr(tydesc, true);
286 /// Allocates a new item in the arena, using `op` to initialize the value,
287 /// and returns a reference to it.
289 pub fn alloc<T>(&self, op: || -> T) -> &T {
291 if intrinsics::needs_drop::<T>() {
292 self.alloc_noncopy(op)
301 fn test_arena_destructors() {
302 let arena = Arena::new();
303 for i in range(0u, 10) {
304 // Arena allocate something with drop glue to make sure it
306 arena.alloc(|| Rc::new(i));
307 // Allocate something with funny size and alignment, to keep
308 // things interesting.
309 arena.alloc(|| [0u8, 1u8, 2u8]);
314 fn test_arena_alloc_nested() {
315 struct Inner { value: uint }
316 struct Outer<'a> { inner: &'a Inner }
318 let arena = Arena::new();
320 let result = arena.alloc(|| Outer {
321 inner: arena.alloc(|| Inner { value: 10 })
324 assert_eq!(result.inner.value, 10);
329 fn test_arena_destructors_fail() {
330 let arena = Arena::new();
331 // Put some stuff in the arena.
332 for i in range(0u, 10) {
333 // Arena allocate something with drop glue to make sure it
335 arena.alloc(|| { Rc::new(i) });
336 // Allocate something with funny size and alignment, to keep
337 // things interesting.
338 arena.alloc(|| { [0u8, 1u8, 2u8] });
340 // Now, fail while allocating
341 arena.alloc::<Rc<int>>(|| {
347 /// A faster arena that can hold objects of only one type.
349 /// Safety note: Modifying objects in the arena that have already had their
350 /// `drop` destructors run can cause leaks, because the destructor will not
351 /// run again for these objects.
352 pub struct TypedArena<T> {
353 /// A pointer to the next object to be allocated.
356 /// A pointer to the end of the allocated area. When this pointer is
357 /// reached, a new chunk is allocated.
360 /// A pointer to the first arena segment.
361 first: RefCell<*mut TypedArenaChunk<T>>,
364 struct TypedArenaChunk<T> {
365 /// Pointer to the next arena segment.
366 next: *mut TypedArenaChunk<T>,
368 /// The number of elements that this chunk can hold.
371 // Objects follow here, suitably aligned.
374 fn calculate_size<T>(capacity: uint) -> uint {
375 let mut size = mem::size_of::<TypedArenaChunk<T>>();
376 size = round_up(size, mem::min_align_of::<T>());
377 let elem_size = mem::size_of::<T>();
378 let elems_size = elem_size.checked_mul(&capacity).unwrap();
379 size = size.checked_add(&elems_size).unwrap();
383 impl<T> TypedArenaChunk<T> {
385 unsafe fn new(next: *mut TypedArenaChunk<T>, capacity: uint)
386 -> *mut TypedArenaChunk<T> {
387 let size = calculate_size::<T>(capacity);
388 let chunk = allocate(size, mem::min_align_of::<TypedArenaChunk<T>>())
389 as *mut TypedArenaChunk<T>;
390 (*chunk).next = next;
391 (*chunk).capacity = capacity;
395 /// Destroys this arena chunk. If the type descriptor is supplied, the
396 /// drop glue is called; otherwise, drop glue is not called.
398 unsafe fn destroy(&mut self, len: uint) {
399 // Destroy all the allocated objects.
400 if intrinsics::needs_drop::<T>() {
401 let mut start = self.start();
402 for _ in range(0, len) {
403 ptr::read(start as *const T); // run the destructor on the pointer
404 start = start.offset(mem::size_of::<T>() as int)
408 // Destroy the next chunk.
409 let next = self.next;
410 let size = calculate_size::<T>(self.capacity);
411 deallocate(self as *mut TypedArenaChunk<T> as *mut u8, size,
412 mem::min_align_of::<TypedArenaChunk<T>>());
413 if next.is_not_null() {
414 let capacity = (*next).capacity;
415 (*next).destroy(capacity);
419 // Returns a pointer to the first allocated object.
421 fn start(&self) -> *const u8 {
422 let this: *const TypedArenaChunk<T> = self;
424 mem::transmute(round_up(this.offset(1) as uint,
425 mem::min_align_of::<T>()))
429 // Returns a pointer to the end of the allocated space.
431 fn end(&self) -> *const u8 {
433 let size = mem::size_of::<T>().checked_mul(&self.capacity).unwrap();
434 self.start().offset(size as int)
439 impl<T> TypedArena<T> {
440 /// Creates a new `TypedArena` with preallocated space for eight objects.
442 pub fn new() -> TypedArena<T> {
443 TypedArena::with_capacity(8)
446 /// Creates a new `TypedArena` with preallocated space for the given number of
449 pub fn with_capacity(capacity: uint) -> TypedArena<T> {
451 let chunk = TypedArenaChunk::<T>::new(ptr::mut_null(), capacity);
453 ptr: Cell::new((*chunk).start() as *const T),
454 end: Cell::new((*chunk).end() as *const T),
455 first: RefCell::new(chunk),
460 /// Allocates an object in the `TypedArena`, returning a reference to it.
462 pub fn alloc(&self, object: T) -> &T {
463 if self.ptr == self.end {
467 let ptr: &T = unsafe {
468 let ptr: &mut T = mem::transmute(self.ptr);
469 ptr::write(ptr, object);
470 self.ptr.set(self.ptr.get().offset(1));
481 let chunk = *self.first.borrow_mut();
482 let new_capacity = (*chunk).capacity.checked_mul(&2).unwrap();
483 let chunk = TypedArenaChunk::<T>::new(chunk, new_capacity);
484 self.ptr.set((*chunk).start() as *const T);
485 self.end.set((*chunk).end() as *const T);
486 *self.first.borrow_mut() = chunk
492 impl<T> Drop for TypedArena<T> {
495 // Determine how much was filled.
496 let start = self.first.borrow().as_ref().unwrap().start() as uint;
497 let end = self.ptr.get() as uint;
498 let diff = (end - start) / mem::size_of::<T>();
500 // Pass that to the `destroy` method.
501 (**self.first.borrow_mut()).destroy(diff)
509 use self::test::Bencher;
510 use super::{Arena, TypedArena};
520 let arena = TypedArena::new();
521 for _ in range(0u, 100000) {
531 pub fn bench_copy(b: &mut Bencher) {
532 let arena = TypedArena::new();
543 pub fn bench_copy_nonarena(b: &mut Bencher) {
554 pub fn bench_copy_old_arena(b: &mut Bencher) {
555 let arena = Arena::new();
573 pub fn test_noncopy() {
574 let arena = TypedArena::new();
575 for _ in range(0u, 100000) {
576 arena.alloc(Noncopy {
577 string: "hello world".to_string(),
578 array: vec!( 1, 2, 3, 4, 5 ),
584 pub fn bench_noncopy(b: &mut Bencher) {
585 let arena = TypedArena::new();
587 arena.alloc(Noncopy {
588 string: "hello world".to_string(),
589 array: vec!( 1, 2, 3, 4, 5 ),
595 pub fn bench_noncopy_nonarena(b: &mut Bencher) {
598 string: "hello world".to_string(),
599 array: vec!( 1, 2, 3, 4, 5 ),
605 pub fn bench_noncopy_old_arena(b: &mut Bencher) {
606 let arena = Arena::new();
608 arena.alloc(|| Noncopy {
609 string: "hello world".to_string(),
610 array: vec!( 1, 2, 3, 4, 5 ),