2004-02-16 23:12:40 +01:00
|
|
|
#ifndef QEMU_OSDEP_H
|
|
|
|
#define QEMU_OSDEP_H
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
2009-09-12 11:58:46 +02:00
|
|
|
#include <stddef.h>
|
2008-08-15 20:33:42 +02:00
|
|
|
#ifdef __OpenBSD__
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/signal.h>
|
|
|
|
#endif
|
2004-02-16 23:12:40 +01:00
|
|
|
|
2009-01-07 18:40:15 +01:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
2007-11-19 01:38:33 +01:00
|
|
|
#ifndef glue
|
|
|
|
#define xglue(x, y) x ## y
|
|
|
|
#define glue(x, y) xglue(x, y)
|
|
|
|
#define stringify(s) tostring(s)
|
|
|
|
#define tostring(s) #s
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef likely
|
|
|
|
#if __GNUC__ < 3
|
|
|
|
#define __builtin_expect(x, n) (x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define likely(x) __builtin_expect(!!(x), 1)
|
|
|
|
#define unlikely(x) __builtin_expect(!!(x), 0)
|
|
|
|
#endif
|
|
|
|
|
2009-09-12 11:58:46 +02:00
|
|
|
#ifdef CONFIG_NEED_OFFSETOF
|
2008-09-16 15:36:57 +02:00
|
|
|
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *) 0)->MEMBER)
|
|
|
|
#endif
|
|
|
|
#ifndef container_of
|
2008-08-21 22:11:11 +02:00
|
|
|
#define container_of(ptr, type, member) ({ \
|
2008-09-16 15:36:57 +02:00
|
|
|
const typeof(((type *) 0)->member) *__mptr = (ptr); \
|
|
|
|
(type *) ((char *) __mptr - offsetof(type, member));})
|
|
|
|
#endif
|
2008-08-21 22:11:11 +02:00
|
|
|
|
2009-11-25 19:49:03 +01:00
|
|
|
/* Convert from a base type to a parent type, with compile time checking. */
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#define DO_UPCAST(type, field, dev) ( __extension__ ( { \
|
|
|
|
char __attribute__((unused)) offset_must_be_zero[ \
|
|
|
|
-offsetof(type, field)]; \
|
|
|
|
container_of(dev, type, field);}))
|
|
|
|
#else
|
|
|
|
#define DO_UPCAST(type, field, dev) container_of(dev, type, field)
|
|
|
|
#endif
|
|
|
|
|
2009-08-20 19:42:19 +02:00
|
|
|
#define typeof_field(type, field) typeof(((type *)0)->field)
|
|
|
|
#define type_check(t1,t2) ((t1*)0 - (t2*)0)
|
|
|
|
|
2007-11-19 01:38:33 +01:00
|
|
|
#ifndef MIN
|
|
|
|
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
#ifndef MAX
|
|
|
|
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
2011-02-04 09:06:04 +01:00
|
|
|
#ifndef DIV_ROUND_UP
|
|
|
|
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
|
|
|
|
#endif
|
|
|
|
|
2008-03-11 22:01:02 +01:00
|
|
|
#ifndef ARRAY_SIZE
|
|
|
|
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
|
|
|
|
#endif
|
|
|
|
|
2007-11-19 01:38:33 +01:00
|
|
|
#ifndef always_inline
|
2009-08-16 11:06:54 +02:00
|
|
|
#if !((__GNUC__ < 3) || defined(__APPLE__))
|
2008-11-19 02:31:52 +01:00
|
|
|
#ifdef __OPTIMIZE__
|
2009-08-16 11:06:54 +02:00
|
|
|
#define inline __attribute__ (( always_inline )) __inline__
|
2007-11-19 01:38:33 +01:00
|
|
|
#endif
|
2008-11-19 02:31:52 +01:00
|
|
|
#endif
|
2008-06-06 00:55:54 +02:00
|
|
|
#else
|
2007-11-19 01:38:33 +01:00
|
|
|
#define inline always_inline
|
2008-06-06 00:55:54 +02:00
|
|
|
#endif
|
2007-11-19 01:38:33 +01:00
|
|
|
|
|
|
|
#ifdef __i386__
|
2008-01-31 10:22:27 +01:00
|
|
|
#define REGPARM __attribute((regparm(3)))
|
2007-11-19 01:38:33 +01:00
|
|
|
#else
|
2008-01-31 10:22:27 +01:00
|
|
|
#define REGPARM
|
2007-11-19 01:38:33 +01:00
|
|
|
#endif
|
|
|
|
|
2006-08-01 17:50:14 +02:00
|
|
|
#define qemu_printf printf
|
2004-02-16 23:12:40 +01:00
|
|
|
|
2008-12-01 02:53:55 +01:00
|
|
|
#if defined (__GNUC__) && defined (__GNUC_MINOR__)
|
2008-10-12 18:15:04 +02:00
|
|
|
# define QEMU_GNUC_PREREQ(maj, min) \
|
|
|
|
((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
|
|
|
|
#else
|
|
|
|
# define QEMU_GNUC_PREREQ(maj, min) 0
|
|
|
|
#endif
|
|
|
|
|
2007-12-24 15:33:24 +01:00
|
|
|
void *qemu_memalign(size_t alignment, size_t size);
|
2005-02-10 22:59:25 +01:00
|
|
|
void *qemu_vmalloc(size_t size);
|
|
|
|
void qemu_vfree(void *ptr);
|
2004-02-16 23:12:40 +01:00
|
|
|
|
2010-09-25 13:26:05 +02:00
|
|
|
#define QEMU_MADV_INVALID -1
|
|
|
|
|
|
|
|
#if defined(CONFIG_MADVISE)
|
|
|
|
|
|
|
|
#define QEMU_MADV_WILLNEED MADV_WILLNEED
|
|
|
|
#define QEMU_MADV_DONTNEED MADV_DONTNEED
|
|
|
|
#ifdef MADV_DONTFORK
|
|
|
|
#define QEMU_MADV_DONTFORK MADV_DONTFORK
|
|
|
|
#else
|
|
|
|
#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
|
|
|
|
#endif
|
|
|
|
#ifdef MADV_MERGEABLE
|
|
|
|
#define QEMU_MADV_MERGEABLE MADV_MERGEABLE
|
|
|
|
#else
|
|
|
|
#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#elif defined(CONFIG_POSIX_MADVISE)
|
|
|
|
|
|
|
|
#define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED
|
|
|
|
#define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED
|
|
|
|
#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
|
|
|
|
#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
|
|
|
|
|
|
|
|
#else /* no-op */
|
|
|
|
|
|
|
|
#define QEMU_MADV_WILLNEED QEMU_MADV_INVALID
|
|
|
|
#define QEMU_MADV_DONTNEED QEMU_MADV_INVALID
|
|
|
|
#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
|
|
|
|
#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int qemu_madvise(void *addr, size_t len, int advice);
|
|
|
|
|
2007-03-25 23:33:06 +02:00
|
|
|
int qemu_create_pidfile(const char *filename);
|
2011-03-15 12:26:31 +01:00
|
|
|
int qemu_get_thread_id(void);
|
2007-03-25 23:33:06 +02:00
|
|
|
|
2011-03-13 11:30:52 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
static inline void qemu_timersub(const struct timeval *val1,
|
|
|
|
const struct timeval *val2,
|
|
|
|
struct timeval *res)
|
|
|
|
{
|
|
|
|
res->tv_sec = val1->tv_sec - val2->tv_sec;
|
|
|
|
if (val1->tv_usec < val2->tv_usec) {
|
|
|
|
res->tv_sec--;
|
|
|
|
res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000;
|
|
|
|
} else {
|
|
|
|
res->tv_usec = val1->tv_usec - val2->tv_usec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define qemu_timersub timersub
|
|
|
|
#endif
|
|
|
|
|
2004-02-16 23:12:40 +01:00
|
|
|
#endif
|