1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
use crate::{
finalizer_safe,
internals::{EphemeronBox, GcBox},
trace::{Finalize, Trace},
Allocator, Ephemeron, GcErasedPointer, Tracer, WeakGc,
};
use std::{
cmp::Ordering,
fmt::{self, Debug, Display},
hash::{Hash, Hasher},
marker::PhantomData,
ops::Deref,
ptr::NonNull,
rc::Rc,
};
use super::addr_eq;
/// Zero sized struct that is used to ensure that we do not call trace methods,
/// call its finalization method or drop it.
///
/// This can only happen if we are accessing a [`GcErasedPointer`] directly which is a bug.
/// Panics if any of it's methods are called.
///
/// Note: Accessing the [`crate::internals::GcHeader`] of [`GcErasedPointer`] is fine.
pub(crate) struct NonTraceable(());
impl Finalize for NonTraceable {
fn finalize(&self) {
unreachable!()
}
}
unsafe impl Trace for NonTraceable {
unsafe fn trace(&self, _tracer: &mut Tracer) {
unreachable!()
}
unsafe fn trace_non_roots(&self) {
unreachable!()
}
fn run_finalizer(&self) {
unreachable!()
}
}
impl Drop for NonTraceable {
fn drop(&mut self) {
unreachable!()
}
}
/// A garbage-collected pointer type over an immutable value.
pub struct Gc<T: Trace + ?Sized + 'static> {
pub(crate) inner_ptr: NonNull<GcBox<T>>,
pub(crate) marker: PhantomData<Rc<T>>,
}
impl<T: Trace + ?Sized> Gc<T> {
/// Constructs a new `Gc<T>` with the given value.
#[must_use]
pub fn new(value: T) -> Self
where
T: Sized,
{
// Create GcBox and allocate it to heap.
//
// Note: Allocator can cause Collector to run
let inner_ptr = Allocator::alloc_gc(GcBox::new(value));
Self {
inner_ptr,
marker: PhantomData,
}
}
/// Constructs a new `Gc<T>` while giving you a `WeakGc<T>` to the allocation, to allow
/// constructing a T which holds a weak pointer to itself.
///
/// Since the new `Gc<T>` is not fully-constructed until `Gc<T>::new_cyclic` returns, calling
/// [`upgrade`][WeakGc::upgrade] on the weak reference inside the closure will fail and result
/// in a `None` value.
#[must_use]
pub fn new_cyclic<F>(data_fn: F) -> Self
where
F: FnOnce(&WeakGc<T>) -> T,
T: Sized,
{
// SAFETY: The newly allocated ephemeron is only live here, meaning `Ephemeron` is the
// sole owner of the allocation after passing it to `from_raw`, making this operation safe.
let weak = unsafe {
Ephemeron::from_raw(Allocator::alloc_ephemeron(EphemeronBox::new_empty())).into()
};
let gc = Self::new(data_fn(&weak));
// SAFETY:
// - `as_mut`: `weak` is properly initialized by `alloc_ephemeron` and cannot escape the
// `unsafe` block.
// - `set_kv`: `weak` is a newly created `EphemeronBox`, meaning it isn't possible to
// collect it since `weak` is still live.
unsafe { weak.inner().inner_ptr().as_mut().set(&gc, ()) }
gc
}
/// Consumes the `Gc`, returning a wrapped raw pointer.
///
/// To avoid a memory leak, the pointer must be converted back to a `Gc` using [`Gc::from_raw`].
#[must_use]
pub fn into_raw(this: Self) -> NonNull<GcBox<T>> {
let ptr = this.inner_ptr();
std::mem::forget(this);
ptr
}
/// Returns `true` if the two `Gc`s point to the same allocation.
#[must_use]
pub fn ptr_eq(this: &Self, other: &Self) -> bool {
addr_eq(this.inner(), other.inner())
}
/// Constructs a `Gc<T>` from a raw pointer.
///
/// The raw pointer must have been returned by a previous call to [`Gc<U>::into_raw`][Gc::into_raw]
/// where `U` must have the same size and alignment as `T`.
///
/// # Safety
///
/// This function is unsafe because improper use may lead to memory corruption, double-free,
/// or misbehaviour of the garbage collector.
#[must_use]
pub const unsafe fn from_raw(inner_ptr: NonNull<GcBox<T>>) -> Self {
Self {
inner_ptr,
marker: PhantomData,
}
}
pub(crate) fn as_erased(&self) -> GcErasedPointer {
self.inner_ptr.cast()
}
}
impl<T: Trace + ?Sized> Gc<T> {
pub(crate) fn inner_ptr(&self) -> NonNull<GcBox<T>> {
assert!(finalizer_safe());
self.inner_ptr
}
fn inner(&self) -> &GcBox<T> {
// SAFETY: Please see Gc::inner_ptr()
unsafe { self.inner_ptr().as_ref() }
}
}
impl<T: Trace + ?Sized> Finalize for Gc<T> {
fn finalize(&self) {
// SAFETY: inner_ptr should be alive when calling finalize.
// We don't call inner_ptr() to avoid overhead of calling finalizer_safe().
unsafe {
self.inner_ptr.as_ref().dec_ref_count();
}
}
}
// SAFETY: `Gc` maintains it's own rootedness and implements all methods of
// Trace. It is not possible to root an already rooted `Gc` and vice versa.
unsafe impl<T: Trace + ?Sized> Trace for Gc<T> {
unsafe fn trace(&self, tracer: &mut Tracer) {
tracer.enqueue(self.as_erased());
}
unsafe fn trace_non_roots(&self) {
self.inner().inc_non_root_count();
}
fn run_finalizer(&self) {
Finalize::finalize(self);
}
}
impl<T: Trace + ?Sized> Clone for Gc<T> {
fn clone(&self) -> Self {
let ptr = self.inner_ptr();
self.inner().inc_ref_count();
// SAFETY: though `ptr` doesn't come from a `into_raw` call, it essentially does the same,
// but it skips the call to `std::mem::forget` since we have a reference instead of an owned
// value.
unsafe { Self::from_raw(ptr) }
}
}
impl<T: Trace + ?Sized> Deref for Gc<T> {
type Target = T;
fn deref(&self) -> &T {
self.inner().value()
}
}
impl<T: Trace + ?Sized> Drop for Gc<T> {
fn drop(&mut self) {
if finalizer_safe() {
Finalize::finalize(self);
}
}
}
impl<T: Trace + Default> Default for Gc<T> {
fn default() -> Self {
Self::new(Default::default())
}
}
#[allow(clippy::inline_always)]
impl<T: Trace + ?Sized + PartialEq> PartialEq for Gc<T> {
#[inline(always)]
fn eq(&self, other: &Self) -> bool {
**self == **other
}
}
impl<T: Trace + ?Sized + Eq> Eq for Gc<T> {}
#[allow(clippy::inline_always)]
impl<T: Trace + ?Sized + PartialOrd> PartialOrd for Gc<T> {
#[inline(always)]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
(**self).partial_cmp(&**other)
}
#[inline(always)]
fn lt(&self, other: &Self) -> bool {
**self < **other
}
#[inline(always)]
fn le(&self, other: &Self) -> bool {
**self <= **other
}
#[inline(always)]
fn gt(&self, other: &Self) -> bool {
**self > **other
}
#[inline(always)]
fn ge(&self, other: &Self) -> bool {
**self >= **other
}
}
impl<T: Trace + ?Sized + Ord> Ord for Gc<T> {
fn cmp(&self, other: &Self) -> Ordering {
(**self).cmp(&**other)
}
}
impl<T: Trace + ?Sized + Hash> Hash for Gc<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
(**self).hash(state);
}
}
impl<T: Trace + ?Sized + Display> Display for Gc<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&**self, f)
}
}
impl<T: Trace + ?Sized + Debug> Debug for Gc<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Debug::fmt(&**self, f)
}
}
impl<T: Trace + ?Sized> fmt::Pointer for Gc<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Pointer::fmt(&self.inner(), f)
}
}
impl<T: Trace + ?Sized> std::borrow::Borrow<T> for Gc<T> {
fn borrow(&self) -> &T {
self
}
}
impl<T: Trace + ?Sized> AsRef<T> for Gc<T> {
fn as_ref(&self) -> &T {
self
}
}