Ruby: ext/fiddle/libffi-3.2.1/testsuite/libffi.call/struct8.c Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

struct8.c
Go to the documentation of this file.
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 float f1;
12 float f2;
13 float f3;
14 float f4;
15} test_structure_8;
16
17static test_structure_8 ABI_ATTR struct8 (test_structure_8 ts)
18{
19 ts.f1 += 1;
20 ts.f2 += 1;
21 ts.f3 += 1;
22 ts.f4 += 1;
23
24 return ts;
25}
26
27 int main (void)
28{
29 ffi_cif cif;
30 ffi_type *args[MAX_ARGS];
31 void *values[MAX_ARGS];
32 ffi_type ts8_type;
33 ffi_type *ts8_type_elements[5];
34
35 test_structure_8 ts8_arg;
36
37 /* This is a hack to get a properly aligned result buffer */
38 test_structure_8 *ts8_result =
39 (test_structure_8 *) malloc (sizeof(test_structure_8));
40
41 ts8_type.size = 0;
42 ts8_type.alignment = 0;
43 ts8_type.type = FFI_TYPE_STRUCT;
44 ts8_type.elements = ts8_type_elements;
45 ts8_type_elements[0] = &ffi_type_float;
46 ts8_type_elements[1] = &ffi_type_float;
47 ts8_type_elements[2] = &ffi_type_float;
48 ts8_type_elements[3] = &ffi_type_float;
49 ts8_type_elements[4] = NULL;
50
51 args[0] = &ts8_type;
52 values[0] = &ts8_arg;
53
54 /* Initialize the cif */
55 CHECK(ffi_prep_cif(&cif, ABI_NUM, 1, &ts8_type, args) == FFI_OK);
56
57 ts8_arg.f1 = 5.55f;
58 ts8_arg.f2 = 55.5f;
59 ts8_arg.f3 = -5.55f;
60 ts8_arg.f4 = -55.5f;
61
62 printf ("%g\n", ts8_arg.f1);
63 printf ("%g\n", ts8_arg.f2);
64 printf ("%g\n", ts8_arg.f3);
65 printf ("%g\n", ts8_arg.f4);
66
67 ffi_call(&cif, FFI_FN(struct8), ts8_result, values);
68
69 printf ("%g\n", ts8_result->f1);
70 printf ("%g\n", ts8_result->f2);
71 printf ("%g\n", ts8_result->f3);
72 printf ("%g\n", ts8_result->f4);
73
74 CHECK(ts8_result->f1 == 5.55f + 1);
75 CHECK(ts8_result->f2 == 55.5f + 1);
76 CHECK(ts8_result->f3 == -5.55f + 1);
77 CHECK(ts8_result->f4 == -55.5f + 1);
78
79 free (ts8_result);
80 exit(0);
81}
ffi_call
void ffi_call(ffi_cif *cif, void(*fn)(void), void *rvalue, void **avalue)
Definition: ffi.c:813
CHECK
#define CHECK(sub)
Definition: compile.c:429
free
#define free(x)
Definition: dln.c:52
ffitest.h
ABI_NUM
#define ABI_NUM
Definition: ffitest.h:35
ABI_ATTR
#define ABI_ATTR
Definition: ffitest.h:36
MAX_ARGS
#define MAX_ARGS
Definition: function.c:17
ffi_prep_cif
ffi_status ffi_prep_cif(ffi_cif *cif, ffi_abi abi, unsigned int nargs, ffi_type *rtype, ffi_type **atypes)
Definition: prep_cif.c:226
NULL
#define NULL
Definition: regenc.h:69
malloc
#define malloc
Definition: st.c:170
main
int main(void)
Definition: struct8.c:27
test_structure_8
Definition: struct8.c:10
test_structure_8::f1
float f1
Definition: struct8.c:11
test_structure_8::f3
float f3
Definition: struct8.c:13
test_structure_8::f4
float f4
Definition: struct8.c:14
test_structure_8::f2
float f2
Definition: struct8.c:12