Debian Codegraph @

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