From 84dcc38273665cba3f3756a454697edf11ab168a Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 15 Dec 2011 04:15:54 +0000 Subject: [PATCH] * regcomp.c (onig_region_memsize): implemented for memsize_of(). * ext/objspace/objspace.c (memsize_of): use it. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34050 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ ext/objspace/objspace.c | 2 +- regcomp.c | 10 ++++++++++ regint.h | 1 + test/objspace/test_objspace.rb | 4 ++++ 5 files changed, 22 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 15f164e096..dd05994d5f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Thu Dec 15 13:15:51 2011 Nobuyoshi Nakada + + * regcomp.c (onig_region_memsize): implemented for memsize_of(). + + * ext/objspace/objspace.c (memsize_of): use it. + Thu Dec 15 10:44:54 2011 Nobuyoshi Nakada * array.c (rb_ary_reject_bang, rb_ary_delete_if): update rdoc. diff --git a/ext/objspace/objspace.c b/ext/objspace/objspace.c index 66e33a38c4..f37499fc27 100644 --- a/ext/objspace/objspace.c +++ b/ext/objspace/objspace.c @@ -97,7 +97,7 @@ memsize_of(VALUE obj) case T_MATCH: if (RMATCH(obj)->rmatch) { struct rmatch *rm = RMATCH(obj)->rmatch; - size += sizeof(struct re_registers); /* TODO: onig_region_memsize(&rm->regs); */ + size += onig_region_memsize(&rm->regs); size += sizeof(struct rmatch_offset) * rm->char_offset_num_allocated; size += sizeof(struct rmatch); } diff --git a/regcomp.c b/regcomp.c index 98ecbe8180..d81334a002 100644 --- a/regcomp.c +++ b/regcomp.c @@ -5244,6 +5244,7 @@ size_t onig_memsize(const regex_t *reg) { size_t size = sizeof(regex_t); + if (!reg) return 0; if (IS_NOT_NULL(reg->p)) size += reg->alloc; if (IS_NOT_NULL(reg->exact)) size += reg->exact_end - reg->exact; if (IS_NOT_NULL(reg->int_map)) size += sizeof(int) * ONIG_CHAR_TABLE_SIZE; @@ -5254,6 +5255,15 @@ onig_memsize(const regex_t *reg) return size; } +size_t +onig_region_memsize(const OnigRegion *regs) +{ + size_t size = sizeof(*regs); + if (!regs) return 0; + size += regs->allocated * (sizeof(*regs->beg) + sizeof(*regs->end)); + return size; +} + #define REGEX_TRANSFER(to,from) do {\ (to)->state = ONIG_STATE_MODIFY;\ onig_free_body(to);\ diff --git a/regint.h b/regint.h index cd3c2a1035..ca293edcc5 100644 --- a/regint.h +++ b/regint.h @@ -842,6 +842,7 @@ typedef int (*ONIGENC_INIT_PROPERTY_LIST_FUNC_TYPE)(void); extern int onigenc_property_list_init P_((ONIGENC_INIT_PROPERTY_LIST_FUNC_TYPE)); extern size_t onig_memsize P_((const regex_t *reg)); +extern size_t onig_region_memsize P_((const struct re_registers *regs)); #if defined __GNUC__ && __GNUC__ >= 4 #pragma GCC visibility pop diff --git a/test/objspace/test_objspace.rb b/test/objspace/test_objspace.rb index 04a33817de..3c22dae551 100644 --- a/test/objspace/test_objspace.rb +++ b/test/objspace/test_objspace.rb @@ -17,6 +17,10 @@ class TestObjSpace < Test::Unit::TestCase f.close assert_kind_of(Integer, ObjectSpace.memsize_of(/a/.match("a"))) assert_kind_of(Integer, ObjectSpace.memsize_of(Struct.new(:a))) + + assert_operator(ObjectSpace.memsize_of(Regexp.new("(a)"*1000).match("a"*1000)), + :>, + ObjectSpace.memsize_of(//.match(""))) end def test_memsize_of_all