Ruby  2.7.2p137(2020-10-01revision5445e0435260b449decf2ac16f9d09bae3cafe72)
struct9.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 
10 typedef struct
11 {
12  float f;
13  int i;
15 
16 static test_structure_9 ABI_ATTR struct9 (test_structure_9 ts)
17 {
18  ts.f += 1;
19  ts.i += 1;
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 ts9_type;
30  ffi_type *ts9_type_elements[3];
31 
32  test_structure_9 ts9_arg;
33 
34  /* This is a hack to get a properly aligned result buffer */
35  test_structure_9 *ts9_result =
37 
38  ts9_type.size = 0;
39  ts9_type.alignment = 0;
40  ts9_type.type = FFI_TYPE_STRUCT;
41  ts9_type.elements = ts9_type_elements;
42  ts9_type_elements[0] = &ffi_type_float;
43  ts9_type_elements[1] = &ffi_type_sint;
44  ts9_type_elements[2] = NULL;
45 
46  args[0] = &ts9_type;
47  values[0] = &ts9_arg;
48 
49  /* Initialize the cif */
50  CHECK(ffi_prep_cif(&cif, ABI_NUM, 1, &ts9_type, args) == FFI_OK);
51 
52  ts9_arg.f = 5.55f;
53  ts9_arg.i = 5;
54 
55  printf ("%g\n", ts9_arg.f);
56  printf ("%d\n", ts9_arg.i);
57 
58  ffi_call(&cif, FFI_FN(struct9), ts9_result, values);
59 
60  printf ("%g\n", ts9_result->f);
61  printf ("%d\n", ts9_result->i);
62 
63  CHECK(ts9_result->f == 5.55f + 1);
64  CHECK(ts9_result->i == 5 + 1);
65 
66  free (ts9_result);
67  exit(0);
68 }
ABI_NUM
#define ABI_NUM
Definition: ffitest.h:35
NULL
#define NULL
Definition: _sdbm.c:101
ffitest.h
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
test_structure_9::f
float f
Definition: struct9.c:12
MAX_ARGS
#define MAX_ARGS
Definition: function.c:15
main
int main(void)
Definition: struct9.c:24
free
#define free(x)
Definition: dln.c:52
exit
void exit(int __status) __attribute__((__noreturn__))
test_structure_9
Definition: struct9.c:11
printf
int int int printf(const char *__restrict,...) __attribute__((__format__(__printf__
ffi_call
void ffi_call(ffi_cif *cif, void(*fn)(void), void *rvalue, void **avalue)
Definition: ffi.c:813
test_structure_9::i
int i
Definition: struct9.c:13
malloc
void * malloc(size_t) __attribute__((__malloc__)) __attribute__((__warn_unused_result__)) __attribute__((__alloc_size__(1)))