Skip to content

Commit 5e45f2a

Browse files
committed
Add age to rb_gc_object_metadata
This will allow ObjectSpace.dump to output the age of the object.
1 parent 5acfe30 commit 5e45f2a

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

gc/default/default.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6197,11 +6197,12 @@ rb_gc_impl_object_metadata(void *objspace_ptr, VALUE obj)
61976197
{
61986198
rb_objspace_t *objspace = objspace_ptr;
61996199
size_t n = 0;
6200-
static ID ID_wb_protected, ID_old, ID_uncollectible, ID_marking, ID_marked, ID_pinned;
6200+
static ID ID_wb_protected, ID_age, ID_old, ID_uncollectible, ID_marking, ID_marked, ID_pinned;
62016201

62026202
if (!ID_marked) {
62036203
#define I(s) ID_##s = rb_intern(#s);
62046204
I(wb_protected);
6205+
I(age);
62056206
I(old);
62066207
I(uncollectible);
62076208
I(marking);
@@ -6218,6 +6219,7 @@ rb_gc_impl_object_metadata(void *objspace_ptr, VALUE obj)
62186219
} while (0)
62196220

62206221
if (!RVALUE_WB_UNPROTECTED(objspace, obj)) SET_ENTRY(wb_protected, Qtrue);
6222+
SET_ENTRY(age, INT2FIX(RVALUE_AGE_GET(obj)));
62216223
if (RVALUE_OLD_P(objspace, obj)) SET_ENTRY(old, Qtrue);
62226224
if (RVALUE_UNCOLLECTIBLE(objspace, obj)) SET_ENTRY(uncollectible, Qtrue);
62236225
if (RVALUE_MARKING(objspace, obj)) SET_ENTRY(marking, Qtrue);

test/objspace/test_objspace.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,25 @@ def test_dump_flags
332332
# Ensure that the fstring is promoted to old generation
333333
4.times { GC.start }
334334
info = ObjectSpace.dump("foo".freeze)
335-
assert_match(/"wb_protected":true, "old":true/, info)
335+
assert_include(info, '"wb_protected":true')
336+
assert_include(info, '"age":3')
337+
assert_include(info, '"old":true')
336338
assert_match(/"fstring":true/, info)
337339
JSON.parse(info) if defined?(JSON)
338340
end
339341

342+
def test_dump_flag_age
343+
EnvUtil.without_gc do
344+
o = Object.new
345+
346+
assert_include(ObjectSpace.dump(o), '"age":0')
347+
348+
GC.start
349+
350+
assert_include(ObjectSpace.dump(o), '"age":1')
351+
end
352+
end
353+
340354
if defined?(RubyVM::Shape)
341355
class TooComplex; end
342356

0 commit comments

Comments
 (0)