Skip to content

Commit

Permalink
Merge pull request #101 from casperisfine/test-with-auto-compaction
Browse files Browse the repository at this point in the history
Enable GC.auto_compact in test suite
  • Loading branch information
jhawthorn authored Oct 31, 2024
2 parents 46532aa + a4babd8 commit 1c542dc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
27 changes: 27 additions & 0 deletions ext/vernier/vernier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,9 @@ class BaseCollector {
rb_gc_mark(stack_table_value);
};

virtual void compact() {
};

virtual VALUE get_markers() {
return rb_ary_new();
};
Expand Down Expand Up @@ -1547,6 +1550,23 @@ class RetainedCollector : public BaseCollector {
rb_gc_mark(tp_newobj);
rb_gc_mark(tp_freeobj);
}

void compact() {
RetainedCollector *collector = this;
for (auto& obj: collector->object_list) {
VALUE reloc_obj = rb_gc_location(obj);

const auto search = collector->object_frames.find(obj);
if (search != collector->object_frames.end()) {
int stack_index = search->second;

collector->object_frames.erase(search);
collector->object_frames.emplace(reloc_obj, stack_index);
}

obj = reloc_obj;
}
}
};

class GlobalSignalHandler {
Expand Down Expand Up @@ -1969,12 +1989,19 @@ collector_free(void *data) {
delete collector;
}

static void
collector_compact(void *data) {
BaseCollector *collector = static_cast<BaseCollector *>(data);
collector->compact();
}

static const rb_data_type_t rb_collector_type = {
.wrap_struct_name = "vernier/collector",
.function = {
//.dmemsize = rb_collector_memsize,
.dmark = collector_mark,
.dfree = collector_free,
.dcompact = collector_compact,
},
};

Expand Down
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ def assert_valid_result(result)
end
end
end

GC.auto_compact = true

0 comments on commit 1c542dc

Please sign in to comment.