use std::cell::Cell;
use crate::{buffer::storage, depot::queue::links};
pub(super) mod account;
pub(super) const END: u32 = links::Index::encode(None);
pub(super) const fn link(index: u32, limit: u32) -> u32 {
if index == limit { END } else { index }
}
#[repr(transparent)]
pub(super) struct Free(Cell<u32>);
impl Free {
pub(super) const fn new(limit: u32) -> Self {
Self(Cell::new(link(0, limit)))
}
pub(super) fn head(&self) -> Option<Key> {
Some(Key(links::Index::decode(self.0.get())?.raw()))
}
}
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::buffer::pool) struct Key(u32);
impl Key {
pub(super) const FIRST: Self = Self(0);
pub(super) const unsafe fn new(index: u32) -> Self {
Self(index)
}
pub(super) const fn get(self) -> u32 {
self.0
}
pub(super) unsafe fn recycle(self, head: &Free, prefix: &storage::raw::Prefix) {
unsafe { prefix.write(head.0.get()) };
head.0.set(self.0);
}
pub(super) unsafe fn occupy(self, head: &Free, prefix: &storage::raw::Prefix) {
head.0.set(prefix.word());
unsafe { prefix.write(self.0) };
}
}
const _: () = {
assert!(size_of::<Key>() == size_of::<u32>());
assert!(size_of::<Free>() == size_of::<u32>());
};