From cce2751461729147ecdbf514e6df6a7052f8f6af Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 21 Mar 2012 05:45:52 -0700 Subject: [PATCH] Adjust arena definition to be compatible with placement new --- src/libstd/arena.rs | 19 +++++++++++++------ src/test/run-pass/placement-new-arena.rs | 9 +++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 src/test/run-pass/placement-new-arena.rs diff --git a/src/libstd/arena.rs b/src/libstd/arena.rs index eb293d4f2d9c6..3c316ecf5ccdc 100644 --- a/src/libstd/arena.rs +++ b/src/libstd/arena.rs @@ -20,19 +20,26 @@ fn arena() -> arena { } impl arena for arena { - unsafe fn alloc(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); } } } diff --git a/src/test/run-pass/placement-new-arena.rs b/src/test/run-pass/placement-new-arena.rs new file mode 100644 index 0000000000000..a7bec46573881 --- /dev/null +++ b/src/test/run-pass/placement-new-arena.rs @@ -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; +}