X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=osdep.h;h=a81701749aabd096df6b8bee1261026c7e9b3cdd;hb=d25f89c9e91d6c46b85969922411a211a6347a7d;hp=f1d18202b07948d133c1f7658c869857250db9ce;hpb=d2bfb39ad220a6431e366bdff72353b09f60e3db;p=qemu.git diff --git a/osdep.h b/osdep.h index f1d18202b..a81701749 100644 --- a/osdep.h +++ b/osdep.h @@ -2,49 +2,156 @@ #define QEMU_OSDEP_H #include +#include +#ifdef __OpenBSD__ +#include +#include +#endif -int qemu_vsnprintf(char *buf, int buflen, const char *fmt, va_list args); -void qemu_vprintf(const char *fmt, va_list ap); -void qemu_printf(const char *fmt, ...); +#include -void *qemu_malloc(size_t size); -void *qemu_mallocz(size_t size); -void qemu_free(void *ptr); -char *qemu_strdup(const char *str); +#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 -void *get_mmap_addr(unsigned long size); +#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 -/* specific kludges for OS compatibility (should be moved elsewhere) */ -#if defined(__i386__) && !defined(CONFIG_SOFTMMU) && !defined(CONFIG_USER_ONLY) +#ifdef CONFIG_NEED_OFFSETOF +#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *) 0)->MEMBER) +#endif +#ifndef container_of +#define container_of(ptr, type, member) ({ \ + const typeof(((type *) 0)->member) *__mptr = (ptr); \ + (type *) ((char *) __mptr - offsetof(type, member));}) +#endif -/* disabled pthread version of longjmp which prevent us from using an - alternative signal stack */ -extern void __longjmp(jmp_buf env, int val); -#define longjmp __longjmp +/* 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 -#include +#define typeof_field(type, field) typeof(((type *)0)->field) +#define type_check(t1,t2) ((t1*)0 - (t2*)0) -/* NOTE: it works only because the glibc sigset_t is >= kernel sigset_t */ -struct qemu_sigaction { - union { - void (*_sa_handler)(int); - void (*_sa_sigaction)(int, struct siginfo *, void *); - } _u; - unsigned long sa_flags; - void (*sa_restorer)(void); - sigset_t sa_mask; /* mask last for extensibility */ -}; +#ifndef MIN +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) +#endif +#ifndef MAX +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) +#endif -int qemu_sigaction(int signum, const struct qemu_sigaction *act, - struct qemu_sigaction *oldact); +#ifndef DIV_ROUND_UP +#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) +#endif + +#ifndef ARRAY_SIZE +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +#endif + +#ifndef always_inline +#if !((__GNUC__ < 3) || defined(__APPLE__)) +#ifdef __OPTIMIZE__ +#define inline __attribute__ (( always_inline )) __inline__ +#endif +#endif +#else +#define inline always_inline +#endif + +#ifdef __i386__ +#define REGPARM __attribute((regparm(3))) +#else +#define REGPARM +#endif + +#define qemu_printf printf + +#if defined (__GNUC__) && defined (__GNUC_MINOR__) +# define QEMU_GNUC_PREREQ(maj, min) \ + ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) +#else +# define QEMU_GNUC_PREREQ(maj, min) 0 +#endif + +int qemu_daemon(int nochdir, int noclose); +void *qemu_memalign(size_t alignment, size_t size); +void *qemu_vmalloc(size_t size); +void qemu_vfree(void *ptr); + +#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); + +#if defined(__HAIKU__) && defined(__i386__) +#define FMT_pid "%ld" +#else +#define FMT_pid "%d" +#endif -#undef sigaction -#undef sa_handler -#undef sa_sigaction -#define sigaction qemu_sigaction -#define sa_handler _u._sa_handler -#define sa_sigaction _u._sa_sigaction +int qemu_create_pidfile(const char *filename); +int qemu_get_thread_id(void); +#ifdef _WIN32 +static inline void qemu_timersub(const struct timeval *val1, + const struct timeval *val2, + struct timeval *res) +{ + res->tv_sec = val1->tv_sec - val2->tv_sec; + if (val1->tv_usec < val2->tv_usec) { + res->tv_sec--; + res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000; + } else { + res->tv_usec = val1->tv_usec - val2->tv_usec; + } +} +#else +#define qemu_timersub timersub #endif #endif