Types

Patrik Wenger edited this page Nov 25, 2015 · 12 revisions

This page contains a canonical list of type symbols recognized by attach_function and Struct#layout

Types

The following types can be used as arguments and return types:

Symbol Description Expected/returned type from FFI Notes
:char 8-bit character Integer
:uchar 8-bit unsigned character Integer
:int8 8-bit signed integer Integer
:uint8 8-bit unsigned integer Integer
:short 16-bit signed integer Integer
:ushort 16-bit unsigned integer Integer
:int16 16-bit signed integer Integer
:uint16 16-bit unsigned integer Integer
:int signed integer Integer platform specific size
:uint unsigned integer Integer platform specific size
:int32 32-bit signed integer Integer
:uint32 32-bit unsigned integer Integer
:long long int Integer platform-specific size
:ulong unsigned long int Integer platform-specific size
:int64 64-bit signed integer Integer
:uint64 64-bit unsigned integer Integer
:long_long 64-bit signed integer Integer
:ulong_long 64-bit unsigned integer Integer
:float 32-bit floating point Integer
:double 64-bit floating point (double-precision) Integer
:pointer pointer Integer platform-specific size
:string C-style (NULL-terminated) character string. String If the function modifies the string buffer use :pointer instead. That is, :string should be considered to be const char *. (In fact, MRI will let you modify a :string in place, but JRuby will not, so this usage is not portable)
:bool boolean true/false
:size_t size_t from stddef.h platform-dependent size
:in_addr_t 32-bit address in network byte order Integer (FFI >= 1.0.8)
:in_port_t 16-bit port in network byte order Integer (FFI >= 1.0.8)
Enums your own custom enum group (or its symbol) as a type Symbol/Integer

For function return type only:

Symbol Description Returned type from FFI Notes
:void return type void Pointer for functions that return nothing
:strptr Ruby String and a Pointer to C memory Array (e.g. ["foo", ptr]) useful to free the memory from within Ruby

For function argument type only:

Symbol Description Expected by FFI Notes
:buffer_in Similar to :pointer, but optimized for Buffers that the function can only read (not write). Pointer
:buffer_out Similar to :pointer, but optimized for Buffers that the function can only write (not read). Pointer
:buffer_inout Similar to :pointer, but may be optimized for Buffers. Pointer
:varargs variadic arguments anything see examples

More Types

Apparently there are most types available as well. For a full list see lib/ffi/platform/<your platform>/types.conf (FFI >= 1.0.8)

1 this thread explains some of the different sizes on 64 versus 32-bit, but you shouldn’t have to worry about it.