Skip to content

Commit

Permalink
Add extra::arena::Arena::new{, _with_size}.
Browse files Browse the repository at this point in the history
Signed-off-by: OGINO Masanori <[email protected]>
  • Loading branch information
omasanori committed Aug 5, 2013
1 parent 4fdd720 commit eab97b5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
46 changes: 24 additions & 22 deletions src/libextra/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,16 @@ pub struct Arena {
priv chunks: @mut MutList<Chunk>,
}

#[unsafe_destructor]
impl Drop for Arena {
fn drop(&self) {
unsafe {
destroy_chunk(&self.head);
do self.chunks.each |chunk| {
if !chunk.is_pod {
destroy_chunk(chunk);
}
true
};
impl Arena {
pub fn new() -> Arena {
Arena::new_with_size(32u)
}

pub fn new_with_size(initial_size: uint) -> Arena {
Arena {
head: chunk(initial_size, false),
pod_head: chunk(initial_size, true),
chunks: @mut MutNil,
}
}
}
Expand All @@ -92,18 +91,21 @@ fn chunk(size: uint, is_pod: bool) -> Chunk {
}
}

pub fn arena_with_size(initial_size: uint) -> Arena {
Arena {
head: chunk(initial_size, false),
pod_head: chunk(initial_size, true),
chunks: @mut MutNil,
#[unsafe_destructor]
impl Drop for Arena {
fn drop(&self) {
unsafe {
destroy_chunk(&self.head);
do self.chunks.each |chunk| {
if !chunk.is_pod {
destroy_chunk(chunk);
}
true
};
}
}
}

pub fn Arena() -> Arena {
arena_with_size(32u)
}

#[inline]
fn round_up_to(base: uint, align: uint) -> uint {
(base + (align - 1)) & !(align - 1)
Expand Down Expand Up @@ -276,7 +278,7 @@ impl Arena {

#[test]
fn test_arena_destructors() {
let arena = Arena();
let arena = Arena::new();
for i in range(0u, 10) {
// Arena allocate something with drop glue to make sure it
// doesn't leak.
Expand All @@ -291,7 +293,7 @@ fn test_arena_destructors() {
#[should_fail]
#[ignore(cfg(windows))]
fn test_arena_destructors_fail() {
let arena = Arena();
let arena = Arena::new();
// Put some stuff in the arena.
for i in range(0u, 10) {
// Arena allocate something with drop glue to make sure it
Expand Down
8 changes: 4 additions & 4 deletions src/test/bench/shootout-binarytrees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// except according to those terms.

extern mod extra;
use extra::arena;
use extra::arena::Arena;

enum Tree<'self> {
Nil,
Expand All @@ -25,7 +25,7 @@ fn item_check(t: &Tree) -> int {
}
}

fn bottom_up_tree<'r>(arena: &'r arena::Arena, item: int, depth: int)
fn bottom_up_tree<'r>(arena: &'r Arena, item: int, depth: int)
-> &'r Tree<'r> {
if depth > 0 {
return arena.alloc(
Expand Down Expand Up @@ -57,15 +57,15 @@ fn main() {
max_depth = n;
}

let stretch_arena = arena::Arena();
let stretch_arena = Arena::new();
let stretch_depth = max_depth + 1;
let stretch_tree = bottom_up_tree(&stretch_arena, 0, stretch_depth);

printfln!("stretch tree of depth %d\t check: %d",
stretch_depth,
item_check(stretch_tree));

let long_lived_arena = arena::Arena();
let long_lived_arena = Arena::new();
let long_lived_tree = bottom_up_tree(&long_lived_arena, 0, max_depth);
let mut depth = min_depth;
while depth <= max_depth {
Expand Down
4 changes: 2 additions & 2 deletions src/test/run-pass/placement-new-arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
// except according to those terms.

extern mod extra;
use extra::arena;
use extra::arena::Arena;

pub fn main() {
let mut arena = arena::Arena();
let mut arena = Arena::new();
let p = &mut arena;
let x = p.alloc(|| 4u);
printf!("%u", *x);
Expand Down

0 comments on commit eab97b5

Please sign in to comment.