ffitestcxx.h 1.7 KB

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