summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-10-27 18:16:39 +0000
committerko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-10-27 18:16:39 +0000
commitb36aa7af03a481fc1f41533256a78946498ba0ae (patch)
tree2d071af1f267d55fe95d45116d1d25b1115a5f4c
parent430c1d3fe4d107e141676fedf56ace98221b3b99 (diff)
* ext/objspace/objspace.c (memsize_of): fix rdoc.
* ext/objspace/objspace.c (total_memsize_of_all_objects): added. * test/objspace/test_objspace.rb: - add a test for ObjectSpace.total_memsize_of_all_objects. - add two tests for ObjectSpace.memsize_of (for nil and Fixnum). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29614 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog10
-rw-r--r--ext/objspace/objspace.c56
-rw-r--r--test/objspace/test_objspace.rb6
3 files changed, 69 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index 90da171..78c66b0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+Thu Oct 28 03:13:06 2010 Koichi Sasada <ko1@atdot.net>
+
+ * ext/objspace/objspace.c (memsize_of): fix rdoc.
+
+ * ext/objspace/objspace.c (total_memsize_of_all_objects): added.
+
+ * test/objspace/test_objspace.rb:
+ - add a test for ObjectSpace.total_memsize_of_all_objects.
+ - add two tests for ObjectSpace.memsize_of (for nil and Fixnum).
+
Wed Oct 27 23:55:27 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
* ext/iconv/iconv.c (Init_iconv): warn deprecated use.
diff --git a/ext/objspace/objspace.c b/ext/objspace/objspace.c
index a08f773..02eed03 100644
--- a/ext/objspace/objspace.c
+++ b/ext/objspace/objspace.c
@@ -155,9 +155,9 @@ memsize_of(VALUE obj)
*
* Return consuming memory size of obj.
*
- * Note that this information is incomplete. You need to deal with
+ * Note that the return size is incomplete. You need to deal with
* this information as only a *HINT*. Especaially, the size of
- * T_DATA may not right size.
+ * T_DATA may not be correct.
*
* This method is not expected to work except C Ruby.
*/
@@ -169,6 +169,53 @@ memsize_of_m(VALUE self, VALUE obj)
}
static int
+total_i(void *vstart, void *vend, size_t stride, void *data)
+{
+ size_t *total_storage = (size_t *)data, total = 0;
+ VALUE v;
+
+ for (v = (VALUE)vstart; v != (VALUE)vend; v += stride) {
+ if (RBASIC(v)->flags) {
+ total += memsize_of(v);
+ }
+ }
+ *total_storage += total;
+
+ return 0;
+}
+
+/*
+ * call-seq:
+ * ObjectSpace.total_memsize_of_all_objects() -> Integer
+ *
+ * Return consuming memory size of all living objects.
+ *
+ * Note that the returned size is incomplete. You need to deal with
+ * this information as only a *HINT*. Especaially, the size of
+ * T_DATA may not be correct.
+ *
+ * Note that this method does *NOT* return total malloc'ed memory size.
+ *
+ * This method can be defined by the following Ruby code:
+ *
+ * def total_memsize_of_all_objects
+ * total = 0
+ * ObjectSpace.each_objects{|e| total += ObjectSpace.memsize_of(e)}
+ * total
+ * end
+ *
+ * This method is not expected to work except C Ruby.
+ */
+
+static VALUE
+total_memsize_of_all_objects_m(VALUE self)
+{
+ size_t total;
+ rb_objspace_each_objects(total_i, &total);
+ return SIZET2NUM(total);
+}
+
+static int
set_zero_i(st_data_t key, st_data_t val, st_data_t arg)
{
VALUE k = (VALUE)key;
@@ -544,8 +591,11 @@ Init_objspace(void)
{
VALUE rb_mObjSpace = rb_const_get(rb_cObject, rb_intern("ObjectSpace"));
- rb_define_module_function(rb_mObjSpace, "count_objects_size", count_objects_size, -1);
rb_define_module_function(rb_mObjSpace, "memsize_of", memsize_of_m, 1);
+ rb_define_module_function(rb_mObjSpace, "total_memsize_of_all_objects",
+ total_memsize_of_all_objects_m, 0);
+
+ 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);
}
diff --git a/test/objspace/test_objspace.rb b/test/objspace/test_objspace.rb
index cfdb485..ceb9650 100644
--- a/test/objspace/test_objspace.rb
+++ b/test/objspace/test_objspace.rb
@@ -4,6 +4,8 @@ require "objspace"
class TestObjSpace < Test::Unit::TestCase
def test_memsize_of
assert_equal(0, ObjectSpace.memsize_of(true))
+ assert_equal(0, ObjectSpace.memsize_of(nil))
+ assert_equal(0, ObjectSpace.memsize_of(1))
assert_kind_of(Integer, ObjectSpace.memsize_of(Object.new))
assert_kind_of(Integer, ObjectSpace.memsize_of(Class))
assert_kind_of(Integer, ObjectSpace.memsize_of(""))
@@ -17,6 +19,10 @@ class TestObjSpace < Test::Unit::TestCase
assert_kind_of(Integer, ObjectSpace.memsize_of(Struct.new(:a)))
end
+ def test_total_memsize_of_all_objects
+ assert_kind_of(Integer, ObjectSpace.total_memsize_of_all_objects)
+ end
+
def test_count_objects_size
res = ObjectSpace.count_objects_size
assert_equal(false, res.empty?)