Maintenance of Ruby 2.0.0 ended on February 24, 2016. Read more
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 clal code written in another language.
Here we will use Fiddle::Function to wrap floor(3) from libm
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
The alignment size of a char
The alignment size of a double
The alignment size of a float
The alignment size of an int
The alignment size of a intptr_t
The alignment size of a long
The alignment size of a long long
The alignment size of a ptrdiff_t
The alignment size of a short
The alignment size of a size_t
The alignment size of a ssize_t
The alignment size of a uintptr_t
The alignment size of a void*
Platform built against (i.e. “x86_64-linux”, etc.)
See also RUBY_PLATFORM
Address of the ruby_xfree() function
size of a char
size of a double
size of a float
size of an int
size of a intptr_t
size of a long
size of a long long
size of a ptrdiff_t
size of a short
size of a size_t
size of a ssize_t
size of a uintptr_t
size of a void*
C type - char
C type - double
C type - float
C type - int
C type - intptr_t
C type - long
C type - long long
C type - ptrdiff_t
C type - short
C type - size_t
C type - ssize_t
C type - uintptr_t
C type - void
C type - void*
Returns a boolean regarding whether the host is WIN32
Creates a new handler that opens library
, and returns an
instance of Fiddle::Handle.
See Fiddle::Handle.new for more.
# File fiddle/lib/fiddle.rb, line 36 def dlopen library Fiddle::Handle.new library end
Returns the hexadecimal representation of a memory pointer address
addr
Example:
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"
VALUE rb_fiddle_ptr2value(VALUE self, VALUE addr) { rb_secure(4); return (VALUE)NUM2PTR(addr); }
Returns a memory pointer of a function's hexadecimal address location
val
Example:
lib = Fiddle.dlopen('/lib64/libc-2.15.so') => #<Fiddle::Handle:0x00000001342460> Fiddle.dlwrap(lib['strcpy'].to_s(16)) => 25522520
static VALUE rb_fiddle_value2ptr(VALUE self, VALUE val) { return PTR2NUM((void*)val); }
Free the memory at address addr
VALUE rb_fiddle_free(VALUE self, VALUE addr) { void *ptr = NUM2PTR(addr); rb_secure(4); ruby_xfree(ptr); return Qnil; }
Returns the last Error
of the current executing
Thread
or nil if none
# File fiddle/lib/fiddle.rb, line 20 def self.last_error Thread.current[:__FIDDLE_LAST_ERROR__] end
Sets the last Error
of the current executing
Thread
to error
# File fiddle/lib/fiddle.rb, line 25 def self.last_error= error Thread.current[:__DL2_LAST_ERROR__] = error Thread.current[:__FIDDLE_LAST_ERROR__] = error end
Allocate size
bytes of memory and return the integer memory
address for the allocated memory.
static VALUE rb_fiddle_malloc(VALUE self, VALUE size) { void *ptr; rb_secure(4); ptr = (void*)ruby_xmalloc(NUM2INT(size)); return PTR2NUM(ptr); }
Change the size of the memory allocated at the memory location
addr
to size
bytes. Returns the memory address
of the reallocated memory, which may be different than the address passed
in.
static VALUE rb_fiddle_realloc(VALUE self, VALUE addr, VALUE size) { void *ptr = NUM2PTR(addr); rb_secure(4); ptr = (void*)ruby_xrealloc(ptr, NUM2INT(size)); return PTR2NUM(ptr); }