use std::{marker, mem};
use crate::depot::{
arena::slab::{
self, entries,
key::{self, cycle::Policy},
storage::{self, Plane},
},
queue::links,
};
pub struct Entry<
'a,
T,
const MAX: u32,
M: entries::Mode,
const N: usize,
P,
S: storage::Storage,
const LIVE: bool,
const ISSUED: bool,
> {
pub(in crate::depot) core: &'a entries::Core<T, MAX, M, N, S>,
pub(in crate::depot) slot: &'a storage::Slot<T, MAX, M>,
pub(in crate::depot) plane: marker::PhantomData<fn(P) -> P>,
}
impl<
'a,
T,
const MAX: u32,
M: entries::Mode,
const N: usize,
P,
S: storage::Storage,
const LIVE: bool,
const ISSUED: bool,
> Entry<'a, T, MAX, M, N, P, S, LIVE, ISSUED>
{
pub(in crate::depot) fn slot_index(&self) -> links::Index {
unsafe { self.slot.index(self.core.slots.as_ref()) }
}
pub(in crate::depot) fn generation(&self) -> key::Generation<MAX> {
<M::Cycle as Policy>::generation(self.slot.metadata.generation.get())
}
pub(super) unsafe fn phase<const NEXT_LIVE: bool, const NEXT_ISSUED: bool>(
self,
) -> Entry<'a, T, MAX, M, N, P, S, NEXT_LIVE, NEXT_ISSUED> {
let this = mem::ManuallyDrop::new(self);
Entry {
core: this.core,
slot: this.slot,
plane: this.plane,
}
}
fn unlink_live(&mut self) {
let plane = &self.core.slots;
let pos = self.slot.metadata.position.get();
let tail = self.core.live.get().wrapping_sub(1);
if pos != tail {
unsafe {
let last = plane
.tail()
.get_unchecked(tail as usize)
.get()
.assume_init();
plane
.tail()
.get_unchecked(pos as usize)
.set(mem::MaybeUninit::new(last));
plane
.as_ref()
.get_unchecked(last as usize)
.metadata
.position
.set(pos);
}
}
self.core.live.set(tail);
}
fn release(&mut self) {
let index = self.slot_index().raw();
let head = self.core.partition(index).replace(index);
unsafe {
self.slot.set_links(crate::depot::raw::SlabLinks {
next: head,
prev: entries::NONE,
})
};
self.slot.metadata.state.set(slab::State::Free);
if head != entries::NONE {
unsafe {
self.core
.slots
.as_ref()
.get_unchecked(head as usize)
.set_prev(index)
};
}
}
fn retire(&mut self) {
match <M::Cycle as Policy>::next::<MAX>(self.slot.metadata.generation.get()) {
Ok(epoch) => {
self.slot.metadata.generation.set(epoch);
self.release();
}
Err(retired) => {
self.slot.metadata.state.set(slab::State::Retired(retired));
if let Some(count) = self.core.slots.retired() {
count.set(count.get().wrapping_add(1));
}
}
}
}
}
impl<
'a,
T,
const MAX: u32,
M: entries::Mode,
const N: usize,
P,
S: storage::Storage,
const ISSUED: bool,
> Entry<'a, T, MAX, M, N, P, S, true, ISSUED>
{
pub(in crate::depot) fn index(&self) -> u32 {
self.slot_index().raw()
}
pub(in crate::depot) fn value(&self) -> &T {
unsafe { self.slot.value() }
}
pub(in crate::depot) fn value_mut(&mut self) -> &mut T {
unsafe { &mut *self.slot.value_ptr() }
}
}
impl<'a, T, const MAX: u32, M: entries::Mode, const N: usize, P, S: storage::Storage>
Entry<'a, T, MAX, M, N, P, S, true, false>
{
pub(in crate::depot::arena::slab) fn detach(self) -> entries::Pending<'a, T, MAX, M, N, P, S> {
let mut this = mem::ManuallyDrop::new(self);
this.unlink_live();
Entry {
core: this.core,
slot: this.slot,
plane: this.plane,
}
}
pub(in crate::depot) fn remove(self) -> T {
let value = unsafe { self.slot.take_value() };
self.detach().reject();
value
}
pub(in crate::depot) fn discard(self) {
drop(unsafe { self.phase::<true, true>() });
}
pub(super) fn own(self) -> Entry<'a, T, MAX, M, N, P, S, true, true> {
unsafe { self.phase() }
}
}
impl<'a, T, const MAX: u32, M: entries::Mode, const N: usize, P, S: storage::Storage>
Entry<'a, T, MAX, M, N, P, S, false, true>
{
pub(in crate::depot::arena::slab) fn unissued(
self,
) -> entries::Pending<'a, T, MAX, M, N, P, S> {
unsafe { self.phase() }
}
}
impl<'a, T, const MAX: u32, M: entries::Mode, const N: usize, P, S: storage::Storage>
Entry<'a, T, MAX, M, N, P, S, true, true>
{
pub(in crate::depot::arena::slab) fn publish(self) -> entries::Busy<'a, T, MAX, M, N, P, S> {
unsafe { self.phase() }
}
}
impl<
T,
const MAX: u32,
M: entries::Mode,
const N: usize,
P,
S: storage::Storage,
const LIVE: bool,
const ISSUED: bool,
> Drop for Entry<'_, T, MAX, M, N, P, S, LIVE, ISSUED>
{
fn drop(&mut self) {
if ISSUED {
if LIVE {
unsafe { self.slot.drop_value() };
self.unlink_live();
}
self.retire();
} else if !LIVE {
self.release();
} else if M::REENTRANT {
self.slot.metadata.state.set(slab::State::Occupied);
}
}
}