Back to index

python3.2  3.2.2
ffitest.h
Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <fcntl.h>
00005 #include <ffi.h>
00006 #include "fficonfig.h"
00007 
00008 #if defined HAVE_STDINT_H
00009 #include <stdint.h>
00010 #endif
00011 
00012 #if defined HAVE_INTTYPES_H
00013 #include <inttypes.h>
00014 #endif
00015 
00016 #define MAX_ARGS 256
00017 
00018 #define CHECK(x) !(x) ? abort() : 0
00019 
00020 /* Define __UNUSED__ that also other compilers than gcc can run the tests.  */
00021 #undef __UNUSED__
00022 #if defined(__GNUC__)
00023 #define __UNUSED__ __attribute__((__unused__))
00024 #else
00025 #define __UNUSED__
00026 #endif
00027 
00028 /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
00029    file open.  */
00030 #ifdef HAVE_MMAP_ANON
00031 # undef HAVE_MMAP_DEV_ZERO
00032 
00033 # include <sys/mman.h>
00034 # ifndef MAP_FAILED
00035 #  define MAP_FAILED -1
00036 # endif
00037 # if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
00038 #  define MAP_ANONYMOUS MAP_ANON
00039 # endif
00040 # define USING_MMAP
00041 
00042 #endif
00043 
00044 #ifdef HAVE_MMAP_DEV_ZERO
00045 
00046 # include <sys/mman.h>
00047 # ifndef MAP_FAILED
00048 #  define MAP_FAILED -1
00049 # endif
00050 # define USING_MMAP
00051 
00052 #endif
00053 
00054 /* MinGW kludge.  */
00055 #ifdef _WIN64
00056 #define PRIdLL "I64d"
00057 #define PRIuLL "I64u"
00058 #else
00059 #define PRIdLL "lld"
00060 #define PRIuLL "llu"
00061 #endif
00062 
00063 /* Tru64 UNIX kludge.  */
00064 #if defined(__alpha__) && defined(__osf__)
00065 /* Tru64 UNIX V4.0 doesn't support %lld/%lld, but long is 64-bit.  */
00066 #undef PRIdLL
00067 #define PRIdLL "ld"
00068 #undef PRIuLL
00069 #define PRIuLL "lu"
00070 #define PRId64 "ld"
00071 #define PRIu64 "lu"
00072 #define PRIuPTR "lu"
00073 #endif
00074 
00075 /* PA HP-UX kludge.  */
00076 #if defined(__hppa__) && defined(__hpux__) && !defined(PRIuPTR)
00077 #define PRIuPTR "lu"
00078 #endif
00079 
00080 /* Solaris < 10 kludge.  */
00081 #if defined(__sun__) && defined(__svr4__) && !defined(PRIuPTR)
00082 #if defined(__arch64__) || defined (__x86_64__)
00083 #define PRIuPTR "lu"
00084 #else
00085 #define PRIuPTR "u"
00086 #endif
00087 #endif
00088 
00089 #ifdef USING_MMAP
00090 static inline void *
00091 allocate_mmap (size_t size)
00092 {
00093   void *page;
00094 #if defined (HAVE_MMAP_DEV_ZERO)
00095   static int dev_zero_fd = -1;
00096 #endif
00097 
00098 #ifdef HAVE_MMAP_DEV_ZERO
00099   if (dev_zero_fd == -1)
00100     {
00101       dev_zero_fd = open ("/dev/zero", O_RDONLY);
00102       if (dev_zero_fd == -1)
00103        {
00104          perror ("open /dev/zero: %m");
00105          exit (1);
00106        }
00107     }
00108 #endif
00109 
00110 
00111 #ifdef HAVE_MMAP_ANON
00112   page = mmap (NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC,
00113               MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
00114 #endif
00115 #ifdef HAVE_MMAP_DEV_ZERO
00116   page = mmap (NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC,
00117               MAP_PRIVATE, dev_zero_fd, 0);
00118 #endif
00119 
00120   if (page == (void *) MAP_FAILED)
00121     {
00122       perror ("virtual memory exhausted");
00123       exit (1);
00124     }
00125 
00126   return page;
00127 }
00128 
00129 #endif