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.
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 int16_t
The alignment size of a int32_t
The alignment size of a int64_t
The alignment size of a int8_t
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 const char*
size of a double
size of a float
size of an int
size of a int16_t
size of a int32_t
size of a int64_t
size of a int8_t
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 - const char* ('0' terminated const char*)
C type - double
C type - float
C type - int
C type - int16_t
C type - int32_t
C type - int64_t
C type - int8_t
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 - …
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
.
If 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 rb_str_new
.
See Fiddle::Handle.new
for more.
static VALUE rb_fiddle_realloc(VALUE self, VALUE addr, VALUE size) { void *ptr = NUM2PTR(addr); ptr = (void*)ruby_xrealloc(ptr, NUM2SIZET(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.
Creates a new handler that opens library
, and returns an instance of Fiddle::Handle
.
If 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 rb_str_new
.
See Fiddle::Handle.new
for more.
Ruby Core © 1993–2020 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.