The foreign function interface provides a mechanism by which a function can generate a call to another function at runtime without requiring knowledge of the called function's interface at compile time. This enables use of native libraries that LWJGL does not provide bindings for.
libffi assumes that you have a pointer to the function you wish to call and that you know the number and types of arguments to pass it, as well as the return type of the function.
The first thing you must do is create an FFICIF object that matches the signature of the function you wish to call. This is a separate step
because it is common to make multiple calls using a single FFICIF. The cif in ffi_cif stands for Call InterFace. To prepare a
call interface object, use the function prep_cif. To call a function using an initialized ffi_cif, use the call function.
| Modifier and Type | Field and Description |
|---|---|
| public static final int | FFI_BAD_ABI
Status codes. |
| public static final int | FFI_BAD_ARGTYPE
Status codes. |
| public static final int | FFI_BAD_TYPEDEF
Status codes. |
| public static final int | FFI_DEFAULT_ABI
ABI enumeration. |
| public static final int | FFI_EFI64
ABI enumeration. |
| public static final int | FFI_FASTCALL
ABI enumeration. |
| public static final int | FFI_FIRST_ABI
ABI enumeration. |
| public static final int | FFI_GNUW64
ABI enumeration. |
| public static final int | FFI_LAST_ABI
ABI enumeration. |
| public static final int | FFI_MS_CDECL
ABI enumeration. |
| public static final int | FFI_OK
Status codes. |
| public static final int | FFI_PASCAL
ABI enumeration. |
| public static final int | FFI_REGISTER
ABI enumeration. |
| public static final int | FFI_STDCALL
ABI enumeration. |
| public static final int | FFI_SYSV
ABI enumeration. |
| public static final int | FFI_THISCALL
ABI enumeration. |
| public static final short | FFI_TYPE_DOUBLE
Types used to create custom |
| public static final FFIType | ffi_type_double
The |
| public static final short | FFI_TYPE_FLOAT
Types used to create custom |
| public static final FFIType | ffi_type_float
The |
| public static final short | FFI_TYPE_INT
Types used to create custom |
| public static final short | FFI_TYPE_LONGDOUBLE
Types used to create custom |
| public static final FFIType | ffi_type_longdouble
The |
| public static final short | FFI_TYPE_POINTER
Types used to create custom |
| public static final FFIType | ffi_type_pointer
The |
| public static final FFIType | ffi_type_schar
The |
| public static final FFIType | ffi_type_sint
The |
| public static final short | FFI_TYPE_SINT16
Types used to create custom |
| public static final FFIType | ffi_type_sint16
The |
| public static final short | FFI_TYPE_SINT32
Types used to create custom |
| public static final FFIType | ffi_type_sint32
The |
| public static final short | FFI_TYPE_SINT64
Types used to create custom |
| public static final FFIType | ffi_type_sint64
The |
| public static final short | FFI_TYPE_SINT8
Types used to create custom |
| public static final FFIType | ffi_type_sint8
The |
| public static final FFIType | ffi_type_slong
The |
| public static final FFIType | ffi_type_sshort
The |
| public static final short | FFI_TYPE_STRUCT
Types used to create custom |
| public static final FFIType | ffi_type_uchar
The |
| public static final FFIType | ffi_type_uint
The |
| public static final short | FFI_TYPE_UINT16
Types used to create custom |
| public static final FFIType | ffi_type_uint16
The |
| public static final short | FFI_TYPE_UINT32
Types used to create custom |
| public static final FFIType | ffi_type_uint32
The |
| public static final short | FFI_TYPE_UINT64
Types used to create custom |
| public static final FFIType | ffi_type_uint64
The |
| public static final short | FFI_TYPE_UINT8
Types used to create custom |
| public static final FFIType | ffi_type_uint8
The |
| public static final FFIType | ffi_type_ulong
The |
| public static final FFIType | ffi_type_ushort
The |
| public static final short | FFI_TYPE_VOID
Types used to create custom |
| public static final FFIType | ffi_type_void
The |
| public static final int | FFI_UNIX64
ABI enumeration. |
| public static final int | FFI_VFP
ABI enumeration. |
| public static final int | FFI_WIN64
ABI enumeration. |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public static void | ffi_call(FFICIF
a cif, long FFICIF structure. It must be initialized with prep_cif or prep_cif_var before it is used with ffi_call.the function to call fn, ByteBuffer a pointer to a chunk of memory that will hold the result of the function call.
rvalue, PointerBuffer This must be large enough to hold the result, no smaller than the system register size (generally 32 or 64 bits), and must be suitably aligned; it
is the caller's responsibility to ensure this. If CIF declares that the function returns In most situations, libffi will handle promotion according to the ABI. However, for historical reasons, there is a special case with return values
that must be handled by your code. In particular, for integral (not a vector of avalues)void * pointers that point to the memory locations holding the argument values for a call.
If Note that while the return value must be register-sized, arguments should exactly match their declared type. For example, if an argument is a
Calls the function |
| public static FFIClosure | Returns: a pointer to the writable addressthe number of bytes to allocate. Should be sufficient to hold an size, PointerBuffer ffi_closure object (FFIClosure#SIZEOF).a buffer in which to place the returned executable address code)Allocates a chunk of memory holding |
| public static void | ffi_closure_free(FFIClosure
the address of an writable)FFIClosure structureFrees memory allocated using |
| private static native int | |
| private static native int | |
| private static native int | |
| private static native int | |
| public static int | Returns: returnsOK on success; BAD_ABI if abi is invalid; or BAD_TYPEDEF if struct_type is invalid in some way. Note that only
FFI_STRUCT types are valid here.the ABI to use; this is needed because in some cases the layout depends on the ABI abi, FFIType struct_type, PointerBuffer an out parameter. The caller is responsible for providing enough space for all the results to be written - one element per element type in
offsets)struct_type. If offsets is NULL, then the type will be laid out but not otherwise modified. This can be useful for accessing the
type's size or layout.Computes the offset of each element of the given structure type. |
| private static native int | |
| private static native int | |
| private static native int | |
| private static native int | |
| public static int | Returns: a status code, of typeffi_status.
This will be either the cif, int abi, FFIType ffi_cif structure to preparea pointer to an rtype, PointerBuffer ffi_type structure that describes the return type of the functiona vector of atypes)ffi_type pointers. atypes must have nargs elements. If nargs is 0, this argument is ignored.Initializes the specified Call Interface (CIF) according to the given parameters. |
| public static int | Returns: a status code, of typeffi_status.
This will be either the cif, int abi, int ffi_cif structure to preparethe number of fixed arguments, prior to any variadic arguments. It must be greater than zero. nfixedargs, FFIType a pointer to an rtype, PointerBuffer ffi_type structure that describes the return type of the functiona vector of atypes)ffi_type pointers. atypes must have ntotalargs elements.Initializes the specified Call Interface (CIF) according to the given parameters for a call to a variadic function. |
| public static int | ffi_prep_closure_loc(FFIClosure
the address of an closure, FFICIF ffi_closure object; this is the writable address returned by closure_alloc.the cif, long ffi_cif describing the function parametersthe function which will be called when the closure is invoked. It is called with the arguments:
fun, long
an arbitrary datum that is passed, uninterpreted, to your closure function user_data, long the executable address returned by codeloc)closure_alloc.Prepares a closure function. |
| private static native int | |
| private static native int | |
| private static native int | |
| private static native int | |
| private static native short | |
| private static FFIType | |
| private static FFIType | |
| private static FFIType | |
| private static FFIType | |
| private static FFIType | |
| private static FFIType | |
| private static FFIType | |
| private static FFIType | |
| private static FFIType | |
| private static FFIType | |
| private static FFIType | |
| private static FFIType | |
| private static FFIType | |
| private static FFIType | |
| private static FFIType | |
| private static FFIType | |
| private static FFIType | |
| private static FFIType | |
| private static FFIType | |
| private static FFIType | |
| private static FFIType | |
| private static native int | |
| private static native int | |
| private static native int | |
| public static native void | |
| public static native long | |
| public static native void | |
| public static native int | nffi_get_struct_offsets(int abi, long struct_type, long offsets)
Unsafe version of: |
| public static native int | nffi_prep_cif(long cif, int abi, int
the number of arguments that this function accepts nargs, long rtype, long atypes)Unsafe version of: |
| public static native int | nffi_prep_cif_var(long cif, int abi, int nfixedargs, int
the total number of arguments, including variadic and fixed arguments ntotalargs, long rtype, long atypes)Unsafe version of: |
| public static native int | nffi_prep_closure_loc(long closure, long cif, long fun, long user_data, long codeloc)
Unsafe version of: |
| private static native long | |
| private static native long | |
| private static native long | |
| private static native long | |
| private static native long | |
| private static native long | |
| private static native long | |
| private static native long | |
| private static native long | |
| private static native long | |
| private static native long | |
| private static native long | |
| private static native long | |
| private static native long | |
| private static native long | |
| private static native long | |
| private static native long | |
| private static native long | |
| private static native long | |
| private static native long | |
| private static native long |