Debian Codegraph @

testsuite/libffi.call/callback3.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
15 typedef struct
16 {
17 struct { unsigned char c; double d; } s1;
18 double s[2];
19 unsigned char c2;
20 } test_structure_12;
21
22 static test_structure_12 ABI_ATTR struct12 (test_structure_12 ts, void (*func)(void))
23 {
24 ts.s1.c += 1;
25 ts.s1.d += 1;
26 ts.c2 += 1;
27 ts.s[0] += 1;
28 ts.s[1] += 1;
29
30 func();
31 return ts;
32 }
33
34 int main (void)
35 {
36 ffi_cif cif;
37 ffi_type *args[MAX_ARGS];
38 void *values[MAX_ARGS];
39 ffi_type ts12_type,ts12b_type, ts12a_type;
40 ffi_type *ts12_type_elements[4];
41 ffi_type *ts12b_type_elements[3];
42 ffi_type *ts12a_type_elements[3];
43
44 test_structure_12 ts12_arg;
45 void (*ptr)(void)=&callback;
46
47 test_structure_12 *ts12_result =
48 (test_structure_12 *) malloc (sizeof(test_structure_12));
49
50 ts12a_type.size = 0;
51 ts12a_type.alignment = 0;
52 ts12a_type.type = FFI_TYPE_STRUCT;
53 ts12a_type.elements = ts12a_type_elements;
54 ts12a_type_elements[0] = &ffi_type_uchar;
55 ts12a_type_elements[1] = &ffi_type_double;
56 ts12a_type_elements[2] = NULL;
57
58 ts12b_type.size = 0;
59 ts12b_type.alignment = 0;
60 ts12b_type.type = FFI_TYPE_STRUCT;
61 ts12b_type.elements = ts12b_type_elements;
62 ts12b_type_elements[0] = &ffi_type_double;
63 ts12b_type_elements[1] = &ffi_type_double;
64 ts12b_type_elements[2] = NULL;
65
66 ts12_type.size = 0;
67 ts12_type.alignment = 0;
68 ts12_type.type = FFI_TYPE_STRUCT;
69 ts12_type.elements = ts12_type_elements;
70 ts12_type_elements[0] = &ts12a_type;
71 ts12_type_elements[1] = &ts12b_type;
72 ts12_type_elements[2] = &ffi_type_uchar;
73 ts12_type_elements[3] = NULL;
74
75
76 args[0] = &ts12_type;
77 args[1] = &ffi_type_pointer;
78 values[0] = &ts12_arg;
79 values[1] = &ptr;
80
81 CHECK(ffi_prep_cif(&cif, ABI_NUM, 2, &ts12_type, args) == FFI_OK);
82
83 ts12_arg.s1.c = 5;
84 ts12_arg.s1.d = 5.55;
85 ts12_arg.c2 = 6;
86 ts12_arg.s[0] = 7.77;
87 ts12_arg.s[1] = 8.88;
88
89 printf ("%d\n", ts12_arg.s1.c);
90 printf ("%g\n", ts12_arg.s1.d);
91 printf ("%u\n", ts12_arg.c2);
92 printf ("%g\n", ts12_arg.s[0]);
93 printf ("%g\n", ts12_arg.s[1]);
94 printf ("%d\n", i);
95
96 ffi_call(&cif, FFI_FN(struct12), ts12_result, values);
97
98 printf ("%d\n", ts12_result->s1.c);
99 printf ("%g\n", ts12_result->s1.d);
100 printf ("%u\n", ts12_result->c2);
101 printf ("%g\n", ts12_result->s[0]);
102 printf ("%g\n", ts12_result->s[1]);
103 printf ("%d\n", i);
104 CHECK(ts12_result->s1.c == 5 + 1);
105 CHECK(ts12_result->s1.d == 5.55 + 1);
106 CHECK(ts12_result->c2 == 6 + 1);
107 CHECK(ts12_result->s[0] == 7.77 + 1);
108 CHECK(ts12_result->s[1] == 8.88 + 1);
109 CHECK(i == 5 + 1);
110 CHECK(ts12_type.size == sizeof(test_structure_12));
111
112 free (ts12_result);
113 exit(0);
114 }