Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions oscars/src/collectors/mark_sweep/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,35 @@ use super::WeakGc;
use super::WeakMap;
use super::cell::GcRefCell;

#[test]
fn unsafe_empty_trace_runs_finalize() {
use core::sync::atomic::{AtomicUsize, Ordering};

static FINALIZED: AtomicUsize = AtomicUsize::new(0);

struct Probe;

impl Finalize for Probe {
fn finalize(&self) {
FINALIZED.fetch_add(1, Ordering::SeqCst);
}
}

// SAFETY: `Probe` has no GC references to trace.
unsafe impl Trace for Probe {
crate::unsafe_empty_trace!();
}

FINALIZED.store(0, Ordering::SeqCst);
let probe = Probe;
<Probe as Trace>::run_finalizer(&probe);
assert_eq!(
FINALIZED.load(Ordering::SeqCst),
1,
"unsafe_empty_trace! must delegate to Finalize::finalize"
);
}

#[test]
fn basic_gc() {
let collector = &mut MarkSweepGarbageCollector::default()
Expand Down
17 changes: 17 additions & 0 deletions oscars/src/collectors/mark_sweep/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ macro_rules! empty_trace {
};
}

/// Utility macro to define an empty implementation of [`Trace`] inside an
/// `unsafe impl Trace` block.
///
/// This mirrors `empty_trace!` semantics while making the unsafety explicit at
/// the call site.
#[macro_export]
macro_rules! unsafe_empty_trace {
() => {
#[inline]
unsafe fn trace(&self, _color: $crate::collectors::mark_sweep::TraceColor) {}
#[inline]
fn run_finalizer(&self) {
$crate::collectors::mark_sweep::Finalize::finalize(self);
}
};
}

/// Utility macro to manually implement [`Trace`] on a type.
///
/// You define a `this` parameter name and pass in a body, which should call `mark` on every
Expand Down
29 changes: 29 additions & 0 deletions oscars/src/collectors/mark_sweep_arena2/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,35 @@ use super::WeakGc;
use super::WeakMap;
use super::cell::GcRefCell;

#[test]
fn unsafe_empty_trace_runs_finalize() {
use core::sync::atomic::{AtomicUsize, Ordering};

static FINALIZED: AtomicUsize = AtomicUsize::new(0);

struct Probe;

impl Finalize for Probe {
fn finalize(&self) {
FINALIZED.fetch_add(1, Ordering::SeqCst);
}
}

// SAFETY: `Probe` has no GC references to trace.
unsafe impl Trace for Probe {
crate::unsafe_empty_trace!();
}

FINALIZED.store(0, Ordering::SeqCst);
let probe = Probe;
<Probe as Trace>::run_finalizer(&probe);
assert_eq!(
FINALIZED.load(Ordering::SeqCst),
1,
"unsafe_empty_trace! must delegate to Finalize::finalize"
);
}

#[test]
fn basic_gc() {
let collector = &mut MarkSweepGarbageCollector::default()
Expand Down
6 changes: 3 additions & 3 deletions oscars/src/collectors/mark_sweep_arena2/trace.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Both collectors use the exact same `Trace` types
// NOTE: `empty_trace!` and `custom_trace!` hardcode `mark_sweep` paths
// This works now but will silently break if the types ever diverge.
// Both collectors use the exact same `Trace` types.
// NOTE: `empty_trace!` and `custom_trace!` resolve through mark_sweep paths.
// This works today because mark_sweep2 depends on mark_sweep.
pub use crate::collectors::mark_sweep::trace::{Finalize, Trace, TraceColor};