use crate::{
buffer::{
self,
segments::{
self, Count,
assembly::{self, total},
ranges,
},
},
depot::arena::linked::{self, fifo::ends},
};
use std::num::NonZero;
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub(in crate::buffer::segments::raw) struct Totals<L = ()> {
pub(in crate::buffer::segments::raw) nodes: NonZero<u32>,
pub(in crate::buffer::segments::raw) logical: L,
pub(in crate::buffer::segments::raw) len: NonZero<usize>,
pub(in crate::buffer::segments::raw) resident: usize,
}
pub(in crate::buffer::segments::raw) trait Logical: Copy {
fn counts(
left: &Totals<Self>,
right: &Totals<Self>,
) -> Result<(NonZero<u32>, Self), total::Error>;
}
impl Logical for () {
fn counts(
left: &Totals<Self>,
right: &Totals<Self>,
) -> Result<(NonZero<u32>, Self), total::Error> {
Ok((checked_count(left.nodes, right.nodes)?, ()))
}
}
impl Logical for NonZero<u32> {
fn counts(
left: &Totals<Self>,
right: &Totals<Self>,
) -> Result<(NonZero<u32>, Self), total::Error> {
let logical = checked_count(left.logical, right.logical)?;
let nodes = unsafe { NonZero::new_unchecked(left.nodes.get() + right.nodes.get()) };
Ok((nodes, logical))
}
}
impl From<&segments::raw::Part<'_>> for Totals<NonZero<u32>> {
fn from(part: &segments::raw::Part<'_>) -> Self {
Self {
nodes: NonZero::<u32>::MIN,
logical: part.count(),
len: part.len(),
resident: part.resident(),
}
}
}
impl From<Totals<()>> for Totals<NonZero<u32>> {
fn from(totals: Totals<()>) -> Self {
Self {
nodes: totals.nodes,
logical: totals.nodes,
len: totals.len,
resident: totals.resident,
}
}
}
#[derive(Debug)]
pub(in crate::buffer::segments::raw) struct Descriptor<L = ()> {
pub(in crate::buffer::segments::raw) links: ends::Exclusive<linked::raw::Index>,
pub(in crate::buffer::segments::raw) totals: Totals<L>,
}
impl<L> Descriptor<L> {
pub(in crate::buffer::segments::raw) unsafe fn remove_front(
&mut self,
next: linked::raw::Index,
len: usize,
resident: usize,
) {
self.totals.nodes = segments::Plural(self.totals.nodes).tail();
self.totals.len = unsafe { NonZero::new_unchecked(self.totals.len.get() - len) };
self.totals.resident -= resident;
self.links = unsafe { ends::Exclusive::from_ends(next, self.links.tail_raw()) };
}
}
fn checked_count(first: NonZero<u32>, second: NonZero<u32>) -> Result<NonZero<u32>, total::Error> {
first
.checked_add(second.get())
.ok_or(total::Error::Count(assembly::Overflow {
current: Count::new(first.get()),
added: Count::new(second.get()),
}))
}
impl<L: Logical> Totals<L> {
pub(in crate::buffer::segments::raw) fn try_join(
self,
added: Self,
) -> Result<Self, total::Error> {
let (nodes, logical) = L::counts(&self, &added)?;
let len = self
.len
.checked_add(added.len.get())
.ok_or(total::Error::Logical(assembly::Overflow {
current: self.len.get(),
added: added.len.get(),
}))?;
let resident = total::Error::resident(self.resident, added.resident)?;
Ok(Self {
nodes,
logical,
len,
resident,
})
}
}
impl From<&segments::raw::Run<'_>> for Totals<()> {
fn from(owner: &segments::raw::Run<'_>) -> Self {
Self {
nodes: NonZero::<u32>::MIN,
logical: (),
len: owner.len(),
resident: buffer::Residency::resident(owner),
}
}
}
impl From<&ranges::raw::Owned<'_>> for Totals<NonZero<u32>> {
fn from(owner: &ranges::raw::Owned<'_>) -> Self {
Self {
nodes: NonZero::<u32>::MIN,
logical: owner.count(),
len: owner.len,
resident: owner.resident(),
}
}
}
impl From<&segments::raw::Run<'_>> for Totals<NonZero<u32>> {
fn from(owner: &segments::raw::Run<'_>) -> Self {
Totals::<()>::from(owner).into()
}
}