use std::{mem, ptr};
use crate::{
buffer::{
pool,
pool::{owners::Key, state},
storage::regions,
},
mem::rc,
};
#[repr(C, align(8))]
pub(in crate::buffer) struct Slot<K: crate::buffer::raw::Backing = super::Plain> {
prefix: regions::Prefix,
pub(super) link: K::Link,
}
unsafe impl<K: crate::buffer::raw::Backing> crate::mem::raw::Edge for Slot<K> {
fn count(&self) -> &crate::mem::raw::Count {
crate::mem::raw::Edge::count(&self.prefix)
}
}
unsafe impl<K: crate::buffer::raw::Backing> crate::mem::raw::Target for Slot<K> {
unsafe fn destroy(pointer: ptr::NonNull<Self>) {
let (slots, key) = unsafe { super::Ptr::<K, super::Any>::from_owner(pointer.cast()) };
Retired { slots, key }.finish();
}
}
#[repr(transparent)]
pub(in crate::buffer::pool) struct Seed<K, S> {
slots: super::Ptr<K, S>,
}
#[repr(C)]
pub(super) struct Retired<K, S> {
slots: super::Ptr<K, S>,
key: Key,
}
impl<K: crate::buffer::raw::Backing, S> pool::Span<K, pool::Owned, S> {
pub(in crate::buffer::pool) fn acquire(slots: super::Ptr<K, S>) -> Option<Self> {
let key = slots.header().free.head()?;
Some(unsafe { Self::take(slots, key) })
}
unsafe fn take(slots: super::Ptr<K, S>, key: Key) -> Self {
use crate::mem::raw::Edge;
let slot = slots.slot(key);
let header = slots.header();
unsafe {
key.occupy(&header.free, &slot.prefix);
if K::COUNTED {
slot.activate();
}
}
header.available.set(header.available.get() - 1);
Self {
slots,
key,
len: 0,
root: pool::Owned,
}
}
}
impl<K: crate::buffer::raw::Backing, S> Seed<K, S> {
pub(super) const unsafe fn from_ptr(core: super::Ptr<K, S>) -> Self {
Self { slots: core }
}
pub(in crate::buffer::pool) fn take(self) -> pool::Span<K, pool::Owned, S> {
let key = unsafe { Key::new(0) };
unsafe { pool::Span::take(self.slots, key) }
}
}
impl<K: crate::buffer::raw::Backing> Slot<K> {
pub(in crate::buffer::pool) unsafe fn release<S>(slots: super::Ptr<K, S>, key: Key) {
use crate::mem::raw::Edge;
if K::COUNTED && unsafe { Edge::release(slots.slot(key)) } != rc::Release::Last {
return;
}
Retired { slots, key }.finish();
}
}
impl<K: crate::buffer::raw::Backing, M, S> pool::Span<K, M, S> {
pub(in crate::buffer::pool) fn data(&self) -> *mut u8 {
let offset = self.slots.data_offset().get()
+ self.key.get() as usize * self.capacity().get() as usize;
unsafe { self.slots.as_raw().as_ptr().cast::<u8>().add(offset) }
}
pub(in crate::buffer) fn into_edge(self) -> super::Owner<K, M> {
use crate::mem::raw::Edge;
let this = mem::ManuallyDrop::new(self);
let slot = this.slots.ptr(this.key);
if !K::COUNTED {
unsafe { slot.as_ref().activate() };
}
super::Owner::new(slot.cast())
}
}
impl<K: crate::buffer::raw::Backing, S> Retired<K, S> {
pub(super) fn finish(self) {
{
let header = self.slots.header();
let slot = self.slots.slot(self.key);
unsafe { self.key.recycle(&header.free, &slot.prefix) };
header.available.set(header.available.get() + 1);
}
super::Header::retire(self.slots);
}
}
impl<M, S> pool::Span<super::Resident<'_>, M, S> {
pub(in crate::buffer::pool) fn is_unique(&self) -> bool {
use crate::mem::raw::Edge;
self.slots.slot(self.key).is_unique()
}
}
const _: () = {
assert!(size_of::<Slot>() == 8);
assert!(size_of::<Slot<super::Resident<'static>>>() == 16);
assert!(align_of::<Slot>() == 8);
assert!(size_of::<Seed<super::Plain, state::Uninit>>() == size_of::<usize>());
assert!(size_of::<Retired<super::Plain, state::Uninit>>() == 2 * size_of::<usize>());
assert!(size_of::<super::Binding<super::Plain>>() == 2 * size_of::<usize>());
assert!(size_of::<super::Binding<super::Resident<'static>>>() == 2 * size_of::<usize>());
};
impl<K: crate::buffer::raw::Backing> Slot<K> {
pub(super) unsafe fn initialize(base: super::Base<K>, slots: u32, link: K::Link) {
let base = unsafe { base.slots().as_ptr() };
for index in 0..slots {
let next = super::key::link(index + 1, slots);
unsafe {
base.add(index as usize).write(Self {
prefix: regions::Prefix::empty(next),
link: link.clone(),
});
}
}
}
pub(super) unsafe fn key(&self) -> Key {
unsafe { Key::new(self.prefix.word()) }
}
}