]> git.proxmox.com Git - qemu.git/blame - osdep.h
target-mips: Fix incorrect code and test for INSV
[qemu.git] / osdep.h
CommitLineData
ea88812f
FB
1#ifndef QEMU_OSDEP_H
2#define QEMU_OSDEP_H
3
4#include <stdarg.h>
de5071c5 5#include <stddef.h>
0f66998f 6#include <stdbool.h>
128ab2ff
BS
7#ifdef __OpenBSD__
8#include <sys/types.h>
9#include <sys/signal.h>
10#endif
ea88812f 11
f7b4a940 12#include <sys/time.h>
f7b4a940 13
dda3c2ee
AF
14#if defined(CONFIG_SOLARIS) && CONFIG_SOLARIS_VERSION < 10
15/* [u]int_fast*_t not in <sys/int_types.h> */
16typedef unsigned char uint_fast8_t;
17typedef unsigned int uint_fast16_t;
94a49d86 18typedef signed int int_fast16_t;
dda3c2ee
AF
19#endif
20
df2542c7
JM
21#ifndef glue
22#define xglue(x, y) x ## y
23#define glue(x, y) xglue(x, y)
24#define stringify(s) tostring(s)
25#define tostring(s) #s
26#endif
27
28#ifndef likely
29#if __GNUC__ < 3
30#define __builtin_expect(x, n) (x)
31#endif
32
33#define likely(x) __builtin_expect(!!(x), 1)
34#define unlikely(x) __builtin_expect(!!(x), 0)
35#endif
36
ac509d88 37#ifndef container_of
62a6e3e1 38#define container_of(ptr, type, member) ({ \
ac509d88
AZ
39 const typeof(((type *) 0)->member) *__mptr = (ptr); \
40 (type *) ((char *) __mptr - offsetof(type, member));})
41#endif
62a6e3e1 42
5096fae3
MM
43/* Convert from a base type to a parent type, with compile time checking. */
44#ifdef __GNUC__
45#define DO_UPCAST(type, field, dev) ( __extension__ ( { \
46 char __attribute__((unused)) offset_must_be_zero[ \
47 -offsetof(type, field)]; \
48 container_of(dev, type, field);}))
49#else
50#define DO_UPCAST(type, field, dev) container_of(dev, type, field)
51#endif
52
f0d99ad7
JQ
53#define typeof_field(type, field) typeof(((type *)0)->field)
54#define type_check(t1,t2) ((t1*)0 - (t2*)0)
55
df2542c7
JM
56#ifndef MIN
57#define MIN(a, b) (((a) < (b)) ? (a) : (b))
58#endif
59#ifndef MAX
60#define MAX(a, b) (((a) > (b)) ? (a) : (b))
61#endif
62
e0e53b2f
CC
63#ifndef DIV_ROUND_UP
64#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
65#endif
66
0954d0d9
BS
67#ifndef ARRAY_SIZE
68#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
69#endif
70
df2542c7 71#ifndef always_inline
636aa200 72#if !((__GNUC__ < 3) || defined(__APPLE__))
3dec6ecd 73#ifdef __OPTIMIZE__
b595c14a 74#undef inline
636aa200 75#define inline __attribute__ (( always_inline )) __inline__
df2542c7 76#endif
3dec6ecd 77#endif
cebdff77 78#else
b595c14a 79#undef inline
df2542c7 80#define inline always_inline
cebdff77 81#endif
df2542c7 82
d62ca2bb 83#define qemu_printf printf
ea88812f 84
f97742d0 85int qemu_daemon(int nochdir, int noclose);
33f00271 86void *qemu_memalign(size_t alignment, size_t size);
49b470eb
FB
87void *qemu_vmalloc(size_t size);
88void qemu_vfree(void *ptr);
ea88812f 89
e78815a5
AF
90#define QEMU_MADV_INVALID -1
91
92#if defined(CONFIG_MADVISE)
93
94#define QEMU_MADV_WILLNEED MADV_WILLNEED
95#define QEMU_MADV_DONTNEED MADV_DONTNEED
96#ifdef MADV_DONTFORK
97#define QEMU_MADV_DONTFORK MADV_DONTFORK
98#else
99#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
100#endif
101#ifdef MADV_MERGEABLE
102#define QEMU_MADV_MERGEABLE MADV_MERGEABLE
103#else
104#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
105#endif
ddb97f1d
JB
106#ifdef MADV_DONTDUMP
107#define QEMU_MADV_DONTDUMP MADV_DONTDUMP
108#else
109#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
110#endif
ad0b5321
LC
111#ifdef MADV_HUGEPAGE
112#define QEMU_MADV_HUGEPAGE MADV_HUGEPAGE
113#else
114#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
115#endif
e78815a5
AF
116
117#elif defined(CONFIG_POSIX_MADVISE)
118
119#define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED
120#define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED
121#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
122#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
ddb97f1d 123#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
8473f377 124#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
e78815a5
AF
125
126#else /* no-op */
127
128#define QEMU_MADV_WILLNEED QEMU_MADV_INVALID
129#define QEMU_MADV_DONTNEED QEMU_MADV_INVALID
130#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
131#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
ddb97f1d 132#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
8473f377 133#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
e78815a5
AF
134
135#endif
136
137int qemu_madvise(void *addr, size_t len, int advice);
138
17e0b6ab
AF
139int qemu_open(const char *name, int flags, ...);
140int qemu_close(int fd);
141
953ffe0f
AF
142#if defined(__HAIKU__) && defined(__i386__)
143#define FMT_pid "%ld"
0e0167ba
SW
144#elif defined(WIN64)
145#define FMT_pid "%" PRId64
953ffe0f
AF
146#else
147#define FMT_pid "%d"
148#endif
149
aa26bb2d 150int qemu_create_pidfile(const char *filename);
dc7a09cf 151int qemu_get_thread_id(void);
aa26bb2d 152
ad620c29
BS
153#ifdef _WIN32
154static inline void qemu_timersub(const struct timeval *val1,
155 const struct timeval *val2,
156 struct timeval *res)
157{
158 res->tv_sec = val1->tv_sec - val2->tv_sec;
159 if (val1->tv_usec < val2->tv_usec) {
160 res->tv_sec--;
161 res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000;
162 } else {
163 res->tv_usec = val1->tv_usec - val2->tv_usec;
164 }
165}
166#else
167#define qemu_timersub timersub
168#endif
169
49ee3590
AL
170void qemu_set_cloexec(int fd);
171
93bfef4c
CV
172void qemu_set_version(const char *);
173const char *qemu_get_version(void);
174
0f66998f
PM
175void fips_set_state(bool requested);
176bool fips_get_state(void);
177
ea88812f 178#endif