use std::{alloc::Layout, marker::PhantomData, mem, ptr};
use crate::alloc::{self, tail::Plan};
#[repr(C)]
pub(super) struct Prefix<H, T, N: alloc::raw::TailExtent> {
header: H,
plan: Plan<H, T, N>,
}
#[must_use = "initialize the allocation or release it"]
pub struct Uninit<H, T, N: alloc::raw::TailExtent = usize> {
pub(super) memory: ptr::NonNull<Prefix<H, T, N>>,
pub(super) layout: Plan<H, T, N>,
}
#[repr(transparent)]
pub struct Block<H, T, N: alloc::raw::TailExtent = usize> {
pointer: ptr::NonNull<Prefix<H, T, N>>,
}
#[repr(transparent)]
pub struct View<'a, H, T, N: alloc::raw::TailExtent = usize> {
pointer: ptr::NonNull<Prefix<H, T, N>>,
borrow: PhantomData<&'a Block<H, T, N>>,
}
impl<H, T, N: alloc::raw::TailExtent> Prefix<H, T, N> {
pub(super) fn layout(plan: Plan<H, T, N>) -> Layout {
unsafe {
Layout::from_size_align_unchecked(
Plan::<H, T, N>::padded(plan.value.len()),
Plan::<H, T, N>::ALIGN,
)
}
}
}
impl<H, T, N: alloc::raw::TailExtent> Uninit<H, T, N> {
pub fn init(self, header: H) -> Block<H, mem::MaybeUninit<T>, N> {
let owner = mem::ManuallyDrop::new(self);
unsafe {
owner.memory.write(Prefix {
header,
plan: owner.layout,
})
};
Block {
pointer: owner.memory.cast(),
}
}
pub fn fill(self, header: H, mut init: impl FnMut(&H, N::Index) -> T) -> Block<H, T, N> {
let indices = self.layout.value.indices();
let block = self.init(header);
let view = View::new(&block);
let slots = view.data().cast::<T>();
let header = view.header();
for index in indices {
unsafe {
slots
.add(alloc::raw::TailIndex::get(index))
.write(init(header, index))
};
}
unsafe { block.assume_init() }
}
pub fn repeat(self, header: H, value: T) -> Block<H, T, N>
where
T: Copy,
{
let mut block = self.init(header);
block.as_mut_slice().fill(mem::MaybeUninit::new(value));
unsafe { block.assume_init() }
}
}
impl<H, T, N: alloc::raw::TailExtent> Drop for Uninit<H, T, N> {
fn drop(&mut self) {
unsafe { alloc::raw::LayoutExt::deallocate(self.layout.layout(), self.memory) };
}
}
impl<H, T, N: alloc::raw::TailExtent> Block<H, mem::MaybeUninit<T>, N> {
pub(crate) unsafe fn assume_init(self) -> Block<H, T, N> {
Block {
pointer: mem::ManuallyDrop::new(self).pointer.cast(),
}
}
pub(crate) fn into_header(self) -> H {
let owner = mem::ManuallyDrop::new(self);
let layout = View::new(&owner).len_proof();
let header = unsafe { (&raw const (*owner.pointer.as_ptr()).header).read() };
drop(Uninit {
memory: owner.pointer,
layout,
});
header
}
}
impl<H, T, N: alloc::raw::TailExtent> Block<H, T, N> {
pub(crate) fn extent(&self) -> N {
View::new(self).len_proof().extent()
}
pub fn header_mut(&mut self) -> &mut H {
unsafe { &mut (*self.pointer.as_ptr()).header }
}
pub fn parts_mut(&mut self) -> (&mut H, &mut [T]) {
let (_, tail) = View::new(self).parts();
unsafe { (&mut (*self.pointer.as_ptr()).header, &mut *tail) }
}
pub fn as_mut_slice(&mut self) -> &mut [T] {
self.parts_mut().1
}
}
impl<H, T, N: alloc::raw::TailExtent> Drop for Block<H, T, N> {
fn drop(&mut self) {
let (layout, tail) = View::new(self).parts();
unsafe {
ptr::drop_in_place(tail);
ptr::drop_in_place(self.pointer.as_ptr());
}
drop(Uninit {
memory: self.pointer,
layout,
});
}
}
impl<'a, H, T, N: alloc::raw::TailExtent> View<'a, H, T, N> {
pub fn new(block: &'a Block<H, T, N>) -> Self {
Self {
pointer: block.pointer,
borrow: PhantomData,
}
}
pub(crate) unsafe fn from_raw(pointer: ptr::NonNull<u8>) -> Self {
Self {
pointer: pointer.cast(),
borrow: PhantomData,
}
}
pub(crate) const fn as_ptr(&self) -> ptr::NonNull<u8> {
self.pointer.cast()
}
fn prefix(self) -> &'a Prefix<H, T, N> {
unsafe { self.pointer.as_ref() }
}
pub(in crate::alloc) fn data(&self) -> ptr::NonNull<T> {
unsafe { self.pointer.byte_add(Plan::<H, T, N>::tail_offset()).cast() }
}
fn parts(self) -> (Plan<H, T, N>, *mut [T]) {
let plan = self.len_proof();
(
plan,
ptr::slice_from_raw_parts_mut(self.data().as_ptr(), plan.value.len()),
)
}
pub(crate) fn len_proof(self) -> Plan<H, T, N> {
self.prefix().plan
}
pub fn header(self) -> &'a H {
&self.prefix().header
}
pub fn len(self) -> usize {
self.len_proof().value.len()
}
pub fn is_empty(self) -> bool {
self.len() == 0
}
pub fn as_slice(&self) -> &'a [T] {
unsafe { &*self.parts().1 }
}
}
impl<H, T, N: alloc::raw::TailExtent> Clone for View<'_, H, T, N> {
fn clone(&self) -> Self {
*self
}
}
impl<H, T, N: alloc::raw::TailExtent> Copy for View<'_, H, T, N> {}
#[repr(transparent)]
pub struct Resident<'a, H, T, N: alloc::raw::TailExtent = usize> {
pointer: ptr::NonNull<T>,
borrow: PhantomData<&'a Block<H, T, N>>,
}
impl<'a, H, T, N: alloc::raw::TailExtent> Resident<'a, H, T, N> {
pub unsafe fn new(view: View<'a, H, T, N>, index: N::Index) -> Self {
let pointer = unsafe { view.data().add(alloc::raw::TailIndex::get(index)) };
Self {
pointer,
borrow: PhantomData,
}
}
pub fn get(self) -> &'a T {
unsafe { self.pointer.as_ref() }
}
pub unsafe fn view(self, index: N::Index) -> View<'a, H, T, N> {
let pointer = unsafe {
self.pointer
.sub(alloc::raw::TailIndex::get(index))
.cast::<u8>()
.sub(Plan::<H, T, N>::tail_offset())
.cast()
};
unsafe { View::from_raw(pointer) }
}
}
impl<H, T, N: alloc::raw::TailExtent> Clone for Resident<'_, H, T, N> {
fn clone(&self) -> Self {
*self
}
}
impl<H, T, N: alloc::raw::TailExtent> Copy for Resident<'_, H, T, N> {}
const _: () = {
assert!(std::mem::size_of::<Resident<'static, u8, u8>>() == std::mem::size_of::<usize>());
assert!(
std::mem::size_of::<Option<Resident<'static, u8, u8>>>() == std::mem::size_of::<usize>()
);
};