lib.rs 17.5 KB
Newer Older
1
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 3 4 5 6 7 8 9
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
10 11 12 13 14 15 16
//
//! The arena, a fast but limited type of allocator.
//!
//! Arenas are a type of allocator that destroy the objects within, all at
//! once, once the arena itself is destroyed. They do not support deallocation
//! of individual objects while the arena itself is still alive. The benefit
//! of an arena is very fast allocation; just a pointer bump.
17

D
David Manescu 已提交
18 19 20 21
#[crate_id = "arena#0.10-pre"];
#[crate_type = "rlib"];
#[crate_type = "dylib"];
#[license = "MIT/ASL2"];
22
#[allow(missing_doc)];
D
David Manescu 已提交
23
#[feature(managed_boxes)];
24

A
Alex Crichton 已提交
25
extern crate collections;
26 27

use collections::list::{List, Cons, Nil};
28

29 30
use std::cast::{transmute, transmute_mut, transmute_mut_region};
use std::cast;
P
Patrick Walton 已提交
31
use std::cell::{Cell, RefCell};
32
use std::mem;
D
Daniel Micay 已提交
33
use std::ptr::read;
M
Michael Darakananda 已提交
34
use std::cmp;
35
use std::num;
36
use std::kinds::marker;
H
Huon Wilson 已提交
37
use std::rc::Rc;
38
use std::rt::global_heap;
39 40
use std::intrinsics::{TyDesc, get_tydesc};
use std::intrinsics;
H
Huon Wilson 已提交
41
use std::vec;
42

43 44 45
// The way arena uses arrays is really deeply awful. The arrays are
// allocated, and have capacities reserved, but the fill for the array
// will always stay at 0.
46
#[deriving(Clone, Eq)]
47
struct Chunk {
H
Huon Wilson 已提交
48
    data: Rc<RefCell<~[u8]>>,
P
Patrick Walton 已提交
49 50
    fill: Cell<uint>,
    is_pod: Cell<bool>,
51
}
H
Huon Wilson 已提交
52 53 54 55 56 57 58 59 60
impl Chunk {
    fn capacity(&self) -> uint {
        self.data.borrow().borrow().get().capacity()
    }

    unsafe fn as_ptr(&self) -> *u8 {
        self.data.borrow().borrow().get().as_ptr()
    }
}
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
// Arenas are used to quickly allocate objects that share a
// lifetime. The arena uses ~[u8] vectors as a backing store to
// allocate objects from. For each allocated object, the arena stores
// a pointer to the type descriptor followed by the
// object. (Potentially with alignment padding after each of them.)
// When the arena is destroyed, it iterates through all of its chunks,
// and uses the tydesc information to trace through the objects,
// calling the destructors on them.
// One subtle point that needs to be addressed is how to handle
// failures while running the user provided initializer function. It
// is important to not run the destructor on uninitialized objects, but
// how to detect them is somewhat subtle. Since alloc() can be invoked
// recursively, it is not sufficient to simply exclude the most recent
// object. To solve this without requiring extra space, we use the low
// order bit of the tydesc pointer to encode whether the object it
// describes has been fully initialized.

// As an optimization, objects with destructors are stored in
// different chunks than objects without destructors. This reduces
// overhead when initializing plain-old-data and means we don't need
// to waste time running the destructors of POD.
83
pub struct Arena {
T
Tim Chevalier 已提交
84
    // The head is separated out from the list as a unbenchmarked
85 86
    // microoptimization, to avoid needing to case on the list to
    // access the head.
P
Patrick Walton 已提交
87 88
    priv head: Chunk,
    priv pod_head: Chunk,
P
Patrick Walton 已提交
89
    priv chunks: RefCell<@List<Chunk>>,
90
    priv no_freeze: marker::NoFreeze,
91 92
}

93 94 95 96 97 98 99 100 101
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),
P
Patrick Walton 已提交
102
            chunks: RefCell::new(@Nil),
103
            no_freeze: marker::NoFreeze,
104 105
        }
    }
106
}
P
Patrick Walton 已提交
107

B
Ben Striegel 已提交
108
fn chunk(size: uint, is_pod: bool) -> Chunk {
109
    Chunk {
H
Huon Wilson 已提交
110
        data: Rc::new(RefCell::new(vec::with_capacity(size))),
P
Patrick Walton 已提交
111 112
        fill: Cell::new(0u),
        is_pod: Cell::new(is_pod),
113
    }
P
Patrick Walton 已提交
114 115
}

