-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
misc: applied suggestions from code review
- Loading branch information
1 parent
85e6f32
commit 2fcc6c9
Showing
13 changed files
with
229 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,92 @@ | ||
mod handle; | ||
mod mark_sweep; | ||
mod root_handle; | ||
mod ptr; | ||
mod root_ptr; | ||
|
||
pub use handle::{GCPtr, HasIndirectionPtr, RawGCPtr}; | ||
pub use mark_sweep::MarkSweep; | ||
pub use root_handle::GCRootHandle; | ||
pub use ptr::{GcPtr, HasIndirectionPtr, RawGcPtr}; | ||
pub use root_ptr::GcRootPtr; | ||
use std::marker::PhantomData; | ||
|
||
/// Contains stats about the current state of a GC implementation | ||
#[derive(Debug, Clone, Default)] | ||
pub struct Stats { | ||
pub allocated_memory: usize, | ||
} | ||
|
||
/// A trait used by the GC to identify an object. | ||
/// A trait used by the `GcRuntime` to identify an object type. | ||
pub trait Type: Send + Sync { | ||
type Trace: Iterator<Item = GCPtr>; | ||
type Trace: Iterator<Item = GcPtr>; | ||
|
||
/// Returns the size in bytes of an object of this type. | ||
/// Returns the size of an object of this type (in bytes). | ||
fn size(&self) -> usize; | ||
|
||
/// Returns the alignment of a type | ||
/// Returns the alignment of a type (in bytes). | ||
fn alignment(&self) -> usize; | ||
|
||
/// Returns an iterator to iterate over all GC objects that are referenced by the given object. | ||
fn trace(&self, obj: GCPtr) -> Self::Trace; | ||
fn trace(&self, obj: GcPtr) -> Self::Trace; | ||
} | ||
|
||
/// An object that can be used to allocate and collect memory. | ||
pub trait GCRuntime<T: Type>: Send + Sync { | ||
/// Allocates an object of the given type returning a GCPtr | ||
fn alloc(&self, ty: T) -> GCPtr; | ||
pub trait GcRuntime<T: Type>: Send + Sync { | ||
/// Allocates an object of the given type returning a GcPtr | ||
fn alloc(&self, ty: T) -> GcPtr; | ||
|
||
/// Returns the type of the specified `obj`. | ||
/// | ||
/// # Safety | ||
/// | ||
/// This method is unsafe because the passed GCPtr could point to random memory. | ||
unsafe fn ptr_type(&self, obj: GCPtr) -> T; | ||
|
||
/// Tell the runtime that the specified object should be considered a root which keeps all other | ||
/// objects it references alive. Objects marked as root, must also be unrooted before they can | ||
/// be collected. Internally this increments a root refcount. | ||
/// | ||
/// # Safety | ||
/// | ||
/// This method is unsafe because the passed GCPtr could point to random memory. | ||
unsafe fn root(&self, obj: GCPtr); | ||
|
||
/// Tell the runtime that the specified object should unrooted which keeps all other | ||
/// objects it references alive. Objects marked as root, must also be unrooted before they can | ||
/// be collected. Internally this decrements a root refcount. When the refcount reaches 0, the | ||
/// object is considered non-rooted. | ||
/// | ||
/// # Safety | ||
/// | ||
/// This method is unsafe because the passed GCPtr could point to random memory. | ||
unsafe fn unroot(&self, obj: GCPtr); | ||
fn ptr_type(&self, obj: GcPtr) -> T; | ||
|
||
/// Roots the specified `obj`, which keeps its and objects it references alive. Objects marked | ||
/// as root, must call `unroot` before they can be collected. An object can be rooted multiple | ||
/// times, but you must make sure to call `unroot` an equal number of times before the object | ||
/// can be collected. | ||
fn root(&self, obj: GcPtr); | ||
|
||
/// Unroots the specified `obj`, potentially allowing it and objects it references to be | ||
/// collected. Internally this decrements a root refcount. An object can be rooted multiple | ||
/// times, but you must make sure to call `unroot` an equal number of times before the object | ||
/// can be collected. | ||
fn unroot(&self, obj: GcPtr); | ||
|
||
/// Returns stats about the current state of the runtime. | ||
fn stats(&self) -> Stats; | ||
} | ||
|
||
/// An `Event` is an event that can be emitted by a `GcRuntime` through the use of an `Observer`. | ||
/// This enables tracking of the runtimes behavior which is useful for testing. | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub enum Event { | ||
/// The GC performed an allocation | ||
Allocation(GCPtr), | ||
Allocation(GcPtr), | ||
|
||
/// A GC cycle started | ||
Start, | ||
|
||
/// A deallocation took place | ||
Deallocation(GCPtr), | ||
Deallocation(GcPtr), | ||
|
||
/// A GC cycle ended | ||
End, | ||
} | ||
|
||
/// A `Observer` is trait that can receive `Event`s from a GC implementation. A `GCRuntime` can | ||
/// be typed by a `GCObserver` which enables optional tracing of events. | ||
/// The `Observer` trait allows receiving of `Event`s. A `GcRuntime` can be typed by a `Observer` | ||
/// which enables optional tracing of events. | ||
pub trait Observer: Send + Sync { | ||
fn event(&self, _event: Event) {} | ||
type Event; | ||
|
||
fn event(&self, _event: Self::Event) {} | ||
} | ||
|
||
/// A default implementation of a `Observer` which ensures that the compiler does not generate | ||
/// code for event handling. | ||
#[derive(Clone, Default)] | ||
pub struct NoopObserver; | ||
impl Observer for NoopObserver {} | ||
#[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 } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.