dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH] iseq: implement memsize for wrapper
@ 2015-08-12  0:48 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2015-08-12  0:48 UTC (permalink / raw)
  To: spew

---
 iseq.c                         | 98 +++++++++++++++++++++++++++---------------
 test/objspace/test_objspace.rb |  6 +++
 2 files changed, 70 insertions(+), 34 deletions(-)

diff --git a/iseq.c b/iseq.c
index bc1d60b..e59ecd1 100644
--- a/iseq.c
+++ b/iseq.c
@@ -29,7 +29,6 @@
 #define ISEQ_MINOR_VERSION 2
 
 VALUE rb_cISeq;
-ID iseqw_iseq_key;
 
 #define hidden_obj_p(obj) (!SPECIAL_CONST_P(obj) && !RBASIC(obj)->klass)
 
@@ -130,47 +129,67 @@ rb_iseq_mark(const rb_iseq_t *iseq)
     RUBY_MARK_LEAVE("iseq");
 }
 
-#if 0 /* TODO */
+static size_t
+param_keyword_size(const struct rb_iseq_param_keyword *pkw)
+{
+    size_t size = 0;
+
+    if (!pkw) return size;
+
+    size += sizeof(struct rb_iseq_param_keyword);
+    size += sizeof(VALUE) * (pkw->num - pkw->required_num);
+
+    return size;
+}
+
 static size_t
 iseq_memsize(const void *ptr)
 {
-    size_t size = sizeof(rb_iseq_t);
+    const rb_iseq_t *iseq = ptr;
+    size_t size = 0; /* struct already counted as RVALUE size */
+    const struct rb_iseq_variable_body *variable_body;
+    const struct rb_iseq_constant_body *body;
+    const struct iseq_compile_data *compile_data;
 
-    if (ptr) {
-	const rb_iseq_t *iseq = ptr;
+    variable_body = iseq->variable_body;
+    body = iseq->body;
 
-	size += iseq->body->iseq_size * sizeof(VALUE);
-	size += iseq->body->line_info_size * sizeof(struct iseq_line_info_entry);
-	size += iseq->body->local_table_size * sizeof(ID);
-	if (iseq->body->catch_table) {
-	    size += iseq_catch_table_bytes(iseq->body->catch_table->size);
+    if (variable_body) {
+	size += sizeof(struct rb_iseq_variable_body);
+	if (variable_body->iseq && body) {
+	    size += body->iseq_size * sizeof(VALUE);
 	}
-	size += (iseq->body->param.opt_num + 1) * sizeof(VALUE);
-	if (iseq->body->param.keyword != NULL) {
-	    size += sizeof(struct rb_iseq_param_keyword);
-	    size += sizeof(VALUE) * (iseq->body->param.keyword->num - iseq->body->param.keyword->required_num);
+    }
+
+    if (body) {
+	size += sizeof(struct rb_iseq_constant_body);
+	size += body->iseq_size * sizeof(VALUE);
+	size += body->line_info_size * sizeof(struct iseq_line_info_entry);
+	size += body->local_table_size * sizeof(ID);
+	if (body->catch_table) {
+	    size += iseq_catch_table_bytes(body->catch_table->size);
 	}
-	size += iseq->body->is_size * sizeof(union iseq_inline_storage_entry);
-	size += iseq->body->callinfo_size * sizeof(rb_call_info_t);
+	size += (body->param.opt_num + 1) * sizeof(VALUE);
+	size += param_keyword_size(body->param.keyword);
+	size += body->is_size * sizeof(union iseq_inline_storage_entry);
+	size += body->callinfo_size * sizeof(rb_call_info_t);
+    }
 
-	if (iseq->compile_data) {
-	    struct iseq_compile_data_storage *cur;
+    compile_data = iseq->compile_data;
+    if (compile_data) {
+	struct iseq_compile_data_storage *cur;
 
-	    cur = iseq->compile_data->storage_head;
-	    while (cur) {
-		size += cur->size + SIZEOF_ISEQ_COMPILE_DATA_STORAGE;
-		cur = cur->next;
-	    }
-	    size += sizeof(struct iseq_compile_data);
-	}
-	if (iseq->body->iseq) {
-	    size += iseq->body->iseq_size * sizeof(VALUE);
+	size += sizeof(struct iseq_compile_data);
+
+	cur = compile_data->storage_head;
+	while (cur) {
+	    size += cur->size + SIZEOF_ISEQ_COMPILE_DATA_STORAGE;
+	    cur = cur->next;
 	}
     }
 
     return size;
 }
-#endif
 
 static rb_iseq_t *
 iseq_alloc(void)
@@ -639,12 +658,25 @@ rb_iseq_method_name(const rb_iseq_t *iseq)
 
 /* define wrapper class methods (RubyVM::InstructionSequence) */
 
+static void
+iseqw_mark(void *ptr)
+{
+    rb_gc_mark((VALUE)ptr);
+}
+
+static const rb_data_type_t iseqw_data_type = {
+    "T_IMEMO/iseq",
+    {iseqw_mark, NULL, iseq_memsize,},
+    0, 0, RUBY_TYPED_FREE_IMMEDIATELY
+};
+
 static VALUE
 iseqw_new(const rb_iseq_t *iseq)
 {
-    VALUE iseqw = rb_obj_alloc(rb_cISeq);
-    rb_ivar_set(iseqw, iseqw_iseq_key, (VALUE)iseq);
-    return iseqw;
+    union { const rb_iseq_t *in; void *out; } deconst;
+    deconst.in = iseq;
+
+    return TypedData_Wrap_Struct(rb_cISeq, &iseqw_data_type, deconst.out);
 }
 
 VALUE
@@ -795,7 +827,7 @@ iseqw_s_compile_option_get(VALUE self)
 static const rb_iseq_t *
 iseqw_check(VALUE iseqw)
 {
-    const rb_iseq_t *iseq = (rb_iseq_t *)rb_ivar_get(iseqw, iseqw_iseq_key);
+    const rb_iseq_t *iseq = DATA_PTR(iseqw);
 
     if (!iseq->body->location.label) {
 	rb_raise(rb_eTypeError, "uninitialized InstructionSequence");
@@ -2282,6 +2314,4 @@ Init_ISeq(void)
     rb_define_singleton_method(rb_cISeq, "disasm", iseqw_s_disasm, 1);
     rb_define_singleton_method(rb_cISeq, "disassemble", iseqw_s_disasm, 1);
     rb_define_singleton_method(rb_cISeq, "of", iseqw_s_of, 1);
-
-    iseqw_iseq_key = rb_intern("T_IMEMO/iseq");
 }
diff --git a/test/objspace/test_objspace.rb b/test/objspace/test_objspace.rb
index 01f31cc..3036b87 100644
--- a/test/objspace/test_objspace.rb
+++ b/test/objspace/test_objspace.rb
@@ -88,6 +88,12 @@ class TestObjSpace < Test::Unit::TestCase
     assert_not_empty(res)
   end
 
+  def test_memsize_of_iseq
+    iseqw = RubyVM::InstructionSequence.compile('def a; a = :b; end')
+    base_obj_size = ObjectSpace.memsize_of(Object.new)
+    assert_operator(ObjectSpace.memsize_of(iseqw), :>, base_obj_size)
+  end
+
   def test_reachable_objects_from
     assert_separately %w[--disable-gem -robjspace], __FILE__, __LINE__, <<-'eom'
     assert_equal(nil, ObjectSpace.reachable_objects_from(nil))
-- 
EW


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2015-08-12  0:48 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-12  0:48 [PATCH] iseq: implement memsize for wrapper Eric Wong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).