Debian Codegraph @

testsuite/libffi.call/callback.c

raw
1 /* Area: ffi_call
2 Purpose: Check structures with array and callback.
3 Limitations: none.
4 PR: none.
5 Originator: David Tenty <[email protected]> */
6
7 /* { dg-do run } */
8 #include "ffitest.h"
9
10 static int i=5;
11
12 static void callback(void) { i++; }
13
14 typedef struct
15 {
16 unsigned char c1;
17 double s[2];
18 unsigned char c2;
19 } test_structure_12;
20
21 static test_structure_12 ABI_ATTR struct12 (test_structure_12 ts, void (*func)(void))
22 {
23 ts.c1 += 1;
24 ts.c2 += 1;
25 ts.s[0] += 1;
26 ts.s[1] += 1;
27
28 func();
29 return ts;
30 }
31
32 int main (void)
33 {
34 ffi_cif cif;
35 ffi_type *args[MAX_ARGS];
36 void *values[MAX_ARGS];
37 ffi_type ts12_type,ts12a_type;
38 ffi_type *ts12_type_elements[4];
39 ffi_type *ts12a_type_elements[3];
40
41 test_structure_12 ts12_arg;
42 void (*ptr)(void)=&callback;
43
44 test_structure_12 *ts12_result =
45 (test_structure_12 *) malloc (sizeof(test_structure_12));
46
47 ts12a_type.size = 0;
48 ts12a_type.alignment = 0;
49 ts12a_type.type = FFI_TYPE_STRUCT;
50 ts12a_type.elements = ts12a_type_elements;
51 ts12a_type_elements[0] = &ffi_type_double;
52 ts12a_type_elements[1] = &ffi_type_double;
53 ts12a_type_elements[2] = NULL;
54
55 ts12_type.size = 0;
56 ts12_type.alignment = 0;
57 ts12_type.type = FFI_TYPE_STRUCT;
58 ts12_type.elements = ts12_type_elements;
59 ts12_type_elements[0] = &ffi_type_uchar;
60 ts12_type_elements[1] = &ts12a_type;
61 ts12_type_elements[2] = &ffi_type_uchar;
62 ts12_type_elements[3] = NULL;
63
64
65 args[0] = &ts12_type;
66 args[1] = &ffi_type_pointer;
67 values[0] = &ts12_arg;
68 values[1] = &ptr;
69
70 CHECK(ffi_prep_cif(&cif, ABI_NUM, 2, &ts12_type, args) == FFI_OK);
71
72 ts12_arg.c1 = 5;
73 ts12_arg.c2 = 6;
74 ts12_arg.s[0] = 7.77;
75 ts12_arg.s[1] = 8.88;
76
77 printf ("%u\n", ts12_arg.c1);
78 printf ("%u\n", ts12_arg.c2);
79 printf ("%g\n", ts12_arg.s[0]);
80 printf ("%g\n", ts12_arg.s[1]);
81 printf ("%d\n", i);
82
83 ffi_call(&cif, FFI_FN(struct12), ts12_result, values);
84
85 printf ("%u\n", ts12_result->c1);
86 printf ("%u\n", ts12_result->c2);
87 printf ("%g\n", ts12_result->s[0]);
88 printf ("%g\n", ts12_result->s[1]);
89 printf ("%d\n", i);
90 CHECK(ts12_result->c1 == 5 + 1);
91 CHECK(ts12_result->c2 == 6 + 1);
92 CHECK(ts12_result->s[0] == 7.77 + 1);
93 CHECK(ts12_result->s[1] == 8.88 + 1);
94 CHECK(i == 5 + 1);
95 CHECK(ts12_type.size == sizeof(test_structure_12));
96
97 free (ts12_result);
98 exit(0);
99 }