2014-04-02 22:06:55 -05:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// 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.
|
2014-08-04 05:48:39 -05:00
|
|
|
|
2014-01-06 19:03:30 -06:00
|
|
|
//! 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.
|
2014-04-04 06:57:39 -05:00
|
|
|
//!
|
2014-08-04 05:48:39 -05:00
|
|
|
//! This crate has two arenas implemented: `TypedArena`, which is a simpler
|
|
|
|
//! arena but can only hold objects of a single type, and `Arena`, which is a
|
|
|
|
//! more complex, slower arena which can hold objects of any type.
|
2012-08-21 17:32:30 -05:00
|
|
|
|
2014-07-01 09:12:04 -05:00
|
|
|
#![crate_name = "arena"]
|
2015-08-13 12:21:36 -05:00
|
|
|
#![unstable(feature = "rustc_private", issue = "27812")]
|
2014-03-21 20:05:05 -05:00
|
|
|
#![crate_type = "rlib"]
|
|
|
|
#![crate_type = "dylib"]
|
2015-08-09 16:15:05 -05:00
|
|
|
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
|
2015-05-15 18:04:01 -05:00
|
|
|
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
|
2015-11-03 08:03:22 -06:00
|
|
|
html_root_url = "https://doc.rust-lang.org/nightly/",
|
|
|
|
test(no_crate_inject, attr(deny(warnings))))]
|
2014-06-17 18:00:04 -05:00
|
|
|
|
2015-01-22 20:22:03 -06:00
|
|
|
#![feature(alloc)]
|
2015-06-09 13:18:03 -05:00
|
|
|
#![feature(core_intrinsics)]
|
2015-08-11 22:52:37 -05:00
|
|
|
#![feature(drop_in_place)]
|
2015-06-09 13:52:41 -05:00
|
|
|
#![feature(heap_api)]
|
2015-06-09 13:18:03 -05:00
|
|
|
#![feature(raw)]
|
2015-08-12 02:18:41 -05:00
|
|
|
#![feature(heap_api)]
|
2015-01-30 14:26:44 -06:00
|
|
|
#![feature(staged_api)]
|
2015-07-17 09:12:35 -05:00
|
|
|
#![feature(dropck_parametricity)]
|
2015-01-22 14:33:46 -06:00
|
|
|
#![cfg_attr(test, feature(test))]
|
2014-03-27 17:13:16 -05:00
|
|
|
|
2014-10-28 16:06:06 -05:00
|
|
|
extern crate alloc;
|
|
|
|
|
2013-12-30 19:32:53 -06:00
|
|
|
use std::cell::{Cell, RefCell};
|
2014-02-06 01:34:33 -06:00
|
|
|
use std::cmp;
|
2014-05-05 20:56:44 -05:00
|
|
|
use std::intrinsics;
|
2015-08-13 11:48:34 -05:00
|
|
|
use std::marker::{PhantomData, Send};
|
2014-05-05 20:56:44 -05:00
|
|
|
use std::mem;
|
2014-05-29 19:40:18 -05:00
|
|
|
use std::ptr;
|
2015-08-13 11:10:19 -05:00
|
|
|
use std::slice;
|
2015-09-08 17:53:46 -05:00
|
|
|
|
2015-08-11 22:52:37 -05:00
|
|
|
use alloc::heap;
|
|
|
|
use alloc::raw_vec::RawVec;
|
2013-03-05 14:21:02 -06:00
|
|
|
|
2013-01-22 10:44:24 -06:00
|
|
|
struct Chunk {
|
2015-08-12 02:18:41 -05:00
|
|
|
data: RawVec<u8>,
|
2015-08-12 02:50:52 -05:00
|
|
|
/// Index of the first unused byte.
|
2015-02-09 01:00:46 -06:00
|
|
|
fill: Cell<usize>,
|
2015-08-12 02:50:52 -05:00
|
|
|
/// Indicates whether objects with destructors are stored in this chunk.
|
2014-03-26 18:01:11 -05:00
|
|
|
is_copy: Cell<bool>,
|
2013-01-22 10:44:24 -06:00
|
|
|
}
|
2014-09-05 08:08:30 -05:00
|
|
|
|
2014-01-31 22:53:11 -06:00
|
|
|
impl Chunk {
|
2015-08-12 02:18:41 -05:00
|
|
|
fn new(size: usize, is_copy: bool) -> Chunk {
|
|
|
|
Chunk {
|
|
|
|
data: RawVec::with_capacity(size),
|
|
|
|
fill: Cell::new(0),
|
|
|
|
is_copy: Cell::new(is_copy),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-09 01:00:46 -06:00
|
|
|
fn capacity(&self) -> usize {
|
2015-08-12 02:18:41 -05:00
|
|
|
self.data.cap()
|
2014-01-31 22:53:11 -06:00
|
|
|
}
|
|
|
|
|
2014-06-25 14:47:34 -05:00
|
|
|
unsafe fn as_ptr(&self) -> *const u8 {
|
2015-08-12 02:18:41 -05:00
|
|
|
self.data.ptr()
|
2014-01-31 22:53:11 -06:00
|
|
|
}
|
2015-08-12 02:50:52 -05:00
|
|
|
|
|
|
|
// Walk down a chunk, running the destructors for any objects stored
|
|
|
|
// in it.
|
|
|
|
unsafe fn destroy(&self) {
|
|
|
|
let mut idx = 0;
|
|
|
|
let buf = self.as_ptr();
|
|
|
|
let fill = self.fill.get();
|
|
|
|
|
|
|
|
while idx < fill {
|
|
|
|
let tydesc_data = buf.offset(idx as isize) as *const usize;
|
|
|
|
let (tydesc, is_done) = un_bitpack_tydesc_ptr(*tydesc_data);
|
|
|
|
let (size, align) = ((*tydesc).size, (*tydesc).align);
|
|
|
|
|
|
|
|
let after_tydesc = idx + mem::size_of::<*const TyDesc>();
|
|
|
|
|
|
|
|
let start = round_up(after_tydesc, align);
|
|
|
|
|
|
|
|
if is_done {
|
|
|
|
((*tydesc).drop_glue)(buf.offset(start as isize) as *const i8);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find where the next tydesc lives
|
|
|
|
idx = round_up(start + size, mem::align_of::<*const TyDesc>());
|
|
|
|
}
|
|
|
|
}
|
2014-01-31 22:53:11 -06:00
|
|
|
}
|
2012-07-11 17:00:40 -05:00
|
|
|
|
2014-04-04 06:57:39 -05:00
|
|
|
/// A slower reflection-based arena that can allocate objects of any type.
|
|
|
|
///
|
2015-08-12 02:50:52 -05:00
|
|
|
/// This arena uses `RawVec<u8>` as a backing store to allocate objects from.
|
|
|
|
/// For each allocated object, the arena stores a pointer to the type descriptor
|
2014-08-04 05:48:39 -05:00
|
|
|
/// followed by the object (potentially with alignment padding after each
|
|
|
|
/// element). When the arena is destroyed, it iterates through all of its
|
2014-04-04 06:57:39 -05:00
|
|
|
/// chunks, and uses the tydesc information to trace through the objects,
|
2014-08-04 05:48:39 -05:00
|
|
|
/// calling the destructors on them. One subtle point that needs to be
|
2014-10-09 14:17:22 -05:00
|
|
|
/// addressed is how to handle panics while running the user provided
|
2014-04-04 06:57:39 -05:00
|
|
|
/// initializer function. It is important to not run the destructor on
|
|
|
|
/// uninitialized objects, but how to detect them is somewhat subtle. Since
|
2014-08-04 05:48:39 -05:00
|
|
|
/// `alloc()` can be invoked recursively, it is not sufficient to simply exclude
|
2014-04-04 06:57:39 -05:00
|
|
|
/// 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.
|
|
|
|
///
|
2014-08-04 05:48:39 -05:00
|
|
|
/// As an optimization, objects with destructors are stored in different chunks
|
|
|
|
/// than objects without destructors. This reduces overhead when initializing
|
|
|
|
/// plain-old-data (`Copy` types) and means we don't need to waste time running
|
|
|
|
/// their destructors.
|
2015-02-02 07:10:36 -06:00
|
|
|
pub struct Arena<'longer_than_self> {
|
2015-08-13 11:10:19 -05:00
|
|
|
// The heads are separated out from the list as a unbenchmarked
|
|
|
|
// microoptimization, to avoid needing to case on the list to access a head.
|
2014-06-09 22:29:36 -05:00
|
|
|
head: RefCell<Chunk>,
|
|
|
|
copy_head: RefCell<Chunk>,
|
2014-03-27 17:13:16 -05:00
|
|
|
chunks: RefCell<Vec<Chunk>>,
|
2015-08-13 11:48:34 -05:00
|
|
|
_marker: PhantomData<*mut &'longer_than_self ()>,
|
2012-11-13 20:38:18 -06:00
|
|
|
}
|
|
|
|
|
2015-02-02 07:10:36 -06:00
|
|
|
impl<'a> Arena<'a> {
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Allocates a new Arena with 32 bytes preallocated.
|
2015-02-02 07:10:36 -06:00
|
|
|
pub fn new() -> Arena<'a> {
|
2015-01-24 08:39:32 -06:00
|
|
|
Arena::new_with_size(32)
|
2013-08-05 03:43:40 -05:00
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Allocates a new Arena with `initial_size` bytes preallocated.
|
2015-02-02 07:10:36 -06:00
|
|
|
pub fn new_with_size(initial_size: usize) -> Arena<'a> {
|
2013-08-05 03:43:40 -05:00
|
|
|
Arena {
|
2015-08-12 02:18:41 -05:00
|
|
|
head: RefCell::new(Chunk::new(initial_size, false)),
|
|
|
|
copy_head: RefCell::new(Chunk::new(initial_size, true)),
|
2014-03-18 23:31:40 -05:00
|
|
|
chunks: RefCell::new(Vec::new()),
|
2015-08-13 11:48:34 -05:00
|
|
|
_marker: PhantomData,
|
2012-08-21 17:32:30 -05:00
|
|
|
}
|
|
|
|
}
|
2012-07-11 17:00:40 -05:00
|
|
|
}
|
2012-03-20 21:06:04 -05:00
|
|
|
|
2015-02-02 07:10:36 -06:00
|
|
|
impl<'longer_than_self> Drop for Arena<'longer_than_self> {
|
2013-09-16 20:18:07 -05:00
|
|
|
fn drop(&mut self) {
|
2013-08-05 03:43:40 -05:00
|
|
|
unsafe {
|
2015-08-12 02:50:52 -05:00
|
|
|
self.head.borrow().destroy();
|
2015-06-11 07:56:07 -05:00
|
|
|
for chunk in self.chunks.borrow().iter() {
|
2014-03-26 18:01:11 -05:00
|
|
|
if !chunk.is_copy.get() {
|
2015-08-12 02:50:52 -05:00
|
|
|
chunk.destroy();
|
2013-08-05 03:43:40 -05:00
|
|
|
}
|
2014-02-24 20:18:19 -06:00
|
|
|
}
|
2013-08-05 03:43:40 -05:00
|
|
|
}
|
2013-01-22 10:44:24 -06:00
|
|
|
}
|
2012-03-20 21:06:04 -05:00
|
|
|
}
|
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2015-02-09 01:00:46 -06:00
|
|
|
fn round_up(base: usize, align: usize) -> usize {
|
2014-11-09 07:11:28 -06:00
|
|
|
(base.checked_add(align - 1)).unwrap() & !(align - 1)
|
2012-08-21 17:32:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// We encode whether the object a tydesc describes has been
|
|
|
|
// initialized in the arena in the low bit of the tydesc pointer. This
|
2014-10-09 14:17:22 -05:00
|
|
|
// is necessary in order to properly do cleanup if a panic occurs
|
2012-08-21 17:32:30 -05:00
|
|
|
// during an initializer.
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2015-02-09 01:00:46 -06:00
|
|
|
fn bitpack_tydesc_ptr(p: *const TyDesc, is_done: bool) -> usize {
|
|
|
|
p as usize | (is_done as usize)
|
2012-08-21 17:32:30 -05:00
|
|
|
}
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2015-02-09 01:00:46 -06:00
|
|
|
fn un_bitpack_tydesc_ptr(p: usize) -> (*const TyDesc, bool) {
|
2014-06-25 14:47:34 -05:00
|
|
|
((p & !1) as *const TyDesc, p & 1 == 1)
|
2012-08-21 17:32:30 -05:00
|
|
|
}
|
|
|
|
|
2015-03-14 21:01:57 -05:00
|
|
|
// HACK(eddyb) TyDesc replacement using a trait object vtable.
|
|
|
|
// This could be replaced in the future with a custom DST layout,
|
|
|
|
// or `&'static (drop_glue, size, align)` created by a `const fn`.
|
2015-08-12 02:50:52 -05:00
|
|
|
// Requirements:
|
|
|
|
// * rvalue promotion (issue #1056)
|
|
|
|
// * mem::{size_of, align_of} must be const fns
|
2015-03-14 21:01:57 -05:00
|
|
|
struct TyDesc {
|
|
|
|
drop_glue: fn(*const i8),
|
|
|
|
size: usize,
|
2015-10-11 21:33:51 -05:00
|
|
|
align: usize,
|
2015-03-14 21:01:57 -05:00
|
|
|
}
|
|
|
|
|
2015-11-22 20:32:40 -06:00
|
|
|
trait AllTypes {
|
|
|
|
fn dummy(&self) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: ?Sized> AllTypes for T {}
|
2015-03-31 18:58:15 -05:00
|
|
|
|
2015-03-14 21:01:57 -05:00
|
|
|
unsafe fn get_tydesc<T>() -> *const TyDesc {
|
|
|
|
use std::raw::TraitObject;
|
|
|
|
|
2015-08-12 02:50:52 -05:00
|
|
|
let ptr = &*(heap::EMPTY as *const T);
|
2015-03-14 21:01:57 -05:00
|
|
|
|
|
|
|
// Can use any trait that is implemented for all types.
|
2015-03-31 18:58:15 -05:00
|
|
|
let obj = mem::transmute::<&AllTypes, TraitObject>(ptr);
|
2015-03-14 21:01:57 -05:00
|
|
|
obj.vtable as *const TyDesc
|
|
|
|
}
|
|
|
|
|
2015-02-02 07:10:36 -06:00
|
|
|
impl<'longer_than_self> Arena<'longer_than_self> {
|
2015-08-12 02:50:52 -05:00
|
|
|
// Grows a given chunk and returns `false`, or replaces it with a bigger
|
|
|
|
// chunk and returns `true`.
|
|
|
|
// This method is shared by both parts of the arena.
|
2015-08-12 02:18:41 -05:00
|
|
|
#[cold]
|
2015-08-12 02:50:52 -05:00
|
|
|
fn alloc_grow(&self, head: &mut Chunk, used_cap: usize, n_bytes: usize) -> bool {
|
|
|
|
if head.data.reserve_in_place(used_cap, n_bytes) {
|
|
|
|
// In-place reallocation succeeded.
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
// Allocate a new chunk.
|
|
|
|
let new_min_chunk_size = cmp::max(n_bytes, head.capacity());
|
|
|
|
let new_chunk = Chunk::new((new_min_chunk_size + 1).next_power_of_two(), false);
|
|
|
|
let old_chunk = mem::replace(head, new_chunk);
|
|
|
|
if old_chunk.fill.get() != 0 {
|
|
|
|
self.chunks.borrow_mut().push(old_chunk);
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
2012-10-05 16:58:42 -05:00
|
|
|
}
|
|
|
|
|
2015-08-12 02:50:52 -05:00
|
|
|
// Functions for the copyable part of the arena.
|
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2015-02-09 01:00:46 -06:00
|
|
|
fn alloc_copy_inner(&self, n_bytes: usize, align: usize) -> *const u8 {
|
2015-08-12 02:50:52 -05:00
|
|
|
let mut copy_head = self.copy_head.borrow_mut();
|
|
|
|
let fill = copy_head.fill.get();
|
|
|
|
let mut start = round_up(fill, align);
|
|
|
|
let mut end = start + n_bytes;
|
|
|
|
|
|
|
|
if end > copy_head.capacity() {
|
|
|
|
if self.alloc_grow(&mut *copy_head, fill, end - fill) {
|
|
|
|
// Continuing with a newly allocated chunk
|
|
|
|
start = 0;
|
|
|
|
end = n_bytes;
|
|
|
|
copy_head.is_copy.set(true);
|
2015-08-12 02:18:41 -05:00
|
|
|
}
|
2014-06-09 22:29:36 -05:00
|
|
|
}
|
2012-10-05 16:58:42 -05:00
|
|
|
|
2014-06-09 22:29:36 -05:00
|
|
|
copy_head.fill.set(end);
|
2012-10-05 16:58:42 -05:00
|
|
|
|
2015-10-11 21:33:51 -05:00
|
|
|
unsafe { copy_head.as_ptr().offset(start as isize) }
|
2012-10-05 16:58:42 -05:00
|
|
|
}
|
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2015-10-13 04:53:57 -05:00
|
|
|
fn alloc_copy<T, F>(&self, op: F) -> &mut T
|
|
|
|
where F: FnOnce() -> T
|
|
|
|
{
|
2012-10-05 16:58:42 -05:00
|
|
|
unsafe {
|
2015-10-11 21:33:51 -05:00
|
|
|
let ptr = self.alloc_copy_inner(mem::size_of::<T>(), mem::align_of::<T>());
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 12:34:51 -05:00
|
|
|
let ptr = ptr as *mut T;
|
2014-05-29 19:40:18 -05:00
|
|
|
ptr::write(&mut (*ptr), op());
|
2015-09-07 17:36:29 -05:00
|
|
|
&mut *ptr
|
2012-10-05 16:58:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 02:50:52 -05:00
|
|
|
// Functions for the non-copyable part of the arena.
|
2012-10-05 16:58:42 -05:00
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2015-10-11 21:33:51 -05:00
|
|
|
fn alloc_noncopy_inner(&self, n_bytes: usize, align: usize) -> (*const u8, *const u8) {
|
2015-08-12 02:50:52 -05:00
|
|
|
let mut head = self.head.borrow_mut();
|
|
|
|
let fill = head.fill.get();
|
|
|
|
|
|
|
|
let mut tydesc_start = fill;
|
|
|
|
let after_tydesc = fill + mem::size_of::<*const TyDesc>();
|
|
|
|
let mut start = round_up(after_tydesc, align);
|
|
|
|
let mut end = round_up(start + n_bytes, mem::align_of::<*const TyDesc>());
|
|
|
|
|
|
|
|
if end > head.capacity() {
|
|
|
|
if self.alloc_grow(&mut *head, tydesc_start, end - tydesc_start) {
|
|
|
|
// Continuing with a newly allocated chunk
|
|
|
|
tydesc_start = 0;
|
|
|
|
start = round_up(mem::size_of::<*const TyDesc>(), align);
|
|
|
|
end = round_up(start + n_bytes, mem::align_of::<*const TyDesc>());
|
|
|
|
}
|
2014-06-09 22:29:36 -05:00
|
|
|
}
|
2012-10-05 16:58:42 -05:00
|
|
|
|
2015-08-12 02:50:52 -05:00
|
|
|
head.fill.set(end);
|
2012-10-05 16:58:42 -05:00
|
|
|
|
2014-06-09 22:29:36 -05:00
|
|
|
unsafe {
|
|
|
|
let buf = head.as_ptr();
|
2015-10-11 21:33:51 -05:00
|
|
|
(buf.offset(tydesc_start as isize),
|
|
|
|
buf.offset(start as isize))
|
2012-10-05 16:58:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2015-10-13 04:53:57 -05:00
|
|
|
fn alloc_noncopy<T, F>(&self, op: F) -> &mut T
|
|
|
|
where F: FnOnce() -> T
|
|
|
|
{
|
2012-10-05 16:58:42 -05:00
|
|
|
unsafe {
|
2013-06-20 04:39:49 -05:00
|
|
|
let tydesc = get_tydesc::<T>();
|
2015-10-11 21:33:51 -05:00
|
|
|
let (ty_ptr, ptr) = self.alloc_noncopy_inner(mem::size_of::<T>(), mem::align_of::<T>());
|
2015-02-09 01:00:46 -06:00
|
|
|
let ty_ptr = ty_ptr as *mut usize;
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 12:34:51 -05:00
|
|
|
let ptr = ptr as *mut T;
|
2012-10-05 16:58:42 -05:00
|
|
|
// Write in our tydesc along with a bit indicating that it
|
|
|
|
// has *not* been initialized yet.
|
2015-07-23 20:04:55 -05:00
|
|
|
*ty_ptr = bitpack_tydesc_ptr(tydesc, false);
|
2012-10-05 16:58:42 -05:00
|
|
|
// Actually initialize it
|
2015-10-11 21:33:51 -05:00
|
|
|
ptr::write(&mut (*ptr), op());
|
2012-10-05 16:58:42 -05:00
|
|
|
// Now that we are done, update the tydesc to indicate that
|
|
|
|
// the object is there.
|
|
|
|
*ty_ptr = bitpack_tydesc_ptr(tydesc, true);
|
|
|
|
|
2015-09-07 17:36:29 -05:00
|
|
|
&mut *ptr
|
2012-10-05 16:58:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Allocates a new item in the arena, using `op` to initialize the value,
|
|
|
|
/// and returns a reference to it.
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2015-10-13 04:53:57 -05:00
|
|
|
pub fn alloc<T: 'longer_than_self, F>(&self, op: F) -> &mut T
|
|
|
|
where F: FnOnce() -> T
|
|
|
|
{
|
2013-04-10 15:14:06 -05:00
|
|
|
unsafe {
|
2013-06-16 10:50:16 -05:00
|
|
|
if intrinsics::needs_drop::<T>() {
|
2014-06-09 22:29:36 -05:00
|
|
|
self.alloc_noncopy(op)
|
2013-06-16 10:50:16 -05:00
|
|
|
} else {
|
2014-06-09 22:29:36 -05:00
|
|
|
self.alloc_copy(op)
|
2013-04-10 15:14:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-13 11:10:19 -05:00
|
|
|
|
|
|
|
/// Allocates a slice of bytes of requested length. The bytes are not guaranteed to be zero
|
|
|
|
/// if the arena has previously been cleared.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if the requested length is too large and causes overflow.
|
|
|
|
pub fn alloc_bytes(&self, len: usize) -> &mut [u8] {
|
|
|
|
unsafe {
|
|
|
|
// Check for overflow.
|
|
|
|
self.copy_head.borrow().fill.get().checked_add(len).expect("length overflow");
|
|
|
|
let ptr = self.alloc_copy_inner(len, 1);
|
|
|
|
intrinsics::assume(!ptr.is_null());
|
|
|
|
slice::from_raw_parts_mut(ptr as *mut _, len)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clears the arena. Deallocates all but the longest chunk which may be reused.
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
self.head.borrow().destroy();
|
|
|
|
self.head.borrow().fill.set(0);
|
|
|
|
self.copy_head.borrow().fill.set(0);
|
|
|
|
for chunk in self.chunks.borrow().iter() {
|
|
|
|
if !chunk.is_copy.get() {
|
|
|
|
chunk.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.chunks.borrow_mut().clear();
|
|
|
|
}
|
|
|
|
}
|
2012-10-05 16:58:42 -05:00
|
|
|
}
|
2012-03-20 21:06:04 -05:00
|
|
|
|
2014-04-04 06:57:39 -05:00
|
|
|
/// A faster arena that can hold objects of only one type.
|
2014-01-06 19:03:30 -06:00
|
|
|
pub struct TypedArena<T> {
|
|
|
|
/// A pointer to the next object to be allocated.
|
2015-08-11 22:52:37 -05:00
|
|
|
ptr: Cell<*mut T>,
|
2014-01-06 19:03:30 -06:00
|
|
|
|
|
|
|
/// A pointer to the end of the allocated area. When this pointer is
|
|
|
|
/// reached, a new chunk is allocated.
|
2015-08-11 22:52:37 -05:00
|
|
|
end: Cell<*mut T>,
|
2014-01-06 19:03:30 -06:00
|
|
|
|
2015-08-11 22:52:37 -05:00
|
|
|
/// A vector arena segments.
|
|
|
|
chunks: RefCell<Vec<TypedArenaChunk<T>>>,
|
2015-01-21 13:02:52 -06:00
|
|
|
|
|
|
|
/// Marker indicating that dropping the arena causes its owned
|
|
|
|
/// instances of `T` to be dropped.
|
2015-08-13 11:48:34 -05:00
|
|
|
_own: PhantomData<T>,
|
2014-01-06 19:03:30 -06:00
|
|
|
}
|
|
|
|
|
2014-02-21 06:25:17 -06:00
|
|
|
struct TypedArenaChunk<T> {
|
2014-01-06 19:03:30 -06:00
|
|
|
/// Pointer to the next arena segment.
|
2015-08-11 22:52:37 -05:00
|
|
|
storage: RawVec<T>,
|
2014-09-05 08:08:30 -05:00
|
|
|
}
|
|
|
|
|
2014-02-21 06:25:17 -06:00
|
|
|
impl<T> TypedArenaChunk<T> {
|
2014-01-06 19:03:30 -06:00
|
|
|
#[inline]
|
2015-08-11 22:52:37 -05:00
|
|
|
unsafe fn new(capacity: usize) -> TypedArenaChunk<T> {
|
|
|
|
TypedArenaChunk { storage: RawVec::with_capacity(capacity) }
|
2014-04-25 20:24:51 -05:00
|
|
|
}
|
|
|
|
|
2015-08-11 22:52:37 -05:00
|
|
|
/// Destroys this arena chunk.
|
2014-01-06 19:03:30 -06:00
|
|
|
#[inline]
|
2015-02-09 01:00:46 -06:00
|
|
|
unsafe fn destroy(&mut self, len: usize) {
|
2015-08-11 22:52:37 -05:00
|
|
|
// The branch on needs_drop() is an -O1 performance optimization.
|
|
|
|
// Without the branch, dropping TypedArena<u8> takes linear time.
|
2014-02-21 06:25:17 -06:00
|
|
|
if intrinsics::needs_drop::<T>() {
|
|
|
|
let mut start = self.start();
|
2015-08-11 22:52:37 -05:00
|
|
|
// Destroy all allocated objects.
|
2015-01-26 14:46:12 -06:00
|
|
|
for _ in 0..len {
|
2015-08-11 22:52:37 -05:00
|
|
|
ptr::drop_in_place(start);
|
|
|
|
start = start.offset(1);
|
2014-01-06 19:03:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a pointer to the first allocated object.
|
|
|
|
#[inline]
|
2015-08-11 22:52:37 -05:00
|
|
|
fn start(&self) -> *mut T {
|
|
|
|
self.storage.ptr()
|
2014-01-06 19:03:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a pointer to the end of the allocated space.
|
|
|
|
#[inline]
|
2015-08-11 22:52:37 -05:00
|
|
|
fn end(&self) -> *mut T {
|
2014-01-06 19:03:30 -06:00
|
|
|
unsafe {
|
2015-08-11 22:52:37 -05:00
|
|
|
if mem::size_of::<T>() == 0 {
|
|
|
|
// A pointer as large as possible for zero-sized elements.
|
|
|
|
!0 as *mut T
|
|
|
|
} else {
|
|
|
|
self.start().offset(self.storage.cap() as isize)
|
|
|
|
}
|
2014-01-06 19:03:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-11 22:52:37 -05:00
|
|
|
const PAGE: usize = 4096;
|
|
|
|
|
2014-01-06 19:03:30 -06:00
|
|
|
impl<T> TypedArena<T> {
|
2015-08-11 22:52:37 -05:00
|
|
|
/// Creates a new `TypedArena` with preallocated space for many objects.
|
2014-01-06 19:03:30 -06:00
|
|
|
#[inline]
|
|
|
|
pub fn new() -> TypedArena<T> {
|
2015-08-11 22:52:37 -05:00
|
|
|
// Reserve at least one page.
|
|
|
|
let elem_size = cmp::max(1, mem::size_of::<T>());
|
|
|
|
TypedArena::with_capacity(PAGE / elem_size)
|
2014-01-06 19:03:30 -06:00
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Creates a new `TypedArena` with preallocated space for the given number of
|
2014-01-06 19:03:30 -06:00
|
|
|
/// objects.
|
|
|
|
#[inline]
|
2015-02-09 01:00:46 -06:00
|
|
|
pub fn with_capacity(capacity: usize) -> TypedArena<T> {
|
2014-09-05 08:08:30 -05:00
|
|
|
unsafe {
|
2015-08-11 22:52:37 -05:00
|
|
|
let chunk = TypedArenaChunk::<T>::new(cmp::max(1, capacity));
|
2014-09-05 08:08:30 -05:00
|
|
|
TypedArena {
|
2015-08-11 22:52:37 -05:00
|
|
|
ptr: Cell::new(chunk.start()),
|
|
|
|
end: Cell::new(chunk.end()),
|
|
|
|
chunks: RefCell::new(vec![chunk]),
|
2015-08-13 11:48:34 -05:00
|
|
|
_own: PhantomData,
|
2014-09-05 08:08:30 -05:00
|
|
|
}
|
2014-01-06 19:03:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Allocates an object in the `TypedArena`, returning a reference to it.
|
2014-01-06 19:03:30 -06:00
|
|
|
#[inline]
|
2014-10-27 15:31:41 -05:00
|
|
|
pub fn alloc(&self, object: T) -> &mut T {
|
2014-06-09 22:41:44 -05:00
|
|
|
if self.ptr == self.end {
|
|
|
|
self.grow()
|
|
|
|
}
|
2014-01-06 19:03:30 -06:00
|
|
|
|
2015-09-07 17:36:29 -05:00
|
|
|
unsafe {
|
2015-08-11 22:52:37 -05:00
|
|
|
if mem::size_of::<T>() == 0 {
|
|
|
|
self.ptr.set(intrinsics::arith_offset(self.ptr.get() as *mut u8, 1) as *mut T);
|
|
|
|
let ptr = heap::EMPTY as *mut T;
|
|
|
|
// Don't drop the object. This `write` is equivalent to `forget`.
|
|
|
|
ptr::write(ptr, object);
|
|
|
|
&mut *ptr
|
|
|
|
} else {
|
|
|
|
let ptr = self.ptr.get();
|
|
|
|
// Advance the pointer.
|
|
|
|
self.ptr.set(self.ptr.get().offset(1));
|
|
|
|
// Write into uninitialized memory.
|
|
|
|
ptr::write(ptr, object);
|
|
|
|
&mut *ptr
|
|
|
|
}
|
2015-09-07 17:36:29 -05:00
|
|
|
}
|
2014-01-06 19:03:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Grows the arena.
|
|
|
|
#[inline(never)]
|
2015-08-11 22:52:37 -05:00
|
|
|
#[cold]
|
2014-06-09 22:41:44 -05:00
|
|
|
fn grow(&self) {
|
2014-09-05 08:08:30 -05:00
|
|
|
unsafe {
|
2015-08-11 22:52:37 -05:00
|
|
|
let mut chunks = self.chunks.borrow_mut();
|
|
|
|
let prev_capacity = chunks.last().unwrap().storage.cap();
|
|
|
|
let new_capacity = prev_capacity.checked_mul(2).unwrap();
|
|
|
|
if chunks.last_mut().unwrap().storage.double_in_place() {
|
|
|
|
self.end.set(chunks.last().unwrap().end());
|
|
|
|
} else {
|
|
|
|
let chunk = TypedArenaChunk::<T>::new(new_capacity);
|
|
|
|
self.ptr.set(chunk.start());
|
|
|
|
self.end.set(chunk.end());
|
|
|
|
chunks.push(chunk);
|
|
|
|
}
|
2014-09-05 08:08:30 -05:00
|
|
|
}
|
2014-01-06 19:03:30 -06:00
|
|
|
}
|
2015-08-13 11:10:19 -05:00
|
|
|
/// Clears the arena. Deallocates all but the longest chunk which may be reused.
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
// Clear the last chunk, which is partially filled.
|
|
|
|
let mut chunks_borrow = self.chunks.borrow_mut();
|
|
|
|
let last_idx = chunks_borrow.len() - 1;
|
|
|
|
self.clear_last_chunk(&mut chunks_borrow[last_idx]);
|
|
|
|
// If `T` is ZST, code below has no effect.
|
|
|
|
for mut chunk in chunks_borrow.drain(..last_idx) {
|
|
|
|
let cap = chunk.storage.cap();
|
|
|
|
chunk.destroy(cap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drops the contents of the last chunk. The last chunk is partially empty, unlike all other
|
|
|
|
// chunks.
|
|
|
|
fn clear_last_chunk(&self, last_chunk: &mut TypedArenaChunk<T>) {
|
|
|
|
// Determine how much was filled.
|
|
|
|
let start = last_chunk.start() as usize;
|
|
|
|
// We obtain the value of the pointer to the first uninitialized element.
|
|
|
|
let end = self.ptr.get() as usize;
|
|
|
|
// We then calculate the number of elements to be dropped in the last chunk,
|
|
|
|
// which is the filled area's length.
|
|
|
|
let diff = if mem::size_of::<T>() == 0 {
|
|
|
|
// `T` is ZST. It can't have a drop flag, so the value here doesn't matter. We get
|
|
|
|
// the number of zero-sized values in the last and only chunk, just out of caution.
|
|
|
|
// Recall that `end` was incremented for each allocated value.
|
|
|
|
end - start
|
|
|
|
} else {
|
|
|
|
(end - start) / mem::size_of::<T>()
|
|
|
|
};
|
|
|
|
// Pass that to the `destroy` method.
|
|
|
|
unsafe {
|
|
|
|
last_chunk.destroy(diff);
|
|
|
|
}
|
|
|
|
// Reset the chunk.
|
|
|
|
self.ptr.set(last_chunk.start());
|
|
|
|
}
|
2014-01-06 19:03:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Drop for TypedArena<T> {
|
2015-07-17 09:12:35 -05:00
|
|
|
#[unsafe_destructor_blind_to_params]
|
2014-01-06 19:03:30 -06:00
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
2014-09-05 08:08:30 -05:00
|
|
|
// Determine how much was filled.
|
2015-08-11 22:52:37 -05:00
|
|
|
let mut chunks_borrow = self.chunks.borrow_mut();
|
|
|
|
let mut last_chunk = chunks_borrow.pop().unwrap();
|
2015-08-13 11:10:19 -05:00
|
|
|
// Drop the contents of the last chunk.
|
|
|
|
self.clear_last_chunk(&mut last_chunk);
|
|
|
|
// The last chunk will be dropped. Destroy all other chunks.
|
2015-08-11 22:52:37 -05:00
|
|
|
for chunk in chunks_borrow.iter_mut() {
|
|
|
|
let cap = chunk.storage.cap();
|
|
|
|
chunk.destroy(cap);
|
|
|
|
}
|
2015-08-13 11:10:19 -05:00
|
|
|
// RawVec handles deallocation of `last_chunk` and `self.chunks`.
|
2014-01-06 19:03:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 11:48:34 -05:00
|
|
|
unsafe impl<T: Send> Send for TypedArena<T> {}
|
|
|
|
|
2014-01-06 19:03:30 -06:00
|
|
|
#[cfg(test)]
|
2014-02-13 19:49:11 -06:00
|
|
|
mod tests {
|
|
|
|
extern crate test;
|
2014-03-31 20:16:35 -05:00
|
|
|
use self::test::Bencher;
|
2014-01-06 19:03:30 -06:00
|
|
|
use super::{Arena, TypedArena};
|
2015-08-13 11:10:19 -05:00
|
|
|
use std::rc::Rc;
|
2014-01-06 19:03:30 -06:00
|
|
|
|
2014-09-09 04:32:58 -05:00
|
|
|
#[allow(dead_code)]
|
2014-01-06 19:03:30 -06:00
|
|
|
struct Point {
|
2015-02-09 01:00:46 -06:00
|
|
|
x: i32,
|
|
|
|
y: i32,
|
|
|
|
z: i32,
|
2014-01-06 19:03:30 -06:00
|
|
|
}
|
|
|
|
|
2015-02-02 07:10:36 -06:00
|
|
|
#[test]
|
|
|
|
fn test_arena_alloc_nested() {
|
2015-10-11 21:33:51 -05:00
|
|
|
struct Inner {
|
|
|
|
value: u8,
|
|
|
|
}
|
|
|
|
struct Outer<'a> {
|
|
|
|
inner: &'a Inner,
|
|
|
|
}
|
|
|
|
enum EI<'e> {
|
|
|
|
I(Inner),
|
|
|
|
O(Outer<'e>),
|
|
|
|
}
|
2015-02-02 07:10:36 -06:00
|
|
|
|
|
|
|
struct Wrap<'a>(TypedArena<EI<'a>>);
|
|
|
|
|
|
|
|
impl<'a> Wrap<'a> {
|
2015-10-11 21:33:51 -05:00
|
|
|
fn alloc_inner<F: Fn() -> Inner>(&self, f: F) -> &Inner {
|
2015-02-02 07:10:36 -06:00
|
|
|
let r: &EI = self.0.alloc(EI::I(f()));
|
|
|
|
if let &EI::I(ref i) = r {
|
|
|
|
i
|
|
|
|
} else {
|
|
|
|
panic!("mismatch");
|
|
|
|
}
|
|
|
|
}
|
2015-10-11 21:33:51 -05:00
|
|
|
fn alloc_outer<F: Fn() -> Outer<'a>>(&self, f: F) -> &Outer {
|
2015-02-02 07:10:36 -06:00
|
|
|
let r: &EI = self.0.alloc(EI::O(f()));
|
|
|
|
if let &EI::O(ref o) = r {
|
|
|
|
o
|
|
|
|
} else {
|
|
|
|
panic!("mismatch");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let arena = Wrap(TypedArena::new());
|
|
|
|
|
2015-10-11 21:33:51 -05:00
|
|
|
let result = arena.alloc_outer(|| {
|
|
|
|
Outer { inner: arena.alloc_inner(|| Inner { value: 10 }) }
|
|
|
|
});
|
2015-02-02 07:10:36 -06:00
|
|
|
|
|
|
|
assert_eq!(result.inner.value, 10);
|
|
|
|
}
|
|
|
|
|
2014-01-06 19:03:30 -06:00
|
|
|
#[test]
|
2014-03-26 18:01:11 -05:00
|
|
|
pub fn test_copy() {
|
2014-01-06 19:03:30 -06:00
|
|
|
let arena = TypedArena::new();
|
2015-01-24 08:39:32 -06:00
|
|
|
for _ in 0..100000 {
|
2015-10-11 21:33:51 -05:00
|
|
|
arena.alloc(Point { x: 1, y: 2, z: 3 });
|
2014-01-06 19:03:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
pub fn bench_copy(b: &mut Bencher) {
|
2014-01-06 19:03:30 -06:00
|
|
|
let arena = TypedArena::new();
|
2015-10-11 21:33:51 -05:00
|
|
|
b.iter(|| arena.alloc(Point { x: 1, y: 2, z: 3 }))
|
2014-01-06 19:03:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
pub fn bench_copy_nonarena(b: &mut Bencher) {
|
|
|
|
b.iter(|| {
|
2015-08-12 02:18:41 -05:00
|
|
|
let _: Box<_> = Box::new(Point {
|
|
|
|
x: 1,
|
|
|
|
y: 2,
|
|
|
|
z: 3
|
|
|
|
});
|
2014-01-06 19:03:30 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
pub fn bench_copy_old_arena(b: &mut Bencher) {
|
2014-01-06 19:03:30 -06:00
|
|
|
let arena = Arena::new();
|
2015-10-11 21:33:51 -05:00
|
|
|
b.iter(|| arena.alloc(|| Point { x: 1, y: 2, z: 3 }))
|
2014-01-06 19:03:30 -06:00
|
|
|
}
|
|
|
|
|
2014-09-09 04:32:58 -05:00
|
|
|
#[allow(dead_code)]
|
2014-03-26 18:01:11 -05:00
|
|
|
struct Noncopy {
|
2014-05-22 18:57:53 -05:00
|
|
|
string: String,
|
2015-02-09 01:00:46 -06:00
|
|
|
array: Vec<i32>,
|
2014-01-06 19:03:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2014-03-26 18:01:11 -05:00
|
|
|
pub fn test_noncopy() {
|
2014-01-06 19:03:30 -06:00
|
|
|
let arena = TypedArena::new();
|
2015-01-24 08:39:32 -06:00
|
|
|
for _ in 0..100000 {
|
2014-03-26 18:01:11 -05:00
|
|
|
arena.alloc(Noncopy {
|
2014-05-25 05:17:19 -05:00
|
|
|
string: "hello world".to_string(),
|
2015-11-22 20:32:40 -06:00
|
|
|
array: vec![1, 2, 3, 4, 5],
|
2014-01-06 19:03:30 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-11 22:52:37 -05:00
|
|
|
#[test]
|
2015-08-13 11:10:19 -05:00
|
|
|
pub fn test_typed_arena_zero_sized() {
|
2015-08-11 22:52:37 -05:00
|
|
|
let arena = TypedArena::new();
|
|
|
|
for _ in 0..100000 {
|
|
|
|
arena.alloc(());
|
|
|
|
}
|
|
|
|
}
|
2015-08-13 11:10:19 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_arena_zero_sized() {
|
|
|
|
let arena = Arena::new();
|
|
|
|
for _ in 0..1000 {
|
|
|
|
for _ in 0..100 {
|
|
|
|
arena.alloc(|| ());
|
|
|
|
}
|
|
|
|
arena.alloc(|| Point {
|
|
|
|
x: 1,
|
|
|
|
y: 2,
|
|
|
|
z: 3,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_typed_arena_clear() {
|
|
|
|
let mut arena = TypedArena::new();
|
|
|
|
for _ in 0..10 {
|
|
|
|
arena.clear();
|
|
|
|
for _ in 0..10000 {
|
|
|
|
arena.alloc(Point {
|
|
|
|
x: 1,
|
|
|
|
y: 2,
|
|
|
|
z: 3,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_arena_clear() {
|
|
|
|
let mut arena = Arena::new();
|
|
|
|
for _ in 0..10 {
|
|
|
|
arena.clear();
|
|
|
|
for _ in 0..10000 {
|
|
|
|
arena.alloc(|| Point {
|
|
|
|
x: 1,
|
|
|
|
y: 2,
|
|
|
|
z: 3,
|
|
|
|
});
|
|
|
|
arena.alloc(|| Noncopy {
|
|
|
|
string: "hello world".to_string(),
|
|
|
|
array: vec![],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_arena_alloc_bytes() {
|
|
|
|
let arena = Arena::new();
|
|
|
|
for i in 0..10000 {
|
|
|
|
arena.alloc(|| Point {
|
|
|
|
x: 1,
|
|
|
|
y: 2,
|
|
|
|
z: 3,
|
|
|
|
});
|
|
|
|
for byte in arena.alloc_bytes(i % 42).iter_mut() {
|
|
|
|
*byte = i as u8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-14 07:20:09 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_arena_destructors() {
|
|
|
|
let arena = Arena::new();
|
|
|
|
for i in 0..10 {
|
|
|
|
// Arena allocate something with drop glue to make sure it
|
|
|
|
// doesn't leak.
|
|
|
|
arena.alloc(|| Rc::new(i));
|
|
|
|
// Allocate something with funny size and alignment, to keep
|
|
|
|
// things interesting.
|
|
|
|
arena.alloc(|| [0u8, 1u8, 2u8]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_arena_destructors_fail() {
|
|
|
|
let arena = Arena::new();
|
|
|
|
// Put some stuff in the arena.
|
|
|
|
for i in 0..10 {
|
|
|
|
// Arena allocate something with drop glue to make sure it
|
|
|
|
// doesn't leak.
|
|
|
|
arena.alloc(|| { Rc::new(i) });
|
|
|
|
// Allocate something with funny size and alignment, to keep
|
|
|
|
// things interesting.
|
|
|
|
arena.alloc(|| { [0u8, 1, 2] });
|
|
|
|
}
|
|
|
|
// Now, panic while allocating
|
|
|
|
arena.alloc::<Rc<i32>, _>(|| {
|
|
|
|
panic!();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
pub fn bench_noncopy(b: &mut Bencher) {
|
|
|
|
let arena = TypedArena::new();
|
|
|
|
b.iter(|| {
|
|
|
|
arena.alloc(Noncopy {
|
|
|
|
string: "hello world".to_string(),
|
|
|
|
array: vec!( 1, 2, 3, 4, 5 ),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
pub fn bench_noncopy_nonarena(b: &mut Bencher) {
|
|
|
|
b.iter(|| {
|
|
|
|
let _: Box<_> = Box::new(Noncopy {
|
|
|
|
string: "hello world".to_string(),
|
|
|
|
array: vec!( 1, 2, 3, 4, 5 ),
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
pub fn bench_noncopy_old_arena(b: &mut Bencher) {
|
|
|
|
let arena = Arena::new();
|
|
|
|
b.iter(|| {
|
|
|
|
arena.alloc(|| Noncopy {
|
|
|
|
string: "hello world".to_string(),
|
|
|
|
array: vec!( 1, 2, 3, 4, 5 ),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2014-01-06 19:03:30 -06:00
|
|
|
}
|