• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, not C++ (if __cplusplus is
19 // not defined), and only for C11-17.
20 #if !defined(static_assert) && (defined(__GNUC__) || defined(__clang__)) \
21      && !defined(__cplusplus) && defined(__STDC_VERSION__) \
22      && __STDC_VERSION__ >= 201112L && __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 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \
50      && !defined(__cplusplus))
51 #  define Py_BUILD_ASSERT_EXPR(cond) \
52     ((void)sizeof(struct { int dummy; _Static_assert(cond, #cond); }), \
53      0)
54 #else
55    /* Assert a build-time dependency, as an expression.
56     *
57     * Your compile will fail if the condition isn't true, or can't be evaluated
58     * by the compiler. This can be used in an expression: its value is 0.
59     *
60     * Example:
61     *
62     * #define foo_to_char(foo)  \
63     *     ((char *)(foo)        \
64     *      + Py_BUILD_ASSERT_EXPR(offsetof(struct foo, string) == 0))
65     *
66     * Written by Rusty Russell, public domain, http://ccodearchive.net/
67     */
68 #  define Py_BUILD_ASSERT_EXPR(cond) \
69     (sizeof(char [1 - 2*!(cond)]) - 1)
70 #endif
71 
72 #if ((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) \
73      || (defined(__cplusplus) && __cplusplus >= 201103L))
74    // Use static_assert() on C11 and newer
75 #  define Py_BUILD_ASSERT(cond) \
76         do { \
77             static_assert((cond), #cond); \
78         } while (0)
79 #else
80 #  define Py_BUILD_ASSERT(cond)  \
81         do { \
82             (void)Py_BUILD_ASSERT_EXPR(cond); \
83         } while(0)
84 #endif
85 
86 /* Get the number of elements in a visible array
87 
88    This does not work on pointers, or arrays declared as [], or function
89    parameters. With correct compiler support, such usage will cause a build
90    error (see Py_BUILD_ASSERT_EXPR).
91 
92    Written by Rusty Russell, public domain, http://ccodearchive.net/
93 
94    Requires at GCC 3.1+ */
95 #if (defined(__GNUC__) && !defined(__STRICT_ANSI__) && \
96     (((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)) || (__GNUC__ >= 4)))
97 /* Two gcc extensions.
98    &a[0] degrades to a pointer: a different type from an array */
99 #define Py_ARRAY_LENGTH(array) \
100     (sizeof(array) / sizeof((array)[0]) \
101      + Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(typeof(array), \
102                                                           typeof(&(array)[0]))))
103 #else
104 #define Py_ARRAY_LENGTH(array) \
105     (sizeof(array) / sizeof((array)[0]))
106 #endif
107 
108 
109 /* Define macros for inline documentation. */
110 #define PyDoc_VAR(name) static const char name[]
111 #define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str)
112 #ifdef WITH_DOC_STRINGS
113 #define PyDoc_STR(str) str
114 #else
115 #define PyDoc_STR(str) ""
116 #endif
117 
118 /* Below "a" is a power of 2. */
119 /* Round down size "n" to be a multiple of "a". */
120 #define _Py_SIZE_ROUND_DOWN(n, a) ((size_t)(n) & ~(size_t)((a) - 1))
121 /* Round up size "n" to be a multiple of "a". */
122 #define _Py_SIZE_ROUND_UP(n, a) (((size_t)(n) + \
123         (size_t)((a) - 1)) & ~(size_t)((a) - 1))
124 /* Round pointer "p" down to the closest "a"-aligned address <= "p". */
125 #define _Py_ALIGN_DOWN(p, a) ((void *)((uintptr_t)(p) & ~(uintptr_t)((a) - 1)))
126 /* Round pointer "p" up to the closest "a"-aligned address >= "p". */
127 #define _Py_ALIGN_UP(p, a) ((void *)(((uintptr_t)(p) + \
128         (uintptr_t)((a) - 1)) & ~(uintptr_t)((a) - 1)))
129 /* Check if pointer "p" is aligned to "a"-bytes boundary. */
130 #define _Py_IS_ALIGNED(p, a) (!((uintptr_t)(p) & (uintptr_t)((a) - 1)))
131 
132 /* Use this for unused arguments in a function definition to silence compiler
133  * warnings. Example:
134  *
135  * int func(int a, int Py_UNUSED(b)) { return a; }
136  */
137 #if defined(__GNUC__) || defined(__clang__)
138 #  define Py_UNUSED(name) _unused_ ## name __attribute__((unused))
139 #elif defined(_MSC_VER)
140    // Disable warning C4100: unreferenced formal parameter,
141    // declare the parameter,
142    // restore old compiler warnings.
143 #  define Py_UNUSED(name) \
144         __pragma(warning(push)) \
145         __pragma(warning(suppress: 4100)) \
146         _unused_ ## name \
147         __pragma(warning(pop))
148 #else
149 #  define Py_UNUSED(name) _unused_ ## name
150 #endif
151 
152 #if defined(RANDALL_WAS_HERE)
153 #  define Py_UNREACHABLE() \
154     Py_FatalError( \
155         "If you're seeing this, the code is in what I thought was\n" \
156         "an unreachable state.\n\n" \
157         "I could give you advice for what to do, but honestly, why\n" \
158         "should you trust me?  I clearly screwed this up.  I'm writing\n" \
159         "a message that should never appear, yet I know it will\n" \
160         "probably appear someday.\n\n" \
161         "On a deep level, I know I'm not up to this task.\n" \
162         "I'm so sorry.\n" \
163         "https://xkcd.com/2200")
164 #elif defined(Py_DEBUG)
165 #  define Py_UNREACHABLE() \
166     Py_FatalError( \
167         "We've reached an unreachable state. Anything is possible.\n" \
168         "The limits were in our heads all along. Follow your dreams.\n" \
169         "https://xkcd.com/2200")
170 #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
171 #  define Py_UNREACHABLE() __builtin_unreachable()
172 #elif defined(__clang__) || defined(__INTEL_COMPILER)
173 #  define Py_UNREACHABLE() __builtin_unreachable()
174 #elif defined(_MSC_VER)
175 #  define Py_UNREACHABLE() __assume(0)
176 #else
177 #  define Py_UNREACHABLE() \
178     Py_FatalError("Unreachable C code path reached")
179 #endif
180 
181 #define _Py_CONTAINER_OF(ptr, type, member) \
182     (type*)((char*)ptr - offsetof(type, member))
183 
184 // Prevent using an expression as a l-value.
185 // For example, "int x; _Py_RVALUE(x) = 1;" fails with a compiler error.
186 #define _Py_RVALUE(EXPR) ((void)0, (EXPR))
187 
188 // Return non-zero if the type is signed, return zero if it's unsigned.
189 // Use "<= 0" rather than "< 0" to prevent the compiler warning:
190 // "comparison of unsigned expression in '< 0' is always false".
191 #define _Py_IS_TYPE_SIGNED(type) ((type)(-1) <= 0)
192 
193 #endif /* Py_PYMACRO_H */
194