osdep.h 3.7 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
#if defined(CONFIG_SOLARIS) && CONFIG_SOLARIS_VERSION < 10
/* [u]int_fast*_t not in <sys/int_types.h> */
typedef unsigned char           uint_fast8_t;
typedef unsigned int            uint_fast16_t;
17
typedef signed int              int_fast16_t;
18 19
#endif

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#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

36
#ifndef container_of
37
#define container_of(ptr, type, member) ({                      \
38 39 40
        const typeof(((type *) 0)->member) *__mptr = (ptr);     \
        (type *) ((char *) __mptr - offsetof(type, member));})
#endif
41

42 43 44 45 46 47 48 49 50 51
/* 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

52 53 54
#define typeof_field(type, field) typeof(((type *)0)->field)
#define type_check(t1,t2) ((t1*)0 - (t2*)0)

55 56 57 58 59 60 61
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif

62 63 64 65
#ifndef DIV_ROUND_UP
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
#endif

66 67 68 69
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif

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

B
bellard 已提交
80
#define qemu_printf printf
81

82
int qemu_daemon(int nochdir, int noclose);
83
void *qemu_memalign(size_t alignment, size_t size);
B
bellard 已提交
84 85
void *qemu_vmalloc(size_t size);
void qemu_vfree(void *ptr);
86

A
Andreas Färber 已提交
87 88 89 90 91 92 93 94 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
#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);

122 123
#if defined(__HAIKU__) && defined(__i386__)
#define FMT_pid "%ld"
S
Stefan Weil 已提交
124 125
#elif defined(WIN64)
#define FMT_pid "%" PRId64
126 127 128 129
#else
#define FMT_pid "%d"
#endif

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 151
void qemu_set_cloexec(int fd);

152
#endif