ยปCore Development>Code coverage>Modules/_ctypes/libffi/testsuite/libffi.call/problem1.c

Python code coverage for Modules/_ctypes/libffi/testsuite/libffi.call/problem1.c

#countcontent
1n/a/* Area: ffi_call, closure_call
2n/a Purpose: Check structure passing with different structure size.
3n/a Limitations: none.
4n/a PR: none.
5n/a Originator: <andreast@gcc.gnu.org> 20030828 */
6n/a
7n/a/* { dg-do run } */
8n/a#include "ffitest.h"
9n/a
10n/atypedef struct my_ffi_struct {
11n/a double a;
12n/a double b;
13n/a double c;
14n/a} my_ffi_struct;
15n/a
16n/amy_ffi_struct callee(struct my_ffi_struct a1, struct my_ffi_struct a2)
17n/a{
18n/a struct my_ffi_struct result;
19n/a result.a = a1.a + a2.a;
20n/a result.b = a1.b + a2.b;
21n/a result.c = a1.c + a2.c;
22n/a
23n/a
24n/a printf("%g %g %g %g %g %g: %g %g %g\n", a1.a, a1.b, a1.c,
25n/a a2.a, a2.b, a2.c, result.a, result.b, result.c);
26n/a
27n/a return result;
28n/a}
29n/a
30n/avoid stub(ffi_cif* cif __UNUSED__, void* resp, void** args,
31n/a void* userdata __UNUSED__)
32n/a{
33n/a struct my_ffi_struct a1;
34n/a struct my_ffi_struct a2;
35n/a
36n/a a1 = *(struct my_ffi_struct*)(args[0]);
37n/a a2 = *(struct my_ffi_struct*)(args[1]);
38n/a
39n/a *(my_ffi_struct *)resp = callee(a1, a2);
40n/a}
41n/a
42n/a
43n/aint main(void)
44n/a{
45n/a ffi_type* my_ffi_struct_fields[4];
46n/a ffi_type my_ffi_struct_type;
47n/a ffi_cif cif;
48n/a void *code;
49n/a ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
50n/a void* args[4];
51n/a ffi_type* arg_types[3];
52n/a
53n/a struct my_ffi_struct g = { 1.0, 2.0, 3.0 };
54n/a struct my_ffi_struct f = { 1.0, 2.0, 3.0 };
55n/a struct my_ffi_struct res;
56n/a
57n/a my_ffi_struct_type.size = 0;
58n/a my_ffi_struct_type.alignment = 0;
59n/a my_ffi_struct_type.type = FFI_TYPE_STRUCT;
60n/a my_ffi_struct_type.elements = my_ffi_struct_fields;
61n/a
62n/a my_ffi_struct_fields[0] = &ffi_type_double;
63n/a my_ffi_struct_fields[1] = &ffi_type_double;
64n/a my_ffi_struct_fields[2] = &ffi_type_double;
65n/a my_ffi_struct_fields[3] = NULL;
66n/a
67n/a arg_types[0] = &my_ffi_struct_type;
68n/a arg_types[1] = &my_ffi_struct_type;
69n/a arg_types[2] = NULL;
70n/a
71n/a CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &my_ffi_struct_type,
72n/a arg_types) == FFI_OK);
73n/a
74n/a args[0] = &g;
75n/a args[1] = &f;
76n/a args[2] = NULL;
77n/a ffi_call(&cif, FFI_FN(callee), &res, args);
78n/a /* { dg-output "1 2 3 1 2 3: 2 4 6" } */
79n/a printf("res: %g %g %g\n", res.a, res.b, res.c);
80n/a /* { dg-output "\nres: 2 4 6" } */
81n/a
82n/a CHECK(ffi_prep_closure_loc(pcl, &cif, stub, NULL, code) == FFI_OK);
83n/a
84n/a res = ((my_ffi_struct(*)(struct my_ffi_struct, struct my_ffi_struct))(code))(g, f);
85n/a /* { dg-output "\n1 2 3 1 2 3: 2 4 6" } */
86n/a printf("res: %g %g %g\n", res.a, res.b, res.c);
87n/a /* { dg-output "\nres: 2 4 6" } */
88n/a
89n/a exit(0);;
90n/a}