Skip to content

Commit cce2751

Browse files
committed
Adjust arena definition to be compatible with placement new
1 parent 8404ea0 commit cce2751

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/libstd/arena.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,26 @@ fn arena() -> arena {
2020
}
2121

2222
impl arena for arena {
23-
unsafe fn alloc<T>(n_bytes: uint) -> &self.T {
23+
fn alloc(n_bytes: uint, align: uint) -> *() {
24+
let alignm1 = align - 1u;
2425
let mut head = list::head(self.chunks);
25-
if head.fill + n_bytes > vec::len(head.data) {
26+
27+
let mut start = head.fill;
28+
start = (start + alignm1) & !alignm1;
29+
let mut end = start + n_bytes;
30+
31+
if end > vec::len(head.data) {
2632
// Allocate a new chunk.
2733
let new_min_chunk_size = uint::max(n_bytes, vec::len(head.data));
2834
head = chunk(uint::next_power_of_two(new_min_chunk_size));
2935
self.chunks = list::cons(head, @self.chunks);
36+
start = 0u;
37+
end = n_bytes;
3038
}
3139

32-
let start = vec::unsafe::to_ptr(head.data);
33-
let p = ptr::offset(start, head.fill);
34-
head.fill += n_bytes;
35-
ret unsafe::reinterpret_cast(p);
40+
let p = ptr::offset(ptr::addr_of(head.fill), start);
41+
head.fill = end;
42+
unsafe { ret unsafe::reinterpret_cast(p); }
3643
}
3744
}
3845

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use std;
2+
import std::arena::arena;
3+
4+
fn main() {
5+
let p = &arena();
6+
let x = new(*p) 4u;
7+
io::print(#fmt["%u", *x]);
8+
assert *x == 4u;
9+
}

0 commit comments

Comments
 (0)