Initial commit
[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 #ifdef USING_MMAP
58 static inline void *
59 allocate_mmap (size_t size)
60 {
61 void *page;
62 #if defined (HAVE_MMAP_DEV_ZERO)
63 static int dev_zero_fd = -1;
64 #endif
65
66 #ifdef HAVE_MMAP_DEV_ZERO
67 if (dev_zero_fd == -1)
68 {
69 dev_zero_fd = open ("/dev/zero", O_RDONLY);
70 if (dev_zero_fd == -1)
71 {
72 perror ("open /dev/zero: %m");
73 exit (1);
74 }
75 }
76 #endif
77
78
79 #ifdef HAVE_MMAP_ANON
80 page = mmap (NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC,
81 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
82 #endif
83 #ifdef HAVE_MMAP_DEV_ZERO
84 page = mmap (NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC,
85 MAP_PRIVATE, dev_zero_fd, 0);
86 #endif
87
88 if (page == (void *) MAP_FAILED)
89 {
90 perror ("virtual memory exhausted");
91 exit (1);
92 }
93
94 return page;
95 }
96
97 #endif