Skip to content

Commit

Permalink
Adjust arena definition to be compatible with placement new
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed Mar 21, 2012
1 parent 8404ea0 commit cce2751
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/libstd/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,26 @@ fn arena() -> arena {
}

impl arena for arena {
unsafe fn alloc<T>(n_bytes: uint) -> &self.T {
fn alloc(n_bytes: uint, align: uint) -> *() {
let alignm1 = align - 1u;
let mut head = list::head(self.chunks);
if head.fill + n_bytes > vec::len(head.data) {

let mut start = head.fill;
start = (start + alignm1) & !alignm1;
let mut end = start + n_bytes;

if end > vec::len(head.data) {
// Allocate a new chunk.
let new_min_chunk_size = uint::max(n_bytes, vec::len(head.data));
head = chunk(uint::next_power_of_two(new_min_chunk_size));
self.chunks = list::cons(head, @self.chunks);
start = 0u;
end = n_bytes;
}

let start = vec::unsafe::to_ptr(head.data);
let p = ptr::offset(start, head.fill);
head.fill += n_bytes;
ret unsafe::reinterpret_cast(p);
let p = ptr::offset(ptr::addr_of(head.fill), start);
head.fill = end;
unsafe { ret unsafe::reinterpret_cast(p); }
}
}

9 changes: 9 additions & 0 deletions src/test/run-pass/placement-new-arena.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use std;
import std::arena::arena;

fn main() {
let p = &arena();
let x = new(*p) 4u;
io::print(#fmt["%u", *x]);
assert *x == 4u;
}

0 comments on commit cce2751

Please sign in to comment.