116 117
#[unsafe_destructor]
impl Drop for Arena {
D
Daniel Micay 已提交
118
    fn drop(&mut self) {
119 120
        unsafe {
            destroy_chunk(&self.head);
121
            for chunk in self.chunks.get().iter() {
P
Patrick Walton 已提交
122
                if !chunk.is_pod.get() {
123 124
                    destroy_chunk(chunk);
                }
125
            }
126
        }
127
    }
P
Patrick Walton 已提交
128 129
}

130
#[inline]
131 132
fn round_up(base: uint, align: uint) -> uint {
    (base.checked_add(&(align - 1))).unwrap() & !(&(align - 1))
133 134 135 136
}

// Walk down a chunk, running the destructors for any objects stored
// in it.
B
Brian Anderson 已提交
137
unsafe fn destroy_chunk(chunk: &Chunk) {
138
    let mut idx = 0;
H
Huon Wilson 已提交
139
    let buf = chunk.as_ptr();
P
Patrick Walton 已提交
140
    let fill = chunk.fill.get();
141 142

    while idx < fill {
143
        let tydesc_data: *uint = transmute(buf.offset(idx as int));
144
        let (tydesc, is_done) = un_bitpack_tydesc_ptr(*tydesc_data);
145
        let (size, align) = ((*tydesc).size, (*tydesc).align);
146

147
        let after_tydesc = idx + mem::size_of::<*TyDesc>();
148

149
        let start = round_up(after_tydesc, align);
150

151
        //debug!("freeing object: idx = {}, size = {}, align = {}, done = {}",
152 153
        //       start, size, align, is_done);
        if is_done {
154
            ((*tydesc).drop_glue)(buf.offset(start as int) as *i8);
155 156 157
        }

        // Find where the next tydesc lives
158
        idx = round_up(start + size, mem::pref_align_of::<*TyDesc>());
159
    }
160 161
}

162 163 164 165
// We encode whether the object a tydesc describes has been
// initialized in the arena in the low bit of the tydesc pointer. This
// is necessary in order to properly do cleanup if a failure occurs
// during an initializer.
166
#[inline]
167 168
fn bitpack_tydesc_ptr(p: *TyDesc, is_done: bool) -> uint {
    p as uint | (is_done as uint)
169
}
170
#[inline]
171 172
fn un_bitpack_tydesc_ptr(p: uint) -> (*TyDesc, bool) {
    ((p & !1) as *TyDesc, p & 1 == 1)
173 174
}

175
impl Arena {
H
Huon Wilson 已提交
176 177 178
    fn chunk_size(&self) -> uint {
        self.pod_head.capacity()
    }
179
    // Functions for the POD part of the arena
180
    fn alloc_pod_grow(&mut self, n_bytes: uint, align: uint) -> *u8 {
181
        // Allocate a new chunk.
M
Michael Darakananda 已提交
182
        let new_min_chunk_size = cmp::max(n_bytes, self.chunk_size());
P
Patrick Walton 已提交
183
        self.chunks.set(@Cons(self.pod_head.clone(), self.chunks.get()));
184
        self.pod_head =
185
            chunk(num::next_power_of_two(new_min_chunk_size + 1u), true);
186 187 188 189

        return self.alloc_pod_inner(n_bytes, align);
    }

190
    #[inline]
191
    fn alloc_pod_inner(&mut self, n_bytes: uint, align: uint) -> *u8 {
P
Patrick Walton 已提交
192
        unsafe {
193
            let this = transmute_mut_region(self);
194
            let start = round_up(this.pod_head.fill.get(), align);
P
Patrick Walton 已提交
195
            let end = start + n_bytes;
H
Huon Wilson 已提交
196
            if end > self.chunk_size() {
197
                return this.alloc_pod_grow(n_bytes, align);
P
Patrick Walton 已提交
198
            }
P
Patrick Walton 已提交
199
            this.pod_head.fill.set(end);
200

201
            //debug!("idx = {}, size = {}, align = {}, fill = {}",
P
Patrick Walton 已提交
202
            //       start, n_bytes, align, head.fill.get());
203

H
Huon Wilson 已提交
204
            this.pod_head.as_ptr().offset(start as int)
205 206 207
        }
    }

208
    #[inline]
209
    fn alloc_pod<'a, T>(&'a mut self, op: || -> T) -> &'a T {
210
        unsafe {
211
            let ptr = self.alloc_pod_inner(mem::size_of::<T>(), mem::min_align_of::<T>());
212
            let ptr: *mut T = transmute(ptr);
213
            mem::move_val_init(&mut (*ptr), op());
214
            return transmute(ptr);
215 216 217 218
        }
    }

    // Functions for the non-POD part of the arena
