• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <fcntl.h>
4 #include <ffi.h>
5 #include "fficonfig.h"
6 
7 #define MAX_ARGS 256
8 
9 
10 /* Define __UNUSED__ that also other compilers than gcc can run the tests.  */
11 #undef __UNUSED__
12 #if defined(__GNUC__)
13 #define __UNUSED__ __attribute__((__unused__))
14 #else
15 #define __UNUSED__
16 #endif
17 
18 #define CHECK(x) (!(x) ? abort() : (void)0)
19 
20 /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
21    file open.  */
22 #ifdef HAVE_MMAP_ANON
23 # undef HAVE_MMAP_DEV_ZERO
24 
25 # include <sys/mman.h>
26 # ifndef MAP_FAILED
27 #  define MAP_FAILED -1
28 # endif
29 # if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
30 #  define MAP_ANONYMOUS MAP_ANON
31 # endif
32 # define USING_MMAP
33 
34 #endif
35 
36 #ifdef HAVE_MMAP_DEV_ZERO
37 
38 # include <sys/mman.h>
39 # ifndef MAP_FAILED
40 #  define MAP_FAILED -1
41 # endif
42 # define USING_MMAP
43 
44 #endif
45 
46 #ifdef USING_MMAP
47 static inline void *
allocate_mmap(size_t size)48 allocate_mmap (size_t size)
49 {
50   void *page;
51 #if defined (HAVE_MMAP_DEV_ZERO)
52   static int dev_zero_fd = -1;
53 #endif
54 
55 #ifdef HAVE_MMAP_DEV_ZERO
56   if (dev_zero_fd == -1)
57     {
58       dev_zero_fd = open ("/dev/zero", O_RDONLY);
59       if (dev_zero_fd == -1)
60 	{
61 	  perror ("open /dev/zero: %m");
62 	  exit (1);
63 	}
64     }
65 #endif
66 
67 
68 #ifdef HAVE_MMAP_ANON
69   page = mmap (NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC,
70 	       MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
71 #endif
72 #ifdef HAVE_MMAP_DEV_ZERO
73   page = mmap (NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC,
74 	       MAP_PRIVATE, dev_zero_fd, 0);
75 #endif
76 
77   if (page == MAP_FAILED)
78     {
79       perror ("virtual memory exhausted");
80       exit (1);
81     }
82 
83   return page;
84 }
85 
86 #endif
87