blob: c5fd3d91ff79c4f0562085183d550f998993e945 [file] [log] [blame]
bellardea888122004-02-16 22:12:40 +00001#ifndef QEMU_OSDEP_H
2#define QEMU_OSDEP_H
3
4#include <stdarg.h>
Blue Swirlde5071c2009-09-12 09:58:46 +00005#include <stddef.h>
Paul Moore0f669982012-08-03 14:39:21 -04006#include <stdbool.h>
blueswir1128ab2f2008-08-15 18:33:42 +00007#ifdef __OpenBSD__
8#include <sys/types.h>
9#include <sys/signal.h>
10#endif
bellardea888122004-02-16 22:12:40 +000011
aliguorif7b4a942009-01-07 17:40:15 +000012#include <sys/time.h>
aliguorif7b4a942009-01-07 17:40:15 +000013
Andreas Färberdda3c2e2012-04-26 00:15:54 +020014#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;
Andreas Färber94a49d82012-04-26 00:15:56 +020018typedef signed int int_fast16_t;
Andreas Färberdda3c2e2012-04-26 00:15:54 +020019#endif
20
j_mayerdf2542c2007-11-19 00:38:33 +000021#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
balrogac509d82008-09-16 13:36:57 +000037#ifndef container_of
aliguori62a6e3e2008-08-21 20:11:11 +000038#define container_of(ptr, type, member) ({ \
balrogac509d82008-09-16 13:36:57 +000039 const typeof(((type *) 0)->member) *__mptr = (ptr); \
40 (type *) ((char *) __mptr - offsetof(type, member));})
41#endif
aliguori62a6e3e2008-08-21 20:11:11 +000042
Mark McLoughlin5096fae2009-11-25 18:49:03 +000043/* 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
Juan Quintelaf0d99ad2009-08-20 19:42:19 +020053#define typeof_field(type, field) typeof(((type *)0)->field)
54#define type_check(t1,t2) ((t1*)0 - (t2*)0)
55
j_mayerdf2542c2007-11-19 00:38:33 +000056#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
Corentin Charye0e53b22011-02-04 09:06:04 +010063#ifndef DIV_ROUND_UP
64#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
65#endif
66
blueswir10954d0d2008-03-11 21:01:02 +000067#ifndef ARRAY_SIZE
68#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
69#endif
70
j_mayerdf2542c2007-11-19 00:38:33 +000071#ifndef always_inline
Blue Swirl636aa202009-08-16 09:06:54 +000072#if !((__GNUC__ < 3) || defined(__APPLE__))
pbrook3dec6ec2008-11-19 01:31:52 +000073#ifdef __OPTIMIZE__
Blue Swirlb595c142012-07-08 07:00:50 +000074#undef inline
Blue Swirl636aa202009-08-16 09:06:54 +000075#define inline __attribute__ (( always_inline )) __inline__
thscebdff72008-06-05 22:55:54 +000076#endif
pbrook3dec6ec2008-11-19 01:31:52 +000077#endif
thscebdff72008-06-05 22:55:54 +000078#else
Blue Swirlb595c142012-07-08 07:00:50 +000079#undef inline
thscebdff72008-06-05 22:55:54 +000080#define inline always_inline
81#endif
j_mayerdf2542c2007-11-19 00:38:33 +000082
bellardd62ca2b2006-08-01 15:50:14 +000083#define qemu_printf printf
bellardea888122004-02-16 22:12:40 +000084
Alexandre Raymondf97742d2011-06-06 23:34:10 -040085int qemu_daemon(int nochdir, int noclose);
balrog33f00272007-12-24 14:33:24 +000086void *qemu_memalign(size_t alignment, size_t size);
bellard49b470e2005-02-10 21:59:25 +000087void *qemu_vmalloc(size_t size);
88void qemu_vfree(void *ptr);
89
Andreas Färbere78815a2010-09-25 11:26:05 +000090#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
Jason Baronddb97f12012-08-02 15:44:16 -0400106#ifdef MADV_DONTDUMP
107#define QEMU_MADV_DONTDUMP MADV_DONTDUMP
108#else
109#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
110#endif
Luiz Capitulinoad0b5322012-10-05 16:47:57 -0300111#ifdef MADV_HUGEPAGE
112#define QEMU_MADV_HUGEPAGE MADV_HUGEPAGE
113#else
114#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
115#endif
Andreas Färbere78815a2010-09-25 11:26:05 +0000116
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
Jason Baronddb97f12012-08-02 15:44:16 -0400123#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
Andreas Färbere78815a2010-09-25 11:26:05 +0000124
125#else /* no-op */
126
127#define QEMU_MADV_WILLNEED QEMU_MADV_INVALID
128#define QEMU_MADV_DONTNEED QEMU_MADV_INVALID
129#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
130#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
Jason Baronddb97f12012-08-02 15:44:16 -0400131#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
Andreas Färbere78815a2010-09-25 11:26:05 +0000132
133#endif
134
135int qemu_madvise(void *addr, size_t len, int advice);
136
Andreas Färber953ffe02011-06-02 19:58:06 +0200137#if defined(__HAIKU__) && defined(__i386__)
138#define FMT_pid "%ld"
Stefan Weil0e0167b2011-07-15 21:38:13 +0200139#elif defined(WIN64)
140#define FMT_pid "%" PRId64
Andreas Färber953ffe02011-06-02 19:58:06 +0200141#else
142#define FMT_pid "%d"
143#endif
144
thsaa26bb22007-03-25 21:33:06 +0000145int qemu_create_pidfile(const char *filename);
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100146int qemu_get_thread_id(void);
thsaa26bb22007-03-25 21:33:06 +0000147
Blue Swirlad620c22011-03-13 10:30:52 +0000148#ifdef _WIN32
149static inline void qemu_timersub(const struct timeval *val1,
150 const struct timeval *val2,
151 struct timeval *res)
152{
153 res->tv_sec = val1->tv_sec - val2->tv_sec;
154 if (val1->tv_usec < val2->tv_usec) {
155 res->tv_sec--;
156 res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000;
157 } else {
158 res->tv_usec = val1->tv_usec - val2->tv_usec;
159 }
160}
161#else
162#define qemu_timersub timersub
163#endif
164
Anthony Liguori49ee3592012-03-28 15:42:05 +0200165void qemu_set_cloexec(int fd);
166
Crístian Viana93bfef42012-05-30 00:35:51 -0300167void qemu_set_version(const char *);
168const char *qemu_get_version(void);
169
Paul Moore0f669982012-08-03 14:39:21 -0400170void fips_set_state(bool requested);
171bool fips_get_state(void);
172
bellardea888122004-02-16 22:12:40 +0000173#endif