Merge remote-tracking branch 'remotes/amit-migration/tags/for-2.2-2' into staging
[qemu-kvm.git] / include / qemu / osdep.h
blobc0324344d59b288be7ac1c53a61c5f63b1db8019
1 #ifndef QEMU_OSDEP_H
2 #define QEMU_OSDEP_H
4 #include "config-host.h"
5 #include <stdarg.h>
6 #include <stddef.h>
7 #include <stdbool.h>
8 #include <sys/types.h>
9 #ifdef __OpenBSD__
10 #include <sys/signal.h>
11 #endif
13 #ifndef _WIN32
14 #include <sys/wait.h>
15 #else
16 #define WIFEXITED(x) 1
17 #define WEXITSTATUS(x) (x)
18 #endif
20 #include <sys/time.h>
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;
26 typedef signed int int_fast16_t;
27 #endif
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
36 #ifndef likely
37 #if __GNUC__ < 3
38 #define __builtin_expect(x, n) (x)
39 #endif
41 #define likely(x) __builtin_expect(!!(x), 1)
42 #define unlikely(x) __builtin_expect(!!(x), 0)
43 #endif
45 #ifndef container_of
46 #define container_of(ptr, type, member) ({ \
47 const typeof(((type *) 0)->member) *__mptr = (ptr); \
48 (type *) ((char *) __mptr - offsetof(type, member));})
49 #endif
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
61 #define typeof_field(type, field) typeof(((type *)0)->field)
62 #define type_check(t1,t2) ((t1*)0 - (t2*)0)
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
71 /* Minimum function that returns zero only iff both values are zero.
72 * Intended for use with unsigned values only. */
73 #ifndef MIN_NON_ZERO
74 #define MIN_NON_ZERO(a, b) (((a) != 0 && (a) < (b)) ? (a) : (b))
75 #endif
77 #ifndef ROUND_UP
78 #define ROUND_UP(n,d) (((n) + (d) - 1) & -(d))
79 #endif
81 #ifndef DIV_ROUND_UP
82 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
83 #endif
85 #ifndef ARRAY_SIZE
86 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
87 #endif
89 #ifndef always_inline
90 #if !((__GNUC__ < 3) || defined(__APPLE__))
91 #ifdef __OPTIMIZE__
92 #undef inline
93 #define inline __attribute__ (( always_inline )) __inline__
94 #endif
95 #endif
96 #else
97 #undef inline
98 #define inline always_inline
99 #endif
101 #define qemu_printf printf
103 int qemu_daemon(int nochdir, int noclose);
104 void *qemu_try_memalign(size_t alignment, size_t size);
105 void *qemu_memalign(size_t alignment, size_t size);
106 void *qemu_anon_ram_alloc(size_t size);
107 void qemu_vfree(void *ptr);
108 void qemu_anon_ram_free(void *ptr, size_t size);
110 #define QEMU_MADV_INVALID -1
112 #if defined(CONFIG_MADVISE)
114 #define QEMU_MADV_WILLNEED MADV_WILLNEED
115 #define QEMU_MADV_DONTNEED MADV_DONTNEED
116 #ifdef MADV_DONTFORK
117 #define QEMU_MADV_DONTFORK MADV_DONTFORK
118 #else
119 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
120 #endif
121 #ifdef MADV_MERGEABLE
122 #define QEMU_MADV_MERGEABLE MADV_MERGEABLE
123 #else
124 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
125 #endif
126 #ifdef MADV_UNMERGEABLE
127 #define QEMU_MADV_UNMERGEABLE MADV_UNMERGEABLE
128 #else
129 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
130 #endif
131 #ifdef MADV_DODUMP
132 #define QEMU_MADV_DODUMP MADV_DODUMP
133 #else
134 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID
135 #endif
136 #ifdef MADV_DONTDUMP
137 #define QEMU_MADV_DONTDUMP MADV_DONTDUMP
138 #else
139 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
140 #endif
141 #ifdef MADV_HUGEPAGE
142 #define QEMU_MADV_HUGEPAGE MADV_HUGEPAGE
143 #else
144 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
145 #endif
147 #elif defined(CONFIG_POSIX_MADVISE)
149 #define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED
150 #define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED
151 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
152 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
153 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
154 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID
155 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
156 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
158 #else /* no-op */
160 #define QEMU_MADV_WILLNEED QEMU_MADV_INVALID
161 #define QEMU_MADV_DONTNEED QEMU_MADV_INVALID
162 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
163 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
164 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
165 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID
166 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
167 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
169 #endif
171 int qemu_madvise(void *addr, size_t len, int advice);
173 int qemu_open(const char *name, int flags, ...);
174 int qemu_close(int fd);
176 #if defined(__HAIKU__) && defined(__i386__)
177 #define FMT_pid "%ld"
178 #elif defined(WIN64)
179 #define FMT_pid "%" PRId64
180 #else
181 #define FMT_pid "%d"
182 #endif
184 int qemu_create_pidfile(const char *filename);
185 int qemu_get_thread_id(void);
187 #ifndef CONFIG_IOVEC
188 struct iovec {
189 void *iov_base;
190 size_t iov_len;
193 * Use the same value as Linux for now.
195 #define IOV_MAX 1024
197 ssize_t readv(int fd, const struct iovec *iov, int iov_cnt);
198 ssize_t writev(int fd, const struct iovec *iov, int iov_cnt);
199 #else
200 #include <sys/uio.h>
201 #endif
203 #ifdef _WIN32
204 static inline void qemu_timersub(const struct timeval *val1,
205 const struct timeval *val2,
206 struct timeval *res)
208 res->tv_sec = val1->tv_sec - val2->tv_sec;
209 if (val1->tv_usec < val2->tv_usec) {
210 res->tv_sec--;
211 res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000;
212 } else {
213 res->tv_usec = val1->tv_usec - val2->tv_usec;
216 #else
217 #define qemu_timersub timersub
218 #endif
220 void qemu_set_cloexec(int fd);
222 void qemu_set_version(const char *);
223 const char *qemu_get_version(void);
225 void fips_set_state(bool requested);
226 bool fips_get_state(void);
228 /* Return a dynamically allocated pathname denoting a file or directory that is
229 * appropriate for storing local state.
231 * @relative_pathname need not start with a directory separator; one will be
232 * added automatically.
234 * The caller is responsible for releasing the value returned with g_free()
235 * after use.
237 char *qemu_get_local_state_pathname(const char *relative_pathname);
239 /* Find program directory, and save it for later usage with
240 * qemu_get_exec_dir().
241 * Try OS specific API first, if not working, parse from argv0. */
242 void qemu_init_exec_dir(const char *argv0);
244 /* Get the saved exec dir.
245 * Caller needs to release the returned string by g_free() */
246 char *qemu_get_exec_dir(void);
249 * qemu_getauxval:
250 * @type: the auxiliary vector key to lookup
252 * Search the auxiliary vector for @type, returning the value
253 * or 0 if @type is not present.
255 unsigned long qemu_getauxval(unsigned long type);
257 void qemu_set_tty_echo(int fd, bool echo);
259 void os_mem_prealloc(int fd, char *area, size_t sz);
261 #endif