Skip to content

Commit ba39e27

Browse files
committed
stdlib: Implement arenas
1 parent 575692c commit ba39e27

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/libstd/arena.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Dynamic arenas.
2+
3+
export arena, arena_with_size;
4+
5+
import list;
6+
7+
type chunk = {data: [u8], mut fill: uint};
8+
type arena = {mut chunks: list::list<@chunk>};
9+
10+
fn chunk(size: uint) -> @chunk {
11+
@{ data: vec::from_elem(size, 0u8), mut fill: 0u }
12+
}
13+
14+
fn arena_with_size(initial_size: uint) -> arena {
15+
ret {mut chunks: list::cons(chunk(initial_size), @list::nil)};
16+
}
17+
18+
fn arena() -> arena {
19+
arena_with_size(32u)
20+
}
21+
22+
impl arena for arena {
23+
unsafe fn alloc<T>(n_bytes: uint) -> &self.T {
24+
let mut head = list::head(self.chunks);
25+
if head.fill + n_bytes > vec::len(head.data) {
26+
// Allocate a new chunk.
27+
let new_min_chunk_size = uint::max(n_bytes, vec::len(head.data));
28+
head = chunk(uint::next_power_of_two(new_min_chunk_size));
29+
self.chunks = list::cons(head, @self.chunks);
30+
}
31+
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);
36+
}
37+
}
38+

src/libstd/std.rc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
export net, uv;
1212
export c_vec, four, tri, util;
1313
export bitv, deque, fun_treemap, list, map, smallintmap, sort, treemap, ufind;
14-
export rope;
14+
export rope, arena;
1515
export ebml, dbg, getopts, json, rand, sha1, term, time, prettyprint;
1616
export test, tempfile, serialization;
1717

@@ -56,6 +56,7 @@ mod tempfile;
5656
mod term;
5757
mod time;
5858
mod prettyprint;
59+
mod arena;
5960

6061
#[cfg(unicode)]
6162
mod unicode;

src/rustc/metadata/encoder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,9 +672,9 @@ fn encode_info_for_items(ecx: @encode_ctxt, ebml_w: ebml::writer,
672672
*pt, none, tps, ctor.node.dec)
673673
}
674674
_ {}
675+
}
676+
}
675677
}
676-
}
677-
}
678678
},
679679
visit_native_item: {|ni, cx, v|
680680
visit::visit_native_item(ni, cx, v);

0 commit comments

Comments
 (0)