From e3822ad9d4a59ebc23a1a5d41a528d1501958a1e Mon Sep 17 00:00:00 2001 From: Charlie Savage Date: Tue, 10 Jan 2006 00:30:43 +0000 Subject: [PATCH] Rearranged the code a bit to store a global reference to the Ruby hash table delete method, thereby making it possible to look it up once instead of each time SWIG_RubyRemoveTracking is called. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@8339 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Lib/ruby/rubytracking.swg | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Lib/ruby/rubytracking.swg b/Lib/ruby/rubytracking.swg index 84cfb7003..1d66b6c85 100644 --- a/Lib/ruby/rubytracking.swg +++ b/Lib/ruby/rubytracking.swg @@ -17,6 +17,10 @@ extern "C" { structs to Ruby Objects. */ static VALUE swig_ruby_trackings; +/* Global variable that stores a reference to the ruby + hash table delete function. */ +static ID swig_ruby_hash_delete = 0; + /* Setup a Ruby hash table to store Trackings */ SWIGRUNTIME void SWIG_RubyInitializeTrackings() { /* Create a ruby hash table to store Trackings from C++ @@ -24,6 +28,10 @@ SWIGRUNTIME void SWIG_RubyInitializeTrackings() { the garabage collector about the hash table. */ swig_ruby_trackings = rb_hash_new(); rb_gc_register_address(&swig_ruby_trackings); + + /* Now store a reference to the hash table delete function + so that we only have to look it up once.*/ + swig_ruby_hash_delete = rb_intern("delete"); } /* Get a Ruby number to reference a pointer */ @@ -95,18 +103,17 @@ SWIGRUNTIME VALUE SWIG_RubyInstanceFor(void* ptr) { } } -/* Remove a Tracking from a C/C++ struct to a Ruby object */ +/* Remove a Tracking from a C/C++ struct to a Ruby object. It + is very important to remove objects once they are destroyed + since the same memory address may be reused later to create + a new object. */ SWIGRUNTIME void SWIG_RubyRemoveTracking(void* ptr) { /* Get a reference to the pointer as a Ruby number */ VALUE key = SWIG_RubyPtrToReference(ptr); - /* Define delete method - in C++ this could be marked as - static but unfortunately not in C. */ - VALUE delete_function = rb_intern("delete"); - /* Delete the object from the hash table by calling Ruby's do this we need to call the Hash.delete method.*/ - rb_funcall(swig_ruby_trackings, delete_function, 1, key); + rb_funcall(swig_ruby_trackings, swig_ruby_hash_delete, 1, key); } /* This is a helper method that unlinks a Ruby object from its