compiler.h 2.9 KB

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