pymacro.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. /* Argument must be a char or an int in [-128, 127] or [0, 255]. */
  16. #define Py_CHARMASK(c) ((unsigned char)((c) & 0xff))
  17. /* Assert a build-time dependency, as an expression.
  18. Your compile will fail if the condition isn't true, or can't be evaluated
  19. by the compiler. This can be used in an expression: its value is 0.
  20. Example:
  21. #define foo_to_char(foo) \
  22. ((char *)(foo) \
  23. + Py_BUILD_ASSERT_EXPR(offsetof(struct foo, string) == 0))
  24. Written by Rusty Russell, public domain, http://ccodearchive.net/ */
  25. #define Py_BUILD_ASSERT_EXPR(cond) \
  26. (sizeof(char [1 - 2*!(cond)]) - 1)
  27. /* Get the number of elements in a visible array
  28. This does not work on pointers, or arrays declared as [], or function
  29. parameters. With correct compiler support, such usage will cause a build
  30. error (see Py_BUILD_ASSERT_EXPR).
  31. Written by Rusty Russell, public domain, http://ccodearchive.net/
  32. Requires at GCC 3.1+ */
  33. #if (defined(__GNUC__) && !defined(__STRICT_ANSI__) && \
  34. (((__GNUC__ == 3) && (__GNU_MINOR__ >= 1)) || (__GNUC__ >= 4)))
  35. /* Two gcc extensions.
  36. &a[0] degrades to a pointer: a different type from an array */
  37. #define Py_ARRAY_LENGTH(array) \
  38. (sizeof(array) / sizeof((array)[0]) \
  39. + Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(typeof(array), \
  40. typeof(&(array)[0]))))
  41. #else
  42. #define Py_ARRAY_LENGTH(array) \
  43. (sizeof(array) / sizeof((array)[0]))
  44. #endif
  45. /* Define macros for inline documentation. */
  46. #define PyDoc_VAR(name) static char name[]
  47. #define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str)
  48. #ifdef WITH_DOC_STRINGS
  49. #define PyDoc_STR(str) str
  50. #else
  51. #define PyDoc_STR(str) ""
  52. #endif
  53. /* Below "a" is a power of 2. */
  54. /* Round down size "n" to be a multiple of "a". */
  55. #define _Py_SIZE_ROUND_DOWN(n, a) ((size_t)(n) & ~(size_t)((a) - 1))
  56. /* Round up size "n" to be a multiple of "a". */
  57. #define _Py_SIZE_ROUND_UP(n, a) (((size_t)(n) + \
  58. (size_t)((a) - 1)) & ~(size_t)((a) - 1))
  59. /* Round pointer "p" down to the closest "a"-aligned address <= "p". */
  60. #define _Py_ALIGN_DOWN(p, a) ((void *)((Py_uintptr_t)(p) & ~(Py_uintptr_t)((a) - 1)))
  61. /* Round pointer "p" up to the closest "a"-aligned address >= "p". */
  62. #define _Py_ALIGN_UP(p, a) ((void *)(((Py_uintptr_t)(p) + \
  63. (Py_uintptr_t)((a) - 1)) & ~(Py_uintptr_t)((a) - 1)))
  64. /* Check if pointer "p" is aligned to "a"-bytes boundary. */
  65. #define _Py_IS_ALIGNED(p, a) (!((Py_uintptr_t)(p) & (Py_uintptr_t)((a) - 1)))
  66. #ifdef __GNUC__
  67. #define Py_UNUSED(name) _unused_ ## name __attribute__((unused))
  68. #else
  69. #define Py_UNUSED(name) _unused_ ## name
  70. #endif
  71. #endif /* Py_PYMACRO_H */