blob: a81701749aabd096df6b8bee1261026c7e9b3cdd [file] [log] [blame]
bellardea888122004-02-16 22:12:40 +00001#ifndef QEMU_OSDEP_H
2#define QEMU_OSDEP_H
3
4#include <stdarg.h>
Blue Swirlde5071c2009-09-12 09:58:46 +00005#include <stddef.h>
blueswir1128ab2f2008-08-15 18:33:42 +00006#ifdef __OpenBSD__
7#include <sys/types.h>
8#include <sys/signal.h>
9#endif
bellardea888122004-02-16 22:12:40 +000010
aliguorif7b4a942009-01-07 17:40:15 +000011#include <sys/time.h>
aliguorif7b4a942009-01-07 17:40:15 +000012
j_mayerdf2542c2007-11-19 00:38:33 +000013#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
Blue Swirlde5071c2009-09-12 09:58:46 +000029#ifdef CONFIG_NEED_OFFSETOF
balrogac509d82008-09-16 13:36:57 +000030#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *) 0)->MEMBER)
31#endif
32#ifndef container_of
aliguori62a6e3e2008-08-21 20:11:11 +000033#define container_of(ptr, type, member) ({ \
balrogac509d82008-09-16 13:36:57 +000034 const typeof(((type *) 0)->member) *__mptr = (ptr); \
35 (type *) ((char *) __mptr - offsetof(type, member));})
36#endif
aliguori62a6e3e2008-08-21 20:11:11 +000037
Mark McLoughlin5096fae2009-11-25 18:49:03 +000038/* 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
47
Juan Quintelaf0d99ad2009-08-20 19:42:19 +020048#define typeof_field(type, field) typeof(((type *)0)->field)
49#define type_check(t1,t2) ((t1*)0 - (t2*)0)
50
j_mayerdf2542c2007-11-19 00:38:33 +000051#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
57
Corentin Charye0e53b22011-02-04 09:06:04 +010058#ifndef DIV_ROUND_UP
59#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
60#endif
61
blueswir10954d0d2008-03-11 21:01:02 +000062#ifndef ARRAY_SIZE
63#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
64#endif
65
j_mayerdf2542c2007-11-19 00:38:33 +000066#ifndef always_inline
Blue Swirl636aa202009-08-16 09:06:54 +000067#if !((__GNUC__ < 3) || defined(__APPLE__))
pbrook3dec6ec2008-11-19 01:31:52 +000068#ifdef __OPTIMIZE__
Blue Swirl636aa202009-08-16 09:06:54 +000069#define inline __attribute__ (( always_inline )) __inline__
thscebdff72008-06-05 22:55:54 +000070#endif
pbrook3dec6ec2008-11-19 01:31:52 +000071#endif
thscebdff72008-06-05 22:55:54 +000072#else
73#define inline always_inline
74#endif
j_mayerdf2542c2007-11-19 00:38:33 +000075
76#ifdef __i386__
bellardd6564692008-01-31 09:22:27 +000077#define REGPARM __attribute((regparm(3)))
j_mayerdf2542c2007-11-19 00:38:33 +000078#else
bellardd6564692008-01-31 09:22:27 +000079#define REGPARM
j_mayerdf2542c2007-11-19 00:38:33 +000080#endif
81
bellardd62ca2b2006-08-01 15:50:14 +000082#define qemu_printf printf
bellardea888122004-02-16 22:12:40 +000083
balrog80fe30e2008-12-01 01:53:55 +000084#if defined (__GNUC__) && defined (__GNUC_MINOR__)
aurel32bad5b1e2008-10-12 16:15:04 +000085# define QEMU_GNUC_PREREQ(maj, min) \
86 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
87#else
88# define QEMU_GNUC_PREREQ(maj, min) 0
89#endif
90
Alexandre Raymondf97742d2011-06-06 23:34:10 -040091int qemu_daemon(int nochdir, int noclose);
balrog33f00272007-12-24 14:33:24 +000092void *qemu_memalign(size_t alignment, size_t size);
bellard49b470e2005-02-10 21:59:25 +000093void *qemu_vmalloc(size_t size);
94void qemu_vfree(void *ptr);
95
Andreas Färbere78815a2010-09-25 11:26:05 +000096#define QEMU_MADV_INVALID -1
97
98#if defined(CONFIG_MADVISE)
99
100#define QEMU_MADV_WILLNEED MADV_WILLNEED
101#define QEMU_MADV_DONTNEED MADV_DONTNEED
102#ifdef MADV_DONTFORK
103#define QEMU_MADV_DONTFORK MADV_DONTFORK
104#else
105#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
106#endif
107#ifdef MADV_MERGEABLE
108#define QEMU_MADV_MERGEABLE MADV_MERGEABLE
109#else
110#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
111#endif
112
113#elif defined(CONFIG_POSIX_MADVISE)
114
115#define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED
116#define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED
117#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
118#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
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
126
127#endif
128
129int qemu_madvise(void *addr, size_t len, int advice);
130
Andreas Färber953ffe02011-06-02 19:58:06 +0200131#if defined(__HAIKU__) && defined(__i386__)
132#define FMT_pid "%ld"
133#else
134#define FMT_pid "%d"
135#endif
136
thsaa26bb22007-03-25 21:33:06 +0000137int qemu_create_pidfile(const char *filename);
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100138int qemu_get_thread_id(void);
thsaa26bb22007-03-25 21:33:06 +0000139
Blue Swirlad620c22011-03-13 10:30:52 +0000140#ifdef _WIN32
141static inline void qemu_timersub(const struct timeval *val1,
142 const struct timeval *val2,
143 struct timeval *res)
144{
145 res->tv_sec = val1->tv_sec - val2->tv_sec;
146 if (val1->tv_usec < val2->tv_usec) {
147 res->tv_sec--;
148 res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000;
149 } else {
150 res->tv_usec = val1->tv_usec - val2->tv_usec;
151 }
152}
153#else
154#define qemu_timersub timersub
155#endif
156
bellardea888122004-02-16 22:12:40 +0000157#endif