//! This crate implements `TypedArena`, a simple arena that can only hold
//! objects of a single type.
-#![doc(html_root_url = "https://doc.rust-lang.org/nightly/",
- test(no_crate_inject, attr(deny(warnings))))]
-
+#![doc(
+ html_root_url = "https://doc.rust-lang.org/nightly/",
+ test(no_crate_inject, attr(deny(warnings)))
+)]
#![feature(core_intrinsics)]
#![feature(dropck_eyepatch)]
#![feature(raw_vec_internals)]
#![cfg_attr(test, feature(test))]
-
#![allow(deprecated)]
extern crate alloc;
use rustc_data_structures::cold_path;
-use rustc_data_structures::sync::MTLock;
use smallvec::SmallVec;
use std::cell::{Cell, RefCell};
impl<T> TypedArenaChunk<T> {
#[inline]
unsafe fn new(capacity: usize) -> TypedArenaChunk<T> {
- TypedArenaChunk {
- storage: RawVec::with_capacity(capacity),
- entries: 0,
- }
+ TypedArenaChunk { storage: RawVec::with_capacity(capacity), entries: 0 }
}
/// Destroys this arena chunk.
}
impl<T> TypedArena<T> {
- pub fn in_arena(&self, ptr: *const T) -> bool {
- let ptr = ptr as *const T as *mut T;
-
- self.chunks.borrow().iter().any(|chunk| chunk.start() <= ptr && ptr < chunk.end())
- }
/// Allocates an object in the `TypedArena`, returning a reference to it.
#[inline]
pub fn alloc(&self, object: T) -> &mut T {
unsafe {
if mem::size_of::<T>() == 0 {
- self.ptr
- .set(intrinsics::arith_offset(self.ptr.get() as *mut u8, 1)
- as *mut T);
+ self.ptr.set(intrinsics::arith_offset(self.ptr.get() as *mut u8, 1) as *mut T);
let ptr = mem::align_of::<T>() as *mut T;
// Don't drop the object. This `write` is equivalent to `forget`.
ptr::write(ptr, object);
#[inline]
pub fn alloc_from_iter<I: IntoIterator<Item = T>>(&self, iter: I) -> &mut [T] {
assert!(mem::size_of::<T>() != 0);
- let mut iter = iter.into_iter();
- let size_hint = iter.size_hint();
-
- match size_hint {
- (min, Some(max)) if min == max => {
- // We know the exact number of elements the iterator will produce here
- let len = min;
-
- if len == 0 {
- return &mut [];
- }
-
- self.ensure_capacity(len);
-
- let slice = self.ptr.get();
-
- unsafe {
- let mut ptr = self.ptr.get();
- for _ in 0..len {
- // Write into uninitialized memory.
- ptr::write(ptr, iter.next().unwrap());
- // Advance the pointer.
- ptr = ptr.offset(1);
- // Update the pointer per iteration so if `iter.next()` panics
- // we destroy the correct amount
- self.ptr.set(ptr);
- }
- slice::from_raw_parts_mut(slice, len)
- }
- }
- _ => {
- cold_path(move || -> &mut [T] {
- let mut vec: SmallVec<[_; 8]> = iter.collect();
- if vec.is_empty() {
- return &mut [];
- }
- // Move the content to the arena by copying it and then forgetting
- // the content of the SmallVec
- unsafe {
- let len = vec.len();
- let start_ptr = self.alloc_raw_slice(len);
- vec.as_ptr().copy_to_nonoverlapping(start_ptr, len);
- vec.set_len(0);
- slice::from_raw_parts_mut(start_ptr, len)
- }
- })
- }
+ let mut vec: SmallVec<[_; 8]> = iter.into_iter().collect();
+ if vec.is_empty() {
+ return &mut [];
+ }
+ // Move the content to the arena by copying it and then forgetting
+ // the content of the SmallVec
+ unsafe {
+ let len = vec.len();
+ let start_ptr = self.alloc_raw_slice(len);
+ vec.as_ptr().copy_to_nonoverlapping(start_ptr, len);
+ vec.set_len(0);
+ slice::from_raw_parts_mut(start_ptr, len)
}
}
self.clear_last_chunk(&mut last_chunk);
let len = chunks_borrow.len();
// If `T` is ZST, code below has no effect.
- for mut chunk in chunks_borrow.drain(..len-1) {
+ for mut chunk in chunks_borrow.drain(..len - 1) {
chunk.destroy(chunk.entries);
}
}
}
impl DroplessArena {
- pub fn in_arena<T: ?Sized>(&self, ptr: *const T) -> bool {
- let ptr = ptr as *const u8 as *mut u8;
-
- self.chunks.borrow().iter().any(|chunk| chunk.start() <= ptr && ptr < chunk.end())
- }
-
#[inline]
fn align(&self, align: usize) {
let final_address = ((self.ptr.get() as usize) + align - 1) & !(align - 1);
let (chunk, mut new_capacity);
if let Some(last_chunk) = chunks.last_mut() {
let used_bytes = self.ptr.get() as usize - last_chunk.start() as usize;
- if last_chunk
- .storage
- .reserve_in_place(used_bytes, needed_bytes)
- {
+ if last_chunk.storage.reserve_in_place(used_bytes, needed_bytes) {
self.end.set(last_chunk.end());
return;
} else {
let ptr = self.ptr.get();
// Set the pointer past ourselves
- self.ptr.set(
- intrinsics::arith_offset(self.ptr.get(), bytes as isize) as *mut u8,
- );
+ self.ptr.set(intrinsics::arith_offset(self.ptr.get(), bytes as isize) as *mut u8);
slice::from_raw_parts_mut(ptr, bytes)
}
}
pub fn alloc<T>(&self, object: T) -> &mut T {
assert!(!mem::needs_drop::<T>());
- let mem = self.alloc_raw(
- mem::size_of::<T>(),
- mem::align_of::<T>()) as *mut _ as *mut T;
+ let mem = self.alloc_raw(mem::size_of::<T>(), mem::align_of::<T>()) as *mut _ as *mut T;
unsafe {
// Write into uninitialized memory.
assert!(mem::size_of::<T>() != 0);
assert!(!slice.is_empty());
- let mem = self.alloc_raw(
- slice.len() * mem::size_of::<T>(),
- mem::align_of::<T>()) as *mut _ as *mut T;
+ let mem = self.alloc_raw(slice.len() * mem::size_of::<T>(), mem::align_of::<T>()) as *mut _
+ as *mut T;
unsafe {
let arena_slice = slice::from_raw_parts_mut(mem, slice.len());
let len = min;
if len == 0 {
- return &mut []
+ return &mut [];
}
let size = len.checked_mul(mem::size_of::<T>()).unwrap();
let mem = self.alloc_raw(size, mem::align_of::<T>()) as *mut _ as *mut T;
- unsafe {
- self.write_from_iter(iter, len, mem)
- }
+ unsafe { self.write_from_iter(iter, len, mem) }
}
(_, _) => {
cold_path(move || -> &mut [T] {
// the content of the SmallVec
unsafe {
let len = vec.len();
- let start_ptr = self.alloc_raw(
- len * mem::size_of::<T>(),
- mem::align_of::<T>()
- ) as *mut _ as *mut T;
+ let start_ptr = self
+ .alloc_raw(len * mem::size_of::<T>(), mem::align_of::<T>())
+ as *mut _ as *mut T;
vec.as_ptr().copy_to_nonoverlapping(start_ptr, len);
vec.set_len(0);
slice::from_raw_parts_mut(start_ptr, len)
}
}
-#[derive(Default)]
-// FIXME(@Zoxc): this type is entirely unused in rustc
-pub struct SyncTypedArena<T> {
- lock: MTLock<TypedArena<T>>,
-}
-
-impl<T> SyncTypedArena<T> {
- #[inline(always)]
- pub fn alloc(&self, object: T) -> &mut T {
- // Extend the lifetime of the result since it's limited to the lock guard
- unsafe { &mut *(self.lock.lock().alloc(object) as *mut T) }
- }
-
- #[inline(always)]
- pub fn alloc_slice(&self, slice: &[T]) -> &mut [T]
- where
- T: Copy,
- {
- // Extend the lifetime of the result since it's limited to the lock guard
- unsafe { &mut *(self.lock.lock().alloc_slice(slice) as *mut [T]) }
- }
-
- #[inline(always)]
- pub fn clear(&mut self) {
- self.lock.get_mut().clear();
- }
-}
-
-#[derive(Default)]
-pub struct SyncDroplessArena {
- lock: MTLock<DroplessArena>,
-}
-
-impl SyncDroplessArena {
- #[inline(always)]
- pub fn in_arena<T: ?Sized>(&self, ptr: *const T) -> bool {
- self.lock.lock().in_arena(ptr)
- }
-
- #[inline(always)]
- pub fn alloc_raw(&self, bytes: usize, align: usize) -> &mut [u8] {
- // Extend the lifetime of the result since it's limited to the lock guard
- unsafe { &mut *(self.lock.lock().alloc_raw(bytes, align) as *mut [u8]) }
- }
-
- #[inline(always)]
- pub fn alloc<T>(&self, object: T) -> &mut T {
- // Extend the lifetime of the result since it's limited to the lock guard
- unsafe { &mut *(self.lock.lock().alloc(object) as *mut T) }
- }
-
- #[inline(always)]
- pub fn alloc_slice<T>(&self, slice: &[T]) -> &mut [T]
- where
- T: Copy,
- {
- // Extend the lifetime of the result since it's limited to the lock guard
- unsafe { &mut *(self.lock.lock().alloc_slice(slice) as *mut [T]) }
- }
-}
-
#[cfg(test)]
mod tests;