block: use Error mechanism instead of -errno for block_job_create()
[qemu/agraf.git] / osdep.h
blob428285c7e0ef0d1a91a5c2937ed3f011c403a7bf
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 #ifndef container_of
30 #define container_of(ptr, type, member) ({ \
31 const typeof(((type *) 0)->member) *__mptr = (ptr); \
32 (type *) ((char *) __mptr - offsetof(type, member));})
33 #endif
35 /* Convert from a base type to a parent type, with compile time checking. */
36 #ifdef __GNUC__
37 #define DO_UPCAST(type, field, dev) ( __extension__ ( { \
38 char __attribute__((unused)) offset_must_be_zero[ \
39 -offsetof(type, field)]; \
40 container_of(dev, type, field);}))
41 #else
42 #define DO_UPCAST(type, field, dev) container_of(dev, type, field)
43 #endif
45 #define typeof_field(type, field) typeof(((type *)0)->field)
46 #define type_check(t1,t2) ((t1*)0 - (t2*)0)
48 #ifndef MIN
49 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
50 #endif
51 #ifndef MAX
52 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
53 #endif
55 #ifndef DIV_ROUND_UP
56 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
57 #endif
59 #ifndef ARRAY_SIZE
60 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
61 #endif
63 #ifndef always_inline
64 #if !((__GNUC__ < 3) || defined(__APPLE__))
65 #ifdef __OPTIMIZE__
66 #define inline __attribute__ (( always_inline )) __inline__
67 #endif
68 #endif
69 #else
70 #define inline always_inline
71 #endif
73 #define qemu_printf printf
75 int qemu_daemon(int nochdir, int noclose);
76 void *qemu_memalign(size_t alignment, size_t size);
77 void *qemu_vmalloc(size_t size);
78 void qemu_vfree(void *ptr);
80 #define QEMU_MADV_INVALID -1
82 #if defined(CONFIG_MADVISE)
84 #define QEMU_MADV_WILLNEED MADV_WILLNEED
85 #define QEMU_MADV_DONTNEED MADV_DONTNEED
86 #ifdef MADV_DONTFORK
87 #define QEMU_MADV_DONTFORK MADV_DONTFORK
88 #else
89 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
90 #endif
91 #ifdef MADV_MERGEABLE
92 #define QEMU_MADV_MERGEABLE MADV_MERGEABLE
93 #else
94 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
95 #endif
97 #elif defined(CONFIG_POSIX_MADVISE)
99 #define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED
100 #define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED
101 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
102 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
104 #else /* no-op */
106 #define QEMU_MADV_WILLNEED QEMU_MADV_INVALID
107 #define QEMU_MADV_DONTNEED QEMU_MADV_INVALID
108 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
109 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
111 #endif
113 int qemu_madvise(void *addr, size_t len, int advice);
115 #if defined(__HAIKU__) && defined(__i386__)
116 #define FMT_pid "%ld"
117 #elif defined(WIN64)
118 #define FMT_pid "%" PRId64
119 #else
120 #define FMT_pid "%d"
121 #endif
123 int qemu_create_pidfile(const char *filename);
124 int qemu_get_thread_id(void);
126 #ifdef _WIN32
127 static inline void qemu_timersub(const struct timeval *val1,
128 const struct timeval *val2,
129 struct timeval *res)
131 res->tv_sec = val1->tv_sec - val2->tv_sec;
132 if (val1->tv_usec < val2->tv_usec) {
133 res->tv_sec--;
134 res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000;
135 } else {
136 res->tv_usec = val1->tv_usec - val2->tv_usec;
139 #else
140 #define qemu_timersub timersub
141 #endif
143 void qemu_set_cloexec(int fd);
145 #endif