Skip to content

Commit 6f2e429

Browse files
committed
libstd: De-mut arena
1 parent ba84251 commit 6f2e429

File tree

2 files changed

+89
-51
lines changed

2 files changed

+89
-51
lines changed

src/libstd/arena.rs

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
// overhead when initializing plain-old-data and means we don't need
3333
// to waste time running the destructors of POD.
3434

35+
use list::{MutList, MutCons, MutNil};
3536
use list;
36-
use list::{List, Cons, Nil};
3737

3838
use core::at_vec;
39-
use core::cast::transmute;
39+
use core::cast::{transmute, transmute_mut_region};
4040
use core::cast;
4141
use core::libc::size_t;
4242
use core::ptr;
@@ -74,26 +74,28 @@ static tydesc_drop_glue_index: size_t = 3 as size_t;
7474
// will always stay at 0.
7575
struct Chunk {
7676
data: @[u8],
77-
mut fill: uint,
77+
fill: uint,
7878
is_pod: bool,
7979
}
8080

8181
pub struct Arena {
8282
// The head is seperated out from the list as a unbenchmarked
8383
// microoptimization, to avoid needing to case on the list to
8484
// access the head.
85-
priv mut head: Chunk,
86-
priv mut pod_head: Chunk,
87-
priv mut chunks: @List<Chunk>,
85+
priv head: Chunk,
86+
priv pod_head: Chunk,
87+
priv chunks: @mut MutList<Chunk>,
8888
}
8989

