compiler.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef COMPILER_H
  2. #define COMPILER_H
  3. #ifndef __ASSEMBLY__
  4. #include <stddef.h>
  5. #include <errno.h>
  6. #include <inttypes.h>
  7. #include <limits.h>
  8. #include <stdarg.h>
  9. #include <stdbool.h>
  10. #include <stdlib.h>
  11. #ifdef __cplusplus
  12. # define extern_c extern "C"
  13. # define EXTERN_C(...) extern "C" { __VA_ARGS__ }
  14. #else
  15. # define extern_c extern
  16. # define EXTERN_C(...) __VA_ARGS__
  17. #endif
  18. #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
  19. #undef likely
  20. #define likely(x) __builtin_expect(!!(x), 1)
  21. #undef unlikely
  22. #define unlikely(x) __builtin_expect(!!(x), 0)
  23. /* Handy composite pointer types */
  24. typedef union xptr {
  25. uint32_t *l;
  26. uint16_t *w;
  27. uint8_t *b;
  28. void *v;
  29. size_t a;
  30. } xptr_t;
  31. typedef union xcptr {
  32. const uint32_t *l;
  33. const uint16_t *w;
  34. const uint8_t *b;
  35. const void *v;
  36. size_t a;
  37. } xcptr_t;
  38. /* Subword types */
  39. typedef union {
  40. uint8_t b[8];
  41. uint16_t h[4];
  42. uint32_t l[2];
  43. uint64_t q;
  44. } qword_t;
  45. typedef union {
  46. uint8_t b[4];
  47. uint16_t h[2];
  48. uint32_t l;
  49. } lword_t;
  50. typedef union {
  51. uint8_t b[2];
  52. uint16_t h;
  53. } hword_t;
  54. /* The container_of construct: if p is a pointer to member m of
  55. container class c, then return a pointer to the container of which
  56. *p is a member. */
  57. #ifndef container_of
  58. # define container_of(p, c, m) ((c *)((char *)(p) - offsetof(c,m)))
  59. #endif
  60. #define offset_diff(c, m1, m2) ((ptrdiff_t)offsetof(c,m2) - \
  61. (ptrdiff_t)offsetof(c,m1))
  62. /* Type-safeish comparison macros */
  63. #define Min(a,b) ({ \
  64. __typeof__((a)*(b)) __a = (a); \
  65. __typeof__((a)*(b)) __b = (b); \
  66. (__a < __b) ? __a : __b; \
  67. })
  68. #define Max(a,b) ({ \
  69. __typeof__((a)*(b)) __a = (a); \
  70. __typeof__((a)*(b)) __b = (b); \
  71. (__a > __b) ? __a : __b; \
  72. })
  73. #if SIZE_MAX == UINT16_MAX
  74. # define SIZE_BITS 16
  75. typedef uint32_t size2_t;
  76. #elif SIZE_MAX == UINT32_MAX
  77. # define SIZE_BITS 32
  78. typedef uint64_t size2_t;
  79. #elif SIZE_MAX == UINT64_MAX
  80. # define SIZE_BITS 64
  81. typedef unsigned __int128 size2_t;
  82. #else
  83. # error "Unknown size_t"
  84. #endif
  85. #define alignof(a) __alignof__(a)
  86. #define no_return void __attribute__((noreturn))
  87. #define ___section(s,a,...) __attribute__((__section__(s)))
  88. #define __hot __attribute__((__hot__))
  89. #define __cold __attribute__((__cold__))
  90. #define __aligned(x) __attribute__((__aligned__(x)))
  91. #define __unused __attribute__((__unused__))
  92. #define __must_inline __attribute__((__always_inline__))
  93. #define __noinline __attribute__ ((__noinline__))
  94. #define __constfunc __attribute__((__const__))
  95. #define __purefunc __attribute__((__pure__))
  96. #undef __alloc_size
  97. #define __alloc_size(...) __attribute__((__alloc_size__(__VA_ARGS__)))
  98. #define __malloc_func __attribute__((__malloc__))
  99. #define __fmt_printf(fstr,farg) __attribute__((__format__(__printf__,fstr,farg)))
  100. #define __nonnull_arg(...) __attribute__((__nonnull__(__VA_ARGS__)))
  101. #define __no_return void __attribute__((__noreturn__))
  102. #define __nonnull_ret __attribute__((__returns_nonnull__))
  103. #define __packed __attribute__((__packed__))
  104. #define __safe_alloc(...) __nonnull_ret __alloc_size(__VA_ARGS__)
  105. #define __is_constant(expr) __builtin_constant_p(expr)
  106. #ifndef __cplusplus
  107. # define atomic(x) (*(volatile __typeof__(x) *)&(x))
  108. #endif
  109. #else /* __ASSEMBLY__ */
  110. #define ___section(s,a,...) .pushsection s, a, ## __VA_ARGS__
  111. #endif /* __ASSEMBLY__ */
  112. #endif /* COMPILER_H */