use std::{array, cell, mem, ops, ptr};
use cycle::Policy;
use crate::{
alloc,
base::marker::ThreadBound,
depot::{
arena::slab::{
self, HeapPlane, Plane, Storage,
entries::{Exclusive, Mode},
key::{self, cycle},
},
capacity,
queue::links,
},
};
pub(in crate::depot) struct Core<
T,
const MAX: u32,
M: Mode,
const PARTITIONS: usize = 1,
S = slab::Heap,
> where
S: slab::Storage,
{
pub(in crate::depot) slots: S::Plane<T, MAX, M>,
pub(in crate::depot::arena::slab) heads: [cell::Cell<u32>; PARTITIONS],
pub(in crate::depot::arena::slab) live: cell::Cell<u32>,
_thread: ThreadBound,
}
impl<T, const MAX: u32, M: Mode, const PARTITIONS: usize, S> Core<T, MAX, M, PARTITIONS, S>
where
S: slab::Storage,
{
const VALID_PARTITIONS: () = assert!(
PARTITIONS == 1 || PARTITIONS == 2,
"slab partition count must be one or two"
);
pub(in crate::depot::arena::slab) fn slot(
index: usize,
count: usize,
epoch: <M::Cycle as Policy>::Stored<MAX>,
) -> slab::Slot<T, MAX, M> {
let () = Self::VALID_PARTITIONS;
let boundary = count.div_ceil(PARTITIONS);
let (start, end) = if index < boundary {
(0, boundary)
} else {
(boundary, count)
};
crate::depot::raw::SlabSlot::free(
epoch,
if index + 1 == end {
links::Index::END
} else {
index as u32 + 1
},
if index == start {
links::Index::END
} else {
index as u32 - 1
},
)
}
pub(in crate::depot) fn try_new(cap: capacity::Count) -> Result<Self, alloc::Error>
where
S::Plane<T, MAX, M>: HeapPlane<T, MAX, M>,
{
let () = Self::VALID_PARTITIONS;
let slots = S::Plane::<T, MAX, M>::try_alloc::<PARTITIONS>(cap)?;
Ok(Self::from_slots(slots))
}
fn from_slots(slots: S::Plane<T, MAX, M>) -> Self {
let count = slots.capacity();
let boundary = count.div_ceil(PARTITIONS);
Self {
slots,
heads: array::from_fn(|group| {
let index = if group == 0 { 0 } else { boundary };
cell::Cell::new(if S::LAZY || index >= count {
links::Index::END
} else {
index as u32
})
}),
live: cell::Cell::new(0),
_thread: ThreadBound::NEW,
}
}
pub(in crate::depot) fn partition(&self, index: u32) -> &cell::Cell<u32> {
if PARTITIONS == 1 || (index as usize) < self.slots.capacity().div_ceil(2) {
&self.heads[0]
} else {
&self.heads[1]
}
}
pub(in crate::depot) fn len(&self) -> capacity::Count {
capacity::Count::new(self.live.get())
}
pub(in crate::depot::arena::slab) fn values_mut(&mut self) -> impl Iterator<Item = &mut T> {
let slots = self.slots.as_ref();
let dense = unsafe { self.slots.tail().get_unchecked(..self.len().get()) };
dense.iter().map(move |position| unsafe {
&mut *slots
.get_unchecked(position.get().assume_init() as usize)
.value_ptr()
})
}
}
impl<T, const MAX: u32, M: Mode, const PARTITIONS: usize> Core<T, MAX, M, PARTITIONS> {
pub(in crate::depot::arena::slab) fn try_free(
&self,
) -> Result<slab::raw::Entry<'_, T, MAX, M, PARTITIONS, slab::Heap, false, false>, slab::Vacancy>
{
self.free().ok_or_else(|| self.slots.refusal())
}
}
impl<T, const MAX: u32, M: Mode, const PARTITIONS: usize, const N: usize>
Core<T, MAX, M, PARTITIONS, slab::Inline<N>>
{
pub(in crate::depot) fn new() -> Self {
let () = Self::VALID_PARTITIONS;
let slots = <slab::Inline<N> as Storage>::Plane::<T, MAX, M>::inline::<PARTITIONS>();
Self::from_slots(slots)
}
}
impl<T, const MAX: u32, M: Mode, const PARTITIONS: usize, S> Drop for Core<T, MAX, M, PARTITIONS, S>
where
S: slab::Storage,
{
fn drop(&mut self) {
if !mem::needs_drop::<T>() {
return;
}
for value in self.values_mut() {
unsafe { ptr::drop_in_place(value) };
}
}
}
impl<T, const MAX: u32, const RECYCLE: bool, const PARTITIONS: usize, S>
Core<T, MAX, Exclusive<RECYCLE>, PARTITIONS, S>
where
cycle::Mode<RECYCLE>: Policy,
S: slab::Storage,
{
pub(in crate::depot) unsafe fn read(&self, index: links::Index) -> &T {
unsafe { self.slots.as_ref().get_unchecked(index.get()).value() }
}
pub(in crate::depot) unsafe fn project(
&self,
index: links::Index,
) -> slab::raw::Entry<'_, T, MAX, Exclusive<RECYCLE>, PARTITIONS, S, true, false> {
unsafe { slab::raw::Entry::new(self, index) }
}
}
impl<'a, T, const MAX: u32, M: Mode, const PARTITIONS: usize, S> Core<T, MAX, M, PARTITIONS, S>
where
S: slab::Storage,
{
pub(in crate::depot) fn free(
&'a self,
) -> Option<slab::raw::Entry<'a, T, MAX, M, PARTITIONS, S, false, false>> {
for head in &self.heads {
if let Some(entry) = self.pop_head(head) {
return Some(entry);
}
}
if !S::LAZY {
return None;
}
let _ = const { key::Generation::<MAX>::MIN };
let epoch: <M::Cycle as Policy>::Stored<MAX> = Default::default();
let slot = crate::depot::raw::SlabSlot::free(epoch, links::Index::END, links::Index::END);
slot.metadata.state.set(slab::State::Reserved);
let index = self.slots.push(slot)?;
Some(unsafe { slab::raw::Entry::new(self, index) })
}
fn pop_head(
&'a self,
head: &cell::Cell<u32>,
) -> Option<slab::raw::Entry<'a, T, MAX, M, PARTITIONS, S, false, false>> {
let index = links::Index::decode(head.get())?;
Some(unsafe { slab::raw::Free::take_head(self, head, index) })
}
pub(in crate::depot) fn in_range(
&'a mut self,
range: ops::Range<u32>,
) -> Option<slab::raw::Entry<'a, T, MAX, M, PARTITIONS, S, false, false>> {
let cap = self.slots.capacity() as u32;
if range == (0..cap) {
return self.free();
}
if PARTITIONS == 2 {
let split = cap.div_ceil(2);
if range == (0..split) {
return self.pop_head(&self.heads[0]);
}
if range == (split..cap) {
return self.pop_head(&self.heads[1]);
}
}
slab::raw::Free::find(self, range, None).map(slab::raw::Free::take)
}
pub(in crate::depot) fn at(
&'a self,
index: u32,
) -> Option<slab::raw::Entry<'a, T, MAX, M, PARTITIONS, S, false, false>> {
if index >= self.slots.built() {
return None;
}
unsafe { self.allocated(index) }
}
pub(in crate::depot) unsafe fn allocated(
&'a self,
index: u32,
) -> Option<slab::raw::Entry<'a, T, MAX, M, PARTITIONS, S, false, false>> {
let slot = unsafe { self.slots.as_ref().get_unchecked(index as usize) };
if slot.metadata.state.get() != slab::State::Free {
return None;
}
Some(unsafe { slab::raw::Free::new(self, links::Index::new_raw(index)).unlink(slot) })
}
pub(in crate::depot) fn is_full(&self) -> bool {
self.slots.built() as usize >= self.slots.capacity()
&& self
.heads
.iter()
.all(|head| head.get() == links::Index::END)
}
}