A libffi wrapper for Ruby.
Fiddle is an extension to translate a foreign function interface (FFI) with ruby.
It wraps libffi, a popular C library which provides a portable interface that allows code written in one language to call code written in another language.
require 'fiddle' libm = Fiddle.dlopen('/lib/libm.so.6') floor = Fiddle::Function.new( libm['floor'], [Fiddle::TYPE_DOUBLE], Fiddle::TYPE_DOUBLE ) puts floor.call(3.14159) #=> 3.0
Creates a new handler that opens
library, and returns an instance of
nil is given for the
library, Fiddle::Handle::DEFAULT is used, which is the equivalent to RTLD_DEFAULT. See
man 3 dlopen for more.
lib = Fiddle.dlopen(nil)
The default is dependent on OS, and provide a handle for all libraries already loaded. For example, in most cases you can use this to access
libc functions, or ruby functions like
Fiddle::Handle.new for more.
Returns the hexadecimal representation of a memory pointer address
lib = Fiddle.dlopen('/lib64/libc-2.15.so') => #<Fiddle::Handle:0x00000001342460> lib['strcpy'].to_s(16) => "7f59de6dd240" Fiddle.dlunwrap(Fiddle.dlwrap(lib['strcpy'].to_s(16))) => "7f59de6dd240"
Returns a memory pointer of a function's hexadecimal address location
lib = Fiddle.dlopen('/lib64/libc-2.15.so') => #<Fiddle::Handle:0x00000001342460> Fiddle.dlwrap(lib['strcpy'].to_s(16)) => 25522520
Free the memory at address
size bytes of memory and return the integer memory address for the allocated memory.
Change the size of the memory allocated at the memory location
size bytes. Returns the memory address of the reallocated memory, which may be different than the address passed in.