Skip to content

Commit f63c3fb

Browse files
committed
arena: stop using @[].
1 parent 2ed980f commit f63c3fb

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

src/libarena/lib.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,38 @@ extern mod extra;
2727
use extra::list::{List, Cons, Nil};
2828
use extra::list;
2929

30-
use std::at_vec;
3130
use std::cast::{transmute, transmute_mut, transmute_mut_region};
3231
use std::cast;
3332
use std::cell::{Cell, RefCell};
3433
use std::num;
3534
use std::ptr;
3635
use std::kinds::marker;
3736
use std::mem;
37+
use std::rc::Rc;
3838
use std::rt::global_heap;
3939
use std::unstable::intrinsics::{TyDesc, get_tydesc};
4040
use std::unstable::intrinsics;
4141
use std::util;
42+
use std::vec;
4243

4344
// The way arena uses arrays is really deeply awful. The arrays are
4445
// allocated, and have capacities reserved, but the fill for the array
4546
// will always stay at 0.
4647
#[deriving(Clone)]
4748
struct Chunk {
48-
data: RefCell<@[u8]>,
49+
data: Rc<RefCell<~[u8]>>,
4950
fill: Cell<uint>,
5051
is_pod: Cell<bool>,
5152
}
53+
impl Chunk {
54+
fn capacity(&self) -> uint {
55+
self.data.borrow().borrow().get().capacity()
56+
}
57+
58+
unsafe fn as_ptr(&self) -> *u8 {
59+
self.data.borrow().borrow().get().as_ptr()
60+
}
61+
}
5262

5363
// Arenas are used to quickly allocate objects that share a
5464
// lifetime. The arena uses ~[u8] vectors as a backing store to
@@ -97,10 +107,8 @@ impl Arena {
97107
}
98108

99109
fn chunk(size: uint, is_pod: bool) -> Chunk {
100-
let mut v: @[u8] = @[];
101-
unsafe { at_vec::raw::reserve(&mut v, size); }
102110
Chunk {
103-
data: RefCell::new(unsafe { cast::transmute(v) }),
111+
data: Rc::new(RefCell::new(vec::with_capacity(size))),
104112
fill: Cell::new(0u),
105113
is_pod: Cell::new(is_pod),
106114
}
@@ -131,10 +139,7 @@ fn round_up(base: uint, align: uint) -> uint {
131139
// in it.
132140
unsafe fn destroy_chunk(chunk: &Chunk) {
133141
let mut idx = 0;
134-
let buf = {
135-
let data = chunk.data.borrow();
136-
data.get().as_ptr()
137-
};
142+
let buf = chunk.as_ptr();
138143
let fill = chunk.fill.get();
139144

140145
while idx < fill {
@@ -172,11 +177,13 @@ unsafe fn un_bitpack_tydesc_ptr(p: uint) -> (*TyDesc, bool) {
172177
}
173178

174179
impl Arena {
180+
fn chunk_size(&self) -> uint {
181+
self.pod_head.capacity()
182+
}
175183
// Functions for the POD part of the arena
176184
fn alloc_pod_grow(&mut self, n_bytes: uint, align: uint) -> *u8 {
177185
// Allocate a new chunk.
178-
let chunk_size = at_vec::capacity(self.pod_head.data.get());
179-
let new_min_chunk_size = num::max(n_bytes, chunk_size);
186+
let new_min_chunk_size = num::max(n_bytes, self.chunk_size());
180187
self.chunks.set(@Cons(self.pod_head.clone(), self.chunks.get()));
181188
self.pod_head =
182189
chunk(num::next_power_of_two(new_min_chunk_size + 1u), true);
@@ -190,15 +197,15 @@ impl Arena {
190197
let this = transmute_mut_region(self);
191198
let start = round_up(this.pod_head.fill.get(), align);
192199
let end = start + n_bytes;
193-
if end > at_vec::capacity(this.pod_head.data.get()) {
200+
if end > self.chunk_size() {
194201
return this.alloc_pod_grow(n_bytes, align);
195202
}
196203
this.pod_head.fill.set(end);
197204

198205
//debug!("idx = {}, size = {}, align = {}, fill = {}",
199206
// start, n_bytes, align, head.fill.get());
200207

201-
ptr::offset(this.pod_head.data.get().as_ptr(), start as int)
208+
this.pod_head.as_ptr().offset(start as int)
202209
}
203210
}
204211

@@ -217,8 +224,7 @@ impl Arena {
217224
fn alloc_nonpod_grow(&mut self, n_bytes: uint, align: uint)
218225
-> (*u8, *u8) {
219226
// Allocate a new chunk.
220-
let chunk_size = at_vec::capacity(self.head.data.get());
221-
let new_min_chunk_size = num::max(n_bytes, chunk_size);
227+
let new_min_chunk_size = num::max(n_bytes, self.chunk_size());
222228
self.chunks.set(@Cons(self.head.clone(), self.chunks.get()));
223229
self.head =
224230
chunk(num::next_power_of_two(new_min_chunk_size + 1u), false);
@@ -244,7 +250,7 @@ impl Arena {
244250
end = start + n_bytes;
245251
}
246252

247-
if end > at_vec::capacity(self.head.data.get()) {
253+
if end > self.head.capacity() {
248254
return self.alloc_nonpod_grow(n_bytes, align);
249255
}
250256

@@ -254,7 +260,7 @@ impl Arena {
254260
//debug!("idx = {}, size = {}, align = {}, fill = {}",
255261
// start, n_bytes, align, head.fill);
256262

257-
let buf = self.head.data.get().as_ptr();
263+
let buf = self.head.as_ptr();
258264
return (ptr::offset(buf, tydesc_start as int), ptr::offset(buf, start as int));
259265
}
260266
}
@@ -606,5 +612,3 @@ mod test {
606612
})
607613
}
608614
}
609-
610-

0 commit comments

Comments
 (0)