123456789_123456789_123456789_123456789_123456789_

Module: Fiddle

Overview

A libffi wrapper for Ruby.

Description

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.

Example

Here we will use 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

Constant Summary

Class Attribute Summary

Class Method Summary

  • .dlwrap(val)

    Returns the memory address of the Ruby object stored at val

  • .free(addr)

    Free the memory at address addr

  • .malloc(size)

    Allocate size bytes of memory and return the integer memory address for the allocated memory.

  • .dlunwrap(addr) mod_func

    Returns the Ruby object stored at the memory address addr

  • .realloc(addr, size) mod_func

    Change the size of the memory allocated at the memory location addr to size bytes.

Class Attribute Details

.last_error (rw)

Returns the last ::Fiddle::Error of the current executing Thread or nil if none

[ GitHub ]

  
# File 'lib/fiddle.rb', line 41

def self.last_error
  FFI.errno
end

.last_error=(error) (rw)

Sets the last ::Fiddle::Error of the current executing Thread to error

[ GitHub ]

  
# File 'lib/fiddle.rb', line 46

def self.last_error=(error)
  FFI.errno = error || 0
end

.win32_last_error (rw)

Returns the last win32 ::Fiddle::Error of the current executing Thread or nil if none

[ GitHub ]

  
# File 'lib/fiddle.rb', line 54

def win32_last_error
  FFI.errno.nonzero?
end

.win32_last_error=(error) (rw)

Sets the last win32 ::Fiddle::Error of the current executing Thread to error

[ GitHub ]

  
# File 'lib/fiddle.rb', line 59

def win32_last_error=(error)
  FFI.errno = error || 0
end

.win32_last_socket_error (rw)

Returns the last win32 socket ::Fiddle::Error of the current executing Thread or nil if none

[ GitHub ]

  
# File 'lib/fiddle.rb', line 65

def win32_last_socket_error
  FFI.errno.nonzero?
end

.win32_last_socket_error=(error) (rw)

Sets the last win32 socket ::Fiddle::Error of the current executing Thread to error

[ GitHub ]

  
# File 'lib/fiddle.rb', line 71

def win32_last_socket_error=(error)
  FFI.errno = error || 0
end

Class Method Details

.dlunwrap(addr) (mod_func)

Returns the Ruby object stored at the memory address addr

Example:

x = Object.new
# => #<Object:0x0000000107c7d870>
Fiddle.dlwrap(x)
# => 4425504880
Fiddle.dlunwrap(_)
# => #<Object:0x0000000107c7d870>
[ GitHub ]

  
# File 'ext/fiddle/fiddle.c', line 74

VALUE
rb_fiddle_ptr2value(VALUE self, VALUE addr)
{
    return (VALUE)NUM2PTR(addr);
}

.dlwrap(val)

Returns the memory address of the Ruby object stored at val

Example:

x = Object.new
# => #<Object:0x0000000107c7d870>
Fiddle.dlwrap(x)
# => 4425504880

In the case val is not a heap allocated object, this method will return the tagged pointer value.

Example:

Fiddle.dlwrap(123)
# => 247
[ GitHub ]

  
# File 'ext/fiddle/fiddle.c', line 100

static VALUE
rb_fiddle_value2ptr(VALUE self, VALUE val)
{
    return PTR2NUM((void*)val);
}

.free(addr)

Free the memory at address addr

[ GitHub ]

  
# File 'ext/fiddle/fiddle.c', line 51

VALUE
rb_fiddle_free(VALUE self, VALUE addr)
{
    void *ptr = NUM2PTR(addr);

    ruby_xfree(ptr);
    return Qnil;
}

.malloc(size)

Allocate size bytes of memory and return the integer memory address for the allocated memory.

[ GitHub ]

  
# File 'ext/fiddle/fiddle.c', line 22

static VALUE
rb_fiddle_malloc(VALUE self, VALUE size)
{
    void *ptr;
    ptr = (void*)ruby_xcalloc(1, NUM2SIZET(size));
    return PTR2NUM(ptr);
}

.realloc(addr, size) (mod_func)

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.

[ GitHub ]

  
# File 'ext/fiddle/fiddle.c', line 37

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);
}