Skip to content

Commit ea13401

Browse files
committed
Revert "Make GcRef a trait once again"
This reverts commit d2ab662. We all make mistakes, don't we? ;)
1 parent a71eefe commit ea13401

File tree

11 files changed

+237
-317
lines changed

11 files changed

+237
-317
lines changed

libs/context/src/collector.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use alloc::sync::Arc;
88

99
use slog::{Logger, o};
1010

11-
use zerogc::{GcRef, GcSafe, GcSystem, Trace, GcSimpleAlloc, NullTrace, TraceImmutable, GcVisitor};
11+
use zerogc::{Gc, GcSafe, GcSystem, Trace, GcSimpleAlloc, NullTrace, TraceImmutable, GcVisitor};
1212

1313
use crate::{CollectorContext};
1414
use crate::state::{CollectionManager, RawContext};
@@ -54,7 +54,11 @@ pub unsafe trait RawCollectorImpl: 'static + Sized {
5454
fn id(&self) -> CollectorId<Self> {
5555
CollectorId { ptr: unsafe { Self::Ptr::from_raw(self as *const _ as *mut _) } }
5656
}
57-
57+
unsafe fn gc_write_barrier<'gc, T, V>(
58+
owner: &Gc<'gc, T, CollectorId<Self>>,
59+
value: &Gc<'gc, V, CollectorId<Self>>,
60+
field_offset: usize
61+
) where T: GcSafe + ?Sized + 'gc, V: GcSafe + ?Sized + 'gc;
5862
/// The logger associated with this collector
5963
fn logger(&self) -> &Logger;
6064

