use core::mem::needs_drop;
use core::sync::atomic::{AtomicU32, Ordering};
use crossbeam_utils::Backoff;
use crate::Trace;
use crate::layout::GRANULE;
use crate::trace::Tracer;
const LARGE_BIT: u32 = 1 << 31;
const FORWARDED: u32 = 1 << 31;
const BUSY: u32 = 1 << 30;
pub(crate) const MAX_EPOCH: u32 = BUSY - 1;
#[repr(C)]
pub(crate) struct Header {
visit: unsafe fn(*mut u8, &mut Tracer<'_>),
drop: Option<unsafe fn(*mut u8)>,
mark: AtomicU32,
granules: u32,
}
pub(crate) const HEADER: usize = size_of::<Header>();
const _: () = assert!(HEADER.is_multiple_of(GRANULE), "对象数据要按粒度对齐");
const _: () = assert!(size_of::<Option<unsafe fn(*mut u8)>>() == size_of::<*const Header>(), "转发地址要放得进 drop 那个字");
pub(crate) enum Claim {
Mine,
Moved(*const Header),
}
impl Header {
pub(crate) fn new<T: Trace>(bytes: usize, large: bool, mark: u32) -> Self {
let granules = u32::try_from(bytes / GRANULE).expect("对象太大");
assert!(granules & LARGE_BIT == 0, "对象太大");
Self {
visit: Self::visit_as::<T>,
drop: needs_drop::<T>().then_some(Self::drop_as::<T> as unsafe fn(*mut u8)),
mark: AtomicU32::new(mark),
granules: granules | if large { LARGE_BIT } else { 0 },
}
}
unsafe fn visit_as<T: Trace>(data: *mut u8, t: &mut Tracer<'_>) {
if t.relocating() {
unsafe { &mut *data.cast::<T>() }.relocate(t);
} else {
unsafe { &*data.cast::<T>() }.trace(t);
}
}
unsafe fn drop_as<T>(data: *mut u8) {
unsafe { data.cast::<T>().drop_in_place() };
}
pub(crate) fn data(this: *const Self) -> *mut u8 {
this.cast::<u8>().cast_mut().wrapping_add(HEADER)
}
pub(crate) fn of(data: *const u8) -> *const Self {
data.wrapping_sub(HEADER).cast()
}
pub(crate) fn bytes(&self) -> usize {
(self.granules & !LARGE_BIT) as usize * GRANULE
}
pub(crate) fn is_large(&self) -> bool {
self.granules & LARGE_BIT != 0
}
pub(crate) fn mark(&self, epoch: u32) -> bool {
self.mark.load(Ordering::Relaxed) != epoch && self.mark.swap(epoch, Ordering::AcqRel) != epoch
}
pub(crate) fn is_marked(&self, epoch: u32) -> bool {
self.mark.load(Ordering::Acquire) == epoch
}
pub(crate) fn is_forwarded(&self) -> bool {
self.mark.load(Ordering::Acquire) & FORWARDED != 0
}
pub(crate) fn claim(&self) -> Claim {
let backoff = Backoff::new();
loop {
let word = self.mark.load(Ordering::Acquire);
if word & FORWARDED != 0 {
return Claim::Moved(self.forwardee());
}
if word & BUSY != 0 {
backoff.snooze();
continue;
}
if self.mark.compare_exchange_weak(word, BUSY, Ordering::AcqRel, Ordering::Acquire).is_ok() {
return Claim::Mine;
}
}
}
pub(crate) unsafe fn forward(this: *const Self, to: *const Self) {
unsafe { (&raw const (*this).drop).cast_mut().cast::<*const Self>().write(to) };
unsafe { &*this }.mark.store(FORWARDED, Ordering::Release);
}
fn forwardee(&self) -> *const Self {
unsafe { (&raw const self.drop).cast::<*const Self>().read() }
}
pub(crate) fn set_mark(&self, epoch: u32) {
self.mark.store(epoch, Ordering::Release);
}
pub(crate) unsafe fn visit(this: *const Self, t: &mut Tracer<'_>) {
unsafe { ((*this).visit)(Self::data(this), t) };
}
pub(crate) unsafe fn finalize(this: *const Self) {
if let Some(drop) = unsafe { (*this).drop } {
unsafe { drop(Self::data(this)) };
}
}
}