@@ -183,6 +183,18 @@ rb_mmtk_block_for_gc(MMTk_VMMutatorThread mutator)
183183 RB_GC_VM_UNLOCK (lock_lev );
184184}
185185
186+ static void
187+ rb_mmtk_before_updating_jit_code (void )
188+ {
189+ rb_gc_before_updating_jit_code ();
190+ }
191+
192+ static void
193+ rb_mmtk_after_updating_jit_code (void )
194+ {
195+ rb_gc_after_updating_jit_code ();
196+ }
197+
186198static size_t
187199rb_mmtk_number_of_mutators (void )
188200{
@@ -247,9 +259,19 @@ rb_mmtk_scan_objspace(void)
247259}
248260
249261static void
250- rb_mmtk_scan_object_ruby_style (MMTk_ObjectReference object )
262+ rb_mmtk_move_obj_during_marking (MMTk_ObjectReference from , MMTk_ObjectReference to )
251263{
252- rb_gc_mark_children (rb_gc_get_objspace (), (VALUE )object );
264+ rb_gc_move_obj_during_marking ((VALUE )from , (VALUE )to );
265+ }
266+
267+ static void
268+ rb_mmtk_update_object_references (MMTk_ObjectReference mmtk_object )
269+ {
270+ VALUE object = (VALUE )mmtk_object ;
271+
272+ if (!RB_FL_TEST (object , RUBY_FL_WEAK_REFERENCE )) {
273+ rb_gc_update_object_references (rb_gc_get_objspace (), object );
274+ }
253275}
254276
255277static void
@@ -259,9 +281,15 @@ rb_mmtk_call_gc_mark_children(MMTk_ObjectReference object)
259281}
260282
261283static void
262- rb_mmtk_handle_weak_references (MMTk_ObjectReference object )
284+ rb_mmtk_handle_weak_references (MMTk_ObjectReference mmtk_object , bool moving )
263285{
264- rb_gc_handle_weak_references ((VALUE )object );
286+ VALUE object = (VALUE )mmtk_object ;
287+
288+ rb_gc_handle_weak_references (object );
289+
290+ if (moving ) {
291+ rb_gc_update_object_references (rb_gc_get_objspace (), object );
292+ }
265293}
266294
267295static void
@@ -332,27 +360,50 @@ rb_mmtk_update_finalizer_table(void)
332360}
333361
334362static int
335- rb_mmtk_update_table_i (VALUE val , void * data )
363+ rb_mmtk_global_tables_count (void )
364+ {
365+ return RB_GC_VM_WEAK_TABLE_COUNT ;
366+ }
367+
368+ static inline VALUE rb_mmtk_call_object_closure (VALUE obj , bool pin );
369+
370+ static int
371+ rb_mmtk_update_global_tables_i (VALUE val , void * data )
336372{
337373 if (!mmtk_is_reachable ((MMTk_ObjectReference )val )) {
338374 return ST_DELETE ;
339375 }
340376
377+ // TODO: check only if in moving GC
378+ if (rb_mmtk_call_object_closure (val , false) != val ) {
379+ return ST_REPLACE ;
380+ }
381+
341382 return ST_CONTINUE ;
342383}
343384
344385static int
345- rb_mmtk_global_tables_count ( void )
386+ rb_mmtk_update_global_tables_replace_i ( VALUE * ptr , void * data )
346387{
347- return RB_GC_VM_WEAK_TABLE_COUNT ;
388+ // TODO: cache the new location so we don't call rb_mmtk_call_object_closure twice
389+ * ptr = rb_mmtk_call_object_closure (* ptr , false);
390+
391+ return ST_CONTINUE ;
348392}
349393
350394static void
351395rb_mmtk_update_global_tables (int table )
352396{
353397 RUBY_ASSERT (table < RB_GC_VM_WEAK_TABLE_COUNT );
354398
355- rb_gc_vm_weak_table_foreach (rb_mmtk_update_table_i , NULL , NULL , true, (enum rb_gc_vm_weak_tables )table );
399+ // TODO: set weak_only to true for non-moving GC
400+ rb_gc_vm_weak_table_foreach (
401+ rb_mmtk_update_global_tables_i ,
402+ rb_mmtk_update_global_tables_replace_i ,
403+ NULL ,
404+ false,
405+ (enum rb_gc_vm_weak_tables )table
406+ );
356407}
357408
358409static bool
@@ -376,11 +427,14 @@ MMTk_RubyUpcalls ruby_upcalls = {
376427 rb_mmtk_stop_the_world ,
377428 rb_mmtk_resume_mutators ,
378429 rb_mmtk_block_for_gc ,
430+ rb_mmtk_before_updating_jit_code ,
431+ rb_mmtk_after_updating_jit_code ,
379432 rb_mmtk_number_of_mutators ,
380433 rb_mmtk_get_mutators ,
381434 rb_mmtk_scan_gc_roots ,
382435 rb_mmtk_scan_objspace ,
383- rb_mmtk_scan_object_ruby_style ,
436+ rb_mmtk_move_obj_during_marking ,
437+ rb_mmtk_update_object_references ,
384438 rb_mmtk_call_gc_mark_children ,
385439 rb_mmtk_handle_weak_references ,
386440 rb_mmtk_call_obj_free ,
@@ -738,33 +792,42 @@ rb_gc_impl_free(void *objspace_ptr, void *ptr, size_t old_size)
738792void rb_gc_impl_adjust_memory_usage (void * objspace_ptr , ssize_t diff ) { }
739793
740794// Marking
795+ static inline VALUE
796+ rb_mmtk_call_object_closure (VALUE obj , bool pin )
797+ {
798+ return (VALUE )rb_mmtk_gc_thread_tls -> object_closure .c_function (
799+ rb_mmtk_gc_thread_tls -> object_closure .rust_closure ,
800+ rb_mmtk_gc_thread_tls -> gc_context ,
801+ (MMTk_ObjectReference )obj ,
802+ pin
803+ );
804+ }
805+
741806void
742807rb_gc_impl_mark (void * objspace_ptr , VALUE obj )
743808{
744809 if (RB_SPECIAL_CONST_P (obj )) return ;
745810
746- rb_mmtk_gc_thread_tls -> object_closure .c_function (rb_mmtk_gc_thread_tls -> object_closure .rust_closure ,
747- rb_mmtk_gc_thread_tls -> gc_context ,
748- (MMTk_ObjectReference )obj ,
749- false);
811+ rb_mmtk_call_object_closure (obj , false);
750812}
751813
752814void
753815rb_gc_impl_mark_and_move (void * objspace_ptr , VALUE * ptr )
754816{
755817 if (RB_SPECIAL_CONST_P (* ptr )) return ;
756818
757- // TODO: make it movable
758- rb_gc_impl_mark (objspace_ptr , * ptr );
819+ VALUE new_obj = rb_mmtk_call_object_closure (* ptr , false);
820+ if (new_obj != * ptr ) {
821+ * ptr = new_obj ;
822+ }
759823}
760824
761825void
762826rb_gc_impl_mark_and_pin (void * objspace_ptr , VALUE obj )
763827{
764828 if (RB_SPECIAL_CONST_P (obj )) return ;
765829
766- // TODO: also pin
767- rb_gc_impl_mark (objspace_ptr , obj );
830+ rb_mmtk_call_object_closure (obj , true);
768831}
769832
770833void
@@ -775,11 +838,10 @@ rb_gc_impl_mark_maybe(void *objspace_ptr, VALUE obj)
775838 }
776839}
777840
778- // Weak references
779-
780841void
781842rb_gc_impl_declare_weak_references (void * objspace_ptr , VALUE obj )
782843{
844+ RB_FL_SET (obj , RUBY_FL_WEAK_REFERENCE );
783845 mmtk_declare_weak_references ((MMTk_ObjectReference )obj );
784846}
785847
@@ -790,16 +852,22 @@ rb_gc_impl_handle_weak_references_alive_p(void *objspace_ptr, VALUE obj)
790852}
791853
792854// Compaction
855+ void
856+ rb_gc_impl_register_pinning_obj (void * objspace_ptr , VALUE obj )
857+ {
858+ mmtk_register_pinning_obj ((MMTk_ObjectReference )obj );
859+ }
860+
793861bool
794862rb_gc_impl_object_moved_p (void * objspace_ptr , VALUE obj )
795863{
796- rb_bug ( "unimplemented" ) ;
864+ return rb_mmtk_call_object_closure ( obj , false) != obj ;
797865}
798866
799867VALUE
800- rb_gc_impl_location (void * objspace_ptr , VALUE value )
868+ rb_gc_impl_location (void * objspace_ptr , VALUE obj )
801869{
802- rb_bug ( "unimplemented" );
870+ return rb_mmtk_call_object_closure ( obj , false );
803871}
804872
805873// Write barriers
0 commit comments