Update missing changes for 3.0.9r4.
[libffi.git] / testsuite / libffi.call / ffitest.h
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <fcntl.h>
5 #include <stdint.h>
6 #include <inttypes.h>
7 #include <ffi.h>
8 #include "fficonfig.h"
9
10 #define MAX_ARGS 256
11
12 #define CHECK(x) !(x) ? abort() : 0
13
14 /* Define __UNUSED__ that also other compilers than gcc can run the tests. */
15 #undef __UNUSED__
16 #if defined(__GNUC__)
17 #define __UNUSED__ __attribute__((__unused__))
18 #else
19 #define __UNUSED__
20 #endif
21
22 /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
23 file open. */
24 #ifdef HAVE_MMAP_ANON
25 # undef HAVE_MMAP_DEV_ZERO
26
27 # include <sys/mman.h>
28 # ifndef MAP_FAILED
29 # define MAP_FAILED -1
30 # endif
31 # if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
32 # define MAP_ANONYMOUS MAP_ANON
33 # endif
34 # define USING_MMAP
35
36 #endif
37
38 #ifdef HAVE_MMAP_DEV_ZERO
39
40 # include <sys/mman.h>
41 # ifndef MAP_FAILED
42 # define MAP_FAILED -1
43 # endif
44 # define USING_MMAP
45
46 #endif
47
48 /* MinGW kludge. */
49 #ifdef WIN64
50 #define PRIdLL "PRId64"
51 #define PRIuLL "PRIu64"
52 #else
53 #define PRIdLL "lld"
54 #define PRIuLL "llu"
55 #endif
56
57 /* PA HP-UX kludge. */
58 #if defined(__hppa__) && defined(__hpux__) && !defined(PRIuPTR)
59 #define PRIuPTR "lu"
60 #endif
61
62 /* Solaris < 10 kludge. */
63 #if defined(__sun__) && defined(__svr4__) && !defined(PRIuPTR)
64 #if defined(__arch64__) || defined (__x86_64__)
65 #define PRIuPTR "lu"
66 #else
67 #define PRIuPTR "u"
68 #endif
69 #endif
70
71 #ifdef USING_MMAP
72 static inline void *
73 allocate_mmap (size_t size)
74 {
75 void *page;
76 #if defined (HAVE_MMAP_DEV_ZERO)
77 static int dev_zero_fd = -1;
78 #endif
79
80 #ifdef HAVE_MMAP_DEV_ZERO
81 if (dev_zero_fd == -1)
82 {
83 dev_zero_fd = open ("/dev/zero", O_RDONLY);
84 if (dev_zero_fd == -1)
85 {
86 perror ("open /dev/zero: %m");
87 exit (1);
88 }
89 }
90 #endif
91
92
93 #ifdef HAVE_MMAP_ANON
94 page = mmap (NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC,
95 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
96 #endif
97 #ifdef HAVE_MMAP_DEV_ZERO
98 page = mmap (NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC,
99 MAP_PRIVATE, dev_zero_fd, 0);
100 #endif
101
102 if (page == (void *) MAP_FAILED)
103 {
104 perror ("virtual memory exhausted");
105 exit (1);
106 }
107
108 return page;
109 }
110
111 #endif