Ruby  2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)
struct7.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  double d;
15 
16 static test_structure_7 ABI_ATTR struct7 (test_structure_7 ts)
17 {
18  ts.f1 += 1;
19  ts.f2 += 1;
20  ts.d += 1;
21 
22  return ts;
23 }
24 
25 int main (void)
26 {
27  ffi_cif cif;
28  ffi_type *args[MAX_ARGS];
29  void *values[MAX_ARGS];
30  ffi_type ts7_type;
31  ffi_type *ts7_type_elements[4];
32 
33  test_structure_7 ts7_arg;
34 
35  /* This is a hack to get a properly aligned result buffer */
36  test_structure_7 *ts7_result =
38 
39  ts7_type.size = 0;
40  ts7_type.alignment = 0;
41  ts7_type.type = FFI_TYPE_STRUCT;
42  ts7_type.elements = ts7_type_elements;
43  ts7_type_elements[0] = &ffi_type_float;
44  ts7_type_elements[1] = &ffi_type_float;
45  ts7_type_elements[2] = &ffi_type_double;
46  ts7_type_elements[3] = NULL;
47 
48  args[0] = &ts7_type;
49  values[0] = &ts7_arg;
50 
51  /* Initialize the cif */
52  CHECK(ffi_prep_cif(&cif, ABI_NUM, 1, &ts7_type, args) == FFI_OK);
53 
54  ts7_arg.f1 = 5.55f;
55  ts7_arg.f2 = 55.5f;
56  ts7_arg.d = 6.66;
57 
58  printf ("%g\n", ts7_arg.f1);
59  printf ("%g\n", ts7_arg.f2);
60  printf ("%g\n", ts7_arg.d);
61 
62  ffi_call(&cif, FFI_FN(struct7), ts7_result, values);
63 
64  printf ("%g\n", ts7_result->f1);
65  printf ("%g\n", ts7_result->f2);
66  printf ("%g\n", ts7_result->d);
67 
68  CHECK(ts7_result->f1 == 5.55f + 1);
69  CHECK(ts7_result->f2 == 55.5f + 1);
70  CHECK(ts7_result->d == 6.66 + 1);
71 
72  free (ts7_result);
73  exit(0);
74 }
test_structure_7::d
double d
Definition: struct7.c:13
main
int main(void)
Definition: struct7.c:25
malloc
void * malloc(size_t) __attribute__((__malloc__)) __attribute__((__warn_unused_result__)) __attribute__((__alloc_size__(1)))
ABI_NUM
#define ABI_NUM
Definition: ffitest.h:35
printf
int int int printf(const char *__restrict,...) __attribute__((__format__(__printf__
NULL
#define NULL
Definition: _sdbm.c:101
exit
void exit(int __status) __attribute__((__noreturn__))
test_structure_7::f2
float f2
Definition: struct7.c:12
ffitest.h
test_structure_7::f1
float f1
Definition: struct7.c:11
test_structure_7
Definition: struct7.c:9
CHECK
#define CHECK(sub)
Definition: compile.c:448
ABI_ATTR
#define ABI_ATTR
Definition: ffitest.h:36
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
MAX_ARGS
#define MAX_ARGS
Definition: function.c:15
free
#define free(x)
Definition: dln.c:52
ffi_call
void ffi_call(ffi_cif *cif, void(*fn)(void), void *rvalue, void **avalue)
Definition: ffi.c:813