pymacro.h 5.8 KB

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