pymacro.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #ifndef Py_PYMACRO_H
  2. #define Py_PYMACRO_H
  3. // gh-91782: On FreeBSD 12, if the _POSIX_C_SOURCE and _XOPEN_SOURCE macros are
  4. // defined, <sys/cdefs.h> disables C11 support and <assert.h> does not define
  5. // the static_assert() macro.
  6. // https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=255290
  7. //
  8. // macOS <= 10.10 doesn't define static_assert in assert.h at all despite
  9. // having C11 compiler support.
  10. //
  11. // static_assert is defined in glibc from version 2.16. Compiler support for
  12. // the C11 _Static_assert keyword is in gcc >= 4.6.
  13. //
  14. // MSVC makes static_assert a keyword in C11-17, contrary to the standards.
  15. //
  16. // In C++11 and C2x, static_assert is a keyword, redefining is undefined
  17. // behaviour. So only define if building as C (if __STDC_VERSION__ is defined),
  18. // not C++, and only for C11-17.
  19. #if !defined(static_assert) && (defined(__GNUC__) || defined(__clang__)) \
  20. && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \
  21. && __STDC_VERSION__ <= 201710L
  22. # define static_assert _Static_assert
  23. #endif
  24. /* Minimum value between x and y */
  25. #define Py_MIN(x, y) (((x) > (y)) ? (y) : (x))
  26. /* Maximum value between x and y */
  27. #define Py_MAX(x, y) (((x) > (y)) ? (x) : (y))
  28. /* Absolute value of the number x */
  29. #define Py_ABS(x) ((x) < 0 ? -(x) : (x))
  30. #define _Py_XSTRINGIFY(x) #x
  31. /* Convert the argument to a string. For example, Py_STRINGIFY(123) is replaced
  32. with "123" by the preprocessor. Defines are also replaced by their value.
  33. For example Py_STRINGIFY(__LINE__) is replaced by the line number, not
  34. by "__LINE__". */
  35. #define Py_STRINGIFY(x) _Py_XSTRINGIFY(x)
  36. /* Get the size of a structure member in bytes */
  37. #define Py_MEMBER_SIZE(type, member) sizeof(((type *)0)->member)
  38. /* Argument must be a char or an int in [-128, 127] or [0, 255]. */
  39. #define Py_CHARMASK(c) ((unsigned char)((c) & 0xff))
  40. /* Assert a build-time dependency, as an expression.
  41. Your compile will fail if the condition isn't true, or can't be evaluated
  42. by the compiler. This can be used in an expression: its value is 0.
  43. Example:
  44. #define foo_to_char(foo) \
  45. ((char *)(foo) \
  46. + Py_BUILD_ASSERT_EXPR(offsetof(struct foo, string) == 0))
  47. Written by Rusty Russell, public domain, http://ccodearchive.net/ */
  48. #define Py_BUILD_ASSERT_EXPR(cond) \
  49. (sizeof(char [1 - 2*!(cond)]) - 1)
  50. #define Py_BUILD_ASSERT(cond) do { \
  51. (void)Py_BUILD_ASSERT_EXPR(cond); \
  52. } while(0)
  53. /* Get the number of elements in a visible array
  54. This does not work on pointers, or arrays declared as [], or function
  55. parameters. With correct compiler support, such usage will cause a build
  56. error (see Py_BUILD_ASSERT_EXPR).
  57. Written by Rusty Russell, public domain, http://ccodearchive.net/
  58. Requires at GCC 3.1+ */
  59. #if (defined(__GNUC__) && !defined(__STRICT_ANSI__) && \
  60. (((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)) || (__GNUC__ >= 4)))
  61. /* Two gcc extensions.
  62. &a[0] degrades to a pointer: a different type from an array */
  63. #define Py_ARRAY_LENGTH(array) \
  64. (sizeof(array) / sizeof((array)[0]) \
  65. + Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(typeof(array), \
  66. typeof(&(array)[0]))))
  67. #else
  68. #define Py_ARRAY_LENGTH(array) \
  69. (sizeof(array) / sizeof((array)[0]))
  70. #endif
  71. /* Define macros for inline documentation. */
  72. #define PyDoc_VAR(name) static const char name[]
  73. #define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str)
  74. #ifdef WITH_DOC_STRINGS
  75. #define PyDoc_STR(str) str
  76. #else
  77. #define PyDoc_STR(str) ""
  78. #endif
  79. /* Below "a" is a power of 2. */
  80. /* Round down size "n" to be a multiple of "a". */
  81. #define _Py_SIZE_ROUND_DOWN(n, a) ((size_t)(n) & ~(size_t)((a) - 1))
  82. /* Round up size "n" to be a multiple of "a". */
  83. #define _Py_SIZE_ROUND_UP(n, a) (((size_t)(n) + \
  84. (size_t)((a) - 1)) & ~(size_t)((a) - 1))
  85. /* Round pointer "p" down to the closest "a"-aligned address <= "p". */
  86. #define _Py_ALIGN_DOWN(p, a) ((void *)((uintptr_t)(p) & ~(uintptr_t)((a) - 1)))
  87. /* Round pointer "p" up to the closest "a"-aligned address >= "p". */
  88. #define _Py_ALIGN_UP(p, a) ((void *)(((uintptr_t)(p) + \
  89. (uintptr_t)((a) - 1)) & ~(uintptr_t)((a) - 1)))
  90. /* Check if pointer "p" is aligned to "a"-bytes boundary. */
  91. #define _Py_IS_ALIGNED(p, a) (!((uintptr_t)(p) & (uintptr_t)((a) - 1)))
  92. /* Use this for unused arguments in a function definition to silence compiler
  93. * warnings. Example:
  94. *
  95. * int func(int a, int Py_UNUSED(b)) { return a; }
  96. */
  97. #if defined(__GNUC__) || defined(__clang__)
  98. # define Py_UNUSED(name) _unused_ ## name __attribute__((unused))
  99. #else
  100. # define Py_UNUSED(name) _unused_ ## name
  101. #endif
  102. #if defined(RANDALL_WAS_HERE)
  103. # define Py_UNREACHABLE() \
  104. Py_FatalError( \
  105. "If you're seeing this, the code is in what I thought was\n" \
  106. "an unreachable state.\n\n" \
  107. "I could give you advice for what to do, but honestly, why\n" \
  108. "should you trust me? I clearly screwed this up. I'm writing\n" \
  109. "a message that should never appear, yet I know it will\n" \
  110. "probably appear someday.\n\n" \
  111. "On a deep level, I know I'm not up to this task.\n" \
  112. "I'm so sorry.\n" \
  113. "https://xkcd.com/2200")
  114. #elif defined(Py_DEBUG)
  115. # define Py_UNREACHABLE() \
  116. Py_FatalError( \
  117. "We've reached an unreachable state. Anything is possible.\n" \
  118. "The limits were in our heads all along. Follow your dreams.\n" \
  119. "https://xkcd.com/2200")
  120. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
  121. # define Py_UNREACHABLE() __builtin_unreachable()
  122. #elif defined(__clang__) || defined(__INTEL_COMPILER)
  123. # define Py_UNREACHABLE() __builtin_unreachable()
  124. #elif defined(_MSC_VER)
  125. # define Py_UNREACHABLE() __assume(0)
  126. #else
  127. # define Py_UNREACHABLE() \
  128. Py_FatalError("Unreachable C code path reached")
  129. #endif
  130. // Prevent using an expression as a l-value.
  131. // For example, "int x; _Py_RVALUE(x) = 1;" fails with a compiler error.
  132. #define _Py_RVALUE(EXPR) ((void)0, (EXPR))
  133. // Return non-zero if the type is signed, return zero if it's unsigned.
  134. // Use "<= 0" rather than "< 0" to prevent the compiler warning:
  135. // "comparison of unsigned expression in '< 0' is always false".
  136. #define _Py_IS_TYPE_SIGNED(type) ((type)(-1) <= 0)
  137. #endif /* Py_PYMACRO_H */