| 1 |
#include <stdio.h> |
| 2 |
#include <stdarg.h> |
| 3 |
#include <ffi.h> |
| 4 |
|
| 5 |
int |
| 6 |
return_arg(int which, ...) |
| 7 |
{ |
| 8 |
va_list args; |
| 9 |
va_start(args, which); |
| 10 |
int i, val; |
| 11 |
|
| 12 |
for(i=0; i<which; i++) |
| 13 |
{ |
| 14 |
val = va_arg(args, int); |
| 15 |
} |
| 16 |
|
| 17 |
va_end(args); |
| 18 |
|
| 19 |
return val; |
| 20 |
} |
| 21 |
|
| 22 |
int |
| 23 |
basic_test() |
| 24 |
{ |
| 25 |
int answer; |
| 26 |
|
| 27 |
answer = return_arg(4,10,20,30,40,50,60,70); |
| 28 |
|
| 29 |
if(answer != 40) |
| 30 |
{ |
| 31 |
/* basic varadic function fail */ |
| 32 |
printf("basic answer = %d\n", answer); |
| 33 |
return 2; |
| 34 |
} |
| 35 |
|
| 36 |
return 0; |
| 37 |
} |
| 38 |
|
| 39 |
int |
| 40 |
ffi_test() |
| 41 |
{ |
| 42 |
ffi_cif cif; |
| 43 |
ffi_type *args[8] = { &ffi_type_sint32, &ffi_type_sint32, &ffi_type_sint32, &ffi_type_sint32, &ffi_type_sint32, &ffi_type_sint32, &ffi_type_sint32, &ffi_type_sint32 }; |
| 44 |
int values[8] = { 4,10,20,30,40,50,60,70 }; |
| 45 |
void *ptrvalues[8] = { &values[0], &values[1], &values[2], &values[3], &values[4], &values[5], &values[6], &values[7] }; |
| 46 |
int answer = -1; |
| 47 |
|
| 48 |
if(ffi_prep_cif_var(&cif, FFI_DEFAULT_ABI, 1, 8, &ffi_type_sint32, args) == FFI_OK) |
| 49 |
{ |
| 50 |
ffi_call(&cif, (void*) return_arg, &answer, ptrvalues); |
| 51 |
if(answer != 40) |
| 52 |
{ |
| 53 |
printf("ffi ansewr = %d\n", answer); |
| 54 |
return 2; |
| 55 |
} |
| 56 |
else |
| 57 |
{ |
| 58 |
return 0; |
| 59 |
} |
| 60 |
} |
| 61 |
else |
| 62 |
{ |
| 63 |
return 2; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
int |
| 68 |
dlmain(int argc, char *argv[]) |
| 69 |
{ |
| 70 |
if(basic_test()) |
| 71 |
return 2; |
| 72 |
if(ffi_test()) |
| 73 |
return 2; |
| 74 |
return 0; |
| 75 |
} |