use core::cell::Cell;
use core::fmt;
use core::marker::PhantomData;
use core::ops::Deref;
use core::ptr::{NonNull, null_mut};
use core::sync::atomic::{AtomicPtr, Ordering};
use crate::Mutator;
use crate::header::Header;
pub(crate) type Invariant<'gc> = PhantomData<Cell<&'gc ()>>;
type Brand<'gc, T> = PhantomData<(&'gc T, fn(&'gc ()) -> &'gc ())>;
pub struct Gc<'gc, T> {
ptr: NonNull<T>,
_brand: Brand<'gc, T>,
}
unsafe impl<T: Send + Sync> Send for Gc<'_, T> {}
unsafe impl<T: Send + Sync> Sync for Gc<'_, T> {}
impl<T> Clone for Gc<'_, T> {
fn clone(&self) -> Self {
*self
}
}
impl<T> Copy for Gc<'_, T> {}
impl<'gc, T> Gc<'gc, T> {
pub(crate) unsafe fn from_data(ptr: *mut T) -> Self {
Self { ptr: unsafe { NonNull::new_unchecked(ptr) }, _brand: PhantomData }
}
pub(crate) fn header(self) -> *const Header {
Header::of(self.ptr.as_ptr().cast())
}
pub fn as_ptr(self) -> *const T {
self.ptr.as_ptr()
}
pub fn get(self) -> &'gc T {
unsafe { self.ptr.as_ref() }
}
pub fn ptr_eq(a: Self, b: Self) -> bool {
a.ptr == b.ptr
}
}
impl<T> Deref for Gc<'_, T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { self.ptr.as_ref() }
}
}
impl<T: fmt::Debug> fmt::Debug for Gc<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
pub struct Slot<'gc, T> {
ptr: AtomicPtr<T>,
_brand: PhantomData<Gc<'gc, T>>,
}
impl<T> Default for Slot<'_, T> {
fn default() -> Self {
Self { ptr: AtomicPtr::new(null_mut()), _brand: PhantomData }
}
}
impl<'gc, T> From<Option<Gc<'gc, T>>> for Slot<'gc, T> {
fn from(v: Option<Gc<'gc, T>>) -> Self {
Self { ptr: AtomicPtr::new(Self::raw(v)), _brand: PhantomData }
}
}
impl<'gc, T> From<Gc<'gc, T>> for Slot<'gc, T> {
fn from(v: Gc<'gc, T>) -> Self {
Some(v).into()
}
}
impl<'gc, T> Slot<'gc, T> {
fn raw(v: Option<Gc<'gc, T>>) -> *mut T {
v.map_or(null_mut(), |g| g.ptr.as_ptr())
}
fn wrap(p: *mut T) -> Option<Gc<'gc, T>> {
(!p.is_null()).then(|| unsafe { Gc::from_data(p) })
}
pub fn get(&self) -> Option<Gc<'gc, T>> {
Self::wrap(self.ptr.load(Ordering::Acquire))
}
pub fn set(&self, m: &Mutator<'gc>, v: Option<Gc<'gc, T>>) {
self.swap(m, v);
}
pub fn swap(&self, m: &Mutator<'gc>, v: Option<Gc<'gc, T>>) -> Option<Gc<'gc, T>> {
let old = self.ptr.swap(Self::raw(v), Ordering::AcqRel);
Self::barrier(m, old);
Self::wrap(old)
}
pub fn compare_exchange(&self, m: &Mutator<'gc>, current: Option<Gc<'gc, T>>, new: Option<Gc<'gc, T>>) -> Result<Option<Gc<'gc, T>>, Option<Gc<'gc, T>>> {
let done = self.ptr.compare_exchange(Self::raw(current), Self::raw(new), Ordering::AcqRel, Ordering::Acquire);
if let Ok(old) = done {
Self::barrier(m, old);
}
done.map(Self::wrap).map_err(Self::wrap)
}
pub(crate) fn store_exclusive(&mut self, v: Option<Gc<'gc, T>>) {
*self.ptr.get_mut() = Self::raw(v);
}
fn barrier(m: &Mutator<'gc>, old: *mut T) {
if !old.is_null() {
m.overwritten(Header::of(old.cast()));
}
}
}
impl<T: fmt::Debug> fmt::Debug for Slot<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Slot").field(&self.get()).finish()
}
}