2
0

compiler.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* compiler.h: macros to abstract away compiler specifics
  2. *
  3. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  4. * See the COPYING file in the top-level directory.
  5. */
  6. #ifndef COMPILER_H
  7. #define COMPILER_H
  8. #define HOST_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  9. /* HOST_LONG_BITS is the size of a native pointer in bits. */
  10. #define HOST_LONG_BITS (__SIZEOF_POINTER__ * 8)
  11. #if defined __clang_analyzer__ || defined __COVERITY__
  12. #define QEMU_STATIC_ANALYSIS 1
  13. #endif
  14. #ifdef __cplusplus
  15. #define QEMU_EXTERN_C extern "C"
  16. #else
  17. #define QEMU_EXTERN_C extern
  18. #endif
  19. #if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__))
  20. # define QEMU_PACKED __attribute__((gcc_struct, packed))
  21. #else
  22. # define QEMU_PACKED __attribute__((packed))
  23. #endif
  24. #define QEMU_ALIGNED(X) __attribute__((aligned(X)))
  25. #ifndef glue
  26. #define xglue(x, y) x ## y
  27. #define glue(x, y) xglue(x, y)
  28. #define stringify(s) tostring(s)
  29. #define tostring(s) #s
  30. #endif
  31. #ifndef likely
  32. #define likely(x) __builtin_expect(!!(x), 1)
  33. #define unlikely(x) __builtin_expect(!!(x), 0)
  34. #endif
  35. #ifndef container_of
  36. #define container_of(ptr, type, member) ({ \
  37. const typeof(((type *) 0)->member) *__mptr = (ptr); \
  38. (type *) ((char *) __mptr - offsetof(type, member));})
  39. #endif
  40. #define sizeof_field(type, field) sizeof(((type *)0)->field)
  41. /*
  42. * Calculate the number of bytes up to and including the given 'field' of
  43. * 'container'.
  44. */
  45. #define endof(container, field) \
  46. (offsetof(container, field) + sizeof_field(container, field))
  47. /* Convert from a base type to a parent type, with compile time checking. */
  48. #define DO_UPCAST(type, field, dev) ( __extension__ ( { \
  49. char __attribute__((unused)) offset_must_be_zero[ \
  50. -offsetof(type, field)]; \
  51. container_of(dev, type, field);}))
  52. #define typeof_field(type, field) typeof(((type *)0)->field)
  53. #define type_check(t1,t2) ((t1*)0 - (t2*)0)
  54. #define QEMU_BUILD_BUG_ON_STRUCT(x) \
  55. struct { \
  56. int:(x) ? -1 : 1; \
  57. }
  58. #define QEMU_BUILD_BUG_MSG(x, msg) _Static_assert(!(x), msg)
  59. #define QEMU_BUILD_BUG_ON(x) QEMU_BUILD_BUG_MSG(x, "not expecting: " #x)
  60. #define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \
  61. sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))
  62. #if !defined(__clang__) && defined(_WIN32)
  63. /*
  64. * Map __printf__ to __gnu_printf__ because we want standard format strings even
  65. * when MinGW or GLib include files use __printf__.
  66. */
  67. # define __printf__ __gnu_printf__
  68. #endif
  69. #ifndef __has_warning
  70. #define __has_warning(x) 0 /* compatibility with non-clang compilers */
  71. #endif
  72. #ifndef __has_feature
  73. #define __has_feature(x) 0 /* compatibility with non-clang compilers */
  74. #endif
  75. #ifndef __has_builtin
  76. #define __has_builtin(x) 0 /* compatibility with non-clang compilers */
  77. #endif
  78. #if __has_builtin(__builtin_assume_aligned) || !defined(__clang__)
  79. #define HAS_ASSUME_ALIGNED
  80. #endif
  81. #ifndef __has_attribute
  82. #define __has_attribute(x) 0 /* compatibility with older GCC */
  83. #endif
  84. #if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)
  85. # define QEMU_SANITIZE_ADDRESS 1
  86. #endif
  87. #if defined(__SANITIZE_THREAD__) || __has_feature(thread_sanitizer)
  88. # define QEMU_SANITIZE_THREAD 1
  89. #endif
  90. /*
  91. * GCC doesn't provide __has_attribute() until GCC 5, but we know all the GCC
  92. * versions we support have the "flatten" attribute. Clang may not have the
  93. * "flatten" attribute but always has __has_attribute() to check for it.
  94. */
  95. #if __has_attribute(flatten) || !defined(__clang__)
  96. # define QEMU_FLATTEN __attribute__((flatten))
  97. #else
  98. # define QEMU_FLATTEN
  99. #endif
  100. /*
  101. * If __attribute__((error)) is present, use it to produce an error at
  102. * compile time. Otherwise, one must wait for the linker to diagnose
  103. * the missing symbol.
  104. */
  105. #if __has_attribute(error)
  106. # define QEMU_ERROR(X) __attribute__((error(X)))
  107. #else
  108. # define QEMU_ERROR(X)
  109. #endif
  110. /*
  111. * The nonstring variable attribute specifies that an object or member
  112. * declaration with type array of char or pointer to char is intended
  113. * to store character arrays that do not necessarily contain a terminating
  114. * NUL character. This is useful in detecting uses of such arrays or pointers
  115. * with functions that expect NUL-terminated strings, and to avoid warnings
  116. * when such an array or pointer is used as an argument to a bounded string
  117. * manipulation function such as strncpy.
  118. */
  119. #if __has_attribute(nonstring)
  120. # define QEMU_NONSTRING __attribute__((nonstring))
  121. #else
  122. # define QEMU_NONSTRING
  123. #endif
  124. /*
  125. * Forced inlining may be desired to encourage constant propagation
  126. * of function parameters. However, it can also make debugging harder,
  127. * so disable it for a non-optimizing build.
  128. */
  129. #if defined(__OPTIMIZE__)
  130. #define QEMU_ALWAYS_INLINE __attribute__((always_inline))
  131. #else
  132. #define QEMU_ALWAYS_INLINE
  133. #endif
  134. /**
  135. * In most cases, normal "fallthrough" comments are good enough for
  136. * switch-case statements, but sometimes the compiler has problems
  137. * with those. In that case you can use QEMU_FALLTHROUGH instead.
  138. */
  139. #if __has_attribute(fallthrough)
  140. # define QEMU_FALLTHROUGH __attribute__((fallthrough))
  141. #else
  142. # define QEMU_FALLTHROUGH do {} while (0) /* fallthrough */
  143. #endif
  144. #ifdef CONFIG_CFI
  145. /*
  146. * If CFI is enabled, use an attribute to disable cfi-icall on the following
  147. * function
  148. */
  149. #define QEMU_DISABLE_CFI __attribute__((no_sanitize("cfi-icall")))
  150. #else
  151. /* If CFI is not enabled, use an empty define to not change the behavior */
  152. #define QEMU_DISABLE_CFI
  153. #endif
  154. #endif /* COMPILER_H */