mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* ext/objspace/objspace.c: add ObjectSpace#reachable_objects_from.
This method returns an array of objects referenced by given object. If given object is special objects such as true/false/nil/Fixnum etc then it returns nil. See rdoc for details. [ruby-core:39772] * test/objspace/test_objspace.rb: add a test for this method. * gc.c: add rb_objspace_reachable_objects_from(). To make this function, add several member `mark_func_data' to rb_objspace_t. If mark_func_data is not null, then gc_mark() calls mark_func_data::mark_func. * gc.h: export rb_objspace_reachable_objects_from(). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37094 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
7cc6bfa329
commit
e03d6d9eb8
5 changed files with 202 additions and 31 deletions
|
|
@ -68,4 +68,30 @@ class TestObjSpace < Test::Unit::TestCase
|
|||
ObjectSpace.count_tdata_objects(arg)
|
||||
assert_equal(false, arg.empty?)
|
||||
end
|
||||
|
||||
def test_reachable_objects_from
|
||||
assert_equal(nil, ObjectSpace.reachable_objects_from(nil))
|
||||
assert_equal([Array, 'a', 'b', 'c'], ObjectSpace.reachable_objects_from(['a', 'b', 'c']))
|
||||
|
||||
assert_equal([Array, 'a', 'a', 'a'], ObjectSpace.reachable_objects_from(['a', 'a', 'a']))
|
||||
assert_equal([Array, 'a', 'a'], ObjectSpace.reachable_objects_from(['a', v = 'a', v]))
|
||||
assert_equal([Array, 'a'], ObjectSpace.reachable_objects_from([v = 'a', v, v]))
|
||||
|
||||
long_ary = Array.new(1_000){''}
|
||||
max = 0
|
||||
|
||||
ObjectSpace.each_object{|o|
|
||||
refs = ObjectSpace.reachable_objects_from(o)
|
||||
max = [refs.size, max].max
|
||||
|
||||
unless refs.nil?
|
||||
refs.each{|ro|
|
||||
# check this referenced object is not internal object
|
||||
assert_equal(false, ro.nil?)
|
||||
}
|
||||
end
|
||||
}
|
||||
STDERR.puts max
|
||||
assert_equal(true, max >= 1_001) # 1000 elems + Array class
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue