From c1119d264a1fc3831f1252c5054311b3056aa0b8 Mon Sep 17 00:00:00 2001 From: Gonzalo Garramuno Date: Sun, 29 Apr 2007 23:52:22 +0000 Subject: [PATCH] Improved the swig_gc.rb interface. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9718 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Examples/test-suite/ruby/newobject1_runme.rb | 7 ++- Examples/test-suite/ruby/newobject2_runme.rb | 6 +-- Examples/test-suite/ruby/swig_gc.rb | 55 +++++++++++++------- 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/Examples/test-suite/ruby/newobject1_runme.rb b/Examples/test-suite/ruby/newobject1_runme.rb index beb383b5f..697626337 100644 --- a/Examples/test-suite/ruby/newobject1_runme.rb +++ b/Examples/test-suite/ruby/newobject1_runme.rb @@ -23,12 +23,11 @@ require 'newobject1' include Newobject1 - -GC.stats(Foo) +GC.track_class = Foo +GC.start 100.times { foo1 = Foo.makeFoo; foo2 = foo1.makeMore } -GC.stats(Foo) +GC.stats swig_assert( 'Foo.fooCount == 200', "but is #{Foo.fooCount}" ) GC.start -GC.stats(Foo) swig_assert( 'Foo.fooCount <= 2', "but is #{Foo.fooCount}" ) diff --git a/Examples/test-suite/ruby/newobject2_runme.rb b/Examples/test-suite/ruby/newobject2_runme.rb index 0a2cb2645..34cca40ae 100644 --- a/Examples/test-suite/ruby/newobject2_runme.rb +++ b/Examples/test-suite/ruby/newobject2_runme.rb @@ -17,11 +17,11 @@ require 'newobject2' include Newobject2 -GC.stats(Foo) +GC.track_class = Foo +GC.stats 100.times { foo1 = makeFoo } -GC.stats(Foo) +GC.stats swig_assert( 'fooCount == 100', "but is #{fooCount}" ) GC.start -GC.stats(Foo) swig_assert( 'fooCount <= 1', "but is #{fooCount}" ) diff --git a/Examples/test-suite/ruby/swig_gc.rb b/Examples/test-suite/ruby/swig_gc.rb index e3634b984..647d6fd36 100755 --- a/Examples/test-suite/ruby/swig_gc.rb +++ b/Examples/test-suite/ruby/swig_gc.rb @@ -11,27 +11,44 @@ # VERY nice function from Robert Klemme to check memory leaks # and check on what GC has collected since last call. # -module GC - @@last_stat = nil - def self.stats(klass = nil) - return unless $VERBOSE +if $VERBOSE + module GC - stats = Hash.new(0) - ObjectSpace.each_object {|o| stats[o.class] += 1} + class << self + + attr :last_stat + attr_accessor :track_class - if klass - v = stats[klass] - printf "\t%-30s %10d", klass.to_s, v - printf " | delta %10d", (v - @@last_stat[klass]) if @@last_stat - puts - else - stats.sort {|(k1,v1),(k2,v2)| v2 <=> v1}.each do |k,v| - printf "\t%-30s %10d", k, v - printf " | delta %10d", (v - @@last_stat[k]) if @@last_stat - puts + alias :_start :start + + def start + _start + stats + end + + def stats + stats = Hash.new(0) + ObjectSpace.each_object {|o| stats[o.class] += 1} + + if track_class + v = stats[track_class] + printf "\t%-30s %10d", track_class.to_s, v + if last_stat + printf " | delta %10d", (v - last_stat[track_class]) + end + puts + else + stats.sort {|(k1,v1),(k2,v2)| v2 <=> v1}.each do |k,v| + printf "\t%-30s %10d", k, v + printf " | delta %10d", (v - last_stat[k]) if last_stat + puts + end + end + + last_stat = stats end end - - @@last_stat = stats + end -end + +end