Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

ffi_call(3) [debian man page]

ffi_call(3)						   BSD Library Functions Manual 					       ffi_call(3)

NAME
ffi_call -- Invoke a foreign function. SYNOPSIS
#include <ffi.h> void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue); DESCRIPTION
The ffi_call function provides a simple mechanism for invoking a function without requiring knowledge of the function's interface at compile time. fn is called with the values retrieved from the pointers in the avalue array. The return value from fn is placed in storage pointed to by rvalue. cif contains information describing the data types, sizes and alignments of the arguments to and return value from fn, and must be initialized with ffi_prep_cif before it is used with ffi_call. rvalue must point to storage that is sizeof(ffi_arg) or larger for non-floating point types. For smaller-sized return value types, the ffi_arg or ffi_sarg integral type must be used to hold the return value. EXAMPLES
#include <ffi.h> #include <stdio.h> unsigned char foo(unsigned int, float); int main(int argc, const char **argv) { ffi_cif cif; ffi_type *arg_types[2]; void *arg_values[2]; ffi_status status; // Because the return value from foo() is smaller than sizeof(long), it // must be passed as ffi_arg or ffi_sarg. ffi_arg result; // Specify the data type of each argument. Available types are defined // in <ffi/ffi.h>. arg_types[0] = &ffi_type_uint; arg_types[1] = &ffi_type_float; // Prepare the ffi_cif structure. if ((status = ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &ffi_type_uint8, arg_types)) != FFI_OK) { // Handle the ffi_status error. } // Specify the values of each argument. unsigned int arg1 = 42; float arg2 = 5.1; arg_values[0] = &arg1; arg_values[1] = &arg2; // Invoke the function. ffi_call(&cif, FFI_FN(foo), &result, arg_values); // The ffi_arg 'result' now contains the unsigned char returned from foo(), // which can be accessed by a typecast. printf("result is %hhu", (unsigned char)result); return 0; } // The target function. unsigned char foo(unsigned int x, float y) { unsigned char result = x - y; return result; } SEE ALSO
ffi(3), ffi_prep_cif(3) February 15, 2008

Check Out this Related Man Page

OUTB(2) 						     Linux Programmer's Manual							   OUTB(2)

NAME
outb, outw, outl, outsb, outsw, outsl, inb, inw, inl, insb, insw, insl, outb_p, outw_p, outl_p, inb_p, inw_p, inl_p - port I/O SYNOPSIS
#include <sys/io.h> unsigned char inb(unsigned short int port); unsigned char inb_p(unsigned short int port); unsigned short int inw(unsigned short int port); unsigned short int inw_p(unsigned short int port); unsigned int inl(unsigned short int port); unsigned int inl_p(unsigned short int port); void outb(unsigned char value, unsigned short int port); void outb_p(unsigned char value, unsigned short int port); void outw(unsigned short int value, unsigned short int port); void outw_p(unsigned short int value, unsigned short int port); void outl(unsigned int value, unsigned short int port); void outl_p(unsigned int value, unsigned short int port); void insb(unsigned short int port, void *addr, unsigned long int count); void insw(unsigned short int port, void *addr, unsigned long int count); void insl(unsigned short int port, void *addr, unsigned long int count); void outsb(unsigned short int port, const void *addr, unsigned long int count); void outsw(unsigned short int port, const void *addr, unsigned long int count); void outsl(unsigned short int port, const void *addr, unsigned long int count); DESCRIPTION
This family of functions is used to do low-level port input and output. The out* functions do port output, the in* functions do port input; the b-suffix functions are byte-width and the w-suffix functions word-width; the _p-suffix functions pause until the I/O completes. They are primarily designed for internal kernel use, but can be used from user space. You must compile with -O or -O2 or similar. The functions are defined as inline macros, and will not be substituted in without optimiza- tion enabled, causing unresolved references at link time. You use ioperm(2) or alternatively iopl(2) to tell the kernel to allow the user space application to access the I/O ports in question. Failure to do this will cause the application to receive a segmentation fault. CONFORMING TO
outb() and friends are hardware-specific. The value argument is passed first and the port argument is passed second, which is the opposite order from most DOS implementations. SEE ALSO
ioperm(2), iopl(2) Linux 2017-09-15 OUTB(2)
Man Page