Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

closure_simple.c
Go to the documentation of this file.
1 /* Area: closure_call
2 Purpose: Check simple closure handling with all ABIs
3 Limitations: none.
4 PR: none.
5 Originator: <twalljava@dev.java.net> */
6
7 /* { dg-do run } */
8 #include "ffitest.h"
9
10 static void
11 closure_test(ffi_cif* cif __UNUSED__, void* resp, void** args, void* userdata)
12 {
13 *(ffi_arg*)resp =
14 (int)*(int *)args[0] + (int)(*(int *)args[1])
15 + (int)(*(int *)args[2]) + (int)(*(int *)args[3])
16 + (int)(intptr_t)userdata;
17
18 printf("%d %d %d %d: %d\n",
19 (int)*(int *)args[0], (int)(*(int *)args[1]),
20 (int)(*(int *)args[2]), (int)(*(int *)args[3]),
21 (int)*(ffi_arg *)resp);
22
23 }
24
25 typedef int (ABI_ATTR *closure_test_type0)(int, int, int, int);
26
27 int main (void)
28 {
29 ffi_cif cif;
30 void *code;
31 ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
32 ffi_type * cl_arg_types[17];
33 int res;
34
35 cl_arg_types[0] = &ffi_type_uint;
36 cl_arg_types[1] = &ffi_type_uint;
37 cl_arg_types[2] = &ffi_type_uint;
38 cl_arg_types[3] = &ffi_type_uint;
39 cl_arg_types[4] = NULL;
40
41 /* Initialize the cif */
42 CHECK(ffi_prep_cif(&cif, ABI_NUM, 4,
43 &ffi_type_sint, cl_arg_types) == FFI_OK);
44
45 CHECK(ffi_prep_closure_loc(pcl, &cif, closure_test,
46 (void *) 3 /* userdata */, code) == FFI_OK);
47
48 res = (*(closure_test_type0)code)(0, 1, 2, 3);
49 /* { dg-output "0 1 2 3: 9" } */
50
51 printf("res: %d\n",res);
52 /* { dg-output "\nres: 9" } */
53
54 exit(0);
55 }
ffi_arg
unsigned long ffi_arg
Definition: ffitarget.h:30
int
__inline__ int
Definition: rb_mjit_min_header-2.7.0.h:2839
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__))
ffitest.h
closure_test_type0
int(ABI_ATTR * closure_test_type0)(int, int, int, int)
Definition: closure_simple.c:25
__UNUSED__
#define __UNUSED__
Definition: ffitest.h:28
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
intptr_t
int intptr_t
Definition: win32.h:90
ffi_prep_closure_loc
ffi_status ffi_prep_closure_loc(ffi_closure *closure, ffi_cif *cif, void(*fun)(ffi_cif *, void *, void **, void *), void *user_data, void *codeloc)
Definition: ffi.c:928
main
int main(void)
Definition: closure_simple.c:27