osdep.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #ifndef QEMU_OSDEP_H
  2. #define QEMU_OSDEP_H
  3. #include <stdarg.h>
  4. #include <stddef.h>
  5. #ifdef __OpenBSD__
  6. #include <sys/types.h>
  7. #include <sys/signal.h>
  8. #endif
  9. #include <sys/time.h>
  10. #if defined(CONFIG_SOLARIS) && CONFIG_SOLARIS_VERSION < 10
  11. /* [u]int_fast*_t not in <sys/int_types.h> */
  12. typedef unsigned char uint_fast8_t;
  13. typedef unsigned int uint_fast16_t;
  14. typedef signed int int_fast16_t;
  15. #endif
  16. #ifndef glue
  17. #define xglue(x, y) x ## y
  18. #define glue(x, y) xglue(x, y)
  19. #define stringify(s) tostring(s)
  20. #define tostring(s) #s
  21. #endif
  22. #ifndef likely
  23. #if __GNUC__ < 3
  24. #define __builtin_expect(x, n) (x)
  25. #endif
  26. #define likely(x) __builtin_expect(!!(x), 1)
  27. #define unlikely(x) __builtin_expect(!!(x), 0)
  28. #endif
  29. #ifndef container_of
  30. #define container_of(ptr, type, member) ({ \
  31. const typeof(((type *) 0)->member) *__mptr = (ptr); \
  32. (type *) ((char *) __mptr - offsetof(type, member));})
  33. #endif
  34. /* Convert from a base type to a parent type, with compile time checking. */
  35. #ifdef __GNUC__
  36. #define DO_UPCAST(type, field, dev) ( __extension__ ( { \
  37. char __attribute__((unused)) offset_must_be_zero[ \
  38. -offsetof(type, field)]; \
  39. container_of(dev, type, field);}))
  40. #else
  41. #define DO_UPCAST(type, field, dev) container_of(dev, type, field)
  42. #endif
  43. #define typeof_field(type, field) typeof(((type *)0)->field)
  44. #define type_check(t1,t2) ((t1*)0 - (t2*)0)
  45. #ifndef MIN
  46. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  47. #endif
  48. #ifndef MAX
  49. #define MAX(a, b) (((a) > (b)) ? (a) : (b))
  50. #endif
  51. #ifndef DIV_ROUND_UP
  52. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  53. #endif
  54. #ifndef ARRAY_SIZE
  55. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  56. #endif
  57. #ifndef always_inline
  58. #if !((__GNUC__ < 3) || defined(__APPLE__))
  59. #ifdef __OPTIMIZE__
  60. #define inline __attribute__ (( always_inline )) __inline__
  61. #endif
  62. #endif
  63. #else
  64. #define inline always_inline
  65. #endif
  66. #define qemu_printf printf
  67. int qemu_daemon(int nochdir, int noclose);
  68. void *qemu_memalign(size_t alignment, size_t size);
  69. void *qemu_vmalloc(size_t size);
  70. void qemu_vfree(void *ptr);
  71. #define QEMU_MADV_INVALID -1
  72. #if defined(CONFIG_MADVISE)
  73. #define QEMU_MADV_WILLNEED MADV_WILLNEED
  74. #define QEMU_MADV_DONTNEED MADV_DONTNEED
  75. #ifdef MADV_DONTFORK
  76. #define QEMU_MADV_DONTFORK MADV_DONTFORK
  77. #else
  78. #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
  79. #endif
  80. #ifdef MADV_MERGEABLE
  81. #define QEMU_MADV_MERGEABLE MADV_MERGEABLE
  82. #else
  83. #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
  84. #endif
  85. #elif defined(CONFIG_POSIX_MADVISE)
  86. #define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED
  87. #define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED
  88. #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
  89. #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
  90. #else /* no-op */
  91. #define QEMU_MADV_WILLNEED QEMU_MADV_INVALID
  92. #define QEMU_MADV_DONTNEED QEMU_MADV_INVALID
  93. #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
  94. #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
  95. #endif
  96. int qemu_madvise(void *addr, size_t len, int advice);
  97. #if defined(__HAIKU__) && defined(__i386__)
  98. #define FMT_pid "%ld"
  99. #elif defined(WIN64)
  100. #define FMT_pid "%" PRId64
  101. #else
  102. #define FMT_pid "%d"
  103. #endif
  104. int qemu_create_pidfile(const char *filename);
  105. int qemu_get_thread_id(void);
  106. #ifdef _WIN32
  107. static inline void qemu_timersub(const struct timeval *val1,
  108. const struct timeval *val2,
  109. struct timeval *res)
  110. {
  111. res->tv_sec = val1->tv_sec - val2->tv_sec;
  112. if (val1->tv_usec < val2->tv_usec) {
  113. res->tv_sec--;
  114. res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000;
  115. } else {
  116. res->tv_usec = val1->tv_usec - val2->tv_usec;
  117. }
  118. }
  119. #else
  120. #define qemu_timersub timersub
  121. #endif
  122. void qemu_set_cloexec(int fd);
  123. #endif