use crate::base::num::Step;
use crate::depot::{
self,
arena::slab::{
self, Plane, entries, external,
key::{self, cycle, cycle::Policy},
},
queue::links,
};
use std::{marker, mem, mem::MaybeUninit, ops, pin::Pin, ptr};
mod core;
mod entry;
mod free;
mod occupied;
mod position;
pub(in crate::depot) use core::Core;
pub(in crate::depot) use entry::Entry;
pub(in crate::depot::arena::slab) use free::Free;
pub use occupied::Occupied;
pub(in crate::depot) use position::Position;
type Split<'a, T, Tag, const MAX: u32, const N: usize> = position::Lookup<
(
Vacant<'a, T, Tag, MAX, entries::Exclusive, N, false>,
Occupied<'a, T, Tag, MAX, entries::Exclusive, N, false>,
),
Tag,
MAX,
entries::Exclusive,
>;
pub struct Vacant<
'a,
T,
Tag = (),
const MAX: u32 = { u32::MAX },
M: entries::Mode = entries::Exclusive,
const N: usize = 1,
const ROOT: bool = true,
const ISSUED: bool = false,
> {
pub(in crate::depot) entry: slab::raw::Entry<'a, T, MAX, M, N, slab::Heap, false, ISSUED>,
tag: marker::PhantomData<fn() -> Tag>,
}
impl<
'a,
T,
Tag,
const MAX: u32,
M: entries::Mode,
const N: usize,
const ROOT: bool,
const ISSUED: bool,
> Vacant<'a, T, Tag, MAX, M, N, ROOT, ISSUED>
{
pub(in crate::depot::arena::slab) fn new(
entry: slab::raw::Entry<'a, T, MAX, M, N, slab::Heap, false, ISSUED>,
) -> Self {
Self {
entry,
tag: marker::PhantomData,
}
}
pub fn index(&self) -> links::Index {
self.entry.index()
}
}
impl<'a, T, Tag, const MAX: u32, M: entries::Mode, const N: usize, const ROOT: bool>
Vacant<'a, T, Tag, MAX, M, N, ROOT>
{
pub fn insert(mut self, value: T) -> key::Handle<Tag, MAX> {
self.uninit().write(value);
unsafe { self.entry.publish_key() }.0
}
pub fn uninit(&mut self) -> &mut mem::MaybeUninit<T> {
self.entry.uninit()
}
pub fn stage(self, value: T) -> Occupied<'a, T, Tag, MAX, M, N, ROOT, true> {
Occupied::new(self.entry.install(value))
}
}
impl<'a, T, Tag, const MAX: u32> Vacant<'a, T, Tag, MAX, entries::Interior> {
pub fn reserve(self) -> Vacant<'a, T, Tag, MAX, entries::Interior, 1, true, true> {
Vacant::new(self.entry.issue())
}
pub fn occupy(self, value: T) -> Occupied<'a, T, Tag, MAX, entries::Interior> {
Occupied::new(self.entry.install(value))
}
}
impl<'a, T, Tag, const MAX: u32, const N: usize> Vacant<'a, T, Tag, MAX, entries::Exclusive, N> {
pub fn try_new(owner: &'a mut slab::Store<T, Tag, MAX, N>) -> Result<Self, slab::Vacancy> {
Ok(Self::new(owner.core.try_free()?))
}
pub fn at(owner: &'a mut slab::Store<T, Tag, MAX, N>, index: u32) -> Option<Self> {
Some(Self::new(owner.core.at(index)?))
}
pub fn in_range(
owner: &'a mut slab::Store<T, Tag, MAX, N>,
range: ops::Range<u32>,
) -> Option<Self> {
Some(Self::new(owner.core.in_range(range)?))
}
pub fn try_split(self, key: key::Handle<Tag, MAX>) -> Split<'a, T, Tag, MAX, N> {
let entry = slab::raw::Position::try_key(self.entry.core(), key)?;
Ok((Vacant::new(self.entry), Occupied::new(entry.take())))
}
}
impl<'a, T, Tag, const MAX: u32, M: entries::Mode, const N: usize, const ROOT: bool>
Vacant<'a, T, Tag, MAX, M, N, ROOT, true>
{
pub fn key(&self) -> key::Handle<Tag, MAX> {
key::Handle::new(self.entry.index().raw(), self.entry.generation())
}
pub fn occupy(self, value: T) -> Occupied<'a, T, Tag, MAX, M, N, ROOT> {
Occupied::new(self.entry.install(value))
}
}
const _: () = {
assert!(
size_of::<Vacant<'_, u64, (), { u32::MAX }, entries::Exclusive, 1, true, true>>()
== 2 * size_of::<usize>()
);
assert!(
size_of::<Option<Vacant<'_, u64, (), { u32::MAX }, entries::Exclusive, 1, true, true>>>()
== 2 * size_of::<usize>()
);
};
impl<'a, T, Tag, const MAX: u32, const N: usize>
Vacant<'a, T, Tag, MAX, entries::Exclusive, N, false>
{
pub unsafe fn try_reserve(
owner: &'a slab::Store<T, Tag, MAX, N>,
) -> Result<Self, slab::Vacancy> {
Ok(Self::new(owner.core.try_free()?))
}
}
impl<'a, T, Tag, const MAX: u32> Vacant<'a, T, Tag, MAX, entries::Exclusive, 1, false> {
pub unsafe fn claim(owner: &'a slab::Store<T, Tag, MAX>) -> Self {
let head = &owner.core.heads[0];
let index = unsafe { links::Index::new_raw(head.get()) };
Self::new(unsafe { Free::take_head(&owner.core, head, index) })
}
}
impl<'a, T, Tag, const MAX: u32, const N: usize, const ROOT: bool>
Vacant<'a, T, Tag, MAX, entries::Exclusive, N, ROOT>
{
pub unsafe fn publish(self) -> (key::Handle<Tag, MAX>, &'a mut T) {
let (key, entry) = unsafe { self.entry.publish_key() };
(key, entry.into_mut())
}
}
pub struct Anchored<
'a,
T,
Tag = (),
const MAX: u32 = { u32::MAX },
const RECYCLE: bool = false,
const N: usize = 1,
S: slab::Storage = slab::Heap,
> where
cycle::Mode<RECYCLE>: Policy,
{
pub(in crate::depot) entry:
slab::raw::Entry<'a, T, MAX, entries::Exclusive<RECYCLE>, N, S, true, false>,
tag: marker::PhantomData<fn() -> Tag>,
}
impl<'a, T, Tag, const MAX: u32, const RECYCLE: bool, const N: usize, S>
Anchored<'a, T, Tag, MAX, RECYCLE, N, S>
where
cycle::Mode<RECYCLE>: Policy,
S: slab::Storage,
{
pub(in crate::depot) unsafe fn pinned(
entry: slab::raw::Entry<'a, T, MAX, entries::Exclusive<RECYCLE>, N, S, true, false>,
) -> Self {
Self {
entry,
tag: marker::PhantomData,
}
}
pub fn get(&self) -> &T {
self.entry.value()
}
pub fn value(&mut self) -> Pin<&mut T> {
unsafe { Pin::new_unchecked(self.entry.value_mut()) }
}
pub fn into_value(self) -> Pin<&'a mut T> {
unsafe { Pin::new_unchecked(self.entry.into_mut()) }
}
pub fn discard(self) {
self.entry.issue();
}
pub unsafe fn release(self) {
unsafe { self.entry.release() };
}
}
pub struct Anchor<
'a,
T,
Tag = (),
const MAX: u32 = { u32::MAX },
const RECYCLE: bool = false,
const N: usize = 1,
S: slab::Storage = slab::Heap,
> where
cycle::Mode<RECYCLE>: Policy,
{
pub(in crate::depot) entry:
slab::raw::Entry<'a, T, MAX, entries::Exclusive<RECYCLE>, N, S, false, false>,
tag: marker::PhantomData<fn() -> Tag>,
}
impl<'a, T, Tag, const MAX: u32, const RECYCLE: bool, const N: usize, S>
Anchor<'a, T, Tag, MAX, RECYCLE, N, S>
where
cycle::Mode<RECYCLE>: Policy,
S: slab::Storage,
{
pub(in crate::depot) unsafe fn pinned(
entry: slab::raw::Entry<'a, T, MAX, entries::Exclusive<RECYCLE>, N, S, false, false>,
) -> Self {
Self {
entry,
tag: marker::PhantomData,
}
}
pub fn index(&self) -> u32 {
self.entry.index().raw()
}
pub fn insert(self, value: T) -> key::Handle<Tag, MAX> {
self.occupy(value).0
}
pub fn uninit(&mut self) -> &mut mem::MaybeUninit<T> {
self.entry.uninit()
}
pub fn occupy(
mut self,
value: T,
) -> (
key::Handle<Tag, MAX>,
Anchored<'a, T, Tag, MAX, RECYCLE, N, S>,
) {
self.uninit().write(value);
unsafe { self.publish() }
}
pub unsafe fn publish(
self,
) -> (
key::Handle<Tag, MAX>,
Anchored<'a, T, Tag, MAX, RECYCLE, N, S>,
) {
let (key, entry) = unsafe { self.entry.publish_key() };
(key, unsafe { Anchored::pinned(entry) })
}
}
pub struct Retirement<
'a,
T,
const MAX: u32,
M: entries::Mode,
const N: usize,
S: slab::Storage,
const LIVE: bool,
> {
core: &'a Core<T, MAX, M, N, S>,
index: links::Index,
slot: &'a slab::Slot<T, MAX, M>,
}
impl<'a, T, const MAX: u32, M: entries::Mode, const N: usize, S: slab::Storage, const LIVE: bool>
Retirement<'a, T, MAX, M, N, S, LIVE>
{
pub(in crate::depot::arena::slab) fn new(
position: &Position<'a, T, MAX, M, N, S, LIVE>,
) -> Self {
Self {
core: position.core(),
index: position.index(),
slot: position.slot(),
}
}
pub(super) unsafe fn unlink(&self) {
let core = self.core;
let plane = &core.slots;
let pos = self.slot.metadata.position.get();
let tail = core.live.get().wrapping_sub(1);
if pos != tail {
let dense = plane.tail();
unsafe {
let last = dense.get_unchecked(tail as usize).get().assume_init();
dense
.get_unchecked(pos as usize)
.set(MaybeUninit::new(last));
plane
.as_ref()
.get_unchecked(last as usize)
.metadata
.position
.set(pos);
}
}
core.live.set(tail);
}
pub(super) unsafe fn recycle<const ISSUED: bool>(self) {
let slot = self.slot;
if ISSUED {
if LIVE {
unsafe { self.unlink() };
}
match <M::Cycle as Policy>::next::<MAX>(slot.metadata.generation.get()) {
Ok(epoch) => slot.metadata.generation.set(epoch),
Err(retired) => {
slot.metadata.state.set(slab::State::Retired(retired));
if let Some(count) = self.core.slots.retired() {
count.set(count.get().wrapping_add(1));
}
return;
}
}
}
let index = self.index.raw();
let head = self.core.partition(index).replace(index);
unsafe {
slot.set_links(depot::raw::SlabLinks {
next: head,
prev: links::Index::END,
});
slot.metadata.state.set(slab::State::Free);
if head != links::Index::END {
self.core
.slots
.as_ref()
.get_unchecked(head as usize)
.set_prev(index);
}
}
}
}
impl<T, G: Step>
Retirement<'_, external::Stamped<T, G>, 1, entries::Exclusive<true>, 1, slab::Lazy, true>
{
pub(in crate::depot::arena::slab) fn payload(
slot: &slab::Slot<external::Stamped<T, G>, 1, entries::Exclusive<true>>,
) -> *mut T {
unsafe { ptr::addr_of_mut!((*slot.value_ptr()).value) }
}
pub fn as_mut_ptr(&mut self) -> *mut T {
Self::payload(self.slot)
}
pub unsafe fn release(self) {
unsafe { self.recycle::<true>() };
}
}