o3 0.61.0

shared-nothing primitives
Documentation
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,
}

// SAFETY: Slot retains its exact initialized prefix and embedded reference domain.
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)
    }
}

// SAFETY: each slot edge retains its exact published pool until terminal release.
unsafe impl<K: crate::buffer::raw::Backing> crate::mem::raw::Target for Slot<K> {
    unsafe fn destroy(pointer: ptr::NonNull<Self>) {
        // SAFETY: Last transfers this slot with its live key and pool still intact.
        let (slots, key) = unsafe { super::Ptr::<K, super::Any>::from_owner(pointer.cast()) };
        Retired { slots, key }.finish();
    }
}

/// One freshly initialized nonempty slot list before its first acquisition.
#[repr(transparent)]
pub(in crate::buffer::pool) struct Seed<K, S> {
    slots: super::Ptr<K, S>,
}

/// Exact terminal slot state produced only by the last live edge.
#[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()?;
        // SAFETY: this exact vacant head is consumed without intervening reentry.
        Some(unsafe { Self::take(slots, key) })
    }

    /// # Safety
    /// `key` is this pool's exclusively claimed, initialized free-list head.
    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();
        // SAFETY: the caller owns this exact vacant head. Any resident association
        // is empty; only counted spans can issue an alias before publication.
        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> {
    /// # Safety
    /// The core must have just published a nonempty Slot list initialized in
    /// index order and must not have exposed any acquisition capability.
    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> {
        // SAFETY: Seed proves index zero belongs to this exact freshly
        // initialized nonempty list and is its current free-list head.
        let key = unsafe { Key::new(0) };
        // SAFETY: Seed owns this initialized head until the first acquisition.
        unsafe { pool::Span::take(self.slots, key) }
    }
}

impl<K: crate::buffer::raw::Backing> Slot<K> {
    /// # Safety
    /// The caller must transfer one live edge for this exact pool slot.
    pub(in crate::buffer::pool) unsafe fn release<S>(slots: super::Ptr<K, S>, key: Key) {
        use crate::mem::raw::Edge;

        // SAFETY: the caller transfers its one live edge to this exact slot.
        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;
        // SAFETY: this live span retains the allocation and its admitted slot range.
        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 {
            // SAFETY: consuming this unique span publishes its first counted edge.
            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);
            // SAFETY: Retired owns this exact detached slot until publication.
            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) {
        // SAFETY: Layout places exactly `slots` Slot cells immediately after
        // the Header cell owned by this unpublished allocation base.
        let base = unsafe { base.slots().as_ptr() };
        for index in 0..slots {
            let next = super::key::link(index + 1, slots);
            // SAFETY: every index is in the allocated Slot array and is
            // initialized exactly once before the pool is published.
            unsafe {
                base.add(index as usize).write(Self {
                    prefix: regions::Prefix::empty(next),
                    link: link.clone(),
                });
            }
        }
    }

    /// # Safety
    /// The slot still contains its live key, before free-list publication.
    pub(super) unsafe fn key(&self) -> Key {
        // SAFETY: the caller retains the exact key published at activation.
        unsafe { Key::new(self.prefix.word()) }
    }
}