Debian Codegraph @

testsuite/libffi.call/callback2.c

raw
1 /* Area: ffi_call
2 Purpose: Check structures with nested 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 struct { double d; } s1;
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.s1.d += 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, ts12b_type;
38 ffi_type *ts12_type_elements[4];
39 ffi_type *ts12a_type_elements[2];
40 ffi_type *ts12b_type_elements[3];
41
42
43 test_structure_12 ts12_arg;
44 void (*ptr)(void)=&callback;
45
46 test_structure_12 *ts12_result =
47 (test_structure_12 *) malloc (sizeof(test_structure_12));
48
49 ts12a_type.size = 0;
50 ts12a_type.alignment = 0;
51 ts12a_type.type = FFI_TYPE_STRUCT;
52 ts12a_type.elements = ts12a_type_elements;
53 ts12a_type_elements[0] = &ffi_type_double;
54 ts12a_type_elements[1] = NULL;
55
56 ts12b_type.size = 0;
57 ts12b_type.alignment = 0;
58 ts12b_type.type = FFI_TYPE_STRUCT;
59 ts12b_type.elements = ts12b_type_elements;
60 ts12b_type_elements[0] = &ffi_type_double;
61 ts12b_type_elements[1] = &ffi_type_double;
62 ts12b_type_elements[2] = NULL;
63
64 ts12_type.size = 0;
65 ts12_type.alignment = 0;
66 ts12_type.type = FFI_TYPE_STRUCT;
67 ts12_type.elements = ts12_type_elements;
68 ts12_type_elements[0] = &ts12a_type;
69 ts12_type_elements[1] = &ts12b_type;
70 ts12_type_elements[2] = &ffi_type_uchar;
71 ts12_type_elements[3] = NULL;
72
73
74 args[0] = &ts12_type;
75 args[1] = &ffi_type_pointer;
76 values[0] = &ts12_arg;
77 values[1] = &ptr;
78
79 CHECK(ffi_prep_cif(&cif, ABI_NUM, 2, &ts12_type, args) == FFI_OK);
80
81 ts12_arg.s1.d = 5.55;
82 ts12_arg.c2 = 6;
83 ts12_arg.s[0] = 7.77;
84 ts12_arg.s[1] = 8.88;
85
86 printf ("%g\n", ts12_arg.s1.d);
87 printf ("%u\n", ts12_arg.c2);
88 printf ("%g\n", ts12_arg.s[0]);
89 printf ("%g\n", ts12_arg.s[1]);
90 printf ("%d\n", i);
91
92 ffi_call(&cif, FFI_FN(struct12), ts12_result, values);
93
94 printf ("%g\n", ts12_result->s1.d);
95 printf ("%u\n", ts12_result->c2);
96 printf ("%g\n", ts12_result->s[0]);
97 printf ("%g\n", ts12_result->s[1]);
98 printf ("%d\n", i);
99 CHECK(ts12_result->s1.d == 5.55 + 1);
100 CHECK(ts12_result->c2 == 6 + 1);
101 CHECK(ts12_result->s[0] == 7.77 + 1);
102 CHECK(ts12_result->s[1] == 8.88 + 1);
103 CHECK(i == 5 + 1);
104 CHECK(ts12_type.size == sizeof(test_structure_12));
105
106 free (ts12_result);
107 exit(0);
108 }