11//! Core pointer types.
22
33use crate :: {
4- alloc:: mempool3:: PoolItem ,
4+ alloc:: mempool3:: { PoolAllocator , PoolItem } ,
55 collectors:: mark_sweep_branded:: {
66 gc_box:: GcBox ,
77 mutation_ctx:: MutationContext ,
@@ -13,7 +13,8 @@ use core::fmt;
1313use core:: marker:: PhantomData ;
1414use core:: ops:: Deref ;
1515use core:: ptr:: NonNull ;
16- use rust_alloc:: boxed:: Box ;
16+
17+ pub ( crate ) type RootDropFn = unsafe fn ( & mut PoolAllocator < ' static > , NonNull < u8 > ) ;
1718
1819/// A transient pointer to a GC-managed value.
1920#[ derive( Debug ) ]
@@ -61,6 +62,10 @@ pub(crate) struct RootNode<'id, T: Trace> {
6162 pub ( crate ) link : RootLink ,
6263 /// Pointer to the allocation
6364 pub ( crate ) gc_ptr : NonNull < PoolItem < GcBox < T > > > ,
65+ /// Type-erased drop function for freeing this RootNode
66+ pub ( crate ) drop_fn : RootDropFn ,
67+ /// Raw pointer to the Collector for freeing this node
68+ pub ( crate ) collector_ptr : * const crate :: collectors:: mark_sweep_branded:: Collector ,
6469 pub ( crate ) _marker : PhantomData < * mut & ' id ( ) > ,
6570}
6671
@@ -83,21 +88,21 @@ impl<'id, T: Trace> Root<'id, T> {
8388
8489impl < ' id , T : Trace > Drop for Root < ' id , T > {
8590 fn drop ( & mut self ) {
86- // SAFETY:
87- // * `self.raw` was created by `Box::into_raw`
88- // * The address is stable.
8991 unsafe {
90- let node = Box :: from_raw ( self . raw . as_ptr ( ) ) ;
91- if node . link . is_linked ( ) {
92- RootLink :: unlink ( NonNull :: from ( & node . link ) ) ;
92+ let node_ref = self . raw . as_ref ( ) ;
93+ if node_ref . link . is_linked ( ) {
94+ RootLink :: unlink ( NonNull :: from ( & node_ref . link ) ) ;
9395 }
96+ // SAFETY: collector_ptr is valid for the lifetime of the GcContext
97+ let collector = & * node_ref. collector_ptr ;
98+ collector. free_root_node ( self . raw . cast :: < u8 > ( ) , node_ref. drop_fn ) ;
9499 }
95100 }
96101}
97102
98103impl < T : Trace > Finalize for Gc < ' _ , T > { }
99104impl < T : Trace > Trace for Gc < ' _ , T > {
100- fn trace ( & self , color : & crate :: collectors:: mark_sweep_branded:: trace:: TraceColor ) {
101- color . mark ( self ) ;
105+ fn trace ( & mut self , tracer : & mut crate :: collectors:: mark_sweep_branded:: trace:: Tracer ) {
106+ tracer . mark ( self ) ;
102107 }
103108}
0 commit comments