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
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
attach_function :arc_submit, [:int, :pointer], :pointer
attach_function :arc_stat, [:int, :pointer], :pointer
attach_function :arc_get, [:int, :pointer], :pointer
+ attach_function :arc_kill, [:int, :pointer], :int
def self.string_array_as_pointer(arr)
result = FFI::MemoryPointer.new(:pointer, arr.size)
pointers = arr.read_array_of_pointer length
pointers.each do |i|
- p "freeeeeeeeeee#{i}"
LibC::free(i)
end