blob: 75b581630f2a26e7c374884f7b8b700232d5eca3 [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#ifndef _WIN32
12#include <sys/time.h>
13#endif
14
j_mayerdf2542c2007-11-19 00:38:33 +000015#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 Swirlde5071c2009-09-12 09:58:46 +000031#ifdef CONFIG_NEED_OFFSETOF
balrogac509d82008-09-16 13:36:57 +000032#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *) 0)->MEMBER)
33#endif
34#ifndef container_of
aliguori62a6e3e2008-08-21 20:11:11 +000035#define container_of(ptr, type, member) ({ \
balrogac509d82008-09-16 13:36:57 +000036 const typeof(((type *) 0)->member) *__mptr = (ptr); \
37 (type *) ((char *) __mptr - offsetof(type, member));})
38#endif
aliguori62a6e3e2008-08-21 20:11:11 +000039
Mark McLoughlin5096fae2009-11-25 18:49:03 +000040/* 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 Quintelaf0d99ad2009-08-20 19:42:19 +020050#define typeof_field(type, field) typeof(((type *)0)->field)
51#define type_check(t1,t2) ((t1*)0 - (t2*)0)
52
j_mayerdf2542c2007-11-19 00:38:33 +000053#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
blueswir10954d0d2008-03-11 21:01:02 +000060#ifndef ARRAY_SIZE
61#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
62#endif
63
j_mayerdf2542c2007-11-19 00:38:33 +000064#ifndef always_inline
Blue Swirl636aa202009-08-16 09:06:54 +000065#if !((__GNUC__ < 3) || defined(__APPLE__))
pbrook3dec6ec2008-11-19 01:31:52 +000066#ifdef __OPTIMIZE__
Blue Swirl636aa202009-08-16 09:06:54 +000067#define inline __attribute__ (( always_inline )) __inline__
thscebdff72008-06-05 22:55:54 +000068#endif
pbrook3dec6ec2008-11-19 01:31:52 +000069#endif
thscebdff72008-06-05 22:55:54 +000070#else
71#define inline always_inline
72#endif
j_mayerdf2542c2007-11-19 00:38:33 +000073
74#ifdef __i386__
bellardd6564692008-01-31 09:22:27 +000075#define REGPARM __attribute((regparm(3)))
j_mayerdf2542c2007-11-19 00:38:33 +000076#else
bellardd6564692008-01-31 09:22:27 +000077#define REGPARM
j_mayerdf2542c2007-11-19 00:38:33 +000078#endif
79
bellardd62ca2b2006-08-01 15:50:14 +000080#define qemu_printf printf
bellardea888122004-02-16 22:12:40 +000081
balrog80fe30e2008-12-01 01:53:55 +000082#if defined (__GNUC__) && defined (__GNUC_MINOR__)
aurel32bad5b1e2008-10-12 16:15:04 +000083# define QEMU_GNUC_PREREQ(maj, min) \
84 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
85#else
86# define QEMU_GNUC_PREREQ(maj, min) 0
87#endif
88
balrog33f00272007-12-24 14:33:24 +000089void *qemu_memalign(size_t alignment, size_t size);
bellard49b470e2005-02-10 21:59:25 +000090void *qemu_vmalloc(size_t size);
91void qemu_vfree(void *ptr);
92
thsaa26bb22007-03-25 21:33:06 +000093int qemu_create_pidfile(const char *filename);
94
pbrook29b3a662007-06-07 23:09:47 +000095#ifdef _WIN32
balrogc6d29ad2007-12-16 12:55:24 +000096int ffs(int i);
97
pbrook29b3a662007-06-07 23:09:47 +000098typedef struct {
99 long tv_sec;
100 long tv_usec;
101} qemu_timeval;
102int qemu_gettimeofday(qemu_timeval *tp);
103#else
104typedef struct timeval qemu_timeval;
105#define qemu_gettimeofday(tp) gettimeofday(tp, NULL);
106#endif /* !_WIN32 */
107
bellardea888122004-02-16 22:12:40 +0000108#endif