dope-core 0.75.0

Owned native I/O and completion driver for dope
Documentation
use std::{marker, mem::MaybeUninit, ops, ptr};

use o3::{
    alloc::{self, array::Plan},
    depot::array::init,
};

use crate::driver::retained::flight::storage::{
    Flights, Preparation, Prepared, Slots, index_limit, is_layout_valid, state,
};

impl<T: Copy, const INDEX_BITS: u32, const GENERATION_BITS: u32>
    Flights<T, INDEX_BITS, GENERATION_BITS>
{
    pub(super) fn directory(
        &self,
    ) -> &Vec<ptr::NonNull<state::State<T, INDEX_BITS, GENERATION_BITS>>> {
        // SAFETY: each private read ends before the callback-free commit leaf;
        // external completions borrow stable entries, never the directory itself.
        unsafe { &*self.directory.get() }
    }

    /// # Safety
    /// The exact arena stays alive and immovable for `'owner`; the caller
    /// exclusively owns preparation for `'a` until it is committed or dropped.
    pub(in crate::driver::retained::flight) unsafe fn try_batch<
        'a,
        'owner,
        const GROUPS: usize,
        B,
    >(
        &'owner self,
        caps: [usize; GROUPS],
    ) -> Result<Prepared<'a, 'owner, T, INDEX_BITS, GENERATION_BITS, GROUPS, B>, alloc::Error> {
        const { assert!(GROUPS > 0) };
        if !is_layout_valid::<INDEX_BITS, GENERATION_BITS>() {
            return Err(alloc::Error::Overflow);
        }
        let base = self.directory().len();
        let mut end = base;
        let mut rest = caps
            .iter()
            .try_fold(0usize, |total, capacity| total.checked_add(*capacity));
        let mut entries = MaybeUninit::uninit();
        let mut guard = init::Guard::<_, GROUPS, false>::new(&mut entries);
        let states = loop {
            let vacant = match guard.next() {
                ops::ControlFlow::Break(states) => break states.into_array(),
                ops::ControlFlow::Continue(vacant) => vacant,
            };
            let capacity = *vacant.get(&caps);
            let last = vacant.as_slice().len() + 1 == GROUPS;
            rest = rest.map(|rest| rest - capacity);
            let selected = if last {
                state::Group::select::<B>(&self.groups, capacity)
            } else {
                state::Group::select::<()>(&self.groups, capacity)
            };
            let state = match selected {
                Some(slots) => Preparation::Reuse(slots),
                None => {
                    let room = if last && std::mem::size_of::<B>() != 0 {
                        None
                    } else {
                        rest
                    };
                    let (seed, extent, next) = Self::seed(end, capacity, room)?;
                    let allocation = if last {
                        state::Group::try_new::<B>(seed as usize, extent)?
                    } else {
                        state::Group::try_new::<()>(seed as usize, extent)?
                    };
                    end = next;
                    Preparation::New {
                        allocation,
                        capacity,
                    }
                }
            };
            guard = vacant.insert(state);
        };
        let added = end - base;
        let dir = self.directory();
        let spare = dir.capacity() - dir.len();
        let next = if added > spare {
            Some(self.try_plan(end)?)
        } else {
            None
        };
        Ok(Prepared {
            arena: self,
            preparation: marker::PhantomData,
            states,
            directory: next,
        })
    }

    fn try_plan(
        &self,
        required: usize,
    ) -> Result<Vec<ptr::NonNull<state::State<T, INDEX_BITS, GENERATION_BITS>>>, alloc::Error> {
        let dir = self.directory();
        let source = dir.as_slice();
        let mut next: Vec<ptr::NonNull<state::State<T, INDEX_BITS, GENERATION_BITS>>> =
            Plan::try_reserve(dir.capacity(), required)?.try_vec()?;
        // SAFETY: the plan retains at least the old capacity. The new allocation
        // is disjoint from this live directory, and copying initializes its prefix.
        unsafe {
            ptr::copy_nonoverlapping(source.as_ptr(), next.as_mut_ptr(), source.len());
            next.set_len(source.len());
        }
        Ok(next)
    }

    /// Admits exact capacity and rounds only within room left for the batch.
    fn seed(
        len: usize,
        capacity: usize,
        rest: Option<usize>,
    ) -> Result<(u32, usize, usize), alloc::Overflow> {
        let required = len.checked_add(capacity).ok_or(alloc::Overflow)?;
        let limit = index_limit::<INDEX_BITS>();
        if required > limit {
            return Err(alloc::Overflow);
        }
        let extent = match rest {
            Some(rest) if capacity != 0 => capacity
                .checked_next_power_of_two()
                .filter(|extent| *extent <= (limit - len).saturating_sub(rest))
                .unwrap_or(capacity),
            Some(_) | None => capacity,
        };
        Ok((len as u32, extent, len + extent))
    }
}

impl<'owner, T: Copy, const INDEX_BITS: u32, const GENERATION_BITS: u32, const GROUPS: usize, B>
    Prepared<'_, 'owner, T, INDEX_BITS, GENERATION_BITS, GROUPS, B>
{
    /// Publishes into the arena used to prepare this batch, without allocation.
    pub fn groups(self) -> [Slots<'owner, T, INDEX_BITS, GENERATION_BITS>; GROUPS] {
        let Self {
            arena,
            preparation: marker::PhantomData,
            states,
            directory: next,
        } = self;
        // SAFETY: this consumed preparation owns the only outstanding plan for
        // this exact directory. No directory references escape its short reads,
        // and this leaf invokes no completion or user callbacks while mutating it.
        let dir = unsafe { &mut *arena.directory.get() };
        if let Some(next) = next {
            *dir = next;
        }
        let mut index = 0;
        states.map(|state| {
            index += 1;
            match state {
                Preparation::Reuse(slots) => slots,
                Preparation::New {
                    allocation,
                    capacity,
                } if index == GROUPS => {
                    state::Group::publish::<B>(&arena.groups, dir, allocation, capacity)
                }
                Preparation::New {
                    allocation,
                    capacity,
                } => state::Group::publish::<()>(&arena.groups, dir, allocation, capacity),
            }
        })
    }
}