osdep.h 3.6 KB
Newer Older
1 2 3 4
#ifndef QEMU_OSDEP_H
#define QEMU_OSDEP_H

#include <stdarg.h>
5
#include <stddef.h>
6 7 8 9
#ifdef __OpenBSD__
#include <sys/types.h>
#include <sys/signal.h>
#endif
10

11 12
#include <sys/time.h>

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#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

29
#ifdef CONFIG_NEED_OFFSETOF
30 31 32
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *) 0)->MEMBER)
#endif
#ifndef container_of
33
#define container_of(ptr, type, member) ({                      \
34 35 36
        const typeof(((type *) 0)->member) *__mptr = (ptr);     \
        (type *) ((char *) __mptr - offsetof(type, member));})
#endif
37

38 39 40 41 42 43 44 45 46 47
/* 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

48 49 50
#define typeof_field(type, field) typeof(((type *)0)->field)
#define type_check(t1,t2) ((t1*)0 - (t2*)0)

51 52 53 54 55 56 57
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif

58 59 60 61
#ifndef DIV_ROUND_UP
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
#endif

62 63 64 65
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif

66
#ifndef always_inline
B
Blue Swirl 已提交
67
#if !((__GNUC__ < 3) || defined(__APPLE__))
P
pbrook 已提交
68
#ifdef __OPTIMIZE__
B
Blue Swirl 已提交
69
#define inline __attribute__ (( always_inline )) __inline__
70
#endif
P
pbrook 已提交
71
#endif
72
#else
73
#define inline always_inline
74
#endif
75 76

#ifdef __i386__
77
#define REGPARM __attribute((regparm(3)))
78
#else
79
#define REGPARM
80 81
#endif

B
bellard 已提交
82
#define qemu_printf printf
83

84
#if defined (__GNUC__) && defined (__GNUC_MINOR__)
A
aurel32 已提交
85 86 87 88 89 90
# define QEMU_GNUC_PREREQ(maj, min) \
         ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
#else
# define QEMU_GNUC_PREREQ(maj, min) 0
#endif

91
void *qemu_memalign(size_t alignment, size_t size);
B
bellard 已提交
92 93
void *qemu_vmalloc(size_t size);
void qemu_vfree(void *ptr);
94

A
Andreas Färber 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
#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);

130
int qemu_create_pidfile(const char *filename);
J
Jan Kiszka 已提交
131
int qemu_get_thread_id(void);
132

B
Blue Swirl 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
#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

150
#endif