Skip to content

Add new()-style constructors to Arena and Frame #8183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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