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> |
blueswir1 | 128ab2f | 2008-08-15 18:33:42 +0000 | [diff] [blame] | 6 | #ifdef __OpenBSD__ |
| 7 | #include <sys/types.h> |
| 8 | #include <sys/signal.h> |
| 9 | #endif |
bellard | ea88812 | 2004-02-16 22:12:40 +0000 | [diff] [blame] | 10 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 11 | #ifndef _WIN32 |
| 12 | #include <sys/time.h> |
| 13 | #endif |
| 14 | |
j_mayer | df2542c | 2007-11-19 00:38:33 +0000 | [diff] [blame] | 15 | #ifndef glue |
| 16 | #define xglue(x, y) x ## y |
| 17 | #define glue(x, y) xglue(x, y) |
| 18 | #define stringify(s) tostring(s) |
| 19 | #define tostring(s) #s |
| 20 | #endif |
| 21 | |
| 22 | #ifndef likely |
| 23 | #if __GNUC__ < 3 |
| 24 | #define __builtin_expect(x, n) (x) |
| 25 | #endif |
| 26 | |
| 27 | #define likely(x) __builtin_expect(!!(x), 1) |
| 28 | #define unlikely(x) __builtin_expect(!!(x), 0) |
| 29 | #endif |
| 30 | |
Blue Swirl | de5071c | 2009-09-12 09:58:46 +0000 | [diff] [blame] | 31 | #ifdef CONFIG_NEED_OFFSETOF |
balrog | ac509d8 | 2008-09-16 13:36:57 +0000 | [diff] [blame] | 32 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *) 0)->MEMBER) |
| 33 | #endif |
| 34 | #ifndef container_of |
aliguori | 62a6e3e | 2008-08-21 20:11:11 +0000 | [diff] [blame] | 35 | #define container_of(ptr, type, member) ({ \ |
balrog | ac509d8 | 2008-09-16 13:36:57 +0000 | [diff] [blame] | 36 | const typeof(((type *) 0)->member) *__mptr = (ptr); \ |
| 37 | (type *) ((char *) __mptr - offsetof(type, member));}) |
| 38 | #endif |
aliguori | 62a6e3e | 2008-08-21 20:11:11 +0000 | [diff] [blame] | 39 | |
Mark McLoughlin | 5096fae | 2009-11-25 18:49:03 +0000 | [diff] [blame] | 40 | /* Convert from a base type to a parent type, with compile time checking. */ |
| 41 | #ifdef __GNUC__ |
| 42 | #define DO_UPCAST(type, field, dev) ( __extension__ ( { \ |
| 43 | char __attribute__((unused)) offset_must_be_zero[ \ |
| 44 | -offsetof(type, field)]; \ |
| 45 | container_of(dev, type, field);})) |
| 46 | #else |
| 47 | #define DO_UPCAST(type, field, dev) container_of(dev, type, field) |
| 48 | #endif |
| 49 | |
Juan Quintela | f0d99ad | 2009-08-20 19:42:19 +0200 | [diff] [blame] | 50 | #define typeof_field(type, field) typeof(((type *)0)->field) |
| 51 | #define type_check(t1,t2) ((t1*)0 - (t2*)0) |
| 52 | |
j_mayer | df2542c | 2007-11-19 00:38:33 +0000 | [diff] [blame] | 53 | #ifndef MIN |
| 54 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) |
| 55 | #endif |
| 56 | #ifndef MAX |
| 57 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) |
| 58 | #endif |
| 59 | |
Corentin Chary | e0e53b2 | 2011-02-04 09:06:04 +0100 | [diff] [blame] | 60 | #ifndef DIV_ROUND_UP |
| 61 | #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) |
| 62 | #endif |
| 63 | |
blueswir1 | 0954d0d | 2008-03-11 21:01:02 +0000 | [diff] [blame] | 64 | #ifndef ARRAY_SIZE |
| 65 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 66 | #endif |
| 67 | |
j_mayer | df2542c | 2007-11-19 00:38:33 +0000 | [diff] [blame] | 68 | #ifndef always_inline |
Blue Swirl | 636aa20 | 2009-08-16 09:06:54 +0000 | [diff] [blame] | 69 | #if !((__GNUC__ < 3) || defined(__APPLE__)) |
pbrook | 3dec6ec | 2008-11-19 01:31:52 +0000 | [diff] [blame] | 70 | #ifdef __OPTIMIZE__ |
Blue Swirl | 636aa20 | 2009-08-16 09:06:54 +0000 | [diff] [blame] | 71 | #define inline __attribute__ (( always_inline )) __inline__ |
ths | cebdff7 | 2008-06-05 22:55:54 +0000 | [diff] [blame] | 72 | #endif |
pbrook | 3dec6ec | 2008-11-19 01:31:52 +0000 | [diff] [blame] | 73 | #endif |
ths | cebdff7 | 2008-06-05 22:55:54 +0000 | [diff] [blame] | 74 | #else |
| 75 | #define inline always_inline |
| 76 | #endif |
j_mayer | df2542c | 2007-11-19 00:38:33 +0000 | [diff] [blame] | 77 | |
| 78 | #ifdef __i386__ |
bellard | d656469 | 2008-01-31 09:22:27 +0000 | [diff] [blame] | 79 | #define REGPARM __attribute((regparm(3))) |
j_mayer | df2542c | 2007-11-19 00:38:33 +0000 | [diff] [blame] | 80 | #else |
bellard | d656469 | 2008-01-31 09:22:27 +0000 | [diff] [blame] | 81 | #define REGPARM |
j_mayer | df2542c | 2007-11-19 00:38:33 +0000 | [diff] [blame] | 82 | #endif |
| 83 | |
bellard | d62ca2b | 2006-08-01 15:50:14 +0000 | [diff] [blame] | 84 | #define qemu_printf printf |
bellard | ea88812 | 2004-02-16 22:12:40 +0000 | [diff] [blame] | 85 | |
balrog | 80fe30e | 2008-12-01 01:53:55 +0000 | [diff] [blame] | 86 | #if defined (__GNUC__) && defined (__GNUC_MINOR__) |
aurel32 | bad5b1e | 2008-10-12 16:15:04 +0000 | [diff] [blame] | 87 | # define QEMU_GNUC_PREREQ(maj, min) \ |
| 88 | ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) |
| 89 | #else |
| 90 | # define QEMU_GNUC_PREREQ(maj, min) 0 |
| 91 | #endif |
| 92 | |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 93 | void *qemu_memalign(size_t alignment, size_t size); |
bellard | 49b470e | 2005-02-10 21:59:25 +0000 | [diff] [blame] | 94 | void *qemu_vmalloc(size_t size); |
| 95 | void qemu_vfree(void *ptr); |
| 96 | |
Andreas Färber | e78815a | 2010-09-25 11:26:05 +0000 | [diff] [blame] | 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 | |
| 114 | #elif defined(CONFIG_POSIX_MADVISE) |
| 115 | |
| 116 | #define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED |
| 117 | #define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED |
| 118 | #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID |
| 119 | #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID |
| 120 | |
| 121 | #else /* no-op */ |
| 122 | |
| 123 | #define QEMU_MADV_WILLNEED QEMU_MADV_INVALID |
| 124 | #define QEMU_MADV_DONTNEED QEMU_MADV_INVALID |
| 125 | #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID |
| 126 | #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID |
| 127 | |
| 128 | #endif |
| 129 | |
| 130 | int qemu_madvise(void *addr, size_t len, int advice); |
| 131 | |
ths | aa26bb2 | 2007-03-25 21:33:06 +0000 | [diff] [blame] | 132 | int qemu_create_pidfile(const char *filename); |
Jan Kiszka | dc7a09c | 2011-03-15 12:26:31 +0100 | [diff] [blame^] | 133 | int qemu_get_thread_id(void); |
ths | aa26bb2 | 2007-03-25 21:33:06 +0000 | [diff] [blame] | 134 | |
bellard | ea88812 | 2004-02-16 22:12:40 +0000 | [diff] [blame] | 135 | #endif |