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