1616#include <sys/sysctl.h>
1717#endif
1818
19+ #ifndef VM_CHECK_MODE
20+ # define VM_CHECK_MODE RUBY_DEBUG
21+ #endif
22+
23+ // From ractor_core.h
24+ #ifndef RACTOR_CHECK_MODE
25+ # define RACTOR_CHECK_MODE (VM_CHECK_MODE || RUBY_DEBUG) && (SIZEOF_UINT64_T == SIZEOF_VALUE)
26+ #endif
27+
28+ #if RACTOR_CHECK_MODE
29+ # define RVALUE_SUFFIX_SIZE sizeof(VALUE)
30+ void rb_ractor_setup_belonging (VALUE obj );
31+ #else
32+ # define RVALUE_SUFFIX_SIZE 0
33+ #endif
34+
1935struct objspace {
2036 bool measure_gc_time ;
2137 bool gc_stress ;
@@ -557,7 +573,11 @@ void *
557573rb_gc_impl_objspace_alloc (void )
558574{
559575 MMTk_Builder * builder = rb_mmtk_builder_init ();
560- mmtk_init_binding (builder , NULL , & ruby_upcalls );
576+ MMTk_RubyBindingOptions binding_options = {
577+ .ractor_check_mode = RACTOR_CHECK_MODE != 0 ,
578+ .suffix_size = RVALUE_SUFFIX_SIZE ,
579+ };
580+ mmtk_init_binding (builder , & binding_options , & ruby_upcalls );
561581
562582 return calloc (1 , sizeof (struct objspace ));
563583}
@@ -885,15 +905,16 @@ rb_gc_impl_new_obj(void *objspace_ptr, void *cache_ptr, VALUE klass, VALUE flags
885905 mmtk_handle_user_collection_request (ractor_cache , false, false);
886906 }
887907
888- alloc_size += sizeof (VALUE );
908+ // Layout: [hidden size header (sizeof(VALUE))][payload (alloc_size)][suffix (RVALUE_SUFFIX_SIZE)]
909+ alloc_size += sizeof (VALUE ) + RVALUE_SUFFIX_SIZE ;
889910
890911 VALUE * alloc_obj = (VALUE * )rb_mmtk_alloc_fast_path (objspace , ractor_cache , alloc_size );
891912 if (!alloc_obj ) {
892913 alloc_obj = mmtk_alloc (ractor_cache -> mutator , alloc_size , MMTk_MIN_OBJ_ALIGN , 0 , MMTK_ALLOCATION_SEMANTICS_DEFAULT );
893914 }
894915
895916 alloc_obj ++ ;
896- alloc_obj [-1 ] = alloc_size - sizeof (VALUE );
917+ alloc_obj [-1 ] = alloc_size - sizeof (VALUE ) - RVALUE_SUFFIX_SIZE ;
897918 alloc_obj [0 ] = flags ;
898919 alloc_obj [1 ] = klass ;
899920
@@ -905,6 +926,10 @@ rb_gc_impl_new_obj(void *objspace_ptr, void *cache_ptr, VALUE klass, VALUE flags
905926
906927 objspace -> total_allocated_objects ++ ;
907928
929+ #if RACTOR_CHECK_MODE
930+ rb_ractor_setup_belonging ((VALUE )alloc_obj );
931+ #endif
932+
908933 return (VALUE )alloc_obj ;
909934}
910935
0 commit comments