You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

87 lines
2.2 KiB

  1. #pragma once
  2. #define VERCMP(x, y, cx, cy) ((cx > x) || ((cx == x) && (cy >= y)))
  3. #if defined(__GNUC__) && defined(__GNUC_MINOR__)
  4. #define GNUC_AT_LEAST(x, y) VERCMP(x, y, __GNUC__, __GNUC_MINOR__)
  5. #else
  6. #define GNUC_AT_LEAST(x, y) 0
  7. #endif
  8. #ifdef __has_attribute
  9. #define HAS_ATTRIBUTE(x) __has_attribute(x)
  10. #else
  11. #define HAS_ATTRIBUTE(x) 0
  12. #endif
  13. #ifdef __has_builtin
  14. #define HAS_BUILTIN(x) __has_builtin(x)
  15. #else
  16. #define HAS_BUILTIN(x) 0
  17. #endif
  18. #if GNUC_AT_LEAST(3, 0) || HAS_ATTRIBUTE(unused) || defined(__TINYC__)
  19. #define UNUSED __attribute__((__unused__))
  20. #else
  21. #define UNUSED
  22. #endif
  23. #if GNUC_AT_LEAST(3, 0) || HAS_ATTRIBUTE(malloc)
  24. #define MALLOC __attribute__((__malloc__))
  25. #else
  26. #define MALLOC
  27. #endif
  28. #if GNUC_AT_LEAST(3, 0) || HAS_ATTRIBUTE(format)
  29. #define PRINTF(x) __attribute__((__format__(__printf__, (x), (x + 1))))
  30. #define VPRINTF(x) __attribute__((__format__(__printf__, (x), 0)))
  31. #else
  32. #define PRINTF(x)
  33. #define VPRINTF(x)
  34. #endif
  35. #if (GNUC_AT_LEAST(3, 0) || HAS_BUILTIN(__builtin_expect)) && defined(__OPTIMIZE__)
  36. #define likely(x) __builtin_expect(!!(x), 1)
  37. #define unlikely(x) __builtin_expect(!!(x), 0)
  38. #else
  39. #define likely(x) (x)
  40. #define unlikely(x) (x)
  41. #endif
  42. #if GNUC_AT_LEAST(3, 3) || HAS_ATTRIBUTE(nonnull)
  43. #define NONNULL_ARGS __attribute__((__nonnull__))
  44. #define NONNULL_ARG(...) __attribute__((__nonnull__(__VA_ARGS__)))
  45. #else
  46. #define NONNULL_ARGS
  47. #define NONNULL_ARG(...)
  48. #endif
  49. #if GNUC_AT_LEAST(3, 4) || HAS_ATTRIBUTE(warn_unused_result)
  50. #define WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
  51. #else
  52. #define WARN_UNUSED_RESULT
  53. #endif
  54. #if GNUC_AT_LEAST(4, 3) || HAS_ATTRIBUTE(cold)
  55. #define COLD __attribute__((__cold__))
  56. #else
  57. #define COLD
  58. #endif
  59. #if GNUC_AT_LEAST(5, 0) || HAS_ATTRIBUTE(returns_nonnull)
  60. #define RETURNS_NONNULL __attribute__((__returns_nonnull__))
  61. #else
  62. #define RETURNS_NONNULL
  63. #endif
  64. #define XMALLOC MALLOC RETURNS_NONNULL WARN_UNUSED_RESULT
  65. #define XSTRDUP XMALLOC NONNULL_ARGS
  66. #if __STDC_VERSION__ >= 201112L
  67. #define NORETURN _Noreturn
  68. #elif GNUC_AT_LEAST(3, 0)
  69. #define NORETURN __attribute__((__noreturn__))
  70. #else
  71. #define NORETURN
  72. #endif