1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/bootstraptest/test_objectspace.rb
John Hawthorn b99833baec
Use a monotonically increasing number for object_id
This changes object_id from being based on the objects location in
memory (or a nearby memory location in the case of a conflict) to be
based on an always increasing number.

This number is a Ruby Integer which allows it to overflow the size of a
pointer without issue (very unlikely to happen in real programs
especially on 64-bit, but a nice guarantee).

This changes obj_to_id_tbl and id_to_obj_tbl to both be maps of Ruby
objects to Ruby objects (previously they were Ruby object to C integer)
which simplifies updating them after compaction as we can run them
through gc_update_table_refs.

Co-authored-by: Aaron Patterson <tenderlove@ruby-lang.org>
2019-11-07 09:31:07 -08:00

55 lines
1.4 KiB
Ruby

assert_normal_exit %q{
eval("", TOPLEVEL_BINDING)
minobj = ObjectSpace.to_enum(:each_object).min_by {|a| a.object_id }
maxobj = ObjectSpace.to_enum(:each_object).max_by {|a| a.object_id }
(((minobj.object_id-100)..(minobj.object_id+100))+
((maxobj.object_id-100)..(maxobj.object_id+100))).each {|id|
begin
o = ObjectSpace._id2ref(id)
rescue RangeError
next
end
o.inspect if defined?(o.inspect)
}
}, '[ruby-dev:31911]'
assert_normal_exit %q{
ary = (1..10).to_a
ary.permutation(2) {|x|
if x == [1,2]
ObjectSpace.each_object(String) {|s|
s.clear if !s.frozen? && (s.length == 40 || s.length == 80)
}
end
}
}, '[ruby-dev:31982]'
assert_normal_exit %q{
ary = (1..100).to_a
ary.permutation(2) {|x|
if x == [1,2]
ObjectSpace.each_object(Array) {|o| o.clear if o == ary && o.object_id != ary.object_id }
end
}
}, '[ruby-dev:31985]'
assert_normal_exit %q{
ObjectSpace.define_finalizer("") do
Thread::Mutex.new.lock
end
}, '[ruby-dev:44049]'
assert_normal_exit %q{
ObjectSpace.define_finalizer("") do
Thread.new {}
end
}, '[ruby-core:37858]'
assert_equal 'ok', %q{
objects_and_ids = 1000.times.map { o = Object.new; [o, o.object_id] }
objects_and_ids.each { |expected, id|
actual = ObjectSpace._id2ref(id)
raise "expected #{expected.inspect}, got #{actual.inspect}" unless actual.equal?(expected)
}
'ok'
}