diff --git a/ext/vernier/vernier.cc b/ext/vernier/vernier.cc index ba4e880..39d8bbc 100644 --- a/ext/vernier/vernier.cc +++ b/ext/vernier/vernier.cc @@ -1360,6 +1360,9 @@ class BaseCollector { rb_gc_mark(stack_table_value); }; + virtual void compact() { + }; + virtual VALUE get_markers() { return rb_ary_new(); }; @@ -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 { @@ -1969,12 +1989,19 @@ collector_free(void *data) { delete collector; } +static void +collector_compact(void *data) { + BaseCollector *collector = static_cast(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, }, }; diff --git a/test/test_helper.rb b/test/test_helper.rb index feff941..713982d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -23,3 +23,5 @@ def assert_valid_result(result) end end end + +GC.auto_compact = true