use std::sync::{
atomic::{AtomicBool, AtomicUsize, Ordering},
Arc, Mutex,
};
use crate::fixed_address_continuous_allocation::{FaVec, FaVecIndex};
pub(crate) const DATA_HOLDER_BLOCK_SIZE_INTERNAL: usize = 32;
pub(crate) type SharedDataContainerType<T> =
Arc<Mutex<FaVec<DataHolder<T>, DATA_HOLDER_BLOCK_SIZE_INTERNAL>>>;
pub(crate) struct DataHolder<T> {
pub(crate) ref_count: AtomicUsize,
pub(crate) pending_removal: AtomicBool,
pub(crate) owner: SharedDataContainerType<T>,
pub(crate) owning_key: FaVecIndex<32>,
pub data: Mutex<T>,
}
impl<T> DataHolder<T> {
pub(crate) fn deleted(&self) -> bool {
self.pending_removal.load(Ordering::Acquire)
}
pub(crate) fn ref_count(&self) -> usize {
self.ref_count.load(Ordering::Acquire)
}
pub(crate) fn set_deleted(&self) {
self.pending_removal.store(true, Ordering::Release);
}
pub(crate) fn increment_refcount(&self) {
let old_rc = self.ref_count.fetch_add(1, Ordering::Relaxed);
if old_rc >= isize::MAX as usize {
std::process::abort();
}
}
}