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