@@ -6,7 +6,7 @@ pub mod ephemeron;
66pub mod gc;
77pub mod gc_box;
88pub mod mutation_ctx;
9- pub mod root_link ;
9+ pub mod root ;
1010pub mod trace;
1111pub mod weak;
1212
@@ -15,8 +15,9 @@ mod tests;
1515
1616pub use cell:: GcRefCell ;
1717pub use ephemeron:: Ephemeron ;
18- pub use gc:: { Gc , Root } ;
18+ pub use gc:: Gc ;
1919pub use mutation_ctx:: MutationContext ;
20+ pub use root:: Root ;
2021pub use trace:: { Finalize , Trace , Tracer } ;
2122pub use weak:: WeakGc ;
2223
@@ -25,7 +26,7 @@ use core::cell::{Cell, RefCell};
2526use core:: marker:: PhantomData ;
2627use core:: ptr:: NonNull ;
2728use gc_box:: { DropFn , GcBox , GcColor } ;
28- use root_link :: RootSentinel ;
29+ use root :: RootSentinel ;
2930use rust_alloc:: vec:: Vec ;
3031
3132/// Type-erased ephemeron registration.
@@ -70,39 +71,18 @@ impl Collector {
7071 }
7172
7273 /// Allocates a RootNode from the dedicated root pool.
73- pub ( crate ) fn alloc_root_node < ' id , T : trace:: Trace > (
74+ pub ( crate ) fn try_alloc_root_node < ' id , T : trace:: Trace > (
7475 & self ,
7576 gc_ptr : PoolPointer < ' static , GcBox < T > > ,
76- ) -> NonNull < gc:: RootNode < ' id , T > > {
77- unsafe fn drop_and_free < T : trace:: Trace > (
78- pool : & mut PoolAllocator < ' static > ,
79- ptr : NonNull < u8 > ,
80- ) {
81- use crate :: alloc:: mempool3:: PoolItem ;
82- unsafe {
83- let typed_ptr = ptr. cast :: < PoolItem < gc:: RootNode < ' _ , T > > > ( ) ;
84- core:: ptr:: drop_in_place ( typed_ptr. as_ptr ( ) ) ;
85- pool. free_slot ( ptr) ;
86- }
87- }
88-
77+ ) -> Result < NonNull < root:: RootNode < ' id , T > > , PoolAllocError > {
8978 let mut pool = self . root_pool . borrow_mut ( ) ;
90- let ptr = pool
91- . try_alloc ( gc:: RootNode {
92- link : root_link:: RootLink :: new ( ) ,
93- gc_ptr,
94- drop_fn : drop_and_free :: < T > ,
95- collector_ptr : self as * const Collector ,
96- _marker : PhantomData ,
97- } )
98- . expect ( "root pool allocation failed" ) ;
99-
79+ let ptr = pool. try_alloc ( root:: RootNode :: new_in ( gc_ptr, self ) ) ?;
10080 // SAFETY: PoolItem<T> is repr(transparent) over T; pointer address is identical.
101- ptr. as_ptr ( ) . cast :: < gc :: RootNode < ' id , T > > ( )
81+ Ok ( ptr. as_ptr ( ) . cast :: < root :: RootNode < ' id , T > > ( ) )
10282 }
10383
10484 /// Frees a RootNode back to the root pool.
105- pub ( crate ) fn free_root_node ( & self , ptr : NonNull < u8 > , drop_fn : gc :: RootDropFn ) {
85+ pub ( crate ) fn free_root_node ( & self , ptr : NonNull < u8 > , drop_fn : root :: RootDropFn ) {
10686 let mut pool = self . root_pool . borrow_mut ( ) ;
10787 unsafe {
10888 ( drop_fn) ( & mut pool, ptr) ;
@@ -172,7 +152,7 @@ impl Collector {
172152 // SAFETY: link_ptr points to the `link` field which is first in repr(C) RootNode.
173153 // Casting to ErasedRootNode (also repr(C), same first two fields) lets us read
174154 // gc_ptr without knowing T, avoiding manual offset arithmetic.
175- let erased = link_ptr. cast :: < gc :: ErasedRootNode > ( ) ;
155+ let erased = link_ptr. cast :: < root :: ErasedRootNode > ( ) ;
176156 tracer. mark_raw ( ( * erased. as_ptr ( ) ) . gc_ptr . as_ptr ( ) . cast :: < u8 > ( ) ) ;
177157 }
178158 }
@@ -244,12 +224,12 @@ impl Drop for Collector {
244224 use crate :: alloc:: mempool3:: PoolItem ;
245225
246226 // Free all root nodes first
247- let all_roots: Vec < ( NonNull < u8 > , gc :: RootDropFn ) > = self
227+ let all_roots: Vec < ( NonNull < u8 > , root :: RootDropFn ) > = self
248228 . root_pool
249229 . borrow ( )
250230 . iter_live_slots ( )
251231 . map ( |ptr| unsafe {
252- let drop_fn = ( * ptr. cast :: < PoolItem < gc :: RootNode < ' _ , ( ) > > > ( ) . as_ptr ( ) )
232+ let drop_fn = ( * ptr. cast :: < PoolItem < root :: RootNode < ' _ , ( ) > > > ( ) . as_ptr ( ) )
253233 . 0
254234 . drop_fn ;
255235 ( ptr, drop_fn)
0 commit comments