use crate::{
alloc,
base::num::bounded,
depot::{
self,
arena::linked::{self, fifo, head},
},
};
use std::{cell, marker, mem, ptr};
mod batch;
mod fixed;
mod index;
mod live;
mod reserve;
mod store;
mod vacant;
pub use batch::Batch;
pub use fixed::Fixed;
pub use index::Index;
pub use live::Live;
pub use reserve::Reserve;
pub use store::Store;
pub use vacant::Vacant;
pub struct Released;
#[repr(C)]
pub struct Node<T> {
pub(in crate::depot::arena::linked) value: cell::UnsafeCell<mem::MaybeUninit<T>>,
pub(in crate::depot::arena::linked) next: cell::Cell<u32>,
}
impl<T> Node<T> {
pub(crate) fn value_ptr(&self) -> *mut T {
self.value.get().cast()
}
pub(crate) fn next<Tag>(&self) -> Index<Tag> {
Index::new(self.next.get())
}
pub(crate) unsafe fn parts(node: *mut Self) -> (*mut T, *mut u32) {
unsafe {
(
(&raw mut (*node).value).cast(),
(&raw mut (*node).next).cast(),
)
}
}
pub(crate) unsafe fn write(node: *mut Self, value: T, next: u32) {
let (slot, link) = unsafe { Self::parts(node) };
unsafe {
slot.write(value);
link.write(next);
}
}
pub(crate) const fn vacant(index: u32) -> Self {
Self {
value: cell::UnsafeCell::new(mem::MaybeUninit::uninit()),
next: cell::Cell::new(index),
}
}
}
#[repr(transparent)]
#[derive(Debug)]
pub struct Capacity<T> {
raw: bounded::raw::U32<0, { u32::MAX - 1 }>,
value: marker::PhantomData<fn(T) -> T>,
}
impl<T> Capacity<T> {
pub const unsafe fn bind(self, base: ptr::NonNull<Node<T>>) -> Store<T> {
Store {
slots: base.cast(),
capacity: self,
initialized: cell::Cell::new(0),
free: cell::Cell::new(Store::<T>::END),
available: cell::Cell::new(self.get()),
ownership: marker::PhantomData,
}
}
pub fn new(capacity: usize) -> Result<Self, alloc::Overflow> {
let raw = bounded::raw::U32::from_usize(capacity).ok_or(alloc::Overflow)?;
alloc::raw::Layout::<Node<T>>::try_layout(raw.into_usize())?;
Ok(Self {
raw,
value: marker::PhantomData,
})
}
pub(crate) const fn get(self) -> u32 {
self.raw.get()
}
pub(crate) const fn len(self) -> usize {
self.raw.get() as usize
}
pub fn bundle(self) -> alloc::raw::Bundle<alloc::raw::Layout<Node<T>>> {
use alloc::raw::Bundle;
unsafe { Bundle::root::<Node<T>>(self.len()) }
}
}
impl<T> Clone for Capacity<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T> Copy for Capacity<T> {}
const _: () = assert!(mem::size_of::<Capacity<u64>>() == mem::size_of::<u32>());
const _: () = assert!(mem::size_of::<Node<u64>>() == 2 * mem::size_of::<u64>());
#[must_use = "a detached slot retains its sole return entitlement"]
struct Slot<T, H: linked::Source<T>, Tag, const LIVE: bool> {
owner: H,
index: Index<Tag>,
value: marker::PhantomData<T>,
}
impl<T, H: linked::Source<T>, Tag, const LIVE: bool> Slot<T, H, Tag, LIVE> {
unsafe fn new(owner: H, index: Index<Tag>) -> Self {
Self {
owner,
index,
value: marker::PhantomData,
}
}
unsafe fn into_parts(self) -> (H, Index<Tag>) {
let slot = mem::ManuallyDrop::new(self);
(unsafe { ptr::read(&slot.owner) }, slot.index)
}
}
impl<T, H: linked::Source<T>, Tag, const LIVE: bool> Drop for Slot<T, H, Tag, LIVE> {
fn drop(&mut self) {
let store = self.owner.store();
let index = self.index.raw();
let node = unsafe { store.slot(index) };
if LIVE {
unsafe { (&mut *node.value.get()).assume_init_drop() };
}
store.recycle(index, node, H::TRACK);
}
}
#[must_use]
pub struct Permit<'a, T, Tag> {
pub(in crate::depot::arena::linked) store: &'a mut Store<T>,
domain: marker::PhantomData<fn(Tag) -> Tag>,
}
impl<'a, T, Tag> Permit<'a, T, Tag> {
pub(in crate::depot) fn try_new(store: &'a mut Store<T>) -> Option<Self> {
if store.available() == 0 {
return None;
}
Some(Self {
store,
domain: marker::PhantomData,
})
}
pub fn claim(self, value: T) -> Live<T, &'a Store<T>, Tag> {
unsafe { Vacant::insert_one::<0>(&*self.store, value) }
}
}
const _: () = {
assert!(mem::size_of::<Permit<'_, u64, ()>>() == mem::size_of::<usize>());
assert!(mem::size_of::<Option<Permit<'_, u64, ()>>>() == mem::size_of::<usize>());
assert!(mem::size_of::<Vacant<u64, &'_ Store<u64>, ()>>() == 2 * mem::size_of::<usize>());
assert!(mem::size_of::<Live<u64, &'_ Store<u64>, ()>>() == 2 * mem::size_of::<usize>());
};
pub struct Borrow<'a, T, const TRACK: bool>(pub(crate) &'a Store<T>);
impl<T, const TRACK: bool> linked::Sealed<T> for Borrow<'_, T, TRACK> {}
impl<T, const TRACK: bool> linked::Source<T> for Borrow<'_, T, TRACK> {
const TRACK: bool = TRACK;
fn store(&self) -> &Store<T> {
self.0
}
}
pub struct Bound<'a, T, Tag = (), const TRACK: bool = true> {
store: &'a Store<T>,
state: &'a mut fifo::Fifo<Tag>,
}
impl<'a, T, Tag, const TRACK: bool> Bound<'a, T, Tag, TRACK> {
pub unsafe fn new(state: &'a mut fifo::Fifo<Tag>, store: &'a Store<T>) -> Self {
Self { store, state }
}
}
impl<T, Tag, const TRACK: bool> linked::Sealed<T> for Bound<'_, T, Tag, TRACK> {}
impl<T, Tag, const TRACK: bool> linked::Source<T> for Bound<'_, T, Tag, TRACK> {
const TRACK: bool = TRACK;
fn store(&self) -> &Store<T> {
self.store
}
}
impl<T, Tag, const TRACK: bool> Bound<'_, T, Tag, TRACK> {
pub fn pop_front(&mut self) -> Option<T> {
let index = self.state.links.head()?;
Some(unsafe { index.pop::<_, TRACK>(self.state, self.store) })
}
}
impl<'a, T, Tag> Bound<'a, T, Tag> {
pub(in crate::depot) unsafe fn detach(self, index: Index<Tag>) -> Live<T, Self, Tag> {
unsafe { index.unlink(self.state, self.store) };
unsafe { Live::detached(self, index) }
}
pub fn front_entry(self) -> Option<Front<'a, T, Tag>> {
let index = self.state.links.head()?;
Some(Front { owner: self, index })
}
}
pub struct Front<'a, T, Tag> {
owner: Bound<'a, T, Tag>,
index: Index<Tag>,
}
impl<T, Tag> Front<'_, T, Tag> {
pub fn get_mut(&mut self) -> &mut T {
unsafe { &mut *self.owner.store.value_ptr(self.index) }
}
pub fn discard(self) {
drop(unsafe { self.owner.detach(self.index) });
}
}
const _: () = assert!(mem::size_of::<Front<'_, u64, ()>>() == 3 * mem::size_of::<usize>());
pub struct Cursor<'a, T, Tag> {
store: &'a Store<T>,
state: &'a mut head::List<Tag>,
previous: Index<Tag>,
current: Index<Tag>,
}
impl<'a, T, Tag> Cursor<'a, T, Tag> {
pub(in crate::depot::arena::linked) const unsafe fn new(
store: &'a Store<T>,
state: &'a mut head::List<Tag>,
) -> Self {
Self {
store,
current: state.head,
state,
previous: Index::NONE,
}
}
pub fn current(&self) -> Option<&T> {
let current = self.current;
if current == Index::NONE {
return None;
}
Some(unsafe { self.store.value(current) })
}
pub fn advance(&mut self) {
let current = self.current;
if current.get().is_none() {
return;
}
self.previous = current;
self.current = unsafe { self.store.next(current) };
}
pub fn insert(proof: Member<'_, 'a, Self, T, Tag>) {
let (cursor, claim) = proof.into_inner();
let inserted = unsafe { claim.into_index() };
unsafe { cursor.store.set_next(inserted, cursor.current) };
if cursor.previous.get().is_none() {
cursor.state.head = inserted;
} else {
unsafe { cursor.store.set_next(cursor.previous, inserted) };
}
cursor.previous = inserted;
}
}
const _: () = assert!(mem::size_of::<Cursor<'_, u64, ()>>() == 3 * mem::size_of::<usize>());
type Member<'s, 'a, S, T, Tag> =
depot::raw::Proof<(&'s mut S, Live<T, &'a Store<T>, Tag>), linked::Same>;
pub(crate) struct Drain<'a, T, Tag = ()> {
store: &'a Store<T>,
next: Index<Tag>,
}
impl<'a, T, Tag> Drain<'a, T, Tag> {
pub(crate) unsafe fn new(store: &'a Store<T>, next: Index<Tag>) -> Self {
Self { store, next }
}
}
impl<'a, T, Tag> Iterator for Drain<'a, T, Tag> {
type Item = Live<T, &'a Store<T>, Tag>;
fn next(&mut self) -> Option<Self::Item> {
let head = self.next;
head.get()?;
self.next = unsafe { self.store.next(head) };
Some(unsafe { Live::detached(self.store, head) })
}
}
impl<T, Tag> Drop for Drain<'_, T, Tag> {
fn drop(&mut self) {
for _ in self {}
}
}
#[must_use]
pub struct Entry<'a, 'b, T, Tag> {
store: &'a Store<T>,
state: &'b mut head::List<Tag>,
}
impl<'a, 'b, T, Tag> Entry<'a, 'b, T, Tag> {
pub(in crate::depot::arena::linked) const unsafe fn new(
store: &'a Store<T>,
state: &'b mut head::List<Tag>,
) -> Self {
Self { store, state }
}
pub fn get(&self) -> &T {
unsafe { self.store.value(self.state.head) }
}
fn unlink(&mut self) -> Index<Tag> {
let index = self.state.head;
self.state.head = unsafe { self.store.next(index) };
index
}
pub fn pop(mut self) -> T {
let index = self.unlink();
unsafe { self.store.take::<_, true>(index) }
}
pub fn take(mut self) -> (T, Vacant<T, &'a Store<T>, Tag>) {
let index = self.unlink();
unsafe { Live::detached(self.store, index) }.take()
}
}
const _: () = assert!(mem::size_of::<Entry<'_, '_, u64, ()>>() == 2 * mem::size_of::<usize>());