1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <fcntl.h>
5 #include <ffi.h>
6 #include "fficonfig.h"
7 
8 #if defined HAVE_STDINT_H
9 #include <stdint.h>
10 #endif
11 
12 #if defined HAVE_INTTYPES_H
13 #include <inttypes.h>
14 #endif
15 
16 #define MAX_ARGS 256
17 
18 #define CHECK(x) (void)(!(x) ? (abort(), 1) : 0)
19 
20 /* Define macros so that compilers other than gcc can run the tests.  */
21 #undef __UNUSED__
22 #if defined(__GNUC__)
23 #define __UNUSED__ __attribute__((__unused__))
24 #define __STDCALL__ __attribute__((stdcall))
25 #define __THISCALL__ __attribute__((thiscall))
26 #define __FASTCALL__ __attribute__((fastcall))
27 #define __MSABI__ __attribute__((ms_abi))
28 #else
29 #define __UNUSED__
30 #define __STDCALL__ __stdcall
31 #define __THISCALL__ __thiscall
32 #define __FASTCALL__ __fastcall
33 #endif
34 
35 #ifndef ABI_NUM
36 #define ABI_NUM FFI_DEFAULT_ABI
37 #define ABI_ATTR
38 #endif
39 
40 /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
41    file open.  */
42 #ifdef HAVE_MMAP_ANON
43 # undef HAVE_MMAP_DEV_ZERO
44 
45 # include <sys/mman.h>
46 # ifndef MAP_FAILED
47 #  define MAP_FAILED -1
48 # endif
49 # if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
50 #  define MAP_ANONYMOUS MAP_ANON
51 # endif
52 # define USING_MMAP
53 
54 #endif
55 
56 #ifdef HAVE_MMAP_DEV_ZERO
57 
58 # include <sys/mman.h>
59 # ifndef MAP_FAILED
60 #  define MAP_FAILED -1
61 # endif
62 # define USING_MMAP
63 
64 #endif
65 
66 /* MinGW kludge.  */
67 #if defined(_WIN64) | defined(_WIN32)
68 #define PRIdLL "I64d"
69 #define PRIuLL "I64u"
70 #else
71 #define PRIdLL "lld"
72 #define PRIuLL "llu"
73 #endif
74 
75 /* Tru64 UNIX kludge.  */
76 #if defined(__alpha__) && defined(__osf__)
77 /* Tru64 UNIX V4.0 doesn't support %lld/%lld, but long is 64-bit.  */
78 #undef PRIdLL
79 #define PRIdLL "ld"
80 #undef PRIuLL
81 #define PRIuLL "lu"
82 #define PRId8 "hd"
83 #define PRIu8 "hu"
84 #define PRId64 "ld"
85 #define PRIu64 "lu"
86 #define PRIuPTR "lu"
87 #endif
88 
89 /* PA HP-UX kludge.  */
90 #if defined(__hppa__) && defined(__hpux__) && !defined(PRIuPTR)
91 #define PRIuPTR "lu"
92 #endif
93 
94 /* IRIX kludge.  */
95 #if defined(__sgi)
96 /* IRIX 6.5 <inttypes.h> provides all definitions, but only for C99
97    compilations.  */
98 #define PRId8 "hhd"
99 #define PRIu8 "hhu"
100 #if (_MIPS_SZLONG == 32)
101 #define PRId64 "lld"
102 #define PRIu64 "llu"
103 #endif
104 /* This doesn't match <inttypes.h>, which always has "lld" here, but the
105    arguments are uint64_t, int64_t, which are unsigned long, long for
106    64-bit in <sgidefs.h>.  */
107 #if (_MIPS_SZLONG == 64)
108 #define PRId64 "ld"
109 #define PRIu64 "lu"
110 #endif
111 /* This doesn't match <inttypes.h>, which has "u" here, but the arguments
112    are uintptr_t, which is always unsigned long.  */
113 #define PRIuPTR "lu"
114 #endif
115 
116 /* Solaris < 10 kludge.  */
117 #if defined(__sun__) && defined(__svr4__) && !defined(PRIuPTR)
118 #if defined(__arch64__) || defined (__x86_64__)
119 #define PRIuPTR "lu"
120 #else
121 #define PRIuPTR "u"
122 #endif
123 #endif
124 
125 /* MSVC kludge.  */
126 #if defined _MSC_VER
127 #if !defined(__cplusplus) || defined(__STDC_FORMAT_MACROS)
128 #define PRIuPTR "lu"
129 #define PRIu8 "u"
130 #define PRId8 "d"
131 #define PRIu64 "I64u"
132 #define PRId64 "I64d"
133 #endif
134 #endif
135 
136 #ifndef PRIuPTR
137 #define PRIuPTR "u"
138 #endif
139