From 47a796f93e71d780c41ee1c7ae937dbe67cf444c Mon Sep 17 00:00:00 2001 From: Matt Valentine-House Date: Tue, 8 Oct 2024 12:16:14 +0100 Subject: [PATCH] Expose GC.active_gc_name for use in tests And internal GC API function rb_gc_active_gc_name --- gc.c | 18 +++++++++++++++++- gc.rb | 8 ++++++++ gc/default.c | 6 ++++++ gc/mmtk.c | 8 ++++++++ test/ruby/test_gc.rb | 8 ++++++++ 5 files changed, 47 insertions(+), 1 deletion(-) diff --git a/gc.c b/gc.c index 63442ddec0d01a..0dc62072de1b8a 100644 --- a/gc.c +++ b/gc.c @@ -735,6 +735,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; @@ -870,6 +872,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 @@ -952,6 +956,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; @@ -2839,6 +2845,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) @@ -3707,7 +3719,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 5d7c0d9db56cbe..a087ea1dd7fbf1 100644 --- a/gc/default.c +++ b/gc/default.c @@ -6163,6 +6163,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/gc/mmtk.c b/gc/mmtk.c index 65eef8c52fca28..ff458b07a03c88 100644 --- a/gc/mmtk.c +++ b/gc/mmtk.c @@ -1205,3 +1205,11 @@ rb_gc_impl_garbage_object_p(void *objspace_ptr, VALUE obj) void rb_gc_impl_set_event_hook(void *objspace_ptr, const rb_event_flag_t event) { } void rb_gc_impl_copy_attributes(void *objspace_ptr, VALUE dest, VALUE obj) { } + +// GC Identification + +const char * +rb_gc_impl_active_gc_name(void) +{ + return "mmtk"; +} diff --git a/test/ruby/test_gc.rb b/test/ruby/test_gc.rb index 31ad71a1a18a1a..8c754b09937b2b 100644 --- a/test/ruby/test_gc.rb +++ b/test/ruby/test_gc.rb @@ -872,4 +872,12 @@ def test_old_to_young_reference assert_include ObjectSpace.dump(young_obj), '"old":true' end end + + def test_active_gc_name + if /mmtk/.match ENV['RUBY_GC_LIBRARY'] + assert_equal "mmtk", GC.active_gc_name + else + assert_equal "default", GC.active_gc_name + end + end end