osdep.h 3.1 KB
Newer Older
1 2 3 4
#ifndef QEMU_OSDEP_H
#define QEMU_OSDEP_H

#include <stdarg.h>
5
#include <stddef.h>
6 7 8 9
#ifdef __OpenBSD__
#include <sys/types.h>
#include <sys/signal.h>
#endif
10

11 12 13 14
#ifndef _WIN32
#include <sys/time.h>
#endif

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#ifndef glue
#define xglue(x, y) x ## y
#define glue(x, y) xglue(x, y)
#define stringify(s)	tostring(s)
#define tostring(s)	#s
#endif

#ifndef likely
#if __GNUC__ < 3
#define __builtin_expect(x, n) (x)
#endif

#define likely(x)   __builtin_expect(!!(x), 1)
#define unlikely(x)   __builtin_expect(!!(x), 0)
#endif

31
#ifdef CONFIG_NEED_OFFSETOF
32 33 34
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *) 0)->MEMBER)
#endif
#ifndef container_of
35
#define container_of(ptr, type, member) ({                      \
36 37 38
        const typeof(((type *) 0)->member) *__mptr = (ptr);     \
        (type *) ((char *) __mptr - offsetof(type, member));})
#endif
39

40 41 42 43 44 45 46 47 48 49
/* Convert from a base type to a parent type, with compile time checking.  */
#ifdef __GNUC__
#define DO_UPCAST(type, field, dev) ( __extension__ ( { \
    char __attribute__((unused)) offset_must_be_zero[ \
        -offsetof(type, field)]; \
    container_of(dev, type, field);}))
#else
#define DO_UPCAST(type, field, dev) container_of(dev, type, field)
#endif

50 51 52
#define typeof_field(type, field) typeof(((type *)0)->field)
#define type_check(t1,t2) ((t1*)0 - (t2*)0)

53 54 55 56 57 58 59
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif

60 61 62 63
#ifndef DIV_ROUND_UP
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
#endif

64 65 66 67
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif

68
#ifndef always_inline
B
Blue Swirl 已提交
69
#if !((__GNUC__ < 3) || defined(__APPLE__))
P
pbrook 已提交
70
#ifdef __OPTIMIZE__
B
Blue Swirl 已提交
71
#define inline __attribute__ (( always_inline )) __inline__
72
#endif
P
pbrook 已提交
73
#endif
74
#else
75
#define inline always_inline
76
#endif
77 78

#ifdef __i386__
79
#define REGPARM __attribute((regparm(3)))
80
#else
81
#define REGPARM
82 83
#endif

B
bellard 已提交
84
#define qemu_printf printf
85

86
#if defined (__GNUC__) && defined (__GNUC_MINOR__)
A
aurel32 已提交
87 88 89 90 91 92
# define QEMU_GNUC_PREREQ(maj, min) \
         ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
#else
# define QEMU_GNUC_PREREQ(maj, min) 0
#endif

93
void *qemu_memalign(size_t alignment, size_t size);
B
bellard 已提交
94 95
void *qemu_vmalloc(size_t size);
void qemu_vfree(void *ptr);
96

A
Andreas Färber 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#define QEMU_MADV_INVALID -1

#if defined(CONFIG_MADVISE)

#define QEMU_MADV_WILLNEED  MADV_WILLNEED
#define QEMU_MADV_DONTNEED  MADV_DONTNEED
#ifdef MADV_DONTFORK
#define QEMU_MADV_DONTFORK  MADV_DONTFORK
#else
#define QEMU_MADV_DONTFORK  QEMU_MADV_INVALID
#endif
#ifdef MADV_MERGEABLE
#define QEMU_MADV_MERGEABLE MADV_MERGEABLE
#else
#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
#endif

#elif defined(CONFIG_POSIX_MADVISE)

#define QEMU_MADV_WILLNEED  POSIX_MADV_WILLNEED
#define QEMU_MADV_DONTNEED  POSIX_MADV_DONTNEED
#define QEMU_MADV_DONTFORK  QEMU_MADV_INVALID
#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID

#else /* no-op */

#define QEMU_MADV_WILLNEED  QEMU_MADV_INVALID
#define QEMU_MADV_DONTNEED  QEMU_MADV_INVALID
#define QEMU_MADV_DONTFORK  QEMU_MADV_INVALID
#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID

#endif

int qemu_madvise(void *addr, size_t len, int advice);

132 133
int qemu_create_pidfile(const char *filename);

134
#endif