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

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

12 13
#include <sys/time.h>

14 15 16 17
#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;
18
typedef signed int              int_fast16_t;
19 20
#endif

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

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

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

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

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

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

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

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

B
bellard 已提交
83
#define qemu_printf printf
84

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

A
Andreas Färber 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
#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
106 107 108 109 110
#ifdef MADV_DONTDUMP
#define QEMU_MADV_DONTDUMP MADV_DONTDUMP
#else
#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
#endif
111 112 113 114 115
#ifdef MADV_HUGEPAGE
#define QEMU_MADV_HUGEPAGE MADV_HUGEPAGE
#else
#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
#endif
A
Andreas Färber 已提交
116 117 118 119 120 121 122

#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
123
#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
A
Andreas Färber 已提交
124 125 126 127 128 129 130

#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
131
#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
A
Andreas Färber 已提交
132 133 134 135 136

#endif

int qemu_madvise(void *addr, size_t len, int advice);

137 138
#if defined(__HAIKU__) && defined(__i386__)
#define FMT_pid "%ld"
S
Stefan Weil 已提交
139 140
#elif defined(WIN64)
#define FMT_pid "%" PRId64
141 142 143 144
#else
#define FMT_pid "%d"
#endif

145
int qemu_create_pidfile(const char *filename);
J
Jan Kiszka 已提交
146
int qemu_get_thread_id(void);
147

B
Blue Swirl 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
#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

165 166
void qemu_set_cloexec(int fd);

167 168 169
void qemu_set_version(const char *);
const char *qemu_get_version(void);

170 171 172
void fips_set_state(bool requested);
bool fips_get_state(void);

173
#endif