fips: fix build on !Linux
[qemu-kvm.git] / osdep.h
blobd4b887d542f501a834fc3bc2ddd5b4c0d62f13d2
1 #ifndef QEMU_OSDEP_H
2 #define QEMU_OSDEP_H
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
12 #include <sys/time.h>
14 #if defined(CONFIG_SOLARIS) && CONFIG_SOLARIS_VERSION < 10
15 /* [u]int_fast*_t not in <sys/int_types.h> */
16 typedef unsigned char uint_fast8_t;
17 typedef unsigned int uint_fast16_t;
18 typedef signed int int_fast16_t;
19 #endif
21 #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
28 #ifndef likely
29 #if __GNUC__ < 3
30 #define __builtin_expect(x, n) (x)
31 #endif
33 #define likely(x) __builtin_expect(!!(x), 1)
34 #define unlikely(x) __builtin_expect(!!(x), 0)
35 #endif
37 #ifndef container_of
38 #define container_of(ptr, type, member) ({ \
39 const typeof(((type *) 0)->member) *__mptr = (ptr); \
40 (type *) ((char *) __mptr - offsetof(type, member));})
41 #endif
43 /* 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
53 #define typeof_field(type, field) typeof(((type *)0)->field)
54 #define type_check(t1,t2) ((t1*)0 - (t2*)0)
56 #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
63 #ifndef DIV_ROUND_UP
64 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
65 #endif
67 #ifndef ARRAY_SIZE
68 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
69 #endif
71 #ifndef always_inline
72 #if !((__GNUC__ < 3) || defined(__APPLE__))
73 #ifdef __OPTIMIZE__
74 #undef inline
75 #define inline __attribute__ (( always_inline )) __inline__
76 #endif
77 #endif
78 #else
79 #undef inline
80 #define inline always_inline
81 #endif
83 #define qemu_printf printf
85 int qemu_daemon(int nochdir, int noclose);
86 void *qemu_memalign(size_t alignment, size_t size);
87 void *qemu_vmalloc(size_t size);
88 void qemu_vfree(void *ptr);
90 #define QEMU_MADV_INVALID -1
92 #if defined(CONFIG_MADVISE)
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
107 #elif defined(CONFIG_POSIX_MADVISE)
109 #define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED
110 #define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED
111 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
112 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
114 #else /* no-op */
116 #define QEMU_MADV_WILLNEED QEMU_MADV_INVALID
117 #define QEMU_MADV_DONTNEED QEMU_MADV_INVALID
118 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
119 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
121 #endif
123 int qemu_madvise(void *addr, size_t len, int advice);
125 #if defined(__HAIKU__) && defined(__i386__)
126 #define FMT_pid "%ld"
127 #elif defined(WIN64)
128 #define FMT_pid "%" PRId64
129 #else
130 #define FMT_pid "%d"
131 #endif
133 int qemu_create_pidfile(const char *filename);
134 int qemu_get_thread_id(void);
136 #ifdef _WIN32
137 static inline void qemu_timersub(const struct timeval *val1,
138 const struct timeval *val2,
139 struct timeval *res)
141 res->tv_sec = val1->tv_sec - val2->tv_sec;
142 if (val1->tv_usec < val2->tv_usec) {
143 res->tv_sec--;
144 res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000;
145 } else {
146 res->tv_usec = val1->tv_usec - val2->tv_usec;
149 #else
150 #define qemu_timersub timersub
151 #endif
153 void qemu_set_cloexec(int fd);
155 void qemu_set_version(const char *);
156 const char *qemu_get_version(void);
158 void fips_set_state(bool requested);
159 bool fips_get_state(void);
161 #endif