@@ -8,9 +8,9 @@ use core::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering};
8
8
use alloc:: boxed:: Box ;
9
9
use alloc:: vec:: Vec ;
10
10
11
- use zerogc:: { Trace , GcSafe , GcErase , GcRebrand , GcVisitor , NullTrace , TraceImmutable , GcHandleSystem , GcBindHandle , Gc } ;
12
- use crate :: { WeakCollectorRef , CollectorId , CollectorContext , GarbageCollector , CollectionManager } ;
13
- use crate :: collector:: { RawCollectorImpl , CollectorPtr } ;
11
+ use zerogc:: { Trace , GcSafe , GcErase , GcRebrand , GcVisitor , NullTrace , TraceImmutable , GcHandleSystem , GcBindHandle } ;
12
+ use crate :: { GcRef , WeakCollectorRef , CollectorId , CollectorContext , CollectorRef , CollectionManager } ;
13
+ use crate :: collector:: RawCollectorImpl ;
14
14
15
15
const INITIAL_HANDLE_CAPACITY : usize = 64 ;
16
16
@@ -23,6 +23,13 @@ pub unsafe trait RawHandleImpl: RawCollectorImpl {
23
23
24
24
fn handle_list ( & self ) -> & GcHandleList < Self > ;
25
25
}
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
+ }
26
33
27
34
/// Concurrent list of [GcHandle]s
28
35
///
@@ -414,7 +421,7 @@ impl<T: GcSafe, C: RawHandleImpl> GcHandle<T, C> {
414
421
}
415
422
}
416
423
unsafe impl < T : GcSafe , C : RawHandleImpl > :: zerogc:: GcHandle < T > for GcHandle < T , C > {
417
- type System = GarbageCollector < C > ;
424
+ type System = CollectorRef < C > ;
418
425
type Id = CollectorId < C > ;
419
426
420
427
fn use_critical < R > ( & self , func : impl FnOnce ( & T ) -> R ) -> R {
@@ -428,7 +435,7 @@ unsafe impl<T: GcSafe, C: RawHandleImpl> ::zerogc::GcHandle<T> for GcHandle<T, C
428
435
* This is preferable to using `recursive_read`,
429
436
* since that could starve writers (collectors).
430
437
*/
431
- C :: Manager :: prevent_collection ( & * collector. as_ptr ( ) , || {
438
+ C :: Manager :: prevent_collection ( collector. as_ref ( ) , || {
432
439
let value = self . inner . as_ref ( ) . value
433
440
. load ( Ordering :: Acquire ) as * mut T ;
434
441
func ( & * value)
@@ -437,10 +444,11 @@ unsafe impl<T: GcSafe, C: RawHandleImpl> ::zerogc::GcHandle<T> for GcHandle<T, C
437
444
}
438
445
}
439
446
unsafe impl < ' new_gc , T , C > GcBindHandle < ' new_gc , T > for GcHandle < T , C >
440
- where T : GcSafe , T : GcRebrand < ' new_gc , CollectorId < C > > , C : RawHandleImpl ,
441
- T :: Branded : GcSafe {
447
+ where T : GcSafe , T : GcRebrand < ' new_gc , CollectorId < C > > ,
448
+ T :: Branded : GcSafe , C : RawHandleImplHack < ' new_gc , T :: Branded > {
449
+ type Gc = C :: Gc ;
442
450
#[ inline]
443
- fn bind_to ( & self , context : & ' new_gc CollectorContext < C > ) -> Gc < ' new_gc , T :: Branded , CollectorId < C > > {
451
+ fn bind_to ( & self , context : & ' new_gc CollectorContext < C > ) -> Self :: Gc {
444
452
/*
445
453
* We can safely assume the object will
446
454
* be as valid as long as the context.
@@ -457,16 +465,16 @@ unsafe impl<'new_gc, T, C> GcBindHandle<'new_gc, T> for GcHandle<T, C>
457
465
unsafe {
458
466
let collector = self . collector . assume_valid ( ) ;
459
467
assert_eq ! (
460
- collector. as_ptr ( ) as * const C ,
468
+ collector. as_ref ( ) as * const C ,
461
469
context. collector( ) as * const C ,
462
470
"Collectors mismatch"
463
471
) ;
464
472
let inner = self . inner . as_ref ( ) ;
465
473
let value = inner. value . load ( Ordering :: Acquire )
466
474
as * mut T as * mut T :: Branded ;
467
475
debug_assert ! ( !value. is_null( ) ) ;
468
- Gc :: from_raw (
469
- CollectorId { collector } ,
476
+ Self :: Gc :: from_raw (
477
+ collector,
470
478
NonNull :: new_unchecked ( value)
471
479
)
472
480
}
@@ -494,9 +502,7 @@ impl<T: GcSafe, C: RawHandleImpl> Clone for GcHandle<T, C> {
494
502
fn clone ( & self ) -> Self {
495
503
// NOTE: Dead collector -> invalid handle
496
504
let collector = self . collector
497
- . ensure_valid ( |id| unsafe { WeakCollectorRef {
498
- weak : id. create_weak ( )
499
- } } ) ;
505
+ . ensure_valid ( |id| unsafe { id. weak_ref ( ) } ) ;
500
506
let inner = unsafe { self . inner . as_ref ( ) } ;
501
507
debug_assert ! (
502
508
!inner. value
@@ -507,7 +513,7 @@ impl<T: GcSafe, C: RawHandleImpl> Clone for GcHandle<T, C> {
507
513
let mut old_refcnt = inner. refcnt . load ( Ordering :: Relaxed ) ;
508
514
loop {
509
515
assert_ne ! (
510
- old_refcnt, isize :: MAX as usize ,
516
+ old_refcnt, isize :: max_value ( ) as usize ,
511
517
"Reference count overflow"
512
518
) ;
513
519
/*
@@ -546,7 +552,7 @@ impl<T: GcSafe, C: RawHandleImpl> Drop for GcHandle<T, C> {
546
552
*/
547
553
return ;
548
554
} ,
549
- Some ( ref id) => unsafe { & * id. as_ptr ( ) } ,
555
+ Some ( ref id) => unsafe { id. as_ref ( ) } ,
550
556
} ;
551
557
let inner = unsafe { self . inner . as_ref ( ) } ;
552
558
debug_assert ! ( !inner. value
@@ -598,19 +604,20 @@ unsafe impl<T: GcSafe + Sync, C: RawHandleImpl + Sync> Send for GcHandle<T, C> {
598
604
unsafe impl < T : GcSafe + Sync , C : RawHandleImpl + Sync > Sync for GcHandle < T , C > { }
599
605
600
606
/// We support handles
601
- unsafe impl < ' gc , ' a , T , C > GcHandleSystem < ' gc , ' a , T > for GarbageCollector < C >
602
- where C : RawHandleImpl ,
607
+ unsafe impl < ' gc , ' a , T , C > GcHandleSystem < ' gc , ' a , T > for CollectorRef < C >
608
+ where C : RawHandleImplHack < ' gc , T > ,
603
609
T : GcSafe + ' gc ,
604
610
T : GcErase < ' a , CollectorId < C > > ,
605
611
T :: Erased : GcSafe {
612
+ type Gc = C :: Gc ;
606
613
type Handle = GcHandle < T :: Erased , C > ;
607
614
608
615
#[ inline]
609
- fn create_handle ( gc : Gc < ' gc , T , CollectorId < C > > ) -> Self :: Handle {
616
+ fn create_handle ( gc : Self :: Gc ) -> Self :: Handle {
610
617
unsafe {
611
- let collector = gc. system ( ) ;
618
+ let collector = gc. collector_id ( ) ;
612
619
let value = gc. as_raw_ptr ( ) ;
613
- let raw = collector. as_raw ( ) . handle_list ( )
620
+ let raw = collector. as_ref ( ) . handle_list ( )
614
621
. alloc_raw_handle ( value as * mut ( ) ) ;
615
622
/*
616
623
* WARN: Undefined Behavior
@@ -624,7 +631,7 @@ unsafe impl<'gc, 'a, T, C> GcHandleSystem<'gc, 'a, T> for GarbageCollector<C>
624
631
Ordering :: Release
625
632
) ;
626
633
raw. refcnt . store ( 1 , Ordering :: Release ) ;
627
- let weak_collector = collector. as_ref ( ) . create_weak ( ) ;
634
+ let weak_collector = collector. weak_ref ( ) ;
628
635
GcHandle :: new ( NonNull :: from ( raw) , weak_collector)
629
636
}
630
637
}
0 commit comments