]>
Commit | Line | Data |
---|---|---|
ea88812f FB |
1 | #ifndef QEMU_OSDEP_H |
2 | #define QEMU_OSDEP_H | |
3 | ||
4 | #include <stdarg.h> | |
de5071c5 | 5 | #include <stddef.h> |
128ab2ff BS |
6 | #ifdef __OpenBSD__ |
7 | #include <sys/types.h> | |
8 | #include <sys/signal.h> | |
9 | #endif | |
ea88812f | 10 | |
f7b4a940 | 11 | #include <sys/time.h> |
f7b4a940 | 12 | |
df2542c7 JM |
13 | #ifndef glue |
14 | #define xglue(x, y) x ## y | |
15 | #define glue(x, y) xglue(x, y) | |
16 | #define stringify(s) tostring(s) | |
17 | #define tostring(s) #s | |
18 | #endif | |
19 | ||
20 | #ifndef likely | |
21 | #if __GNUC__ < 3 | |
22 | #define __builtin_expect(x, n) (x) | |
23 | #endif | |
24 | ||
25 | #define likely(x) __builtin_expect(!!(x), 1) | |
26 | #define unlikely(x) __builtin_expect(!!(x), 0) | |
27 | #endif | |
28 | ||
de5071c5 | 29 | #ifdef CONFIG_NEED_OFFSETOF |
ac509d88 AZ |
30 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *) 0)->MEMBER) |
31 | #endif | |
32 | #ifndef container_of | |
62a6e3e1 | 33 | #define container_of(ptr, type, member) ({ \ |
ac509d88 AZ |
34 | const typeof(((type *) 0)->member) *__mptr = (ptr); \ |
35 | (type *) ((char *) __mptr - offsetof(type, member));}) | |
36 | #endif | |
62a6e3e1 | 37 | |
5096fae3 MM |
38 | /* Convert from a base type to a parent type, with compile time checking. */ |
39 | #ifdef __GNUC__ | |
40 | #define DO_UPCAST(type, field, dev) ( __extension__ ( { \ | |
41 | char __attribute__((unused)) offset_must_be_zero[ \ | |
42 | -offsetof(type, field)]; \ | |
43 | container_of(dev, type, field);})) | |
44 | #else | |
45 | #define DO_UPCAST(type, field, dev) container_of(dev, type, field) | |
46 | #endif | |
47 | ||
f0d99ad7 JQ |
48 | #define typeof_field(type, field) typeof(((type *)0)->field) |
49 | #define type_check(t1,t2) ((t1*)0 - (t2*)0) | |
50 | ||
df2542c7 JM |
51 | #ifndef MIN |
52 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) | |
53 | #endif | |
54 | #ifndef MAX | |
55 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) | |
56 | #endif | |
57 | ||
e0e53b2f CC |
58 | #ifndef DIV_ROUND_UP |
59 | #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) | |
60 | #endif | |
61 | ||
0954d0d9 BS |
62 | #ifndef ARRAY_SIZE |
63 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) | |
64 | #endif | |
65 | ||
df2542c7 | 66 | #ifndef always_inline |
636aa200 | 67 | #if !((__GNUC__ < 3) || defined(__APPLE__)) |
3dec6ecd | 68 | #ifdef __OPTIMIZE__ |
636aa200 | 69 | #define inline __attribute__ (( always_inline )) __inline__ |
df2542c7 | 70 | #endif |
3dec6ecd | 71 | #endif |
cebdff77 | 72 | #else |
df2542c7 | 73 | #define inline always_inline |
cebdff77 | 74 | #endif |
df2542c7 JM |
75 | |
76 | #ifdef __i386__ | |
d656469f | 77 | #define REGPARM __attribute((regparm(3))) |
df2542c7 | 78 | #else |
d656469f | 79 | #define REGPARM |
df2542c7 JM |
80 | #endif |
81 | ||
d62ca2bb | 82 | #define qemu_printf printf |
ea88812f | 83 | |
80fe30ed | 84 | #if defined (__GNUC__) && defined (__GNUC_MINOR__) |
bad5b1ec AJ |
85 | # define QEMU_GNUC_PREREQ(maj, min) \ |
86 | ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) | |
87 | #else | |
88 | # define QEMU_GNUC_PREREQ(maj, min) 0 | |
89 | #endif | |
90 | ||
33f00271 | 91 | void *qemu_memalign(size_t alignment, size_t size); |
49b470eb FB |
92 | void *qemu_vmalloc(size_t size); |
93 | void qemu_vfree(void *ptr); | |
ea88812f | 94 | |
e78815a5 AF |
95 | #define QEMU_MADV_INVALID -1 |
96 | ||
97 | #if defined(CONFIG_MADVISE) | |
98 | ||
99 | #define QEMU_MADV_WILLNEED MADV_WILLNEED | |
100 | #define QEMU_MADV_DONTNEED MADV_DONTNEED | |
101 | #ifdef MADV_DONTFORK | |
102 | #define QEMU_MADV_DONTFORK MADV_DONTFORK | |
103 | #else | |
104 | #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID | |
105 | #endif | |
106 | #ifdef MADV_MERGEABLE | |
107 | #define QEMU_MADV_MERGEABLE MADV_MERGEABLE | |
108 | #else | |
109 | #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID | |
110 | #endif | |
111 | ||
112 | #elif defined(CONFIG_POSIX_MADVISE) | |
113 | ||
114 | #define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED | |
115 | #define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED | |
116 | #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID | |
117 | #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID | |
118 | ||
119 | #else /* no-op */ | |
120 | ||
121 | #define QEMU_MADV_WILLNEED QEMU_MADV_INVALID | |
122 | #define QEMU_MADV_DONTNEED QEMU_MADV_INVALID | |
123 | #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID | |
124 | #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID | |
125 | ||
126 | #endif | |
127 | ||
128 | int qemu_madvise(void *addr, size_t len, int advice); | |
129 | ||
aa26bb2d | 130 | int qemu_create_pidfile(const char *filename); |
dc7a09cf | 131 | int qemu_get_thread_id(void); |
aa26bb2d | 132 | |
ad620c29 BS |
133 | #ifdef _WIN32 |
134 | static inline void qemu_timersub(const struct timeval *val1, | |
135 | const struct timeval *val2, | |
136 | struct timeval *res) | |
137 | { | |
138 | res->tv_sec = val1->tv_sec - val2->tv_sec; | |
139 | if (val1->tv_usec < val2->tv_usec) { | |
140 | res->tv_sec--; | |
141 | res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000; | |
142 | } else { | |
143 | res->tv_usec = val1->tv_usec - val2->tv_usec; | |
144 | } | |
145 | } | |
146 | #else | |
147 | #define qemu_timersub timersub | |
148 | #endif | |
149 | ||
ea88812f | 150 | #endif |