2012-03-20 21:06:04 -05:00
|
|
|
// Dynamic arenas.
|
|
|
|
|
|
|
|
export arena, arena_with_size;
|
|
|
|
|
|
|
|
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-03-20 21:06:04 -05:00
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
type chunk = {data: ~[u8], mut fill: uint};
|
2012-07-11 17:00:40 -05:00
|
|
|
|
|
|
|
type arena_ = {mut chunks: @list<@chunk>};
|
|
|
|
|
|
|
|
enum arena {
|
|
|
|
arena_(arena_)
|
|
|
|
}
|
2012-03-20 21:06:04 -05:00
|
|
|
|
|
|
|
fn chunk(size: uint) -> @chunk {
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut v = ~[];
|
2012-03-28 23:04:13 -05:00
|
|
|
vec::reserve(v, size);
|
|
|
|
@{ data: v, mut fill: 0u }
|
2012-03-20 21:06:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn arena_with_size(initial_size: uint) -> arena {
|
2012-08-01 19:30:05 -05:00
|
|
|
return arena_({mut chunks: @cons(chunk(initial_size), @nil)});
|
2012-03-20 21:06:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn arena() -> arena {
|
|
|
|
arena_with_size(32u)
|
|
|
|
}
|
|
|
|
|
2012-08-02 18:00:45 -05:00
|
|
|
#[abi = "rust-intrinsic"]
|
|
|
|
extern mod rusti {
|
|
|
|
fn move_val_init<T>(&dst: T, -src: T);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl &arena {
|
2012-03-29 00:09:54 -05:00
|
|
|
fn alloc_grow(n_bytes: uint, align: uint) -> *() {
|
|
|
|
// Allocate a new chunk.
|
|
|
|
let mut head = list::head(self.chunks);
|
2012-03-29 10:57:34 -05:00
|
|
|
let chunk_size = vec::capacity(head.data);
|
2012-03-29 00:09:54 -05:00
|
|
|
let new_min_chunk_size = uint::max(n_bytes, chunk_size);
|
2012-03-29 00:22:50 -05:00
|
|
|
head = chunk(uint::next_power_of_two(new_min_chunk_size + 1u));
|
2012-05-21 11:37:34 -05:00
|
|
|
self.chunks = @cons(head, self.chunks);
|
2012-03-29 00:09:54 -05:00
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
return self.alloc_inner(n_bytes, align);
|
2012-03-29 00:09:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-07-09 19:23:13 -05:00
|
|
|
fn alloc_inner(n_bytes: uint, align: uint) -> *() {
|
2012-03-21 07:45:52 -05:00
|
|
|
let alignm1 = align - 1u;
|
2012-03-20 21:06:04 -05:00
|
|
|
let mut head = list::head(self.chunks);
|
2012-03-21 07:45:52 -05:00
|
|
|
|
|
|
|
let mut start = head.fill;
|
|
|
|
start = (start + alignm1) & !alignm1;
|
2012-03-29 00:09:54 -05:00
|
|
|
let end = start + n_bytes;
|
2012-03-29 10:57:34 -05:00
|
|
|
if end > vec::capacity(head.data) {
|
2012-08-01 19:30:05 -05:00
|
|
|
return self.alloc_grow(n_bytes, align);
|
2012-03-20 21:06:04 -05:00
|
|
|
}
|
|
|
|
|
2012-03-28 22:26:28 -05:00
|
|
|
unsafe {
|
|
|
|
let p = ptr::offset(vec::unsafe::to_ptr(head.data), start);
|
|
|
|
head.fill = end;
|
2012-08-01 19:30:05 -05:00
|
|
|
return unsafe::reinterpret_cast(p);
|
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-02 18:00:45 -05:00
|
|
|
fn alloc<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>();
|
|
|
|
let ptr = self.alloc_inner((*tydesc).size, (*tydesc).align);
|
|
|
|
let ptr: *mut T = reinterpret_cast(ptr);
|
|
|
|
rusti::move_val_init(*ptr, op());
|
|
|
|
return reinterpret_cast(ptr);
|
2012-07-09 19:23:13 -05:00
|
|
|
}
|
|
|
|
}
|
2012-03-20 21:06:04 -05:00
|
|
|
}
|
|
|
|
|