2012-03-20 21:06:04 -05:00
|
|
|
// Dynamic arenas.
|
2012-08-11 09:08:42 -05:00
|
|
|
#[warn(non_camel_case_types)];
|
2012-03-20 21:06:04 -05:00
|
|
|
|
2012-08-21 17:32:30 -05:00
|
|
|
// Arenas are used to quickly allocate objects that share a
|
|
|
|
// lifetime. The arena uses ~[u8] vectors as a backing store to
|
|
|
|
// allocate objects from. For each allocated object, the arena stores
|
|
|
|
// a pointer to the type descriptor followed by the
|
|
|
|
// object. (Potentially with alignment padding after each of them.)
|
|
|
|
// When the arena is destroyed, it iterates through all of its chunks,
|
|
|
|
// and uses the tydesc information to trace through the objects,
|
|
|
|
// calling the destructors on them.
|
|
|
|
// One subtle point that needs to be addressed is how to handle
|
|
|
|
// failures while running the user provided initializer function. It
|
|
|
|
// is important to not run the destructor on uninitalized objects, but
|
|
|
|
// how to detect them is somewhat subtle. Since alloc() can be invoked
|
|
|
|
// recursively, it is not sufficient to simply exclude the most recent
|
|
|
|
// object. To solve this without requiring extra space, we use the low
|
|
|
|
// order bit of the tydesc pointer to encode whether the object it
|
|
|
|
// describes has been fully initialized.
|
|
|
|
|
2012-08-21 18:26:19 -05:00
|
|
|
// As an optimization, objects with destructors are stored in
|
|
|
|
// different chunks than objects without destructors. This reduces
|
|
|
|
// overhead when initializing plain-old-data and means we don't need
|
|
|
|
// to waste time running the destructors of POD.
|
2012-08-21 17:32:30 -05:00
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
export Arena, arena_with_size;
|
2012-03-20 21:06:04 -05:00
|
|
|
|
|
|
|
import list;
|
2012-05-21 11:37:34 -05:00
|
|
|
import list::{list, cons, nil};
|
2012-08-02 18:00:45 -05:00
|
|
|
import unsafe::reinterpret_cast;
|
2012-08-21 17:32:30 -05:00
|
|
|
import sys::TypeDesc;
|
|
|
|
import libc::size_t;
|
2012-03-20 21:06:04 -05:00
|
|
|
|
2012-08-21 17:32:30 -05:00
|
|
|
#[abi = "rust-intrinsic"]
|
|
|
|
extern mod rusti {
|
|
|
|
fn move_val_init<T>(&dst: T, -src: T);
|
2012-08-21 18:26:19 -05:00
|
|
|
fn needs_drop<T>() -> bool;
|
2012-08-21 17:32:30 -05:00
|
|
|
}
|
|
|
|
extern mod rustrt {
|
|
|
|
#[rust_stack]
|
|
|
|
fn rust_call_tydesc_glue(root: *u8, tydesc: *TypeDesc, field: size_t);
|
|
|
|
}
|
|
|
|
// This probably belongs somewhere else. Needs to be kept in sync with
|
|
|
|
// changes to glue...
|
|
|
|
const tydesc_drop_glue_index: size_t = 3 as size_t;
|
2012-07-11 17:00:40 -05:00
|
|
|
|
2012-08-21 17:32:30 -05:00
|
|
|
// The way arena uses arrays is really deeply awful. The arrays are
|
|
|
|
// allocated, and have capacities reserved, but the fill for the array
|
|
|
|
// will always stay at 0.
|
2012-08-11 09:08:42 -05:00
|
|
|
type Chunk = {data: @[u8], mut fill: uint, is_pod: bool};
|
2012-07-11 17:00:40 -05:00
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
struct Arena {
|
2012-08-21 17:32:30 -05:00
|
|
|
// The head is seperated out from the list as a unbenchmarked
|
|
|
|
// microoptimization, to avoid needing to case on the list to
|
|
|
|
// access the head.
|
2012-08-11 09:08:42 -05:00
|
|
|
priv mut head: Chunk;
|
|
|
|
priv mut pod_head: Chunk;
|
|
|
|
priv mut chunks: @list<Chunk>;
|
2012-08-21 17:32:30 -05:00
|
|
|
drop {
|
|
|
|
unsafe {
|
|
|
|
destroy_chunk(self.head);
|
2012-08-21 18:26:19 -05:00
|
|
|
for list::each(self.chunks) |chunk| {
|
|
|
|
if !chunk.is_pod { destroy_chunk(chunk); }
|
|
|
|
}
|
2012-08-21 17:32:30 -05:00
|
|
|
}
|
|
|
|
}
|
2012-07-11 17:00:40 -05:00
|
|
|
}
|
2012-03-20 21:06:04 -05:00
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
fn chunk(size: uint, is_pod: bool) -> Chunk {
|
2012-08-21 18:37:50 -05:00
|
|
|
let mut v = @[];
|
|
|
|
unsafe { at_vec::unsafe::reserve(v, size); }
|
|
|
|
{ data: v, mut fill: 0u, is_pod: is_pod }
|
2012-03-20 21:06:04 -05:00
|
|
|
}
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
fn arena_with_size(initial_size: uint) -> Arena {
|
|
|
|
return Arena {mut head: chunk(initial_size, false),
|
2012-08-21 18:26:19 -05:00
|
|
|
mut pod_head: chunk(initial_size, true),
|
2012-08-21 17:32:30 -05:00
|
|
|
mut chunks: @nil};
|
2012-03-20 21:06:04 -05:00
|
|
|
}
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
fn Arena() -> Arena {
|
2012-03-20 21:06:04 -05:00
|
|
|
arena_with_size(32u)
|
|
|
|
}
|
|
|
|
|
2012-08-21 17:32:30 -05:00
|
|
|
#[inline(always)]
|
|
|
|
fn round_up_to(base: uint, align: uint) -> uint {
|
|
|
|
(base + (align - 1)) & !(align - 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk down a chunk, running the destructors for any objects stored
|
|
|
|
// in it.
|
2012-08-11 09:08:42 -05:00
|
|
|
unsafe fn destroy_chunk(chunk: Chunk) {
|
2012-08-21 17:32:30 -05:00
|
|
|
let mut idx = 0;
|
2012-08-21 18:37:50 -05:00
|
|
|
let buf = vec::unsafe::to_ptr_slice(chunk.data);
|
2012-08-21 17:32:30 -05:00
|
|
|
let fill = chunk.fill;
|
|
|
|
|
|
|
|
while idx < fill {
|
|
|
|
let tydesc_data: *uint = reinterpret_cast(ptr::offset(buf, idx));
|
|
|
|
let (tydesc, is_done) = un_bitpack_tydesc_ptr(*tydesc_data);
|
|
|
|
let size = (*tydesc).size, align = (*tydesc).align;
|
|
|
|
|
|
|
|
let after_tydesc = idx + sys::size_of::<*TypeDesc>();
|
|
|
|
|
|
|
|
let start = round_up_to(after_tydesc, align);
|
|
|
|
|
|
|
|
//debug!("freeing object: idx = %u, size = %u, align = %u, done = %b",
|
|
|
|
// start, size, align, is_done);
|
|
|
|
if is_done {
|
|
|
|
rustrt::rust_call_tydesc_glue(
|
|
|
|
ptr::offset(buf, start), tydesc, tydesc_drop_glue_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find where the next tydesc lives
|
|
|
|
idx = round_up_to(start + size, sys::pref_align_of::<*TypeDesc>());
|
|
|
|
}
|
2012-08-02 18:00:45 -05:00
|
|
|
}
|
|
|
|
|
2012-08-21 17:32:30 -05:00
|
|
|
// We encode whether the object a tydesc describes has been
|
|
|
|
// initialized in the arena in the low bit of the tydesc pointer. This
|
|
|
|
// is necessary in order to properly do cleanup if a failure occurs
|
|
|
|
// during an initializer.
|
|
|
|
#[inline(always)]
|
|
|
|
unsafe fn bitpack_tydesc_ptr(p: *TypeDesc, is_done: bool) -> uint {
|
|
|
|
let p_bits: uint = reinterpret_cast(p);
|
|
|
|
p_bits | (is_done as uint)
|
|
|
|
}
|
|
|
|
#[inline(always)]
|
|
|
|
unsafe fn un_bitpack_tydesc_ptr(p: uint) -> (*TypeDesc, bool) {
|
|
|
|
(reinterpret_cast(p & !1), p & 1 == 1)
|
|
|
|
}
|
|
|
|
|
2012-08-21 18:26:19 -05:00
|
|
|
// The duplication between the POD and non-POD functions is annoying.
|
2012-08-11 09:08:42 -05:00
|
|
|
impl &Arena {
|
2012-08-21 18:26:19 -05:00
|
|
|
// Functions for the POD part of the arena
|
|
|
|
fn alloc_pod_grow(n_bytes: uint, align: uint) -> *u8 {
|
|
|
|
// Allocate a new chunk.
|
2012-08-21 18:37:50 -05:00
|
|
|
let chunk_size = at_vec::capacity(self.pod_head.data);
|
2012-08-21 18:26:19 -05:00
|
|
|
let new_min_chunk_size = uint::max(n_bytes, chunk_size);
|
2012-08-21 18:37:50 -05:00
|
|
|
self.chunks = @cons(copy self.pod_head, self.chunks);
|
2012-08-21 18:26:19 -05:00
|
|
|
self.pod_head =
|
|
|
|
chunk(uint::next_power_of_two(new_min_chunk_size + 1u), true);
|
|
|
|
|
|
|
|
return self.alloc_pod_inner(n_bytes, align);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn alloc_pod_inner(n_bytes: uint, align: uint) -> *u8 {
|
2012-08-21 18:37:50 -05:00
|
|
|
let head = &mut self.pod_head;
|
2012-08-21 18:26:19 -05:00
|
|
|
|
|
|
|
let start = round_up_to(head.fill, align);
|
|
|
|
let end = start + n_bytes;
|
2012-08-21 18:37:50 -05:00
|
|
|
if end > at_vec::capacity(head.data) {
|
2012-08-21 18:26:19 -05:00
|
|
|
return self.alloc_pod_grow(n_bytes, align);
|
|
|
|
}
|
|
|
|
head.fill = end;
|
|
|
|
|
|
|
|
//debug!("idx = %u, size = %u, align = %u, fill = %u",
|
|
|
|
// start, n_bytes, align, head.fill);
|
|
|
|
|
|
|
|
unsafe {
|
2012-08-21 18:37:50 -05:00
|
|
|
ptr::offset(vec::unsafe::to_ptr_slice(head.data), start)
|
2012-08-21 18:26:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn alloc_pod<T>(op: fn() -> T) -> &self/T {
|
|
|
|
unsafe {
|
|
|
|
let tydesc = sys::get_type_desc::<T>();
|
|
|
|
let ptr = self.alloc_pod_inner((*tydesc).size, (*tydesc).align);
|
|
|
|
let ptr: *mut T = reinterpret_cast(ptr);
|
|
|
|
rusti::move_val_init(*ptr, op());
|
|
|
|
return reinterpret_cast(ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Functions for the non-POD part of the arena
|
|
|
|
fn alloc_nonpod_grow(n_bytes: uint, align: uint) -> (*u8, *u8) {
|
2012-03-29 00:09:54 -05:00
|
|
|
// Allocate a new chunk.
|
2012-08-21 18:37:50 -05:00
|
|
|
let chunk_size = at_vec::capacity(self.head.data);
|
2012-03-29 00:09:54 -05:00
|
|
|
let new_min_chunk_size = uint::max(n_bytes, chunk_size);
|
2012-08-21 18:37:50 -05:00
|
|
|
self.chunks = @cons(copy self.head, self.chunks);
|
2012-08-21 18:26:19 -05:00
|
|
|
self.head =
|
|
|
|
chunk(uint::next_power_of_two(new_min_chunk_size + 1u), false);
|
2012-03-29 00:09:54 -05:00
|
|
|
|
2012-08-21 18:26:19 -05:00
|
|
|
return self.alloc_nonpod_inner(n_bytes, align);
|
2012-03-29 00:09:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-08-21 18:26:19 -05:00
|
|
|
fn alloc_nonpod_inner(n_bytes: uint, align: uint) -> (*u8, *u8) {
|
2012-08-21 18:37:50 -05:00
|
|
|
let head = &mut self.head;
|
2012-03-21 07:45:52 -05:00
|
|
|
|
2012-08-21 18:26:19 -05:00
|
|
|
let tydesc_start = head.fill;
|
2012-08-21 17:32:30 -05:00
|
|
|
let after_tydesc = head.fill + sys::size_of::<*TypeDesc>();
|
|
|
|
let start = round_up_to(after_tydesc, align);
|
2012-03-29 00:09:54 -05:00
|
|
|
let end = start + n_bytes;
|
2012-08-21 18:37:50 -05:00
|
|
|
if end > at_vec::capacity(head.data) {
|
2012-08-21 18:26:19 -05:00
|
|
|
return self.alloc_nonpod_grow(n_bytes, align);
|
2012-03-20 21:06:04 -05:00
|
|
|
}
|
2012-08-21 18:26:19 -05:00
|
|
|
head.fill = round_up_to(end, sys::pref_align_of::<*TypeDesc>());
|
2012-03-20 21:06:04 -05:00
|
|
|
|
2012-08-21 17:32:30 -05:00
|
|
|
//debug!("idx = %u, size = %u, align = %u, fill = %u",
|
|
|
|
// start, n_bytes, align, head.fill);
|
|
|
|
|
2012-03-28 22:26:28 -05:00
|
|
|
unsafe {
|
2012-08-21 18:37:50 -05:00
|
|
|
let buf = vec::unsafe::to_ptr_slice(head.data);
|
2012-08-21 18:26:19 -05:00
|
|
|
return (ptr::offset(buf, tydesc_start), ptr::offset(buf, start));
|
2012-03-28 22:26:28 -05:00
|
|
|
}
|
2012-03-20 21:06:04 -05:00
|
|
|
}
|
2012-07-09 19:23:13 -05:00
|
|
|
|
|
|
|
#[inline(always)]
|
2012-08-21 18:26:19 -05:00
|
|
|
fn alloc_nonpod<T>(op: fn() -> T) -> &self/T {
|
2012-07-09 19:23:13 -05:00
|
|
|
unsafe {
|
2012-08-02 18:00:45 -05:00
|
|
|
let tydesc = sys::get_type_desc::<T>();
|
2012-08-21 17:32:30 -05:00
|
|
|
let (ty_ptr, ptr) =
|
2012-08-21 18:26:19 -05:00
|
|
|
self.alloc_nonpod_inner((*tydesc).size, (*tydesc).align);
|
2012-08-21 17:32:30 -05:00
|
|
|
let ty_ptr: *mut uint = reinterpret_cast(ty_ptr);
|
2012-08-02 18:00:45 -05:00
|
|
|
let ptr: *mut T = reinterpret_cast(ptr);
|
2012-08-21 17:32:30 -05:00
|
|
|
// Write in our tydesc along with a bit indicating that it
|
|
|
|
// has *not* been initialized yet.
|
|
|
|
*ty_ptr = reinterpret_cast(tydesc);
|
|
|
|
// Actually initialize it
|
2012-08-02 18:00:45 -05:00
|
|
|
rusti::move_val_init(*ptr, op());
|
2012-08-21 17:32:30 -05:00
|
|
|
// Now that we are done, update the tydesc to indicate that
|
|
|
|
// the object is there.
|
|
|
|
*ty_ptr = bitpack_tydesc_ptr(tydesc, true);
|
|
|
|
|
2012-08-02 18:00:45 -05:00
|
|
|
return reinterpret_cast(ptr);
|
2012-07-09 19:23:13 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-21 18:26:19 -05:00
|
|
|
|
|
|
|
// The external interface
|
|
|
|
#[inline(always)]
|
|
|
|
fn alloc<T>(op: fn() -> T) -> &self/T {
|
|
|
|
if !rusti::needs_drop::<T>() {
|
|
|
|
self.alloc_pod(op)
|
|
|
|
} else { self.alloc_nonpod(op) }
|
|
|
|
}
|
2012-03-20 21:06:04 -05:00
|
|
|
}
|
|
|
|
|
2012-08-21 17:32:30 -05:00
|
|
|
#[test]
|
|
|
|
fn test_arena_destructors() {
|
2012-08-11 09:08:42 -05:00
|
|
|
let arena = arena::Arena();
|
2012-08-21 17:32:30 -05:00
|
|
|
for uint::range(0, 10) |i| {
|
|
|
|
// Arena allocate something with drop glue to make sure it
|
|
|
|
// doesn't leak.
|
|
|
|
do arena.alloc { @i };
|
|
|
|
// Allocate something with funny size and alignment, to keep
|
|
|
|
// things interesting.
|
|
|
|
do arena.alloc { [0u8, 1u8, 2u8]/3 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 12:29:23 -05:00
|
|
|
#[test] #[should_fail] #[ignore(cfg(windows))]
|
2012-08-21 17:32:30 -05:00
|
|
|
fn test_arena_destructors_fail() {
|
2012-08-11 09:08:42 -05:00
|
|
|
let arena = arena::Arena();
|
2012-08-21 17:32:30 -05:00
|
|
|
// Put some stuff in the arena.
|
|
|
|
for uint::range(0, 10) |i| {
|
|
|
|
// Arena allocate something with drop glue to make sure it
|
|
|
|
// doesn't leak.
|
|
|
|
do arena.alloc { @i };
|
|
|
|
// Allocate something with funny size and alignment, to keep
|
|
|
|
// things interesting.
|
|
|
|
do arena.alloc { [0u8, 1u8, 2u8]/3 };
|
|
|
|
}
|
|
|
|
// Now, fail while allocating
|
|
|
|
do arena.alloc::<@int> {
|
|
|
|
// First, recursively allocate something else; that needs to
|
|
|
|
// get freed too.
|
|
|
|
do arena.alloc { @20 };
|
|
|
|
// Now fail.
|
|
|
|
fail;
|
|
|
|
};
|
|
|
|
}
|