diff options
author | ko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-10-05 08:14:09 +0000 |
---|---|---|
committer | ko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-10-05 08:14:09 +0000 |
commit | e03d6d9eb84764cb854dc500ce8c19d85db53656 (patch) | |
tree | f0682f240c20512d7c888921ce72ecf4f697cc46 /ext/objspace | |
parent | 7cc6bfa32947dcc60dab6e47250409b7962b29e7 (diff) | |
download | ruby-e03d6d9eb84764cb854dc500ce8c19d85db53656.tar.gz |
* 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
Diffstat (limited to 'ext/objspace')
-rw-r--r-- | ext/objspace/objspace.c | 47 |
1 files changed, 45 insertions, 2 deletions
diff --git a/ext/objspace/objspace.c b/ext/objspace/objspace.c index 33106382ca..1842209641 100644 --- a/ext/objspace/objspace.c +++ b/ext/objspace/objspace.c @@ -37,6 +37,7 @@ size_t rb_ary_memsize(VALUE); size_t rb_io_memsize(const rb_io_t *); size_t rb_generic_ivar_memsize(VALUE); size_t rb_objspace_data_type_memsize(VALUE obj); +VALUE rb_objspace_reachable_objects_from(VALUE obj); static size_t memsize_of(VALUE obj) @@ -626,6 +627,47 @@ count_tdata_objects(int argc, VALUE *argv, VALUE self) return hash; } +/* + * call-seq: + * ObjectSpace.reachable_objects_from(obj) -> array or nil + * + * [MRI specific feature] Return all reachable objects from `obj'. + * + * This method returns all reachable objects from `obj'. + * If `obj' has references two or more references to same object `x', + * them returned array only include one `x' object. + * If `obj' is non-markable (non-heap management) object such as + * true, false, nil, symbols and Fixnums (and Flonum) them it simply + * returns nil. + * + * With this method, you can find memory leaks. + * + * This method is not expected to work except C Ruby. + * + * Example: + * ObjectSpace.reachable_objects_from(['a', 'b', 'c']) + * #=> [Array, 'a', 'b', 'c'] + * + * ObjectSpace.reachable_objects_from(['a', 'a', 'a']) + * #=> [Array, 'a', 'a', 'a'] # all 'a' strings have different object id + * + * ObjectSpace.reachable_objects_from([v = 'a', v, v]) + * #=> [Array, 'a'] + * + * ObjectSpace.reachable_objects_from(1) + * #=> nil # 1 is not markable (heap managed) object + * + * Limitation: Current implementation can't acquire internal objects. + * This means that you can't acquire complete object graph + * (heap snapshot). This is future work. + * + */ +static VALUE +reachable_objects_from(VALUE self, VALUE obj) +{ + return rb_objspace_reachable_objects_from(obj); +} + /* objspace library extends ObjectSpace module and add several * methods to get internal statistic information about * object/memory management. @@ -642,10 +684,11 @@ Init_objspace(void) VALUE rb_mObjSpace = rb_const_get(rb_cObject, rb_intern("ObjectSpace")); rb_define_module_function(rb_mObjSpace, "memsize_of", memsize_of_m, 1); - rb_define_module_function(rb_mObjSpace, "memsize_of_all", - memsize_of_all_m, -1); + rb_define_module_function(rb_mObjSpace, "memsize_of_all", memsize_of_all_m, -1); rb_define_module_function(rb_mObjSpace, "count_objects_size", count_objects_size, -1); rb_define_module_function(rb_mObjSpace, "count_nodes", count_nodes, -1); rb_define_module_function(rb_mObjSpace, "count_tdata_objects", count_tdata_objects, -1); + + rb_define_module_function(rb_mObjSpace, "reachable_objects_from", reachable_objects_from, 1); } |