diff --git a/gc.c b/gc.c index 9760f343f8ed10..3dd5f4279087dc 100644 --- a/gc.c +++ b/gc.c @@ -642,6 +642,8 @@ typedef struct gc_function_map { bool (*garbage_object_p)(void *objspace_ptr, VALUE obj); void (*set_event_hook)(void *objspace_ptr, const rb_event_flag_t event); void (*copy_attributes)(void *objspace_ptr, VALUE dest, VALUE obj); + // GC Identification + const char *(*active_gc_name)(void); } rb_gc_function_map_t; static rb_gc_function_map_t rb_gc_functions; @@ -774,6 +776,8 @@ ruby_external_gc_init(void) load_external_gc_func(garbage_object_p); load_external_gc_func(set_event_hook); load_external_gc_func(copy_attributes); + //GC Identification + load_external_gc_func(active_gc_name); # undef load_external_gc_func @@ -853,6 +857,8 @@ ruby_external_gc_init(void) # define rb_gc_impl_garbage_object_p rb_gc_functions.garbage_object_p # define rb_gc_impl_set_event_hook rb_gc_functions.set_event_hook # define rb_gc_impl_copy_attributes rb_gc_functions.copy_attributes +// GC Identification +# define rb_gc_impl_active_gc_name rb_gc_functions.active_gc_name #endif static VALUE initial_stress = Qfalse; @@ -2749,6 +2755,12 @@ rb_gc_copy_attributes(VALUE dest, VALUE obj) rb_gc_impl_copy_attributes(rb_gc_get_objspace(), dest, obj); } +const char * +rb_gc_active_gc_name(void) +{ + return rb_gc_impl_active_gc_name(); +} + // TODO: rearchitect this function to work for a generic GC size_t rb_obj_gc_flags(VALUE obj, ID* flags, size_t max) @@ -3512,7 +3524,11 @@ gc_disable(rb_execution_context_t *ec, VALUE _) return rb_gc_disable(); } - +static VALUE +active_gc_name(rb_execution_context_t *ec, VALUE _) { + const char *name = rb_gc_active_gc_name(); + return rb_fstring_new(name, strlen(name)); +} // TODO: think about moving ruby_gc_set_params into Init_heap or Init_gc void diff --git a/gc.rb b/gc.rb index 2a75e166ce98f9..002a02496e51f9 100644 --- a/gc.rb +++ b/gc.rb @@ -368,6 +368,14 @@ def self.total_time ULL2NUM(rb_gc_impl_get_total_time(rb_gc_get_objspace())) } end + + # call-seq: + # GC.active_gc_name -> string + # + # Return the configured name for the active GC module + def self.active_gc_name + Primitive.active_gc_name + end end module ObjectSpace diff --git a/gc/default.c b/gc/default.c index 237f822fb54742..639132a6ec36a2 100644 --- a/gc/default.c +++ b/gc/default.c @@ -6162,6 +6162,12 @@ rb_gc_impl_copy_attributes(void *objspace_ptr, VALUE dest, VALUE obj) rb_gc_copy_finalizer(dest, obj); } +const char * +rb_gc_impl_active_gc_name(void) +{ + return "default"; +} + void rb_gc_impl_writebarrier_remember(void *objspace_ptr, VALUE obj) { diff --git a/test/ruby/test_gc.rb b/test/ruby/test_gc.rb index 31ad71a1a18a1a..0d1501aa2128e4 100644 --- a/test/ruby/test_gc.rb +++ b/test/ruby/test_gc.rb @@ -872,4 +872,8 @@ def test_old_to_young_reference assert_include ObjectSpace.dump(young_obj), '"old":true' end end + + def test_active_gc_name + assert_equal "default", GC.active_gc_name + end end