Fix GC.add_stress_to_class and GC.remove_stress_to_class

These methods were accidentally removed in [Feature #20470]. This commit
adds them back.
This commit is contained in:
Peter Zhu 2025-01-29 10:07:32 -05:00
parent 1b731c1f43
commit 5e644e80e9
Notes: git 2025-01-29 18:22:21 +00:00

View File

@ -9315,6 +9315,52 @@ gc_malloc_allocations(VALUE self)
void rb_gc_impl_before_fork(void *objspace_ptr) { /* no-op */ }
void rb_gc_impl_after_fork(void *objspace_ptr, rb_pid_t pid) { /* no-op */ }
/*
* call-seq:
* GC.add_stress_to_class(class[, ...])
*
* Raises NoMemoryError when allocating an instance of the given classes.
*
*/
static VALUE
rb_gcdebug_add_stress_to_class(int argc, VALUE *argv, VALUE self)
{
rb_objspace_t *objspace = rb_gc_get_objspace();
if (!stress_to_class) {
set_stress_to_class(rb_ary_hidden_new(argc));
}
rb_ary_cat(stress_to_class, argv, argc);
return self;
}
/*
* call-seq:
* GC.remove_stress_to_class(class[, ...])
*
* No longer raises NoMemoryError when allocating an instance of the
* given classes.
*
*/
static VALUE
rb_gcdebug_remove_stress_to_class(int argc, VALUE *argv, VALUE self)
{
rb_objspace_t *objspace = rb_gc_get_objspace();
int i;
if (stress_to_class) {
for (i = 0; i < argc; ++i) {
rb_ary_delete_same(stress_to_class, argv[i]);
}
if (RARRAY_LEN(stress_to_class) == 0) {
set_stress_to_class(0);
}
}
return Qnil;
}
void *
rb_gc_impl_objspace_alloc(void)
{
@ -9410,6 +9456,11 @@ rb_gc_impl_init(void)
rb_define_singleton_method(rb_mGC, "verify_compaction_references", rb_f_notimplement, -1);
}
if (GC_DEBUG_STRESS_TO_CLASS) {
rb_define_singleton_method(rb_mGC, "add_stress_to_class", rb_gcdebug_add_stress_to_class, -1);
rb_define_singleton_method(rb_mGC, "remove_stress_to_class", rb_gcdebug_remove_stress_to_class, -1);
}
/* internal methods */
rb_define_singleton_method(rb_mGC, "verify_internal_consistency", gc_verify_internal_consistency_m, 0);