Improve "ta 0" shutdown
[qemu/agraf.git] / osdep.h
blob432b91ea7293037c758598465a8e846e83356ee2
1 #ifndef QEMU_OSDEP_H
2 #define QEMU_OSDEP_H
4 #include <stdarg.h>
5 #include <stddef.h>
6 #ifdef __OpenBSD__
7 #include <sys/types.h>
8 #include <sys/signal.h>
9 #endif
11 #include <sys/time.h>
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
20 #ifndef likely
21 #if __GNUC__ < 3
22 #define __builtin_expect(x, n) (x)
23 #endif
25 #define likely(x) __builtin_expect(!!(x), 1)
26 #define unlikely(x) __builtin_expect(!!(x), 0)
27 #endif
29 #ifdef CONFIG_NEED_OFFSETOF
30 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *) 0)->MEMBER)
31 #endif
32 #ifndef container_of
33 #define container_of(ptr, type, member) ({ \
34 const typeof(((type *) 0)->member) *__mptr = (ptr); \
35 (type *) ((char *) __mptr - offsetof(type, member));})
36 #endif
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
48 #define typeof_field(type, field) typeof(((type *)0)->field)
49 #define type_check(t1,t2) ((t1*)0 - (t2*)0)
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
58 #ifndef DIV_ROUND_UP
59 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
60 #endif
62 #ifndef ARRAY_SIZE
63 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
64 #endif
66 #ifndef always_inline
67 #if !((__GNUC__ < 3) || defined(__APPLE__))
68 #ifdef __OPTIMIZE__
69 #define inline __attribute__ (( always_inline )) __inline__
70 #endif
71 #endif
72 #else
73 #define inline always_inline
74 #endif
76 #ifdef __i386__
77 #define REGPARM __attribute((regparm(3)))
78 #else
79 #define REGPARM
80 #endif
82 #define qemu_printf printf
84 int qemu_daemon(int nochdir, int noclose);
85 void *qemu_memalign(size_t alignment, size_t size);
86 void *qemu_vmalloc(size_t size);
87 void qemu_vfree(void *ptr);
89 #define QEMU_MADV_INVALID -1
91 #if defined(CONFIG_MADVISE)
93 #define QEMU_MADV_WILLNEED MADV_WILLNEED
94 #define QEMU_MADV_DONTNEED MADV_DONTNEED
95 #ifdef MADV_DONTFORK
96 #define QEMU_MADV_DONTFORK MADV_DONTFORK
97 #else
98 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
99 #endif
100 #ifdef MADV_MERGEABLE
101 #define QEMU_MADV_MERGEABLE MADV_MERGEABLE
102 #else
103 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
104 #endif
106 #elif defined(CONFIG_POSIX_MADVISE)
108 #define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED
109 #define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED
110 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
111 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
113 #else /* no-op */
115 #define QEMU_MADV_WILLNEED QEMU_MADV_INVALID
116 #define QEMU_MADV_DONTNEED QEMU_MADV_INVALID
117 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
118 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
120 #endif
122 int qemu_madvise(void *addr, size_t len, int advice);
124 #if defined(__HAIKU__) && defined(__i386__)
125 #define FMT_pid "%ld"
126 #elif defined(WIN64)
127 #define FMT_pid "%" PRId64
128 #else
129 #define FMT_pid "%d"
130 #endif
132 int qemu_create_pidfile(const char *filename);
133 int qemu_get_thread_id(void);
135 #ifdef _WIN32
136 static inline void qemu_timersub(const struct timeval *val1,
137 const struct timeval *val2,
138 struct timeval *res)
140 res->tv_sec = val1->tv_sec - val2->tv_sec;
141 if (val1->tv_usec < val2->tv_usec) {
142 res->tv_sec--;
143 res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000;
144 } else {
145 res->tv_usec = val1->tv_usec - val2->tv_usec;
148 #else
149 #define qemu_timersub timersub
150 #endif
152 #endif