Debian Codegraph @

testsuite/libffi.call/struct_by_value_3.c

raw
1 /* Area: ffi_call
2 Purpose: Check structures.
3 Limitations: none.
4 PR: none.
5 Originator: From the original ffitest.c */
6
7 /* { dg-do run } */
8 #include "ffitest.h"
9
10 typedef struct
11 {
12 unsigned int ui01;
13 unsigned int ui02;
14 unsigned int ui03;
15 } test_structure_1;
16
17 static test_structure_1 ABI_ATTR struct1(test_structure_1 ts)
18 {
19 ts.ui03++;
20
21 return ts;
22 }
23
24 int main (void)
25 {
26 ffi_cif cif;
27 ffi_type *args[MAX_ARGS];
28 void *values[MAX_ARGS];
29 ffi_type ts1_type;
30 ffi_type *ts1_type_elements[4];
31
32 test_structure_1 ts1_arg;
33
34 /* This is a hack to get a properly aligned result buffer */
35 test_structure_1 *ts1_result =
36 (test_structure_1 *) malloc (sizeof(test_structure_1));
37
38 ts1_type.size = 0;
39 ts1_type.alignment = 0;
40 ts1_type.type = FFI_TYPE_STRUCT;
41 ts1_type.elements = ts1_type_elements;
42 ts1_type_elements[0] = &ffi_type_uint;
43 ts1_type_elements[1] = &ffi_type_uint;
44 ts1_type_elements[2] = &ffi_type_uint;
45 ts1_type_elements[3] = NULL;
46
47 args[0] = &ts1_type;
48 values[0] = &ts1_arg;
49
50 /* Initialize the cif */
51 CHECK(ffi_prep_cif(&cif, ABI_NUM, 1,
52 &ts1_type, args) == FFI_OK);
53
54 ts1_arg.ui03 = 555;
55
56 ffi_call(&cif, FFI_FN(struct1), ts1_result, values);
57
58 CHECK(ts1_result->ui03 == 556);
59
60 /* This will fail if ffi_call isn't passing the struct by value. */
61 CHECK(ts1_arg.ui03 == 555);
62
63 free (ts1_result);
64 exit(0);
65 }