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