]> git.proxmox.com Git - qemu.git/blame - osdep.h
pci: Do not check if a bus exist in pci_parse_devaddr.
[qemu.git] / osdep.h
CommitLineData
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
ac509d88 29#ifndef container_of
62a6e3e1 30#define container_of(ptr, type, member) ({ \
ac509d88
AZ
31 const typeof(((type *) 0)->member) *__mptr = (ptr); \
32 (type *) ((char *) __mptr - offsetof(type, member));})
33#endif
62a6e3e1 34
5096fae3
MM
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
44
f0d99ad7
JQ
45#define typeof_field(type, field) typeof(((type *)0)->field)
46#define type_check(t1,t2) ((t1*)0 - (t2*)0)
47
df2542c7
JM
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
54
e0e53b2f
CC
55#ifndef DIV_ROUND_UP
56#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
57#endif
58
0954d0d9
BS
59#ifndef ARRAY_SIZE
60#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
61#endif
62
df2542c7 63#ifndef always_inline
636aa200 64#if !((__GNUC__ < 3) || defined(__APPLE__))
3dec6ecd 65#ifdef __OPTIMIZE__
636aa200 66#define inline __attribute__ (( always_inline )) __inline__
df2542c7 67#endif
3dec6ecd 68#endif
cebdff77 69#else
df2542c7 70#define inline always_inline
cebdff77 71#endif
df2542c7
JM
72
73#ifdef __i386__
d656469f 74#define REGPARM __attribute((regparm(3)))
df2542c7 75#else
d656469f 76#define REGPARM
df2542c7
JM
77#endif
78
d62ca2bb 79#define qemu_printf printf
ea88812f 80
f97742d0 81int qemu_daemon(int nochdir, int noclose);
33f00271 82void *qemu_memalign(size_t alignment, size_t size);
49b470eb
FB
83void *qemu_vmalloc(size_t size);
84void qemu_vfree(void *ptr);
ea88812f 85
e78815a5
AF
86#define QEMU_MADV_INVALID -1
87
88#if defined(CONFIG_MADVISE)
89
90#define QEMU_MADV_WILLNEED MADV_WILLNEED
91#define QEMU_MADV_DONTNEED MADV_DONTNEED
92#ifdef MADV_DONTFORK
93#define QEMU_MADV_DONTFORK MADV_DONTFORK
94#else
95#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
96#endif
97#ifdef MADV_MERGEABLE
98#define QEMU_MADV_MERGEABLE MADV_MERGEABLE
99#else
100#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
101#endif
102
103#elif defined(CONFIG_POSIX_MADVISE)
104
105#define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED
106#define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED
107#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
108#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
109
110#else /* no-op */
111
112#define QEMU_MADV_WILLNEED QEMU_MADV_INVALID
113#define QEMU_MADV_DONTNEED QEMU_MADV_INVALID
114#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
115#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
116
117#endif
118
119int qemu_madvise(void *addr, size_t len, int advice);
120
953ffe0f
AF
121#if defined(__HAIKU__) && defined(__i386__)
122#define FMT_pid "%ld"
0e0167ba
SW
123#elif defined(WIN64)
124#define FMT_pid "%" PRId64
953ffe0f
AF
125#else
126#define FMT_pid "%d"
127#endif
128
aa26bb2d 129int qemu_create_pidfile(const char *filename);
dc7a09cf 130int qemu_get_thread_id(void);
aa26bb2d 131
ad620c29
BS
132#ifdef _WIN32
133static inline void qemu_timersub(const struct timeval *val1,
134 const struct timeval *val2,
135 struct timeval *res)
136{
137 res->tv_sec = val1->tv_sec - val2->tv_sec;
138 if (val1->tv_usec < val2->tv_usec) {
139 res->tv_sec--;
140 res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000;
141 } else {
142 res->tv_usec = val1->tv_usec - val2->tv_usec;
143 }
144}
145#else
146#define qemu_timersub timersub
147#endif
148
ea88812f 149#endif