219 220
    fn alloc_nonpod_grow(&mut self, n_bytes: uint, align: uint)
                         -> (*u8, *u8) {
221
        // Allocate a new chunk.
M
Michael Darakananda 已提交
222
        let new_min_chunk_size = cmp::max(n_bytes, self.chunk_size());
P
Patrick Walton 已提交
223
        self.chunks.set(@Cons(self.head.clone(), self.chunks.get()));
224
        self.head =
225
            chunk(num::next_power_of_two(new_min_chunk_size + 1u), false);
226 227 228 229

        return self.alloc_nonpod_inner(n_bytes, align);
    }

230
    #[inline]
231 232
    fn alloc_nonpod_inner(&mut self, n_bytes: uint, align: uint)
                          -> (*u8, *u8) {
P
Patrick Walton 已提交
233
        unsafe {
234 235 236 237 238 239 240 241
            let start;
            let end;
            let tydesc_start;
            let after_tydesc;

            {
                let head = transmute_mut_region(&mut self.head);

P
Patrick Walton 已提交
242 243
                tydesc_start = head.fill.get();
                after_tydesc = head.fill.get() + mem::size_of::<*TyDesc>();
244
                start = round_up(after_tydesc, align);
245 246
                end = start + n_bytes;
            }
P
Patrick Walton 已提交
247

H
Huon Wilson 已提交
248
            if end > self.head.capacity() {
P
Patrick Walton 已提交
249 250
                return self.alloc_nonpod_grow(n_bytes, align);
            }
251 252

            let head = transmute_mut_region(&mut self.head);
253
            head.fill.set(round_up(end, mem::pref_align_of::<*TyDesc>()));
254

255
            //debug!("idx = {}, size = {}, align = {}, fill = {}",
P
Patrick Walton 已提交
256
            //       start, n_bytes, align, head.fill);
257

H
Huon Wilson 已提交
258
            let buf = self.head.as_ptr();
259
            return (buf.offset(tydesc_start as int), buf.offset(start as int));
260 261 262
        }
    }

263
    #[inline]
264
    fn alloc_nonpod<'a, T>(&'a mut self, op: || -> T) -> &'a T {
265
        unsafe {
P
Philipp Brüschweiler 已提交
266
            let tydesc = get_tydesc::<T>();
267
            let (ty_ptr, ptr) =
268
                self.alloc_nonpod_inner(mem::size_of::<T>(), mem::min_align_of::<T>());
269 270
            let ty_ptr: *mut uint = transmute(ty_ptr);
            let ptr: *mut T = transmute(ptr);
271 272
            // Write in our tydesc along with a bit indicating that it
            // has *not* been initialized yet.
273
            *ty_ptr = transmute(tydesc);
274
            // Actually initialize it
275
            mem::move_val_init(&mut(*ptr), op());
276 277 278 279
            // Now that we are done, update the tydesc to indicate that
            // the object is there.
            *ty_ptr = bitpack_tydesc_ptr(tydesc, true);

280
            return transmute(ptr);
281 282 283 284
        }
    }

    // The external interface
285
    #[inline]
286
    pub fn alloc<'a, T>(&'a self, op: || -> T) -> &'a T {
287
        unsafe {
288
            // FIXME: Borrow check
289 290 291 292 293
            let this = transmute_mut(self);
            if intrinsics::needs_drop::<T>() {
                this.alloc_nonpod(op)
            } else {
                this.alloc_pod(op)
294 295 296
            }
        }
    }
297
}
P
Patrick Walton 已提交
298

299 300
#[test]
fn test_arena_destructors() {
301
    let arena = Arena::new();
D
Daniel Micay 已提交
302
    for i in range(0u, 10) {
303 304
        // Arena allocate something with drop glue to make sure it
        // doesn't leak.
305
        arena.alloc(|| @i);
306 307
        // Allocate something with funny size and alignment, to keep
        // things interesting.
308
        arena.alloc(|| [0u8, 1u8, 2u8]);
309 310 311
    }
}

P
Patrick Walton 已提交
312 313
#[test]
#[should_fail]
314
fn test_arena_destructors_fail() {
315
    let arena = Arena::new();
316
    // Put some stuff in the arena.
D
Daniel Micay 已提交
317
    for i in range(0u, 10) {
318 319
        // Arena allocate something with drop glue to make sure it
        // doesn't leak.
320
        arena.alloc(|| { @i });
321 322
        // Allocate something with funny size and alignment, to keep
        // things interesting.
323
        arena.alloc(|| { [0u8, 1u8, 2u8] });
324 325
    }
    // Now, fail while allocating
326
    arena.alloc::<@int>(|| {
327
        // Now fail.
328
        fail!();
329
    });
330
}
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345

