Debian Codegraph @

testsuite/libffi.call/struct_return_2H.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 typedef struct
10 {
11 short x;
12 short y;
13 } test_structure_5;
14
15 static test_structure_5 ABI_ATTR struct5(test_structure_5 inp)
16 {
17 inp.x *= 2;
18 inp.y *= 3;
19
20 return inp;
21 }
22
23 int main (void)
24 {
25 ffi_cif cif;
26 ffi_type *args[MAX_ARGS];
27 void *values[MAX_ARGS];
28 ffi_type ts5_type;
29 ffi_type *ts5_type_elements[3];
30
31 test_structure_5 ts5_arg1;
32
33 /* This is a hack to get a properly aligned result buffer */
34 test_structure_5 *ts5_result =
35 (test_structure_5 *) malloc (sizeof(test_structure_5));
36
37 ts5_type.size = 0;
38 ts5_type.alignment = 0;
39 ts5_type.type = FFI_TYPE_STRUCT;
40 ts5_type.elements = ts5_type_elements;
41 ts5_type_elements[0] = &ffi_type_sshort;
42 ts5_type_elements[1] = &ffi_type_sshort;
43 ts5_type_elements[2] = NULL;
44
45 args[0] = &ts5_type;
46 values[0] = &ts5_arg1;
47
48 /* Initialize the cif */
49 CHECK(ffi_prep_cif(&cif, ABI_NUM, 1, &ts5_type, args) == FFI_OK);
50
51 ts5_arg1.x = 99;
52 ts5_arg1.y = 88;
53
54 ffi_call (&cif, FFI_FN(struct5), ts5_result, values);
55
56 CHECK(ts5_result->x == 99*2);
57 CHECK(ts5_result->y == 88*3);
58 CHECK(ts5_arg1.x == 99);
59 CHECK(ts5_arg1.y == 88);
60
61 free (ts5_result);
62 exit(0);
63 }