use std::{cell, convert, marker, mem, pin::Pin};
use cycle::Policy;
use crate::{
alloc,
base::num,
depot::{
arena::slab::{self, external, key::cycle},
capacity,
queue::links,
},
};
#[derive(Clone, Copy)]
pub(in crate::depot) struct SlabLinks {
pub(in crate::depot) next: u32,
pub(in crate::depot) prev: u32,
}
union Data<T> {
links: SlabLinks,
value: mem::ManuallyDrop<T>,
}
pub(in crate::depot) struct SlabMetadata<G: Copy, B: Copy, R: Copy> {
pub(in crate::depot) state: cell::Cell<slab::State<B, R>>,
pub(in crate::depot) generation: cell::Cell<G>,
pub(in crate::depot) position: cell::Cell<u32>,
}
pub struct SlabSlot<T, G: Copy, B: Copy, R: Copy> {
pub(in crate::depot) metadata: SlabMetadata<G, B, R>,
data: cell::UnsafeCell<Data<T>>,
}
impl<T, G: Copy, B: Copy, R: Copy> SlabSlot<T, G, B, R> {
pub(in crate::depot) fn free(epoch: G, next: u32, prev: u32) -> Self {
Self {
metadata: SlabMetadata {
state: cell::Cell::new(slab::State::Free),
generation: cell::Cell::new(epoch),
position: cell::Cell::new(links::Index::END),
},
data: cell::UnsafeCell::new(Data {
links: SlabLinks { next, prev },
}),
}
}
pub(in crate::depot) unsafe fn links(&self) -> SlabLinks {
unsafe { (*self.data.get()).links }
}
pub(in crate::depot) unsafe fn set_links(&self, links: SlabLinks) {
unsafe { (*self.data.get()).links = links };
}
pub(in crate::depot) unsafe fn set_next(&self, next: u32) {
unsafe { (*self.data.get()).links.next = next };
}
pub(in crate::depot) unsafe fn set_prev(&self, prev: u32) {
unsafe { (*self.data.get()).links.prev = prev };
}
pub(in crate::depot) unsafe fn value(&self) -> &T {
unsafe { &*(&raw const (*self.data.get()).value).cast::<T>() }
}
pub(in crate::depot) unsafe fn value_ptr(&self) -> *mut T {
unsafe { (&raw mut (*self.data.get()).value).cast::<T>() }
}
pub(in crate::depot) unsafe fn take_value(&self) -> T {
unsafe { mem::ManuallyDrop::take(&mut (*self.data.get()).value) }
}
pub(in crate::depot) unsafe fn drop_value(&self) {
unsafe { mem::ManuallyDrop::drop(&mut (*self.data.get()).value) };
}
}
pub(in crate::depot) struct SlabCapacity<T, G: Copy, B: Copy, R: Copy> {
pub(in crate::depot) count: capacity::Count,
slot: SlotMarker<SlabSlot<T, G, B, R>>,
}
type SlotMarker<S> = marker::PhantomData<fn() -> S>;
impl<T, G: Copy, B: Copy, R: Copy> SlabCapacity<T, G, B, R> {
pub(in crate::depot) fn new(count: capacity::Count) -> Result<Self, alloc::Overflow> {
use alloc::raw::Rows;
if count.get() > Self::MAX {
return Err(alloc::Overflow);
}
Ok(Self {
count,
slot: marker::PhantomData,
})
}
}
unsafe impl<T, G: Copy, B: Copy, R: Copy> alloc::raw::Shape for SlabCapacity<T, G, B, R> {
type Root = SlabSlot<T, G, B, R>;
type Tail = cell::Cell<mem::MaybeUninit<u32>>;
type Extra = ();
type Last = ();
fn len(&self) -> usize {
self.count.get()
}
}
unsafe impl<T, G: Copy, B: Copy, R: Copy> alloc::raw::Rows for SlabCapacity<T, G, B, R> {}
const _: () = {
assert!(mem::size_of::<slab::State<(), ()>>() == 1);
assert!(mem::size_of::<slab::State<(), convert::Infallible>>() == 1);
assert!(mem::size_of::<slab::State<convert::Infallible, ()>>() == 1);
assert!(mem::size_of::<slab::State<convert::Infallible, convert::Infallible>>() == 1);
assert!(
mem::size_of::<SlabSlot<u64, u32, (), ()>>()
== mem::size_of::<SlabSlot<u64, u32, (), convert::Infallible>>()
);
assert!(
mem::size_of::<SlabSlot<u64, u32, (), ()>>()
== mem::size_of::<SlabSlot<u64, u32, convert::Infallible, ()>>()
);
assert!(
mem::size_of::<SlabSlot<u64, u32, (), ()>>()
== mem::size_of::<SlabSlot<u64, u32, convert::Infallible, convert::Infallible>>()
);
};
unsafe impl<'a, T, G: num::Step, Tag> crate::depot::raw::Project<u32>
for &'a external::Pinned<T, G, Tag>
{
type Output = &'a T;
unsafe fn project(self, index: u32) -> Self::Output {
&unsafe { self.core.read(links::Index::new_raw(index)) }.value
}
}
unsafe impl<'a, T, G: num::Step, Tag> crate::depot::raw::Project<u32>
for Pin<&'a mut external::Pinned<T, G, Tag>>
{
type Output = external::raw::Anchored<'a, T, G, Tag>;
unsafe fn project(self, index: u32) -> Self::Output {
unsafe { external::raw::Anchored::from_slot(self, links::Index::new_raw(index)) }
}
}
unsafe impl<'a, T, Tag, const MAX: u32, const RECYCLE: bool, const PARTITIONS: usize, S>
crate::depot::raw::Project<u32> for &'a slab::Pinned<T, Tag, MAX, RECYCLE, PARTITIONS, S>
where
cycle::Mode<RECYCLE>: Policy,
S: slab::Storage,
{
type Output = &'a T;
unsafe fn project(self, index: u32) -> Self::Output {
let index = unsafe { links::Index::new_raw(index) };
unsafe { self.core.read(index) }
}
}
unsafe impl<'a, T, Tag, const MAX: u32, const RECYCLE: bool, const PARTITIONS: usize, S>
crate::depot::raw::Project<u32>
for Pin<&'a mut slab::Pinned<T, Tag, MAX, RECYCLE, PARTITIONS, S>>
where
cycle::Mode<RECYCLE>: Policy,
S: slab::Storage,
{
type Output = slab::raw::Anchored<'a, T, Tag, MAX, RECYCLE, PARTITIONS, S>;
unsafe fn project(self, index: u32) -> Self::Output {
let index = unsafe { links::Index::new_raw(index) };
let store = self.into_ref().get_ref();
let entry = unsafe { store.core.project(index) };
unsafe { slab::raw::Anchored::pinned(entry) }
}
}