9090
#[unsafe_destructor]
9191
impl Drop for Arena {
9292
fn finalize(&self) {
9393
unsafe {
9494
destroy_chunk(&self.head);
95-
for list::each(self.chunks) |chunk| {
96-
if !chunk.is_pod { destroy_chunk(chunk); }
95+
for self.chunks.each |chunk| {
96+
if !chunk.is_pod {
97+
destroy_chunk(chunk);
98+
}
9799
}
98100
}
99101
}
@@ -113,7 +115,7 @@ pub fn arena_with_size(initial_size: uint) -> Arena {
113115
Arena {
114116
head: chunk(initial_size, false),
115117
pod_head: chunk(initial_size, true),
116-
chunks: @Nil,
118+
chunks: @mut MutNil,
117119
}
118120
}
119121

@@ -170,39 +172,40 @@ unsafe fn un_bitpack_tydesc_ptr(p: uint) -> (*TypeDesc, bool) {
170172

171173
pub impl Arena {
172174
// Functions for the POD part of the arena
173-
priv fn alloc_pod_grow(&self, n_bytes: uint, align: uint) -> *u8 {
175+
priv fn alloc_pod_grow(&mut self, n_bytes: uint, align: uint) -> *u8 {
174176
// Allocate a new chunk.
175177
let chunk_size = at_vec::capacity(self.pod_head.data);
176178
let new_min_chunk_size = uint::max(n_bytes, chunk_size);
177-
self.chunks = @Cons(copy self.pod_head, self.chunks);
179+
self.chunks = @mut MutCons(copy self.pod_head, self.chunks);
178180
self.pod_head =
179181
chunk(uint::next_power_of_two(new_min_chunk_size + 1u), true);
180182

181183
return self.alloc_pod_inner(n_bytes, align);
182184
}
183185

184186
#[inline(always)]
185-
priv fn alloc_pod_inner(&self, n_bytes: uint, align: uint) -> *u8 {
186-
let head = &mut self.pod_head;
187+
priv fn alloc_pod_inner(&mut self, n_bytes: uint, align: uint) -> *u8 {
188+
unsafe {
189+
// XXX: Borrow check
190+
let head = transmute_mut_region(&mut self.pod_head);
187191

188-
let start = round_up_to(head.fill, align);
189-
let end = start + n_bytes;
190-
if end > at_vec::capacity(head.data) {
191-
return self.alloc_pod_grow(n_bytes, align);
192-
}
193-
head.fill = end;
192+
let start = round_up_to(head.fill, align);
193+
let end = start + n_bytes;
194+
if end > at_vec::capacity(head.data) {
195+
return self.alloc_pod_grow(n_bytes, align);
196+
}
197+
head.fill = end;
194198

195-
//debug!("idx = %u, size = %u, align = %u, fill = %u",
196-
// start, n_bytes, align, head.fill);
199+
//debug!("idx = %u, size = %u, align = %u, fill = %u",
200+
// start, n_bytes, align, head.fill);
197201

198-
unsafe {
199202
ptr::offset(vec::raw::to_ptr(head.data), start)
200203
}
201204
}
202205

203206
#[inline(always)]
204207
#[cfg(stage0)]
205-
priv fn alloc_pod<T>(&self, op: &fn() -> T) -> &'self T {
208+
priv fn alloc_pod<T>(&mut self, op: &fn() -> T) -> &'self T {
206209
unsafe {
207210
let tydesc = sys::get_type_desc::<T>();
208211
let ptr = self.alloc_pod_inner((*tydesc).size, (*tydesc).align);
@@ -216,7 +219,7 @@ pub impl Arena {
216219
#[cfg(stage1)]
217220
#[cfg(stage2)]
218221
#[cfg(stage3)]
219-
priv fn alloc_pod<'a, T>(&'a self, op: &fn() -> T) -> &'a T {
222+
priv fn alloc_pod<'a, T>(&'a mut self, op: &fn() -> T) -> &'a T {
220223
unsafe {
221224
let tydesc = sys::get_type_desc::<T>();
222225
let ptr = self.alloc_pod_inner((*tydesc).size, (*tydesc).align);
@@ -227,42 +230,44 @@ pub impl Arena {
227230
}
228231

229232
// Functions for the non-POD part of the arena
230-
priv fn alloc_nonpod_grow(&self, n_bytes: uint, align: uint) -> (*u8, *u8) {
233+
priv fn alloc_nonpod_grow(&mut self, n_bytes: uint, align: uint)
234+
-> (*u8, *u8) {
231235
// Allocate a new chunk.
232236
let chunk_size = at_vec::capacity(self.head.data);
233237
let new_min_chunk_size = uint::max(n_bytes, chunk_size);
234-
self.chunks = @Cons(copy self.head, self.chunks);
238+
self.chunks = @mut MutCons(copy self.head, self.chunks);
235239
self.head =
236240
chunk(uint::next_power_of_two(new_min_chunk_size + 1u), false);
237241

238242
return self.alloc_nonpod_inner(n_bytes, align);
239243
}
240244

241245
#[inline(always)]
242-
priv fn alloc_nonpod_inner(&self, n_bytes: uint, align: uint) -> (*u8, *u8) {
243-
let head = &mut self.head;
244-
245-
let tydesc_start = head.fill;
246-
let after_tydesc = head.fill + sys::size_of::<*TypeDesc>();
247-
let start = round_up_to(after_tydesc, align);
248-
let end = start + n_bytes;
249-
if end > at_vec::capacity(head.data) {
250-
return self.alloc_nonpod_grow(n_bytes, align);
251-
}
252-
head.fill = round_up_to(end, sys::pref_align_of::<*TypeDesc>());
246+
priv fn alloc_nonpod_inner(&mut self, n_bytes: uint, align: uint)
247+
-> (*u8, *u8) {
248+
unsafe {
249+
let head = transmute_mut_region(&mut self.head);
250+
251+
let tydesc_start = head.fill;
252+
let after_tydesc = head.fill + sys::size_of::<*TypeDesc>();
253+
let start = round_up_to(after_tydesc, align);
254+
let end = start + n_bytes;
255+
if end > at_vec::capacity(head.data) {
256+
return self.alloc_nonpod_grow(n_bytes, align);
257+
}
258+
head.fill = round_up_to(end, sys::pref_align_of::<*TypeDesc>());
253259

254-
//debug!("idx = %u, size = %u, align = %u, fill = %u",
255-
// start, n_bytes, align, head.fill);
260+
//debug!("idx = %u, size = %u, align = %u, fill = %u",
261+
// start, n_bytes, align, head.fill);
256262

257-
unsafe {
258263
let buf = vec::raw::to_ptr(head.data);
259264
return (ptr::offset(buf, tydesc_start), ptr::offset(buf, start));
260265
}
261266
}
262267

263268
#[inline(always)]
264269
#[cfg(stage0)]
265-
priv fn alloc_nonpod<T>(&self, op: &fn() -> T) -> &'self T {
270+
priv fn alloc_nonpod<T>(&mut self, op: &fn() -> T) -> &'self T {
266271
unsafe {
267272
let tydesc = sys::get_type_desc::<T>();
268273
let (ty_ptr, ptr) =
@@ -286,7 +291,7 @@ pub impl Arena {
286291
#[cfg(stage1)]
287292
#[cfg(stage2)]
288293
#[cfg(stage3)]
289-
priv fn alloc_nonpod<'a, T>(&'a self, op: &fn() -> T) -> &'a T {
294+
priv fn alloc_nonpod<'a, T>(&'a mut self, op: &fn() -> T) -> &'a T {
290295
unsafe {
291296
let tydesc = sys::get_type_desc::<T>();
292297
let (ty_ptr, ptr) =
@@ -309,13 +314,16 @@ pub impl Arena {
309314
// The external interface
310315
#[inline(always)]
311316
#[cfg(stage0)]
312-
fn alloc<T>(&self, op: &fn() -> T) -> &'self T {
317+
fn alloc<T>(&mut self, op: &fn() -> T) -> &'self T {
313318
unsafe {
319+
// XXX: Borrow check
320+
let this = transmute_mut_region(self);
314321
if !rusti::needs_drop::<T>() {
315-
self.alloc_pod(op)
316-
} else {
317-
self.alloc_nonpod(op)
322+
return this.alloc_pod(op);
318323
}
324+
// XXX: Borrow check
325+
let this = transmute_mut_region(self);
326+
this.alloc_nonpod(op)
319327
}
320328
}
321329

@@ -324,13 +332,16 @@ pub impl Arena {
324332
#[cfg(stage1)]
325333
#[cfg(stage2)]
326334
#[cfg(stage3)]
327-
fn alloc<'a, T>(&'a self, op: &fn() -> T) -> &'a T {
335+
fn alloc<'a, T>(&'a mut self, op: &fn() -> T) -> &'a T {
328336
unsafe {
337+
// XXX: Borrow check
338+
let this = transmute_mut_region(self);
329339
if !rusti::needs_drop::<T>() {
330-
self.alloc_pod(op)
331-
} else {
332-
self.alloc_nonpod(op)
340+
return this.alloc_pod(op);
333341
}
342+
// XXX: Borrow check
343+
let this = transmute_mut_region(self);
344+
this.alloc_nonpod(op)
334345
}
335346
}
336347
}
@@ -348,7 +359,9 @@ fn test_arena_destructors() {
348359
}
349360
}
350361

351-
#[test] #[should_fail] #[ignore(cfg(windows))]
362+
#[test]
363+
#[should_fail]
364+
#[ignore(cfg(windows))]
352365
fn test_arena_destructors_fail() {
353366
let arena = Arena();
354367
// Put some stuff in the arena.

src/libstd/list.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ pub enum List<T> {
1616
Nil,
1717
}
1818

19+
#[deriving(Eq)]
20+
pub enum MutList<T> {
21+
MutCons(T, @mut MutList<T>),
22+
MutNil,
23+
}
24+
1925
/// Create a list from a vector
2026
pub fn from_vec<T:Copy>(v: &[T]) -> @List<T> {
2127
vec::foldr(v, @Nil::<T>, |h, t| @Cons(*h, t))
@@ -147,6 +153,25 @@ pub fn each<T>(l: @List<T>, f: &fn(&T) -> bool) {
147153
}
148154
}
149155

156+
impl<T> MutList<T> {
157+
/// Iterate over a mutable list
158+
pub fn each(@mut self, f: &fn(&mut T) -> bool) {
159+
let mut cur = self;
160+
loop {
161+
let borrowed = &mut *cur;
162+
cur = match *borrowed {
163+
MutCons(ref mut hd, tl) => {
164+
if !f(hd) {
165+
return;
166+
}
167+
tl
168+
}
169+
MutNil => break
170+
}
171+
}
172+
}
173+
}
174+
150175
#[cfg(test)]
151176
mod tests {
152177
use list::*;

0 commit comments

Comments
 (0)