Ruby  2.7.2p137(2020-10-01revision5445e0435260b449decf2ac16f9d09bae3cafe72)
struct1.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  unsigned char uc;
13  double d;
14  unsigned int ui;
16 
18 {
19  ts.uc++;
20  ts.d--;
21  ts.ui++;
22 
23  return ts;
24 }
25 
26 int main (void)
27 {
28  ffi_cif cif;
29  ffi_type *args[MAX_ARGS];
30  void *values[MAX_ARGS];
31  ffi_type ts1_type;
32  ffi_type *ts1_type_elements[4];
33 
34  test_structure_1 ts1_arg;
35 
36  /* This is a hack to get a properly aligned result buffer */
37  test_structure_1 *ts1_result =
39 
40  ts1_type.size = 0;
41  ts1_type.alignment = 0;
42  ts1_type.type = FFI_TYPE_STRUCT;
43  ts1_type.elements = ts1_type_elements;
44  ts1_type_elements[0] = &ffi_type_uchar;
45  ts1_type_elements[1] = &ffi_type_double;
46  ts1_type_elements[2] = &ffi_type_uint;
47  ts1_type_elements[3] = NULL;
48 
49  args[0] = &ts1_type;
50  values[0] = &ts1_arg;
51 
52  /* Initialize the cif */
53  CHECK(ffi_prep_cif(&cif, ABI_NUM, 1,
54  &ts1_type, args) == FFI_OK);
55 
56  ts1_arg.uc = '\x01';
57  ts1_arg.d = 3.14159;
58  ts1_arg.ui = 555;
59 
60  ffi_call(&cif, FFI_FN(struct1), ts1_result, values);
61 
62  CHECK(ts1_result->ui == 556);
63  CHECK(ts1_result->d == 3.14159 - 1);
64 
65  free (ts1_result);
66  exit(0);
67 }
test_structure_1
Definition: struct1.c:11
main
int main(void)
Definition: struct1.c:26
test_structure_1::d
double d
Definition: struct1.c:13
test_structure_1::ui
unsigned int ui
Definition: struct1.c:14
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
test_structure_1::uc
unsigned char uc
Definition: struct1.c:12
ffi_type_uchar
#define ffi_type_uchar
Definition: fiddle.h:55
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
exit
void exit(int __status) __attribute__((__noreturn__))
ffi_call
void ffi_call(ffi_cif *cif, void(*fn)(void), void *rvalue, void **avalue)
Definition: ffi.c:813
malloc
void * malloc(size_t) __attribute__((__malloc__)) __attribute__((__warn_unused_result__)) __attribute__((__alloc_size__(1)))