Fix a bug in cfg fixup
[official-gcc.git] / libffi / testsuite / libffi.call / ffitest.h
blob0e95e164f6e3003d938ab7cc09e49a32ffc9dbd0
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <fcntl.h>
5 #include <ffi.h>
6 #include "fficonfig.h"
8 #if defined HAVE_STDINT_H
9 #include <stdint.h>
10 #endif
12 #if defined HAVE_INTTYPES_H
13 #include <inttypes.h>
14 #endif
16 #define MAX_ARGS 256
18 #define CHECK(x) !(x) ? abort() : 0
20 /* Define __UNUSED__ that also other compilers than gcc can run the tests. */
21 #undef __UNUSED__
22 #if defined(__GNUC__)
23 #define __UNUSED__ __attribute__((__unused__))
24 #else
25 #define __UNUSED__
26 #endif
28 /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
29 file open. */
30 #ifdef HAVE_MMAP_ANON
31 # undef HAVE_MMAP_DEV_ZERO
33 # include <sys/mman.h>
34 # ifndef MAP_FAILED
35 # define MAP_FAILED -1
36 # endif
37 # if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
38 # define MAP_ANONYMOUS MAP_ANON
39 # endif
40 # define USING_MMAP
42 #endif
44 #ifdef HAVE_MMAP_DEV_ZERO
46 # include <sys/mman.h>
47 # ifndef MAP_FAILED
48 # define MAP_FAILED -1
49 # endif
50 # define USING_MMAP
52 #endif
54 /* MinGW kludge. */
55 #ifdef _WIN64
56 #define PRIdLL "I64d"
57 #define PRIuLL "I64u"
58 #else
59 #define PRIdLL "lld"
60 #define PRIuLL "llu"
61 #endif
63 /* Tru64 UNIX kludge. */
64 #if defined(__alpha__) && defined(__osf__)
65 /* Tru64 UNIX V4.0 doesn't support %lld/%lld, but long is 64-bit. */
66 #undef PRIdLL
67 #define PRIdLL "ld"
68 #undef PRIuLL
69 #define PRIuLL "lu"
70 #define PRId8 "hd"
71 #define PRIu8 "hu"
72 #define PRId64 "ld"
73 #define PRIu64 "lu"
74 #define PRIuPTR "lu"
75 #endif
77 /* PA HP-UX kludge. */
78 #if defined(__hppa__) && defined(__hpux__) && !defined(PRIuPTR)
79 #define PRIuPTR "lu"
80 #endif
82 /* IRIX kludge. */
83 #if defined(__sgi)
84 /* IRIX 6.5 <inttypes.h> provides all definitions, but only for C99
85 compilations. */
86 #define PRId8 "hhd"
87 #define PRIu8 "hhu"
88 #if (_MIPS_SZLONG == 32)
89 #define PRId64 "lld"
90 #define PRIu64 "llu"
91 #endif
92 /* This doesn't match <inttypes.h>, which always has "lld" here, but the
93 arguments are uint64_t, int64_t, which are unsigned long, long for
94 64-bit in <sgidefs.h>. */
95 #if (_MIPS_SZLONG == 64)
96 #define PRId64 "ld"
97 #define PRIu64 "lu"
98 #endif
99 /* This doesn't match <inttypes.h>, which has "u" here, but the arguments
100 are uintptr_t, which is always unsigned long. */
101 #define PRIuPTR "lu"
102 #endif
104 /* Solaris < 10 kludge. */
105 #if defined(__sun__) && defined(__svr4__) && !defined(PRIuPTR)
106 #if defined(__arch64__) || defined (__x86_64__)
107 #define PRIuPTR "lu"
108 #else
109 #define PRIuPTR "u"
110 #endif
111 #endif
113 #ifdef USING_MMAP
114 static inline void *
115 allocate_mmap (size_t size)
117 void *page;
118 #if defined (HAVE_MMAP_DEV_ZERO)
119 static int dev_zero_fd = -1;
120 #endif
122 #ifdef HAVE_MMAP_DEV_ZERO
123 if (dev_zero_fd == -1)
125 dev_zero_fd = open ("/dev/zero", O_RDONLY);
126 if (dev_zero_fd == -1)
128 perror ("open /dev/zero: %m");
129 exit (1);
132 #endif
135 #ifdef HAVE_MMAP_ANON
136 page = mmap (NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC,
137 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
138 #endif
139 #ifdef HAVE_MMAP_DEV_ZERO
140 page = mmap (NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC,
141 MAP_PRIVATE, dev_zero_fd, 0);
142 #endif
144 if (page == (void *) MAP_FAILED)
146 perror ("virtual memory exhausted");
147 exit (1);
150 return page;
153 #endif