use std::cell;
use crate::buffer::pool::owners::Key;
#[repr(transparent)]
pub(super) struct Free<const ACCOUNT: bool = false> {
head: cell::Cell<u32>,
}
impl<const ACCOUNT: bool> Free<ACCOUNT> {
pub(super) const fn new(limit: u32) -> Self {
Self {
head: cell::Cell::new(Key::<ACCOUNT>::encode(if limit == 0 {
None
} else {
Some(unsafe { Key::new(0) })
})),
}
}
pub(super) fn head(&self) -> Option<Key<ACCOUNT>> {
unsafe { Key::decode(self.head.get()) }
}
pub(super) unsafe fn pop(&self, next: u32) {
self.head.set(next);
}
pub(super) unsafe fn push(&self, key: Key<ACCOUNT>, write: impl FnOnce(Option<Key<ACCOUNT>>)) {
write(self.head());
self.head.set(key.get());
}
}
const _: () = {
assert!(size_of::<Free>() == size_of::<u32>());
assert!(size_of::<Free<true>>() == size_of::<u32>());
};