Skip to content

Commit

Permalink
Expose GC.active_gc_name for use in tests
Browse files Browse the repository at this point in the history
And internal GC API function rb_gc_active_gc_name
  • Loading branch information
eightbitraptor committed Oct 8, 2024
1 parent d34a5c1 commit 47a796f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
18 changes: 17 additions & 1 deletion gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions gc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions gc/default.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
8 changes: 8 additions & 0 deletions gc/mmtk.c
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
8 changes: 8 additions & 0 deletions test/ruby/test_gc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 47a796f

Please sign in to comment.