/// An arena that can hold objects of only one type.
///
/// Safety note: Modifying objects in the arena that have already had their
/// `drop` destructors run can cause leaks, because the destructor will not
/// run again for these objects.
pub struct TypedArena<T> {
    /// A pointer to the next object to be allocated.
    priv ptr: *T,

    /// A pointer to the end of the allocated area. When this pointer is
    /// reached, a new chunk is allocated.
    priv end: *T,

    /// A pointer to the first arena segment.
D
Daniel Micay 已提交
346
    priv first: Option<~TypedArenaChunk<T>>,
347 348
}

D
Daniel Micay 已提交
349
struct TypedArenaChunk<T> {
350
    /// Pointer to the next arena segment.
D
Daniel Micay 已提交
351
    next: Option<~TypedArenaChunk<T>>,
352 353 354 355 356 357 358

    /// The number of elements that this chunk can hold.
    capacity: uint,

    // Objects follow here, suitably aligned.
}

D
Daniel Micay 已提交
359
impl<T> TypedArenaChunk<T> {
360
    #[inline]
D
Daniel Micay 已提交
361 362
    fn new(next: Option<~TypedArenaChunk<T>>, capacity: uint) -> ~TypedArenaChunk<T> {
        let mut size = mem::size_of::<TypedArenaChunk<T>>();
363 364 365 366 367 368 369
        size = round_up(size, mem::min_align_of::<T>());
        let elem_size = mem::size_of::<T>();
        let elems_size = elem_size.checked_mul(&capacity).unwrap();
        size = size.checked_add(&elems_size).unwrap();

        let mut chunk = unsafe {
            let chunk = global_heap::exchange_malloc(size);
D
Daniel Micay 已提交
370
            let mut chunk: ~TypedArenaChunk<T> = cast::transmute(chunk);
371
            mem::move_val_init(&mut chunk.next, next);
372 373 374 375 376 377 378 379 380 381
            chunk
        };

        chunk.capacity = capacity;
        chunk
    }

    /// Destroys this arena chunk. If the type descriptor is supplied, the
    /// drop glue is called; otherwise, drop glue is not called.
    #[inline]
D
Daniel Micay 已提交
382
    unsafe fn destroy(&mut self, len: uint) {
383
        // Destroy all the allocated objects.
D
Daniel Micay 已提交
384 385 386 387 388
        if intrinsics::needs_drop::<T>() {
            let mut start = self.start();
            for _ in range(0, len) {
                read(start as *T); // run the destructor on the pointer
                start = start.offset(mem::size_of::<T>() as int)
389 390 391 392
            }
        }

        // Destroy the next chunk.
393
        let next_opt = mem::replace(&mut self.next, None);
394 395 396 397
        match next_opt {
            None => {}
            Some(mut next) => {
                // We assume that the next chunk is completely filled.
D
Daniel Micay 已提交
398
                next.destroy(next.capacity)
399 400 401 402 403 404
            }
        }
    }

    // Returns a pointer to the first allocated object.
    #[inline]
D
Daniel Micay 已提交
405 406
    fn start(&self) -> *u8 {
        let this: *TypedArenaChunk<T> = self;
407
        unsafe {
D
Daniel Micay 已提交
408
            cast::transmute(round_up(this.offset(1) as uint, mem::min_align_of::<T>()))
409 410 411 412 413
        }
    }

    // Returns a pointer to the end of the allocated space.
    #[inline]
D
Daniel Micay 已提交
414
    fn end(&self) -> *u8 {
415
        unsafe {
D
Daniel Micay 已提交
416 417
            let size = mem::size_of::<T>().checked_mul(&self.capacity).unwrap();
            self.start().offset(size as int)
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
        }
    }
}

impl<T> TypedArena<T> {
    /// Creates a new arena with preallocated space for 8 objects.
    #[inline]
    pub fn new() -> TypedArena<T> {
        TypedArena::with_capacity(8)
    }

    /// Creates a new arena with preallocated space for the given number of
    /// objects.
    #[inline]
    pub fn with_capacity(capacity: uint) -> TypedArena<T> {
D
Daniel Micay 已提交
433
        let chunk = TypedArenaChunk::<T>::new(None, capacity);
434
        TypedArena {
D
Daniel Micay 已提交
435 436
            ptr: chunk.start() as *T,
            end: chunk.end() as *T,
437 438 439 440 441 442 443 444 445 446 447 448 449 450
            first: Some(chunk),
        }
    }

