| 1 |
#include <ffi.h> |
| 2 |
#include <string.h> |
| 3 |
#include <stdlib.h> |
| 4 |
|
| 5 |
#define is_signed(type) ((((type)-1) < 0) ? 1 : 0) |
| 6 |
|
| 7 |
typedef struct { |
| 8 |
char name[13]; |
| 9 |
int value; |
| 10 |
} foo_t; |
| 11 |
|
| 12 |
foo_t |
| 13 |
get_foo(void) |
| 14 |
{ |
| 15 |
foo_t self; |
| 16 |
strcpy(self.name, "hello"); |
| 17 |
self.value = 42; |
| 18 |
return self; |
| 19 |
} |
| 20 |
|
| 21 |
int |
| 22 |
dlmain(int argc, char *argv[]) |
| 23 |
{ |
| 24 |
ffi_cif cif; |
| 25 |
ffi_type ffi_type_foo_t; |
| 26 |
int i; |
| 27 |
foo_t foo; |
| 28 |
ffi_type *arg_types[1] = { &ffi_type_void }; |
| 29 |
void *args[1] = { NULL }; |
| 30 |
|
| 31 |
ffi_type_foo_t.size = ffi_type_foo_t.alignment = 0; |
| 32 |
ffi_type_foo_t.type = FFI_TYPE_STRUCT; |
| 33 |
ffi_type_foo_t.elements = calloc(15, sizeof(ffi_type*)); |
| 34 |
|
| 35 |
for(i=0; i<13; i++) |
| 36 |
ffi_type_foo_t.elements[i] = is_signed(char) ? &ffi_type_sint8 : &ffi_type_uint8; |
| 37 |
ffi_type_foo_t.elements[13] = &ffi_type_sint32; |
| 38 |
ffi_type_foo_t.elements[14] = NULL; |
| 39 |
|
| 40 |
if(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 0, &ffi_type_foo_t, arg_types) == FFI_OK) |
| 41 |
{ |
| 42 |
|
| 43 |
ffi_call(&cif, (void*) get_foo, &foo, args); |
| 44 |
|
| 45 |
if(strcmp(foo.name, "hello")) |
| 46 |
return 2; |
| 47 |
|
| 48 |
if(foo.value != 42) |
| 49 |
return 2; |
| 50 |
|
| 51 |
} |
| 52 |
else |
| 53 |
return 2; |
| 54 |
|
| 55 |
return 0; |
| 56 |
} |