mod array;
mod mark_sweep;
mod ptr;
mod root_ptr;
use crate::r#type::Type;
use std::marker::PhantomData;
use std::ptr::NonNull;
pub use mark_sweep::MarkSweep;
pub use ptr::{GcPtr, HasIndirectionPtr, RawGcPtr};
pub use root_ptr::GcRootPtr;
#[derive(Debug, Clone, Default)]
pub struct Stats {
pub allocated_memory: usize,
}
pub trait TypeTrace: Send + Sync {
type Trace: Iterator<Item = GcPtr>;
fn trace(&self, obj: GcPtr) -> Self::Trace;
}
pub trait Array: Sized {
type Iterator: Iterator<Item = NonNull<u8>>;
fn as_raw(&self) -> GcPtr;
fn element_type(&self) -> Type;
fn length(&self) -> usize;
fn capacity(&self) -> usize;
fn elements(&self) -> Self::Iterator;
}
pub trait GcRuntime: Send + Sync {
type Array: Array;
fn alloc(&self, ty: &Type) -> GcPtr;
fn alloc_array(&self, ty: &Type, n: usize) -> Self::Array;
fn ptr_type(&self, obj: GcPtr) -> Type;
fn array(&self, handle: GcPtr) -> Option<Self::Array>;
fn root(&self, obj: GcPtr);
fn unroot(&self, obj: GcPtr);
fn stats(&self) -> Stats;
}
pub trait Observer: Send + Sync {
type Event;
fn event(&self, _event: Self::Event) {}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Event {
Allocation(GcPtr),
Start,
Deallocation(GcPtr),
End,
}
#[derive(Clone)]
pub struct NoopObserver<T: Send + Sync> {
data: PhantomData<T>,
}
impl<T: Send + Sync> Observer for NoopObserver<T> {
type Event = T;
}
impl<T: Send + Sync> Default for NoopObserver<T> {
fn default() -> Self {
NoopObserver { data: PhantomData }
}
}