    /// Allocates an object into this arena.
    #[inline]
    pub fn alloc<'a>(&'a self, object: T) -> &'a T {
        unsafe {
            let this = cast::transmute_mut(self);
            if this.ptr == this.end {
                this.grow()
            }

            let ptr: &'a mut T = cast::transmute(this.ptr);
451
            mem::move_val_init(ptr, object);
452 453 454 455 456 457 458 459 460 461 462
            this.ptr = this.ptr.offset(1);
            let ptr: &'a T = ptr;
            ptr
        }
    }

    /// Grows the arena.
    #[inline(never)]
    fn grow(&mut self) {
        let chunk = self.first.take_unwrap();
        let new_capacity = chunk.capacity.checked_mul(&2).unwrap();
D
Daniel Micay 已提交
463 464 465
        let chunk = TypedArenaChunk::<T>::new(Some(chunk), new_capacity);
        self.ptr = chunk.start() as *T;
        self.end = chunk.end() as *T;
466 467 468 469 470 471 472 473
        self.first = Some(chunk)
    }
}

#[unsafe_destructor]
impl<T> Drop for TypedArena<T> {
    fn drop(&mut self) {
        // Determine how much was filled.
D
Daniel Micay 已提交
474
        let start = self.first.get_ref().start() as uint;
475 476 477 478 479
        let end = self.ptr as uint;
        let diff = (end - start) / mem::size_of::<T>();

        // Pass that to the `destroy` method.
        unsafe {
D
Daniel Micay 已提交
480
            self.first.get_mut_ref().destroy(diff)
481 482 483 484 485
        }
    }
}

#[cfg(test)]
L
Liigo Zhuang 已提交
486 487 488
mod tests {
    extern crate test;
    use self::test::BenchHarness;
489 490 491 492 493 494 495 496 497 498 499
    use super::{Arena, TypedArena};

    struct Point {
        x: int,
        y: int,
        z: int,
    }

    #[test]
    pub fn test_pod() {
        let arena = TypedArena::new();
500
        for _ in range(0, 100000) {
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
            arena.alloc(Point {
                x: 1,
                y: 2,
                z: 3,
            });
        }
    }

    #[bench]
    pub fn bench_pod(bh: &mut BenchHarness) {
        let arena = TypedArena::new();
        bh.iter(|| {
            arena.alloc(Point {
                x: 1,
                y: 2,
                z: 3,
517
            })
518 519 520 521 522 523
        })
    }

    #[bench]
    pub fn bench_pod_nonarena(bh: &mut BenchHarness) {
        bh.iter(|| {
524
            ~Point {
525 526 527
                x: 1,
                y: 2,
                z: 3,
528
            }
529 530 531 532 533 534 535 536 537 538 539 540 541
        })
    }

    #[bench]
    pub fn bench_pod_old_arena(bh: &mut BenchHarness) {
        let arena = Arena::new();
        bh.iter(|| {
            arena.alloc(|| {
                Point {
                    x: 1,
                    y: 2,
                    z: 3,
                }
542
            })
543 544 545 546 547 548 549 550 551 552 553
        })
    }

    struct Nonpod {
        string: ~str,
        array: ~[int],
    }

    #[test]
    pub fn test_nonpod() {
        let arena = TypedArena::new();
554
        for _ in range(0, 100000) {
555 556 557 558 559 560 561 562 563 564 565 566 567 568
            arena.alloc(Nonpod {
                string: ~"hello world",
                array: ~[ 1, 2, 3, 4, 5 ],
            });
        }
    }

    #[bench]
    pub fn bench_nonpod(bh: &mut BenchHarness) {
        let arena = TypedArena::new();
        bh.iter(|| {
            arena.alloc(Nonpod {
                string: ~"hello world",
                array: ~[ 1, 2, 3, 4, 5 ],
569
            })
570 571 572 573 574 575
        })
    }

    #[bench]
    pub fn bench_nonpod_nonarena(bh: &mut BenchHarness) {
        bh.iter(|| {
576
            ~Nonpod {
577 578
                string: ~"hello world",
                array: ~[ 1, 2, 3, 4, 5 ],
579
            }
580 581 582 583 584 585 586
        })
    }

    #[bench]
    pub fn bench_nonpod_old_arena(bh: &mut BenchHarness) {
        let arena = Arena::new();
        bh.iter(|| {
587
            arena.alloc(|| Nonpod {
588 589
                string: ~"hello world",
                array: ~[ 1, 2, 3, 4, 5 ],
590
            })
591 592 593
        })
    }
}