use core::{
alloc::{Layout, LayoutErr},
cell::{Cell, UnsafeCell},
mem::{self, MaybeUninit},
ops,
ptr::{self, NonNull},
};
use alloc_traits::AllocTime;
use crate::bump::{Allocation, Failure, Level};
use crate::leaked::LeakBox;
#[cfg_attr(feature = "alloc", doc = "```")]
#[cfg_attr(not(feature = "alloc"), doc = "```ignore")]
#[repr(C)]
pub struct Bump<T> {
_index: Cell<usize>,
_data: UnsafeCell<MaybeUninit<T>>,
}
#[repr(C)]
pub struct MemBump {
index: Cell<usize>,
data: [UnsafeCell<MaybeUninit<u8>>],
}
impl<T> Bump<T> {
pub fn uninit() -> Self {
Bump {
_index: Cell::new(0),
_data: UnsafeCell::new(MaybeUninit::uninit()),
}
}
pub fn zeroed() -> Self {
Bump {
_index: Cell::new(0),
_data: UnsafeCell::new(MaybeUninit::zeroed()),
}
}
}
#[cfg(feature = "alloc")]
impl MemBump {
pub fn new(capacity: usize) -> alloc::boxed::Box<Self> {
let layout = Self::layout_from_size(capacity)
.expect("Bad layout");
let ptr = NonNull::new(unsafe {
alloc::alloc::alloc(layout)
}).unwrap_or_else(|| {
alloc::alloc::handle_alloc_error(layout)
});
let ptr = ptr::slice_from_raw_parts_mut(ptr.as_ptr(), capacity);
unsafe { alloc::boxed::Box::from_raw(ptr as *mut MemBump) }
}
fn header_layout() -> Layout {
Layout::new::<Cell<usize>>()
}
fn data_layout(size: usize) -> Result<Layout, LayoutErr> {
Layout::array::<UnsafeCell<MaybeUninit<u8>>>(size)
}
pub(crate) fn layout_from_size(size: usize) -> Result<Layout, LayoutErr> {
let data_tail = Self::data_layout(size)?;
let (layout, _) = Self::header_layout().extend(data_tail)?;
Ok(layout.pad_to_align())
}
}
impl MemBump {
pub(crate) const fn capacity(&self) -> usize {
self.data.len()
}
}
impl MemBump {
pub fn alloc(&self, layout: Layout) -> Option<NonNull<u8>> {
Some(self.try_alloc(layout)?.ptr)
}
pub fn alloc_at(&self, layout: Layout, level: Level)
-> Result<NonNull<u8>, Failure>
{
let Allocation { ptr, .. } = self.try_alloc_at(layout, level.0)?;
Ok(ptr)
}
pub fn get<V>(&self) -> Option<Allocation<V>> {
let alloc = self.try_alloc(Layout::new::<V>())?;
Some(Allocation {
lifetime: alloc.lifetime,
level: alloc.level,
ptr: alloc.ptr.cast(),
})
}
pub fn get_at<V>(&self, level: Level) -> Result<Allocation<V>, Failure> {
let alloc = self.try_alloc_at(Layout::new::<V>(), level.0)?;
Ok(Allocation {
lifetime: alloc.lifetime,
level: alloc.level,
ptr: alloc.ptr.cast(),
})
}
pub fn bump_box<'bump, T: 'bump>(&'bump self)
-> Result<LeakBox<'bump, MaybeUninit<T>>, Failure>
{
let allocation = self.get_at(self.level())?;
Ok(unsafe { allocation.uninit() }.into())
}
pub fn bump_array<'bump, T: 'bump>(&'bump self, n: usize)
-> Result<LeakBox<'bump, [MaybeUninit<T>]>, Failure>
{
let layout = Layout::array::<T>(n).map_err(|_| Failure::Exhausted)?;
let raw = self.alloc(layout).ok_or(Failure::Exhausted)?;
let slice = ptr::slice_from_raw_parts_mut(raw.cast().as_ptr(), n);
let uninit = unsafe { &mut *slice };
Ok(uninit.into())
}
pub fn level(&self) -> Level {
Level(self.index.get())
}
pub fn reset(&mut self) {
self.index.set(0)
}
fn try_alloc(&self, layout: Layout)
-> Option<Allocation<'_>>
{
let consumed = self.index.get();
match self.try_alloc_at(layout, consumed) {
Ok(alloc) => return Some(alloc),
Err(Failure::Exhausted) => return None,
Err(Failure::Mismatch{ observed: _ }) => {
unreachable!("Count in Cell concurrently modified, this UB")
}
}
}
fn try_alloc_at(&self, layout: Layout, expect_consumed: usize)
-> Result<Allocation<'_>, Failure>
{
assert!(layout.size() > 0);
let length = self.data.len();
let data: &UnsafeCell<[MaybeUninit<u8>]> =
unsafe { &*(&self.data as *const _ as *const UnsafeCell<_>) };
let base_ptr = data.get() as *mut u8;
let alignment = layout.align();
let requested = layout.size();
assert!(expect_consumed <= length);
let available = length.checked_sub(expect_consumed).unwrap();
let ptr_to = base_ptr.wrapping_add(expect_consumed);
let offset = ptr_to.align_offset(alignment);
if Some(requested) > available.checked_sub(offset) {
return Err(Failure::Exhausted); }
assert!(offset < available);
let at_aligned = expect_consumed.checked_add(offset).unwrap();
let new_consumed = at_aligned.checked_add(requested).unwrap();
assert!(new_consumed <= length);
assert!(at_aligned < length);
match self.bump(expect_consumed, new_consumed) {
Ok(()) => (),
Err(observed) => {
return Err(Failure::Mismatch { observed: Level(observed) });
},
}
let aligned = unsafe {
(base_ptr as *mut u8).add(at_aligned)
};
Ok(Allocation {
ptr: NonNull::new(aligned).unwrap(),
lifetime: AllocTime::default(),
level: Level(new_consumed),
})
}
fn bump(&self, expect: usize, consume: usize) -> Result<(), usize> {
debug_assert!(consume <= self.capacity());
debug_assert!(expect <= consume);
let prev = self.index.get();
if prev != expect {
Err(prev)
} else {
self.index.set(consume);
Ok(())
}
}
fn zst_fake_alloc<Z>(&self) -> Allocation<'_, Z> {
Allocation::for_zst(self.level())
}
}
impl<T> ops::Deref for Bump<T> {
type Target = MemBump;
fn deref(&self) -> &MemBump {
let from_layout = Layout::for_value(self);
let data_layout = Layout::new::<MaybeUninit<T>>();
let ptr = self as *const Self as *const MaybeUninit<u8>;
let mem: *const [MaybeUninit<u8>] =
ptr::slice_from_raw_parts(ptr, data_layout.size());
let bump = unsafe { &*(mem as *const MemBump) };
debug_assert_eq!(from_layout, Layout::for_value(bump));
bump
}
}
impl<T> ops::DerefMut for Bump<T> {
fn deref_mut(&mut self) -> &mut MemBump {
let from_layout = Layout::for_value(self);
let data_layout = Layout::new::<MaybeUninit<T>>();
let ptr = self as *mut Self as *mut MaybeUninit<u8>;
let mem: *mut [MaybeUninit<u8>] =
ptr::slice_from_raw_parts_mut(ptr, data_layout.size());
let bump = unsafe { &mut *(mem as *mut MemBump) };
debug_assert_eq!(from_layout, Layout::for_value(bump));
bump
}
}
#[test]
fn mem_bump_derefs_correctly() {
let bump = Bump::<usize>::zeroed();
let mem: &MemBump = ≎
assert_eq!(mem::size_of_val(&bump), mem::size_of_val(mem));
}