change rtld flag to global
authorTamas Jung <tamasjung@gmail.com>
Fri, 19 Nov 2010 12:59:06 +0000 (13:59 +0100)
committerTamas Jung <tamasjung@gmail.com>
Fri, 19 Nov 2010 12:59:06 +0000 (13:59 +0100)
lib/arc_client_c_wrapper.rb

index 241a345..249b2c9 100644 (file)
@@ -1,5 +1,45 @@
 require 'ffi'
 require 'ffi_libc'
+
+#This is a monkey patching follows for dlopen with RTLD_GLOBAL flag.
+#With the original RTLD_LOCAL dynamic_cast did not work in ARC as expected because of the 
+#lack of rtti and returned NULL silently. 
+module FFI
+  module Library
+    def ffi_global_lib(*names)
+
+      ffi_libs = names.map do |name|
+        if name == FFI::CURRENT_PROCESS
+          FFI::DynamicLibrary.open(nil, FFI::DynamicLibrary::RTLD_LAZY | FFI::DynamicLibrary::RTLD_GLOBAL)
+        else
+          libnames = (name.is_a?(::Array) ? name : [ name ]).map { |n| [ n, FFI.map_library_name(n) ].uniq }.flatten.compact
+          lib = nil
+          errors = {}
+
+          libnames.each do |libname|
+            begin
+              lib = FFI::DynamicLibrary.open(libname, FFI::DynamicLibrary::RTLD_LAZY | FFI::DynamicLibrary::RTLD_GLOBAL)
+              break if lib
+            rescue Exception => ex
+              errors[libname] = ex
+            end
+          end
+
+          if lib.nil?
+            raise LoadError.new(errors.values.join('. '))
+          end
+
+          # return the found lib
+          lib
+        end
+      end
+
+      @ffi_libs = ffi_libs
+    end
+  end
+end
+
+
 class ArcClientCWrapper
   
   class BaseStruct < FFI::Struct
@@ -48,9 +88,7 @@ class ArcClientCWrapper
   extend FFI::Library
   
   THIS_DIR = File.dirname(__FILE__)
-  #ffi_lib "/Users/tamas/work/iface/grid/ws/gp-arc-client-c/build/Debug/libarcclientc.dylib"
-  ffi_lib ['arc_client_c.so', 'arc_client_c.bundle'].map {|file| File.join THIS_DIR, file}
-  #ffi_lib "/Users/tamas/tmp/bbbb/gp-arc-client-c/src/arc_client_r.bundle"
+  ffi_global_lib ['arc_client_c.so', 'arc_client_c.bundle'].map {|file| File.join THIS_DIR, file}
   
   attach_function :initialize, [], :string
   
@@ -67,7 +105,6 @@ class ArcClientCWrapper
     pointers = arr.read_array_of_pointer length
     
     pointers.each do |i| 
-      p "freeeeeeeeeee#{i}"
       LibC::free(i) 
     end