@@ -272,6 +276,15 @@ impl<C: RawCollectorImpl> CollectorId<C> {
272276
unsafe impl<C: RawCollectorImpl> ::zerogc::CollectorId for CollectorId<C> {
273277
type System = CollectorRef<C>;
274278

279+
#[inline(always)]
280+
unsafe fn gc_write_barrier<'gc, T, V>(
281+
owner: &Gc<'gc, T, Self>,
282+
value: &Gc<'gc, V, Self>,
283+
field_offset: usize
284+
) where T: GcSafe + ?Sized + 'gc, V: GcSafe + ?Sized + 'gc {
285+
C::gc_write_barrier(owner, value, field_offset)
286+
}
287+
275288
#[inline]
276289
unsafe fn assume_valid_system(&self) -> &Self::System {
277290
// TODO: Make the API nicer? (avoid borrowing and indirection)
@@ -317,21 +330,13 @@ impl<C: RawCollectorImpl> WeakCollectorRef<C> {
317330
}
318331
}
319332

320-
pub unsafe trait RawSimpleAlloc<'gc, T>: RawCollectorImpl
321-
where T: GcSafe + 'gc {
322-
/// The type of GC references that are returned
323-
type Gc: GcRef<'gc, T, Id=CollectorId<Self>>;
324-
/// Allocate a new garbage collected reference
325-
fn alloc(
326-
context: &'gc CollectorContext<Self>,
327-
value: T
328-
) -> Self::Gc;
333+
pub unsafe trait RawSimpleAlloc: RawCollectorImpl {
334+
fn alloc<'gc, T: GcSafe + 'gc>(context: &'gc CollectorContext<Self>, value: T) -> Gc<'gc, T, CollectorId<Self>>;
329335
}
330336
unsafe impl<'gc, T, C> GcSimpleAlloc<'gc, T> for CollectorContext<C>
331-
where T: GcSafe + 'gc, C: RawSimpleAlloc<'gc, T> {
332-
type Gc = C::Gc;
337+
where T: GcSafe + 'gc, C: RawSimpleAlloc {
333338
#[inline]
334-
fn alloc(&'gc self, value: T) -> C::Gc {
339+
fn alloc(&'gc self, value: T) -> Gc<'gc, T, Self::Id> {
335340
C::alloc(self, value)
336341
}
337342
}

libs/context/src/handle.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use alloc::boxed::Box;
99
use alloc::vec::Vec;
1010

1111
use zerogc::{Trace, GcSafe, GcErase, GcRebrand, GcVisitor, NullTrace, TraceImmutable, GcHandleSystem, GcBindHandle};
12-
use crate::{GcRef, WeakCollectorRef, CollectorId, CollectorContext, CollectorRef, CollectionManager};
12+
use crate::{Gc, WeakCollectorRef, CollectorId, CollectorContext, CollectorRef, CollectionManager};
1313
use crate::collector::RawCollectorImpl;
1414

1515
const INITIAL_HANDLE_CAPACITY: usize = 64;
@@ -23,13 +23,6 @@ pub unsafe trait RawHandleImpl: RawCollectorImpl {
2323

2424
fn handle_list(&self) -> &GcHandleList<Self>;
2525
}
26-
/// A type hack on [RawHandleImpl] to get the associated type
27-
/// of [::zerogc::GcRef]
28-
///
29-
/// This is needed because we don't have generic associated types (yet)
30-
pub trait RawHandleImplHack<'gc, T: GcSafe + 'gc>: RawHandleImpl {
31-
type Gc: GcRef<'gc, T, Id=CollectorId<Self>>;
32-
}
3326

3427
/// Concurrent list of [GcHandle]s
3528
///
@@ -445,10 +438,9 @@ unsafe impl<T: GcSafe, C: RawHandleImpl> ::zerogc::GcHandle<T> for GcHandle<T, C
445438
}
446439
unsafe impl<'new_gc, T, C> GcBindHandle<'new_gc, T> for GcHandle<T, C>
447440
where T: GcSafe, T: GcRebrand<'new_gc, CollectorId<C>>,
448-
T::Branded: GcSafe, C: RawHandleImplHack<'new_gc, T::Branded> {
449-
type Gc = C::Gc;
441+
T::Branded: GcSafe, C: RawHandleImpl {
450442
#[inline]
451-
fn bind_to(&self, context: &'new_gc CollectorContext<C>) -> Self::Gc {
443+
fn bind_to(&self, context: &'new_gc CollectorContext<C>) -> Gc<'new_gc, T::Branded, CollectorId<C>> {
452444
/*
453445
* We can safely assume the object will
454446
* be as valid as long as the context.
@@ -473,7 +465,7 @@ unsafe impl<'new_gc, T, C> GcBindHandle<'new_gc, T> for GcHandle<T, C>
473465
let value = inner.value.load(Ordering::Acquire)
474466
as *mut T as *mut T::Branded;
475467
debug_assert!(!value.is_null());
476-
Self::Gc::from_raw(
468+
Gc::from_raw(
477469
collector,
478470
NonNull::new_unchecked(value)
479471
)
@@ -605,15 +597,14 @@ unsafe impl<T: GcSafe + Sync, C: RawHandleImpl + Sync> Sync for GcHandle<T, C> {
605597

606598
/// We support handles
607599
unsafe impl<'gc, 'a, T, C> GcHandleSystem<'gc, 'a, T> for CollectorRef<C>
608-
where C: RawHandleImplHack<'gc, T>,
600+
where C: RawHandleImpl,
609601
T: GcSafe + 'gc,
610602
T: GcErase<'a, CollectorId<C>>,
611603
T::Erased: GcSafe {
612-
type Gc = C::Gc;
613604
type Handle = GcHandle<T::Erased, C>;
614605

615606
#[inline]
616-
fn create_handle(gc: Self::Gc) -> Self::Handle {
607+
fn create_handle(gc: Gc<'gc, T, CollectorId<C>>) -> Self::Handle {
617608
unsafe {
618609
let collector = gc.collector_id();
619610
let value = gc.as_raw_ptr();

libs/context/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use alloc::vec::Vec;
2828
use zerogc::prelude::*;
2929

3030
pub mod state;
31+
3132
pub mod utils;
3233
pub mod collector;
3334
pub mod handle;

libs/derive/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,9 +601,9 @@ fn impl_extras(target: &DeriveInput, info: &GcTypeInfo) -> Result<TokenStream, E
601601
let barrier = quote_spanned!(field.span() => #zerogc_crate::GcDirectBarrier::write_barrier(&value, &self, offset));
602602
extra_items.push(quote! {
603603
#[inline] // TODO: Implement `GcDirectBarrier` ourselves
604-
#mutator_vis fn #mutator_name<G>(self: G, value: #value_ref_type)
605-
where G: #zerogc_crate::GcRef<#gc_lifetime, Self>,
606-
#value_ref_type: #zerogc_crate::GcDirectBarrier<#gc_lifetime, G> {
604+
#mutator_vis fn #mutator_name<Id>(self: #zerogc_crate::Gc<#gc_lifetime, Self, Id>, value: #value_ref_type)
605+
where Id: #zerogc_crate::CollectorId,
606+
#value_ref_type: #zerogc_crate::GcDirectBarrier<#gc_lifetime, #zerogc_crate::Gc<#gc_lifetime, Self, Id>> {
607607
unsafe {
608608
let target_ptr = #field_as_ptr;
609609
let offset = target_ptr as usize - self.as_raw_ptr() as usize;

libs/simple/examples/binary_trees.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fn main() {
6969

7070
let long_lived_tree = bottom_up_tree(&gc, max_depth);
7171

72-
let (long_lived_tree, ()) = safepoint_recurse!(gc, long_lived_tree, |gc, _long_lived_tree| {
72+
let (long_lived_tree, ()) = safepoint_recurse!(gc, long_lived_tree, |gc, long_lived_tree| {
7373
(min_depth / 2..max_depth / 2 + 1).into_iter().for_each(|half_depth| {
7474
let depth = half_depth * 2;
7575
let iterations = 1 << ((max_depth - depth + min_depth) as u32);

libs/simple/src/gc.rs

Lines changed: 0 additions & 145 deletions
This file was deleted.

libs/simple/src/lib.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ use std::any::TypeId;
3131

3232
use slog::{Logger, FnValue, debug};
3333

34-
use zerogc::{GcSafe, Trace, GcVisitor, GcRef};
34+
use zerogc::{GcSafe, Trace, GcVisitor};
3535

3636
use zerogc_context::utils::{ThreadId, AtomicCell, MemorySize};
3737

3838
use crate::alloc::{SmallArenaList, small_object_size};
3939

4040
use zerogc_context::collector::{RawSimpleAlloc};
41-
use zerogc_context::handle::{GcHandleList, RawHandleImpl, RawHandleImplHack};
41+
use zerogc_context::handle::{GcHandleList, RawHandleImpl};
4242
use zerogc_context::{
4343
CollectionManager as AbstractCollectionManager,
4444
RawContext as AbstractRawContext
@@ -67,9 +67,6 @@ mod alloc {
6767
pub fn find<T>(&self) -> Option<FakeArena> { None }
6868
}
6969
}
70-
mod gc;
71-
72-
pub use self::gc::Gc;
7370

7471
#[cfg(feature = "sync")]
7572
type RawContext<C> = zerogc_context::state::sync::RawContext<C>;
@@ -84,14 +81,14 @@ type CollectionManager<C> = zerogc_context::state::nosync::CollectionManager<C>;
8481
pub type SimpleCollector = ::zerogc_context::CollectorRef<RawSimpleCollector>;
8582
pub type SimpleCollectorContext = ::zerogc_context::CollectorContext<RawSimpleCollector>;
8683
pub type CollectorId = ::zerogc_context::CollectorId<RawSimpleCollector>;
84+
pub type Gc<'gc, T> = ::zerogc::Gc<'gc, T, CollectorId>;
8785

8886
#[cfg(not(feature = "multiple-collectors"))]
8987
static GLOBAL_COLLECTOR: AtomicPtr<RawSimpleCollector> = AtomicPtr::new(std::ptr::null_mut());
9088

91-
unsafe impl<'gc, T: GcSafe + 'gc> RawSimpleAlloc<'gc, T> for RawSimpleCollector {
92-
type Gc = Gc<'gc, T>;
89+
unsafe impl RawSimpleAlloc for RawSimpleCollector {
9390
#[inline]
94-
fn alloc(context: &'gc SimpleCollectorContext, value: T) -> Gc<'gc, T> where T: GcSafe + 'gc {
91+
fn alloc<'gc, T>(context: &'gc SimpleCollectorContext, value: T) -> Gc<'gc, T> where T: GcSafe + 'gc {
9592
context.collector().heap.allocator.alloc(value)
9693
}
9794
}
@@ -119,9 +116,6 @@ unsafe impl RawHandleImpl for RawSimpleCollector {
119116
&self.handle_list
120117
}
121118
}
122-
impl<'gc, T: GcSafe + 'gc> RawHandleImplHack<'gc, T> for RawSimpleCollector {
123-
type Gc = Gc<'gc, T>;
124-
}
125119

126120
/// A wrapper for [GcHandleList] that implements [DynTrace]
127121
#[repr(transparent)]
@@ -473,6 +467,14 @@ unsafe impl ::zerogc_context::collector::RawCollectorImpl for RawSimpleCollector
473467
raw_ptr
474468
}
475469

470+
#[inline(always)]
471+
unsafe fn gc_write_barrier<'gc, T, V>(
472+
_owner: &Gc<'gc, T>,
473+
_value: &Gc<'gc, V>,
474+
_field_offset: usize
475+
) where T: GcSafe + ?Sized + 'gc, V: GcSafe + ?Sized + 'gc {
476+
// Simple GC doesn't need write barriers
477+
}
476478
#[inline]
477479
fn logger(&self) -> &Logger {
478480
&self.logger
@@ -674,20 +676,20 @@ unsafe impl GcVisitor for MarkVisitor<'_> {
674676
type Err = !;
675677

676678
#[inline]
677-
unsafe fn visit_gc<'gc, T, G>(
678-
&mut self, gc: &mut G
679+
unsafe fn visit_gc<'gc, T, Id>(
680+
&mut self, gc: &mut ::zerogc::Gc<'gc, T, Id>
679681
) -> Result<(), Self::Err>
680-
where T: GcSafe + 'gc, G: GcRef<'gc, T> {
681-
if TypeId::of::<G::Id>() == TypeId::of::<crate::CollectorId>() {
682+
where T: GcSafe + 'gc, Id: ::zerogc::CollectorId {
683+
if TypeId::of::<Id>() == TypeId::of::<crate::CollectorId>() {
682684
/*
683685
* Since the `TypeId`s match, we know the generic `Id`
684686
* matches our own `crate::CollectorId`.
685687
* Therefore its safe to specific the generic `Id` into the
686688
* `Gc` into its more specific type.
687689
*/
688690
let gc = std::mem::transmute::<
689-
&mut G,
690-
&mut crate::gc::Gc<'gc, T>
691+
&mut ::zerogc::Gc<'gc, T, Id>,
692+
&mut ::zerogc::Gc<'gc, T, crate::CollectorId>
691693
>(gc);
692694
/*
693695
* Check the collectors match. Otherwise we're mutating

0 commit comments

Comments
 (0)