|
| 1 | +//! Lifetime branded null GC |
| 2 | +#![cfg_attr(not(any(test, feature = "std")), allow(unused_imports))] |
| 3 | + |
| 4 | +pub mod cell; |
| 5 | +pub mod ephemeron; |
| 6 | +pub mod gc; |
| 7 | +pub mod gc_box; |
| 8 | +pub mod mutation_ctx; |
| 9 | +pub mod root; |
| 10 | +pub mod trace; |
| 11 | +pub mod weak; |
| 12 | + |
| 13 | +pub use cell::GcRefCell; |
| 14 | +pub use ephemeron::Ephemeron; |
| 15 | +pub use gc::Gc; |
| 16 | +pub use mutation_ctx::MutationContext; |
| 17 | +pub use root::Root; |
| 18 | +pub use trace::{Finalize, Trace, Tracer}; |
| 19 | +pub use weak::WeakGc; |
| 20 | + |
| 21 | +use crate::alloc::mempool3::{PoolAllocError, PoolAllocator, PoolPointer}; |
| 22 | +use core::cell::RefCell; |
| 23 | +use core::marker::PhantomData; |
| 24 | +use core::ptr::NonNull; |
| 25 | +use gc_box::{DropFn, GcBox}; |
| 26 | +use rust_alloc::vec::Vec; |
| 27 | + |
| 28 | +pub(crate) struct Collector { |
| 29 | + // SAFETY: We use 'static here because the PoolAllocator owns its memory, |
| 30 | + // and we ensure that `Gc` objects and pool allocations do not outlive |
| 31 | + // the `Collector` instance |
| 32 | + pub(crate) pool: RefCell<PoolAllocator<'static>>, |
| 33 | +} |
| 34 | + |
| 35 | +impl Collector { |
| 36 | + fn new() -> Self { |
| 37 | + Self { |
| 38 | + pool: RefCell::new(PoolAllocator::default()), |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /// Allocates a value from the pool. |
| 43 | + pub(crate) fn try_alloc<'gc, T: trace::Trace + trace::Finalize + 'gc>( |
| 44 | + &'gc self, |
| 45 | + value: T, |
| 46 | + ) -> Result<Gc<'gc, T>, PoolAllocError> { |
| 47 | + unsafe fn drop_and_free<T: trace::Trace + trace::Finalize>( |
| 48 | + pool: &mut PoolAllocator<'static>, |
| 49 | + ptr: NonNull<u8>, |
| 50 | + ) { |
| 51 | + use crate::alloc::mempool3::PoolItem; |
| 52 | + unsafe { |
| 53 | + let typed_ptr = ptr.cast::<PoolItem<GcBox<T>>>(); |
| 54 | + (*typed_ptr.as_ptr()).0.value.finalize(); |
| 55 | + core::ptr::drop_in_place(typed_ptr.as_ptr()); |
| 56 | + pool.free_slot(ptr); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + let mut pool = self.pool.borrow_mut(); |
| 61 | + let ptr = pool.try_alloc(GcBox::new(value, drop_and_free::<T>))?; |
| 62 | + |
| 63 | + drop(pool); |
| 64 | + |
| 65 | + Ok(Gc { |
| 66 | + ptr: unsafe { ptr.extend_lifetime() }, |
| 67 | + _marker: PhantomData, |
| 68 | + }) |
| 69 | + } |
| 70 | + |
| 71 | + /// Runs a collection cycle (no-op for null collector) |
| 72 | + pub(crate) fn collect(&self) {} |
| 73 | +} |
| 74 | + |
| 75 | +impl Drop for Collector { |
| 76 | + /// Frees all remaining allocations |
| 77 | + fn drop(&mut self) { |
| 78 | + use crate::alloc::mempool3::PoolItem; |
| 79 | + |
| 80 | + // Free all GC allocations |
| 81 | + let all: Vec<(NonNull<u8>, DropFn)> = self |
| 82 | + .pool |
| 83 | + .borrow() |
| 84 | + .iter_live_slots() |
| 85 | + .map(|ptr| unsafe { |
| 86 | + let drop_fn = (*ptr.cast::<PoolItem<GcBox<()>>>().as_ptr()).0.drop_fn; |
| 87 | + (ptr, drop_fn) |
| 88 | + }) |
| 89 | + .collect(); |
| 90 | + let mut pool = self.pool.borrow_mut(); |
| 91 | + for (ptr, drop_fn) in all { |
| 92 | + unsafe { |
| 93 | + (drop_fn)(&mut pool, ptr); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/// Owns the GC and carries the `'id` context brand |
| 100 | +pub struct GcContext<'id> { |
| 101 | + collector: Collector, |
| 102 | + _marker: PhantomData<*mut &'id ()>, |
| 103 | +} |
| 104 | + |
| 105 | +impl<'id> GcContext<'id> { |
| 106 | + /// Opens a mutation window and passes a [`MutationContext`] to `f` |
| 107 | + /// Triggers a gc cycle |
| 108 | + pub fn collect(&self) { |
| 109 | + self.collector.collect(); |
| 110 | + } |
| 111 | + |
| 112 | + pub fn mutate<R>(&self, f: impl for<'gc> FnOnce(&MutationContext<'id, 'gc>) -> R) -> R { |
| 113 | + let cx = MutationContext { |
| 114 | + collector: &self.collector, |
| 115 | + _marker: PhantomData, |
| 116 | + }; |
| 117 | + f(&cx) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +/// Create new GC context |
| 122 | +pub fn with_gc<R, F: for<'id> FnOnce(GcContext<'id>) -> R>(f: F) -> R { |
| 123 | + f(GcContext { |
| 124 | + collector: Collector::new(), |
| 125 | + _marker: PhantomData, |
| 126 | + }) |
| 127 | +} |
0 commit comments