bellard | ea88812 | 2004-02-16 22:12:40 +0000 | [diff] [blame] | 1 | #ifndef QEMU_OSDEP_H |
| 2 | #define QEMU_OSDEP_H |
| 3 | |
| 4 | #include <stdarg.h> |
Blue Swirl | de5071c | 2009-09-12 09:58:46 +0000 | [diff] [blame] | 5 | #include <stddef.h> |
Paul Moore | 0f66998 | 2012-08-03 14:39:21 -0400 | [diff] [blame] | 6 | #include <stdbool.h> |
blueswir1 | 128ab2f | 2008-08-15 18:33:42 +0000 | [diff] [blame] | 7 | #ifdef __OpenBSD__ |
| 8 | #include <sys/types.h> |
| 9 | #include <sys/signal.h> |
| 10 | #endif |
bellard | ea88812 | 2004-02-16 22:12:40 +0000 | [diff] [blame] | 11 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 12 | #include <sys/time.h> |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 13 | |
Andreas Färber | dda3c2e | 2012-04-26 00:15:54 +0200 | [diff] [blame] | 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; |
Andreas Färber | 94a49d8 | 2012-04-26 00:15:56 +0200 | [diff] [blame] | 18 | typedef signed int int_fast16_t; |
Andreas Färber | dda3c2e | 2012-04-26 00:15:54 +0200 | [diff] [blame] | 19 | #endif |
| 20 | |
j_mayer | df2542c | 2007-11-19 00:38:33 +0000 | [diff] [blame] | 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 |
| 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 | |
balrog | ac509d8 | 2008-09-16 13:36:57 +0000 | [diff] [blame] | 37 | #ifndef container_of |
aliguori | 62a6e3e | 2008-08-21 20:11:11 +0000 | [diff] [blame] | 38 | #define container_of(ptr, type, member) ({ \ |
balrog | ac509d8 | 2008-09-16 13:36:57 +0000 | [diff] [blame] | 39 | const typeof(((type *) 0)->member) *__mptr = (ptr); \ |
| 40 | (type *) ((char *) __mptr - offsetof(type, member));}) |
| 41 | #endif |
aliguori | 62a6e3e | 2008-08-21 20:11:11 +0000 | [diff] [blame] | 42 | |
Mark McLoughlin | 5096fae | 2009-11-25 18:49:03 +0000 | [diff] [blame] | 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 |
| 52 | |
Juan Quintela | f0d99ad | 2009-08-20 19:42:19 +0200 | [diff] [blame] | 53 | #define typeof_field(type, field) typeof(((type *)0)->field) |
| 54 | #define type_check(t1,t2) ((t1*)0 - (t2*)0) |
| 55 | |
j_mayer | df2542c | 2007-11-19 00:38:33 +0000 | [diff] [blame] | 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 |
| 62 | |
Corentin Chary | e0e53b2 | 2011-02-04 09:06:04 +0100 | [diff] [blame] | 63 | #ifndef DIV_ROUND_UP |
| 64 | #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) |
| 65 | #endif |
| 66 | |
blueswir1 | 0954d0d | 2008-03-11 21:01:02 +0000 | [diff] [blame] | 67 | #ifndef ARRAY_SIZE |
| 68 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 69 | #endif |
| 70 | |
j_mayer | df2542c | 2007-11-19 00:38:33 +0000 | [diff] [blame] | 71 | #ifndef always_inline |
Blue Swirl | 636aa20 | 2009-08-16 09:06:54 +0000 | [diff] [blame] | 72 | #if !((__GNUC__ < 3) || defined(__APPLE__)) |
pbrook | 3dec6ec | 2008-11-19 01:31:52 +0000 | [diff] [blame] | 73 | #ifdef __OPTIMIZE__ |
Blue Swirl | b595c14 | 2012-07-08 07:00:50 +0000 | [diff] [blame] | 74 | #undef inline |
Blue Swirl | 636aa20 | 2009-08-16 09:06:54 +0000 | [diff] [blame] | 75 | #define inline __attribute__ (( always_inline )) __inline__ |
ths | cebdff7 | 2008-06-05 22:55:54 +0000 | [diff] [blame] | 76 | #endif |
pbrook | 3dec6ec | 2008-11-19 01:31:52 +0000 | [diff] [blame] | 77 | #endif |
ths | cebdff7 | 2008-06-05 22:55:54 +0000 | [diff] [blame] | 78 | #else |
Blue Swirl | b595c14 | 2012-07-08 07:00:50 +0000 | [diff] [blame] | 79 | #undef inline |
ths | cebdff7 | 2008-06-05 22:55:54 +0000 | [diff] [blame] | 80 | #define inline always_inline |
| 81 | #endif |
j_mayer | df2542c | 2007-11-19 00:38:33 +0000 | [diff] [blame] | 82 | |
bellard | d62ca2b | 2006-08-01 15:50:14 +0000 | [diff] [blame] | 83 | #define qemu_printf printf |
bellard | ea88812 | 2004-02-16 22:12:40 +0000 | [diff] [blame] | 84 | |
Alexandre Raymond | f97742d | 2011-06-06 23:34:10 -0400 | [diff] [blame] | 85 | int qemu_daemon(int nochdir, int noclose); |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 86 | void *qemu_memalign(size_t alignment, size_t size); |
bellard | 49b470e | 2005-02-10 21:59:25 +0000 | [diff] [blame] | 87 | void *qemu_vmalloc(size_t size); |
| 88 | void qemu_vfree(void *ptr); |
| 89 | |
Andreas Färber | e78815a | 2010-09-25 11:26:05 +0000 | [diff] [blame] | 90 | #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 Baron | ddb97f1 | 2012-08-02 15:44:16 -0400 | [diff] [blame] | 106 | #ifdef MADV_DONTDUMP |
| 107 | #define QEMU_MADV_DONTDUMP MADV_DONTDUMP |
| 108 | #else |
| 109 | #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID |
| 110 | #endif |
Andreas Färber | e78815a | 2010-09-25 11:26:05 +0000 | [diff] [blame] | 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 |
Jason Baron | ddb97f1 | 2012-08-02 15:44:16 -0400 | [diff] [blame] | 118 | #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID |
Andreas Färber | e78815a | 2010-09-25 11:26:05 +0000 | [diff] [blame] | 119 | |
| 120 | #else /* no-op */ |
| 121 | |
| 122 | #define QEMU_MADV_WILLNEED QEMU_MADV_INVALID |
| 123 | #define QEMU_MADV_DONTNEED QEMU_MADV_INVALID |
| 124 | #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID |
| 125 | #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID |
Jason Baron | ddb97f1 | 2012-08-02 15:44:16 -0400 | [diff] [blame] | 126 | #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID |
Andreas Färber | e78815a | 2010-09-25 11:26:05 +0000 | [diff] [blame] | 127 | |
| 128 | #endif |
| 129 | |
| 130 | int qemu_madvise(void *addr, size_t len, int advice); |
| 131 | |
Andreas Färber | 953ffe0 | 2011-06-02 19:58:06 +0200 | [diff] [blame] | 132 | #if defined(__HAIKU__) && defined(__i386__) |
| 133 | #define FMT_pid "%ld" |
Stefan Weil | 0e0167b | 2011-07-15 21:38:13 +0200 | [diff] [blame] | 134 | #elif defined(WIN64) |
| 135 | #define FMT_pid "%" PRId64 |
Andreas Färber | 953ffe0 | 2011-06-02 19:58:06 +0200 | [diff] [blame] | 136 | #else |
| 137 | #define FMT_pid "%d" |
| 138 | #endif |
| 139 | |
ths | aa26bb2 | 2007-03-25 21:33:06 +0000 | [diff] [blame] | 140 | int qemu_create_pidfile(const char *filename); |
Jan Kiszka | dc7a09c | 2011-03-15 12:26:31 +0100 | [diff] [blame] | 141 | int qemu_get_thread_id(void); |
ths | aa26bb2 | 2007-03-25 21:33:06 +0000 | [diff] [blame] | 142 | |
Blue Swirl | ad620c2 | 2011-03-13 10:30:52 +0000 | [diff] [blame] | 143 | #ifdef _WIN32 |
| 144 | static inline void qemu_timersub(const struct timeval *val1, |
| 145 | const struct timeval *val2, |
| 146 | struct timeval *res) |
| 147 | { |
| 148 | res->tv_sec = val1->tv_sec - val2->tv_sec; |
| 149 | if (val1->tv_usec < val2->tv_usec) { |
| 150 | res->tv_sec--; |
| 151 | res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000; |
| 152 | } else { |
| 153 | res->tv_usec = val1->tv_usec - val2->tv_usec; |
| 154 | } |
| 155 | } |
| 156 | #else |
| 157 | #define qemu_timersub timersub |
| 158 | #endif |
| 159 | |
Anthony Liguori | 49ee359 | 2012-03-28 15:42:05 +0200 | [diff] [blame] | 160 | void qemu_set_cloexec(int fd); |
| 161 | |
Crístian Viana | 93bfef4 | 2012-05-30 00:35:51 -0300 | [diff] [blame] | 162 | void qemu_set_version(const char *); |
| 163 | const char *qemu_get_version(void); |
| 164 | |
Paul Moore | 0f66998 | 2012-08-03 14:39:21 -0400 | [diff] [blame] | 165 | void fips_set_state(bool requested); |
| 166 | bool fips_get_state(void); |
| 167 | |
bellard | ea88812 | 2004-02-16 22:12:40 +0000 | [diff] [blame] | 168 | #endif |