Ruby  2.7.1p83(2020-03-31revisiona0c7c23c9cec0d0ffcba012279cd652d28ad5bf3)
cls_pointer.c
Go to the documentation of this file.
1 /* Area: ffi_call, closure_call
2  Purpose: Check pointer arguments.
3  Limitations: none.
4  PR: none.
5  Originator: Blake Chaffin 6/6/2007 */
6 
7 /* { dg-do run { xfail strongarm*-*-* xscale*-*-* } } */
8 #include "ffitest.h"
9 
10 void* cls_pointer_fn(void* a1, void* a2)
11 {
12  void* result = (void*)((intptr_t)a1 + (intptr_t)a2);
13 
14  printf("0x%08x 0x%08x: 0x%08x\n",
15  (unsigned int)(uintptr_t) a1,
16  (unsigned int)(uintptr_t) a2,
17  (unsigned int)(uintptr_t) result);
18 
19  return result;
20 }
21 
22 static void
23 cls_pointer_gn(ffi_cif* cif __UNUSED__, void* resp,
24  void** args, void* userdata __UNUSED__)
25 {
26  void* a1 = *(void**)(args[0]);
27  void* a2 = *(void**)(args[1]);
28 
29  *(void**)resp = cls_pointer_fn(a1, a2);
30 }
31 
32 int main (void)
33 {
34  ffi_cif cif;
35  void *code;
36  ffi_closure* pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
37  void* args[3];
38  /* ffi_type cls_pointer_type; */
39  ffi_type* arg_types[3];
40 
41 /* cls_pointer_type.size = sizeof(void*);
42  cls_pointer_type.alignment = 0;
43  cls_pointer_type.type = FFI_TYPE_POINTER;
44  cls_pointer_type.elements = NULL;*/
45 
46  void* arg1 = (void*)0x12345678;
47  void* arg2 = (void*)0x89abcdef;
48  ffi_arg res = 0;
49 
50  arg_types[0] = &ffi_type_pointer;
51  arg_types[1] = &ffi_type_pointer;
52  arg_types[2] = NULL;
53 
54  CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &ffi_type_pointer,
55  arg_types) == FFI_OK);
56 
57  args[0] = &arg1;
58  args[1] = &arg2;
59  args[2] = NULL;
60 
61  ffi_call(&cif, FFI_FN(cls_pointer_fn), &res, args);
62  /* { dg-output "0x12345678 0x89abcdef: 0x9be02467" } */
63  printf("res: 0x%08x\n", (unsigned int) res);
64  /* { dg-output "\nres: 0x9be02467" } */
65 
66  CHECK(ffi_prep_closure_loc(pcl, &cif, cls_pointer_gn, NULL, code) == FFI_OK);
67 
68  res = (ffi_arg)(uintptr_t)((void*(*)(void*, void*))(code))(arg1, arg2);
69  /* { dg-output "\n0x12345678 0x89abcdef: 0x9be02467" } */
70  printf("res: 0x%08x\n", (unsigned int) res);
71  /* { dg-output "\nres: 0x9be02467" } */
72 
73  exit(0);
74 }
ffi_arg
unsigned long ffi_arg
Definition: ffitarget.h:30
uintptr_t
unsigned int uintptr_t
Definition: win32.h:106
NULL
#define NULL
Definition: _sdbm.c:101
ffitest.h
__UNUSED__
#define __UNUSED__
Definition: ffitest.h:28
CHECK
#define CHECK(sub)
Definition: compile.c:448
FFI_DEFAULT_ABI
@ FFI_DEFAULT_ABI
Definition: ffitarget.h:38
main
int main(void)
Definition: cls_pointer.c:32
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
cls_pointer_fn
void * cls_pointer_fn(void *a1, void *a2)
Definition: cls_pointer.c:10
exit
void exit(int __status) __attribute__((__noreturn__))
printf
int int int printf(const char *__restrict,...) __attribute__((__format__(__printf__
intptr_t
int intptr_t
Definition: win32.h:90
ffi_call
void ffi_call(ffi_cif *cif, void(*fn)(void), void *rvalue, void **avalue)
Definition: ffi.c:813
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