use std::{marker, mem, num, ptr, slice};
use crate::{
buffer::{
self, resident, storage,
storage::alloc,
write::{self, consume},
},
mem::rc,
};
#[derive(Debug)]
#[repr(transparent)]
pub(in crate::buffer) struct Init<P: buffer::raw::Policy = ()> {
allocation: buffer::raw::Heap<P>,
}
#[derive(Debug)]
#[repr(transparent)]
pub(in crate::buffer) struct Complete<'d> {
pointer: ptr::NonNull<storage::raw::Prefix<num::NonZero<u32>>>,
ownership: marker::PhantomData<Init<resident::Budget<'d>>>,
}
impl<P: buffer::raw::Policy> Init<P> {
pub(in crate::buffer) unsafe fn try_copy(
capacity: num::NonZero<u32>,
sources: impl buffer::raw::Transfer,
seed: P,
) -> Result<Self, alloc::Error<P::Failure>> {
let mut heap = buffer::raw::Heap::try_new(seed, capacity)?;
unsafe { sources.copy_to(heap.as_mut_ptr().cast()) };
Ok(Self { allocation: heap })
}
pub(in crate::buffer) fn try_full(
owner: storage::Owned<P>,
) -> Result<Option<Self>, consume::Error> {
consume::Error::check(owner.capacity(), owner.len())?;
Ok(owner.allocation.map(|heap| Self { allocation: heap }))
}
pub(in crate::buffer) fn capacity(&self) -> num::NonZero<u32> {
self.allocation.capacity()
}
pub(in crate::buffer) fn as_slice(&self) -> &[u8] {
unsafe {
slice::from_raw_parts(
self.allocation.as_ptr().cast(),
self.capacity().get() as usize,
)
}
}
pub(in crate::buffer) fn into_owned(self) -> storage::Owned<P> {
storage::Owned {
len: self.capacity().get(),
allocation: Some(self.allocation),
}
}
pub(in crate::buffer) fn into_buf(
self,
) -> storage::Shared<crate::mem::raw::Owner<buffer::raw::Header<P>>> {
let len = self.capacity();
unsafe { buffer::raw::Header::into_range(self.allocation.into_shared(), 0, len.get()) }
}
}
impl Init<()> {
pub(in crate::buffer) fn bind<'d>(self) -> storage::raw::Complete<'d> {
Complete {
pointer: self.allocation.into_shared().into_raw().cast(),
ownership: marker::PhantomData,
}
}
}
impl<'d> Init<resident::Budget<'d>> {
pub(in crate::buffer) fn bind(self) -> storage::raw::Complete<'d> {
Complete {
pointer: self
.allocation
.into_shared()
.into_raw()
.cast()
.map_addr(|address| address | 1),
ownership: marker::PhantomData,
}
}
}
const _: () = assert!(size_of::<Init>() == size_of::<usize>());
impl<'d> Complete<'d> {
pub(in crate::buffer) fn as_slice<'a>(&'a self) -> &'a [u8] {
let pointer = self.pointer();
let bytes = if self.pointer.addr().get() & 1 == 0 {
unsafe { buffer::raw::Header::<()>::slice(pointer.cast()) }
} else {
unsafe { buffer::raw::Header::<resident::Budget<'d>>::slice(pointer.cast()) }
};
unsafe { bytes.as_ref() }
}
pub(in crate::buffer) fn into_buf(self) -> storage::Shared<resident::State<'d>> {
let this = mem::ManuallyDrop::new(self);
let pointer = this.pointer();
let (bytes, owner) = unsafe {
if this.pointer.addr().get() & 1 == 0 {
let header = pointer.cast::<buffer::raw::Header<()>>();
(
buffer::raw::Header::slice(header),
resident::Kind::Owned(storage::raw::Tag::allocation(
crate::mem::raw::Owner::from_raw(header),
)),
)
} else {
let header = pointer.cast::<buffer::raw::Header<resident::Budget<'d>>>();
(
buffer::raw::Header::slice(header),
resident::Kind::Resident(crate::mem::raw::Owner::from_raw(header)),
)
}
};
storage::Shared {
owner: resident::State(owner),
bytes: storage::raw::Extent(bytes),
}
}
pub(in crate::buffer) fn bytes(&self) -> usize {
self.prefix().word().get() as usize
}
fn pointer(&self) -> ptr::NonNull<storage::raw::Prefix<num::NonZero<u32>>> {
self.pointer
.map_addr(|address| unsafe { num::NonZero::new_unchecked(address.get() & !1) })
}
fn prefix(&self) -> &storage::raw::Prefix<num::NonZero<u32>> {
unsafe { self.pointer().as_ref() }
}
}
impl Complete<'_> {
pub(in crate::buffer) fn try_clone(&self) -> Result<Self, rc::Error> {
use crate::mem::raw::Edge as _;
unsafe { self.prefix().try_retain() }?;
Ok(Self {
pointer: self.pointer,
ownership: marker::PhantomData,
})
}
}
impl<'d> Drop for Complete<'d> {
fn drop(&mut self) {
use crate::mem::raw::Edge as _;
if unsafe { self.prefix().release() } != rc::Release::Last {
return;
}
let pointer = self.pointer();
if self.pointer.addr().get() & 1 == 0 {
unsafe { buffer::raw::Header::<()>::destroy(pointer.cast()) };
} else {
unsafe { buffer::raw::Header::<resident::Budget<'d>>::destroy(pointer.cast()) };
}
}
}
const _: () = {
assert!(align_of::<buffer::raw::Header<()>>() > 1);
assert!(align_of::<buffer::raw::Header<resident::Budget<'static>>>() > 1);
assert!(size_of::<Complete<'static>>() == size_of::<ptr::NonNull<()>>());
assert!(size_of::<Option<Complete<'static>>>() == size_of::<ptr::NonNull<()>>());
};
impl<P: buffer::raw::Policy> write::Init for storage::Owned<P> {
fn try_init<I: buffer::raw::Initializer>(&mut self, source: I) -> Result<(), Self::Error> {
unsafe { write::raw::try_initialize(buffer::raw::Exclusive::spare_parts(self), source) }
}
}
impl Init {
pub(in crate::buffer) unsafe fn try_bytes(
len: num::NonZero<usize>,
sources: impl buffer::raw::Transfer,
) -> Result<Self, storage::AllocError> {
let capacity = num::NonZero::<u32>::try_from(len).map_err(|_range| {
storage::AllocError::Capacity(consume::Error::new(len, u32::MAX as usize))
})?;
Ok(unsafe { Self::try_copy(capacity, sources, ()) }?)
}
}
impl TryFrom<storage::NonEmpty<&[u8]>> for Init {
type Error = storage::AllocError;
fn try_from(source: storage::NonEmpty<&[u8]>) -> Result<Self, Self::Error> {
unsafe { Self::try_bytes(source.len(), source.into_storage()) }
}
}