]> Git Repo - qemu.git/blame - include/qemu/osdep.h
qemu-timer: add timer_init and timer_init_ns/us/ms
[qemu.git] / include / qemu / osdep.h
CommitLineData
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>
a2b257d6 8#include <stdint.h>
128ab2ff 9#include <sys/types.h>
98b2d199 10#ifdef __OpenBSD__
128ab2ff
BS
11#include <sys/signal.h>
12#endif
ea88812f 13
13c7b2da
PB
14#ifndef _WIN32
15#include <sys/wait.h>
16#else
17#define WIFEXITED(x) 1
18#define WEXITSTATUS(x) (x)
19#endif
20
f7b4a940 21#include <sys/time.h>
f7b4a940 22
dda3c2ee
AF
23#if defined(CONFIG_SOLARIS) && CONFIG_SOLARIS_VERSION < 10
24/* [u]int_fast*_t not in <sys/int_types.h> */
25typedef unsigned char uint_fast8_t;
26typedef unsigned int uint_fast16_t;
94a49d86 27typedef signed int int_fast16_t;
dda3c2ee
AF
28#endif
29
df2542c7
JM
30#ifndef glue
31#define xglue(x, y) x ## y
32#define glue(x, y) xglue(x, y)
33#define stringify(s) tostring(s)
34#define tostring(s) #s
35#endif
36
37#ifndef likely
38#if __GNUC__ < 3
39#define __builtin_expect(x, n) (x)
40#endif
41
42#define likely(x) __builtin_expect(!!(x), 1)
43#define unlikely(x) __builtin_expect(!!(x), 0)
44#endif
45
ac509d88 46#ifndef container_of
62a6e3e1 47#define container_of(ptr, type, member) ({ \
ac509d88
AZ
48 const typeof(((type *) 0)->member) *__mptr = (ptr); \
49 (type *) ((char *) __mptr - offsetof(type, member));})
50#endif
62a6e3e1 51
5096fae3
MM
52/* Convert from a base type to a parent type, with compile time checking. */
53#ifdef __GNUC__
54#define DO_UPCAST(type, field, dev) ( __extension__ ( { \
55 char __attribute__((unused)) offset_must_be_zero[ \
56 -offsetof(type, field)]; \
57 container_of(dev, type, field);}))
58#else
59#define DO_UPCAST(type, field, dev) container_of(dev, type, field)
60#endif
61
f0d99ad7
JQ
62#define typeof_field(type, field) typeof(((type *)0)->field)
63#define type_check(t1,t2) ((t1*)0 - (t2*)0)
64
df2542c7
JM
65#ifndef MIN
66#define MIN(a, b) (((a) < (b)) ? (a) : (b))
67#endif
68#ifndef MAX
69#define MAX(a, b) (((a) > (b)) ? (a) : (b))
70#endif
71
ac3a8726
PL
72/* Minimum function that returns zero only iff both values are zero.
73 * Intended for use with unsigned values only. */
74#ifndef MIN_NON_ZERO
75#define MIN_NON_ZERO(a, b) (((a) != 0 && (a) < (b)) ? (a) : (b))
76#endif
77
292c8e50
PB
78#ifndef ROUND_UP
79#define ROUND_UP(n,d) (((n) + (d) - 1) & -(d))
80#endif
81
e0e53b2f
CC
82#ifndef DIV_ROUND_UP
83#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
84#endif
85
0954d0d9
BS
86#ifndef ARRAY_SIZE
87#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
88#endif
89
df2542c7 90#ifndef always_inline
636aa200 91#if !((__GNUC__ < 3) || defined(__APPLE__))
3dec6ecd 92#ifdef __OPTIMIZE__
b595c14a 93#undef inline
636aa200 94#define inline __attribute__ (( always_inline )) __inline__
df2542c7 95#endif
3dec6ecd 96#endif
cebdff77 97#else
b595c14a 98#undef inline
df2542c7 99#define inline always_inline
cebdff77 100#endif
df2542c7 101
d62ca2bb 102#define qemu_printf printf
ea88812f 103
f97742d0 104int qemu_daemon(int nochdir, int noclose);
7d2a35cc 105void *qemu_try_memalign(size_t alignment, size_t size);
33f00271 106void *qemu_memalign(size_t alignment, size_t size);
a2b257d6 107void *qemu_anon_ram_alloc(size_t size, uint64_t *align);
49b470eb 108void qemu_vfree(void *ptr);
e7a09b92 109void qemu_anon_ram_free(void *ptr, size_t size);
ea88812f 110
e78815a5
AF
111#define QEMU_MADV_INVALID -1
112
113#if defined(CONFIG_MADVISE)
114
115#define QEMU_MADV_WILLNEED MADV_WILLNEED
116#define QEMU_MADV_DONTNEED MADV_DONTNEED
117#ifdef MADV_DONTFORK
118#define QEMU_MADV_DONTFORK MADV_DONTFORK
119#else
120#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
121#endif
122#ifdef MADV_MERGEABLE
123#define QEMU_MADV_MERGEABLE MADV_MERGEABLE
124#else
125#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
126#endif
605d0a94
PB
127#ifdef MADV_UNMERGEABLE
128#define QEMU_MADV_UNMERGEABLE MADV_UNMERGEABLE
129#else
130#define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
131#endif
132#ifdef MADV_DODUMP
133#define QEMU_MADV_DODUMP MADV_DODUMP
134#else
135#define QEMU_MADV_DODUMP QEMU_MADV_INVALID
136#endif
ddb97f1d
JB
137#ifdef MADV_DONTDUMP
138#define QEMU_MADV_DONTDUMP MADV_DONTDUMP
139#else
140#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
141#endif
ad0b5321
LC
142#ifdef MADV_HUGEPAGE
143#define QEMU_MADV_HUGEPAGE MADV_HUGEPAGE
144#else
145#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
146#endif
e78815a5
AF
147
148#elif defined(CONFIG_POSIX_MADVISE)
149
150#define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED
151#define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED
152#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
153#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
2925020d
MT
154#define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
155#define QEMU_MADV_DODUMP QEMU_MADV_INVALID
ddb97f1d 156#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
8473f377 157#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
e78815a5
AF
158
159#else /* no-op */
160
161#define QEMU_MADV_WILLNEED QEMU_MADV_INVALID
162#define QEMU_MADV_DONTNEED QEMU_MADV_INVALID
163#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
164#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
2925020d
MT
165#define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
166#define QEMU_MADV_DODUMP QEMU_MADV_INVALID
ddb97f1d 167#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
8473f377 168#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
e78815a5
AF
169
170#endif
171
172int qemu_madvise(void *addr, size_t len, int advice);
173
17e0b6ab
AF
174int qemu_open(const char *name, int flags, ...);
175int qemu_close(int fd);
176
953ffe0f
AF
177#if defined(__HAIKU__) && defined(__i386__)
178#define FMT_pid "%ld"
0e0167ba
SW
179#elif defined(WIN64)
180#define FMT_pid "%" PRId64
953ffe0f
AF
181#else
182#define FMT_pid "%d"
183#endif
184
aa26bb2d 185int qemu_create_pidfile(const char *filename);
dc7a09cf 186int qemu_get_thread_id(void);
aa26bb2d 187
9adea5f7
PB
188#ifndef CONFIG_IOVEC
189struct iovec {
190 void *iov_base;
191 size_t iov_len;
192};
193/*
194 * Use the same value as Linux for now.
195 */
196#define IOV_MAX 1024
197
198ssize_t readv(int fd, const struct iovec *iov, int iov_cnt);
199ssize_t writev(int fd, const struct iovec *iov, int iov_cnt);
200#else
201#include <sys/uio.h>
202#endif
203
ad620c29
BS
204#ifdef _WIN32
205static inline void qemu_timersub(const struct timeval *val1,
206 const struct timeval *val2,
207 struct timeval *res)
208{
209 res->tv_sec = val1->tv_sec - val2->tv_sec;
210 if (val1->tv_usec < val2->tv_usec) {
211 res->tv_sec--;
212 res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000;
213 } else {
214 res->tv_usec = val1->tv_usec - val2->tv_usec;
215 }
216}
217#else
218#define qemu_timersub timersub
219#endif
220
49ee3590
AL
221void qemu_set_cloexec(int fd);
222
93bfef4c
CV
223void qemu_set_version(const char *);
224const char *qemu_get_version(void);
225
0f66998f
PM
226void fips_set_state(bool requested);
227bool fips_get_state(void);
228
e2ea3515
LE
229/* Return a dynamically allocated pathname denoting a file or directory that is
230 * appropriate for storing local state.
231 *
232 * @relative_pathname need not start with a directory separator; one will be
233 * added automatically.
234 *
235 * The caller is responsible for releasing the value returned with g_free()
236 * after use.
237 */
238char *qemu_get_local_state_pathname(const char *relative_pathname);
239
10f5bff6
FZ
240/* Find program directory, and save it for later usage with
241 * qemu_get_exec_dir().
242 * Try OS specific API first, if not working, parse from argv0. */
243void qemu_init_exec_dir(const char *argv0);
244
245/* Get the saved exec dir.
246 * Caller needs to release the returned string by g_free() */
247char *qemu_get_exec_dir(void);
248
b6a3e690
RH
249/**
250 * qemu_getauxval:
251 * @type: the auxiliary vector key to lookup
252 *
253 * Search the auxiliary vector for @type, returning the value
254 * or 0 if @type is not present.
255 */
b6a3e690 256unsigned long qemu_getauxval(unsigned long type);
b6a3e690 257
13401ba0
SH
258void qemu_set_tty_echo(int fd, bool echo);
259
38183310
PB
260void os_mem_prealloc(int fd, char *area, size_t sz);
261
ea88812f 262#endif
This page took 0.723007 seconds and 4 git commands to generate.