2003-09-04 16:49:22 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2003-11-26 14:46:10 +01:00
|
|
|
#include <fcntl.h>
|
2003-09-04 16:49:22 +02:00
|
|
|
#include <ffi.h>
|
2003-11-21 12:24:10 +01:00
|
|
|
#include "fficonfig.h"
|
2003-09-04 16:49:22 +02:00
|
|
|
|
|
|
|
#define MAX_ARGS 256
|
|
|
|
|
2003-11-21 12:24:10 +01:00
|
|
|
#define CHECK(x) !(x) ? abort() : 0
|
2003-09-04 16:49:22 +02:00
|
|
|
|
2006-02-16 22:03:16 +01:00
|
|
|
/* Define our own long for easier handling on multilib targets. */
|
|
|
|
#if LONG_MAX == 2147483647
|
|
|
|
#define ffi_type_mylong ffi_type_uint32
|
|
|
|
#else
|
|
|
|
#if LONG_MAX == 9223372036854775807
|
|
|
|
#define ffi_type_mylong ffi_type_uint64
|
|
|
|
#else
|
|
|
|
#error "Error, size LONG not defined as expected"
|
|
|
|
#endif
|
|
|
|
#endif
|
2003-11-21 12:24:10 +01:00
|
|
|
|
2006-03-01 00:12:00 +01:00
|
|
|
/* Define __UNUSED__ that also other compilers than gcc can run the tests. */
|
|
|
|
#undef __UNUSED__
|
|
|
|
#if defined(__GNUC__)
|
|
|
|
#define __UNUSED__ __attribute__((__unused__))
|
|
|
|
#else
|
|
|
|
#define __UNUSED__
|
|
|
|
#endif
|
|
|
|
|
2003-11-21 12:24:10 +01:00
|
|
|
/* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
|
|
|
|
file open. */
|
|
|
|
#ifdef HAVE_MMAP_ANON
|
|
|
|
# undef HAVE_MMAP_DEV_ZERO
|
|
|
|
|
|
|
|
# include <sys/mman.h>
|
|
|
|
# ifndef MAP_FAILED
|
|
|
|
# define MAP_FAILED -1
|
|
|
|
# endif
|
|
|
|
# if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
|
|
|
|
# define MAP_ANONYMOUS MAP_ANON
|
|
|
|
# endif
|
|
|
|
# define USING_MMAP
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_MMAP_DEV_ZERO
|
|
|
|
|
|
|
|
# include <sys/mman.h>
|
|
|
|
# ifndef MAP_FAILED
|
|
|
|
# define MAP_FAILED -1
|
|
|
|
# endif
|
|
|
|
# define USING_MMAP
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USING_MMAP
|
|
|
|
static inline void *
|
|
|
|
allocate_mmap (size_t size)
|
|
|
|
{
|
|
|
|
void *page;
|
|
|
|
#if defined (HAVE_MMAP_DEV_ZERO)
|
|
|
|
static int dev_zero_fd = -1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_MMAP_DEV_ZERO
|
|
|
|
if (dev_zero_fd == -1)
|
|
|
|
{
|
|
|
|
dev_zero_fd = open ("/dev/zero", O_RDONLY);
|
|
|
|
if (dev_zero_fd == -1)
|
|
|
|
{
|
|
|
|
perror ("open /dev/zero: %m");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_MMAP_ANON
|
|
|
|
page = mmap (NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_MMAP_DEV_ZERO
|
|
|
|
page = mmap (NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC,
|
|
|
|
MAP_PRIVATE, dev_zero_fd, 0);
|
|
|
|
#endif
|
|
|
|
|
2004-01-08 13:15:17 +01:00
|
|
|
if (page == (void *) MAP_FAILED)
|
2003-11-21 12:24:10 +01:00
|
|
|
{
|
|
|
|
perror ("virtual memory exhausted");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|