File: callback4.c

package info (click to toggle)
libffi 3.5.2-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 4,056 kB
  • sloc: ansic: 39,406; asm: 14,495; sh: 3,567; exp: 815; makefile: 357; python: 319; perl: 171; cpp: 134
file content (119 lines) | stat: -rw-r--r-- 2,980 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* Area:	ffi_call
   Purpose:	Check structures with array and callback.
   Limitations:	none.
   PR:		none.
   Originator:	David Tenty <daltenty@ibm.com>  */

/* { dg-do run } */
#include "ffitest.h"

static int i=5;

static void callback(void) { i++; }


typedef struct
{
  unsigned char c1;
  struct { double d; unsigned char c; } s[2];
  unsigned char c2;
} test_structure_12;

static test_structure_12 ABI_ATTR struct12 (test_structure_12 ts, void (*func)(void))
{
  ts.c1 += 1;
  ts.s[0].d += 1;
  ts.s[0].c += 1;
  ts.s[1].d += 1;
  ts.s[1].c += 1;
  ts.c2 += 1;

  func();
  return ts;
}

int main (void)
{
  ffi_cif cif;
  ffi_type *args[MAX_ARGS];
  void *values[MAX_ARGS];
  ffi_type ts12_type,ts12b_type, ts12a_type;
  ffi_type *ts12_type_elements[4];
  ffi_type *ts12b_type_elements[3];
  ffi_type *ts12a_type_elements[3];

  test_structure_12 ts12_arg;
  void (*ptr)(void)=&callback;

  test_structure_12 *ts12_result =
    (test_structure_12 *) malloc (sizeof(test_structure_12));

  ts12a_type.size = 0;
  ts12a_type.alignment = 0;
  ts12a_type.type = FFI_TYPE_STRUCT;
  ts12a_type.elements = ts12a_type_elements;
  ts12a_type_elements[0] = &ffi_type_double;
  ts12a_type_elements[1] = &ffi_type_uchar;
  ts12a_type_elements[2] = NULL;

  ts12b_type.size = 0;
  ts12b_type.alignment = 0;
  ts12b_type.type = FFI_TYPE_STRUCT;
  ts12b_type.elements = ts12b_type_elements;
  ts12b_type_elements[0] = &ts12a_type;
  ts12b_type_elements[1] = &ts12a_type;
  ts12b_type_elements[2] = NULL;

  ts12_type.size = 0;
  ts12_type.alignment = 0;
  ts12_type.type = FFI_TYPE_STRUCT;
  ts12_type.elements = ts12_type_elements;
  ts12_type_elements[0] = &ffi_type_uchar;
  ts12_type_elements[1] = &ts12b_type;
  ts12_type_elements[2] = &ffi_type_uchar;
  ts12_type_elements[3] = NULL;


  args[0] = &ts12_type;
  args[1] = &ffi_type_pointer;
  values[0] = &ts12_arg;
  values[1] = &ptr;

  CHECK(ffi_prep_cif(&cif, ABI_NUM, 2, &ts12_type, args) == FFI_OK);

  ts12_arg.c1 = 5;
  ts12_arg.s[0].d = 5.55;
  ts12_arg.s[0].c = 6;
  ts12_arg.s[1].d = 7.77;
  ts12_arg.s[1].c = 8;
  ts12_arg.c2 = 9;

  printf ("%u\n", ts12_arg.c1);
  printf ("%g\n", ts12_arg.s[0].d);
  printf ("%u\n", ts12_arg.s[0].c);
  printf ("%g\n", ts12_arg.s[1].d);
  printf ("%u\n", ts12_arg.s[1].c);
  printf ("%u\n", ts12_arg.c2);
  printf ("%d\n", i);

  ffi_call(&cif, FFI_FN(struct12), ts12_result, values);

  printf ("%u\n", ts12_result->c1);
  printf ("%g\n", ts12_result->s[0].d);
  printf ("%u\n", ts12_result->s[0].c);
  printf ("%g\n", ts12_result->s[1].d);
  printf ("%u\n", ts12_result->s[1].c);
  printf ("%u\n", ts12_result->c2);
  printf ("%d\n", i);
  CHECK(ts12_result->c1 == 5 + 1);
  CHECK(ts12_result->s[0].d == 5.55 + 1);
  CHECK(ts12_result->s[0].c == 6 + 1);
  CHECK(ts12_result->s[1].d == 7.77 + 1);
  CHECK(ts12_result->s[1].c == 8 + 1);
  CHECK(ts12_result->c2 == 9 + 1);
  CHECK(i == 5 + 1);
  CHECK(ts12_type.size == sizeof(test_structure_12));

  free (ts12_result);
  exit(0);
}