use core::cell::UnsafeCell;
use core::sync::atomic::{AtomicU8, Ordering};
use core::{mem, ptr};
pub const fn ptr_size_bits() -> usize {
mem::size_of::<usize>() * 8
}
pub fn map_in_place_2<T, U, F: FnOnce(U, T) -> T>((k, v): (U, &mut T), f: F) {
unsafe {
let promote_panic_to_abort = AbortOnPanic;
ptr::write(v, f(k, ptr::read(v)));
std::mem::forget(promote_panic_to_abort);
}
}
#[repr(transparent)]
pub struct SharedValue<T> {
value: UnsafeCell<T>,
}
impl<T: Clone> Clone for SharedValue<T> {
fn clone(&self) -> Self {
Self {
value: UnsafeCell::new(self.get().clone()),
}
}
}
unsafe impl<T: Send> Send for SharedValue<T> {}
unsafe impl<T: Sync> Sync for SharedValue<T> {}
impl<T> SharedValue<T> {
pub const fn new(value: T) -> Self {
Self {
value: UnsafeCell::new(value),
}
}
pub fn get(&self) -> &T {
unsafe { &*self.value.get() }
}
pub fn get_mut(&mut self) -> &mut T {
unsafe { &mut *self.value.get() }
}
pub fn into_inner(self) -> T {
self.value.into_inner()
}
pub(crate) fn as_ptr(&self) -> *mut T {
self.value.get()
}
}
pub(crate) struct CacheEntry<V> {
pub(crate) value: SharedValue<V>,
pub(crate) freq: AtomicU8,
pub(crate) loc: u8,
}
impl<V> CacheEntry<V> {
#[inline]
pub(crate) fn new(value: V, loc: u8) -> Self {
Self {
value: SharedValue::new(value),
freq: AtomicU8::new(0),
loc,
}
}
#[inline]
pub(crate) fn bump_freq(&self) {
let _ = self
.freq
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |f| {
if f < crate::shard::MAX_FREQ {
Some(f + 1)
} else {
None
}
});
}
}
impl<V: Clone> Clone for CacheEntry<V> {
fn clone(&self) -> Self {
Self {
value: self.value.clone(),
freq: AtomicU8::new(self.freq.load(Ordering::Relaxed)),
loc: self.loc,
}
}
}
unsafe impl<V: Send> Send for CacheEntry<V> {}
unsafe impl<V: Sync> Sync for CacheEntry<V> {}
struct AbortOnPanic;
impl Drop for AbortOnPanic {
fn drop(&mut self) {
if std::thread::panicking() {
std::process::abort()
}
}
}