Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-19 09:50:54

0001 #ifndef Py_PYMACRO_H
0002 #define Py_PYMACRO_H
0003 
0004 // gh-91782: On FreeBSD 12, if the _POSIX_C_SOURCE and _XOPEN_SOURCE macros are
0005 // defined, <sys/cdefs.h> disables C11 support and <assert.h> does not define
0006 // the static_assert() macro.
0007 // https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=255290
0008 //
0009 // macOS <= 10.10 doesn't define static_assert in assert.h at all despite
0010 // having C11 compiler support.
0011 //
0012 // static_assert is defined in glibc from version 2.16. Compiler support for
0013 // the C11 _Static_assert keyword is in gcc >= 4.6.
0014 //
0015 // MSVC makes static_assert a keyword in C11-17, contrary to the standards.
0016 //
0017 // In C++11 and C2x, static_assert is a keyword, redefining is undefined
0018 // behaviour. So only define if building as C, not C++ (if __cplusplus is
0019 // not defined), and only for C11-17.
0020 #if !defined(static_assert) && (defined(__GNUC__) || defined(__clang__)) \
0021      && !defined(__cplusplus) && defined(__STDC_VERSION__) \
0022      && __STDC_VERSION__ >= 201112L && __STDC_VERSION__ <= 201710L
0023 #  define static_assert _Static_assert
0024 #endif
0025 
0026 /* Minimum value between x and y */
0027 #define Py_MIN(x, y) (((x) > (y)) ? (y) : (x))
0028 
0029 /* Maximum value between x and y */
0030 #define Py_MAX(x, y) (((x) > (y)) ? (x) : (y))
0031 
0032 /* Absolute value of the number x */
0033 #define Py_ABS(x) ((x) < 0 ? -(x) : (x))
0034 
0035 #define _Py_XSTRINGIFY(x) #x
0036 
0037 /* Convert the argument to a string. For example, Py_STRINGIFY(123) is replaced
0038    with "123" by the preprocessor. Defines are also replaced by their value.
0039    For example Py_STRINGIFY(__LINE__) is replaced by the line number, not
0040    by "__LINE__". */
0041 #define Py_STRINGIFY(x) _Py_XSTRINGIFY(x)
0042 
0043 /* Get the size of a structure member in bytes */
0044 #define Py_MEMBER_SIZE(type, member) sizeof(((type *)0)->member)
0045 
0046 /* Argument must be a char or an int in [-128, 127] or [0, 255]. */
0047 #define Py_CHARMASK(c) ((unsigned char)((c) & 0xff))
0048 
0049 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \
0050      && !defined(__cplusplus) && !defined(_MSC_VER))
0051 #  define Py_BUILD_ASSERT_EXPR(cond) \
0052     ((void)sizeof(struct { int dummy; _Static_assert(cond, #cond); }), \
0053      0)
0054 #else
0055    /* Assert a build-time dependency, as an expression.
0056     *
0057     * Your compile will fail if the condition isn't true, or can't be evaluated
0058     * by the compiler. This can be used in an expression: its value is 0.
0059     *
0060     * Example:
0061     *
0062     * #define foo_to_char(foo)  \
0063     *     ((char *)(foo)        \
0064     *      + Py_BUILD_ASSERT_EXPR(offsetof(struct foo, string) == 0))
0065     *
0066     * Written by Rusty Russell, public domain, http://ccodearchive.net/
0067     */
0068 #  define Py_BUILD_ASSERT_EXPR(cond) \
0069     (sizeof(char [1 - 2*!(cond)]) - 1)
0070 #endif
0071 
0072 #if ((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) \
0073      || (defined(__cplusplus) && __cplusplus >= 201103L))
0074    // Use static_assert() on C11 and newer
0075 #  define Py_BUILD_ASSERT(cond) \
0076         do { \
0077             static_assert((cond), #cond); \
0078         } while (0)
0079 #else
0080 #  define Py_BUILD_ASSERT(cond)  \
0081         do { \
0082             (void)Py_BUILD_ASSERT_EXPR(cond); \
0083         } while(0)
0084 #endif
0085 
0086 /* Get the number of elements in a visible array
0087 
0088    This does not work on pointers, or arrays declared as [], or function
0089    parameters. With correct compiler support, such usage will cause a build
0090    error (see Py_BUILD_ASSERT_EXPR).
0091 
0092    Written by Rusty Russell, public domain, http://ccodearchive.net/
0093 
0094    Requires at GCC 3.1+ */
0095 #if (defined(__GNUC__) && !defined(__STRICT_ANSI__) && \
0096     (((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)) || (__GNUC__ >= 4)))
0097 /* Two gcc extensions.
0098    &a[0] degrades to a pointer: a different type from an array */
0099 #define Py_ARRAY_LENGTH(array) \
0100     (sizeof(array) / sizeof((array)[0]) \
0101      + Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(typeof(array), \
0102                                                           typeof(&(array)[0]))))
0103 #else
0104 #define Py_ARRAY_LENGTH(array) \
0105     (sizeof(array) / sizeof((array)[0]))
0106 #endif
0107 
0108 
0109 /* Define macros for inline documentation. */
0110 #define PyDoc_VAR(name) static const char name[]
0111 #define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str)
0112 #ifdef WITH_DOC_STRINGS
0113 #define PyDoc_STR(str) str
0114 #else
0115 #define PyDoc_STR(str) ""
0116 #endif
0117 
0118 /* Below "a" is a power of 2. */
0119 /* Round down size "n" to be a multiple of "a". */
0120 #define _Py_SIZE_ROUND_DOWN(n, a) ((size_t)(n) & ~(size_t)((a) - 1))
0121 /* Round up size "n" to be a multiple of "a". */
0122 #define _Py_SIZE_ROUND_UP(n, a) (((size_t)(n) + \
0123         (size_t)((a) - 1)) & ~(size_t)((a) - 1))
0124 /* Round pointer "p" down to the closest "a"-aligned address <= "p". */
0125 #define _Py_ALIGN_DOWN(p, a) ((void *)((uintptr_t)(p) & ~(uintptr_t)((a) - 1)))
0126 /* Round pointer "p" up to the closest "a"-aligned address >= "p". */
0127 #define _Py_ALIGN_UP(p, a) ((void *)(((uintptr_t)(p) + \
0128         (uintptr_t)((a) - 1)) & ~(uintptr_t)((a) - 1)))
0129 /* Check if pointer "p" is aligned to "a"-bytes boundary. */
0130 #define _Py_IS_ALIGNED(p, a) (!((uintptr_t)(p) & (uintptr_t)((a) - 1)))
0131 
0132 /* Use this for unused arguments in a function definition to silence compiler
0133  * warnings. Example:
0134  *
0135  * int func(int a, int Py_UNUSED(b)) { return a; }
0136  */
0137 #if defined(__GNUC__) || defined(__clang__)
0138 #  define Py_UNUSED(name) _unused_ ## name __attribute__((unused))
0139 #elif defined(_MSC_VER)
0140    // Disable warning C4100: unreferenced formal parameter,
0141    // declare the parameter,
0142    // restore old compiler warnings.
0143 #  define Py_UNUSED(name) \
0144         __pragma(warning(push)) \
0145         __pragma(warning(suppress: 4100)) \
0146         _unused_ ## name \
0147         __pragma(warning(pop))
0148 #else
0149 #  define Py_UNUSED(name) _unused_ ## name
0150 #endif
0151 
0152 #if defined(RANDALL_WAS_HERE)
0153 #  define Py_UNREACHABLE() \
0154     Py_FatalError( \
0155         "If you're seeing this, the code is in what I thought was\n" \
0156         "an unreachable state.\n\n" \
0157         "I could give you advice for what to do, but honestly, why\n" \
0158         "should you trust me?  I clearly screwed this up.  I'm writing\n" \
0159         "a message that should never appear, yet I know it will\n" \
0160         "probably appear someday.\n\n" \
0161         "On a deep level, I know I'm not up to this task.\n" \
0162         "I'm so sorry.\n" \
0163         "https://xkcd.com/2200")
0164 #elif defined(Py_DEBUG)
0165 #  define Py_UNREACHABLE() \
0166     Py_FatalError( \
0167         "We've reached an unreachable state. Anything is possible.\n" \
0168         "The limits were in our heads all along. Follow your dreams.\n" \
0169         "https://xkcd.com/2200")
0170 #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
0171 #  define Py_UNREACHABLE() __builtin_unreachable()
0172 #elif defined(__clang__) || defined(__INTEL_COMPILER)
0173 #  define Py_UNREACHABLE() __builtin_unreachable()
0174 #elif defined(_MSC_VER)
0175 #  define Py_UNREACHABLE() __assume(0)
0176 #else
0177 #  define Py_UNREACHABLE() \
0178     Py_FatalError("Unreachable C code path reached")
0179 #endif
0180 
0181 #define _Py_CONTAINER_OF(ptr, type, member) \
0182     (type*)((char*)ptr - offsetof(type, member))
0183 
0184 // Prevent using an expression as a l-value.
0185 // For example, "int x; _Py_RVALUE(x) = 1;" fails with a compiler error.
0186 #define _Py_RVALUE(EXPR) ((void)0, (EXPR))
0187 
0188 // Return non-zero if the type is signed, return zero if it's unsigned.
0189 // Use "<= 0" rather than "< 0" to prevent the compiler warning:
0190 // "comparison of unsigned expression in '< 0' is always false".
0191 #define _Py_IS_TYPE_SIGNED(type) ((type)(-1) <= 0)
0192 
0193 #endif /* Py_PYMACRO_H */