rustc: Add a vec::alloc_len and fix arena logic to use it

This commit is contained in:
Patrick Walton 2012-03-28 21:53:58 -07:00
parent 5ce3d35f41
commit 1d25594657
2 changed files with 11 additions and 2 deletions

View File

@ -11,6 +11,7 @@ export reserve;
export reserve_at_least;
export capacity;
export len;
export alloc_len;
export from_fn;
export from_elem;
export to_mut;
@ -153,6 +154,13 @@ pure fn len<T>(&&v: [const T]) -> uint unsafe {
(**repr).fill / sys::size_of::<T>()
}
#[doc = "Returns the number of bytes allocated for this vector"]
#[inline(always)]
pure fn alloc_len<T>(&&v: [const T]) -> uint unsafe {
let repr: **unsafe::vec_repr = ::unsafe::reinterpret_cast(addr_of(v));
(**repr).alloc
}
#[doc = "
Creates and initializes an immutable vector.

View File

@ -30,9 +30,10 @@ impl arena for arena {
start = (start + alignm1) & !alignm1;
let mut end = start + n_bytes;
if end > vec::len(head.data) {
if end > vec::alloc_len(head.data) {
// Allocate a new chunk.
let new_min_chunk_size = uint::max(n_bytes, vec::len(head.data));
let new_min_chunk_size = uint::max(n_bytes,
vec::alloc_len(head.data));
head = chunk(uint::next_power_of_two(new_min_chunk_size));
self.chunks = list::cons(head, @self.chunks);
start = 0u;