@@ -75,13 +75,19 @@ unsafe fn free(ptr: ObjPtr) {
75
75
dealloc ( ptr. cast ( ) . as_ptr ( ) , Layout :: for_value ( ptr. as_ref ( ) ) ) ;
76
76
}
77
77
impl CcSync {
78
- /// _suggest_(may or may not) collector to collect garbage.
78
+ /// _suggest_(may or may not) collector to collect garbage. return number of cyclic garbage being collected
79
79
#[ inline]
80
- pub fn gc ( & self ) {
80
+ pub fn gc ( & self ) -> usize {
81
81
if self . should_gc ( ) {
82
- self . collect_cycles ( ) ;
82
+ self . force_gc ( )
83
+ } else {
84
+ 0
83
85
}
84
86
}
87
+ #[ inline]
88
+ pub fn force_gc ( & self ) ->usize {
89
+ self . collect_cycles ( )
90
+ }
85
91
fn roots_len ( & self ) -> usize {
86
92
self . roots . lock ( ) . unwrap ( ) . len ( )
87
93
}
@@ -153,9 +159,9 @@ impl CcSync {
153
159
}
154
160
}
155
161
156
- fn collect_cycles ( & self ) {
162
+ fn collect_cycles ( & self ) -> usize {
157
163
if IS_GC_THREAD . with ( |v| v. get ( ) ) {
158
- return ;
164
+ return 0 ;
159
165
// already call collect_cycle() once
160
166
}
161
167
// order of acquire lock and check IS_GC_THREAD here is important
@@ -169,7 +175,7 @@ impl CcSync {
169
175
// to not stop the world
170
176
// what's left for collection should already be in garbage cycle,
171
177
// no mutator will operate on them
172
- self . collect_roots ( lock) ;
178
+ self . collect_roots ( lock)
173
179
}
174
180
175
181
fn mark_roots ( & self ) {
@@ -212,7 +218,7 @@ impl CcSync {
212
218
} )
213
219
. count ( ) ;
214
220
}
215
- fn collect_roots ( & self , lock : MutexGuard < ( ) > ) {
221
+ fn collect_roots ( & self , lock : MutexGuard < ( ) > ) -> usize {
216
222
// Collecting the nodes into this Vec is difference from the original
217
223
// Bacon-Rajan paper. We need this because we have destructors(RAII) and
218
224
// running them during traversal will cause cycles to be broken which
@@ -231,6 +237,7 @@ impl CcSync {
231
237
self . collect_white ( obj, & mut white) ;
232
238
} )
233
239
. count ( ) ;
240
+ let len_white = white. len ( ) ;
234
241
if !white. is_empty ( ) {
235
242
warn ! ( "Collect cyclic garbage in white.len()={}" , white. len( ) ) ;
236
243
}
@@ -259,6 +266,7 @@ impl CcSync {
259
266
for i in & white {
260
267
unsafe { free ( * i) }
261
268
}
269
+ len_white
262
270
}
263
271
fn collect_white ( & self , obj : ObjRef , white : & mut Vec < NonNull < dyn GcObjPtr > > ) {
264
272
if obj. header ( ) . color ( ) == Color :: White && !obj. header ( ) . buffered ( ) {
0 commit comments