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