pymacro.h 4.8 KB

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