Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:06:50

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 /* Assert a build-time dependency, as an expression.
0050 
0051    Your compile will fail if the condition isn't true, or can't be evaluated
0052    by the compiler. This can be used in an expression: its value is 0.
0053 
0054    Example:
0055 
0056    #define foo_to_char(foo)  \
0057        ((char *)(foo)        \
0058         + Py_BUILD_ASSERT_EXPR(offsetof(struct foo, string) == 0))
0059 
0060    Written by Rusty Russell, public domain, http://ccodearchive.net/ */
0061 #define Py_BUILD_ASSERT_EXPR(cond) \
0062     (sizeof(char [1 - 2*!(cond)]) - 1)
0063 
0064 #define Py_BUILD_ASSERT(cond)  do {         \
0065         (void)Py_BUILD_ASSERT_EXPR(cond);   \
0066     } while(0)
0067 
0068 /* Get the number of elements in a visible array
0069 
0070    This does not work on pointers, or arrays declared as [], or function
0071    parameters. With correct compiler support, such usage will cause a build
0072    error (see Py_BUILD_ASSERT_EXPR).
0073 
0074    Written by Rusty Russell, public domain, http://ccodearchive.net/
0075 
0076    Requires at GCC 3.1+ */
0077 #if (defined(__GNUC__) && !defined(__STRICT_ANSI__) && \
0078     (((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)) || (__GNUC__ >= 4)))
0079 /* Two gcc extensions.
0080    &a[0] degrades to a pointer: a different type from an array */
0081 #define Py_ARRAY_LENGTH(array) \
0082     (sizeof(array) / sizeof((array)[0]) \
0083      + Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(typeof(array), \
0084                                                           typeof(&(array)[0]))))
0085 #else
0086 #define Py_ARRAY_LENGTH(array) \
0087     (sizeof(array) / sizeof((array)[0]))
0088 #endif
0089 
0090 
0091 /* Define macros for inline documentation. */
0092 #define PyDoc_VAR(name) static const char name[]
0093 #define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str)
0094 #ifdef WITH_DOC_STRINGS
0095 #define PyDoc_STR(str) str
0096 #else
0097 #define PyDoc_STR(str) ""
0098 #endif
0099 
0100 /* Below "a" is a power of 2. */
0101 /* Round down size "n" to be a multiple of "a". */
0102 #define _Py_SIZE_ROUND_DOWN(n, a) ((size_t)(n) & ~(size_t)((a) - 1))
0103 /* Round up size "n" to be a multiple of "a". */
0104 #define _Py_SIZE_ROUND_UP(n, a) (((size_t)(n) + \
0105         (size_t)((a) - 1)) & ~(size_t)((a) - 1))
0106 /* Round pointer "p" down to the closest "a"-aligned address <= "p". */
0107 #define _Py_ALIGN_DOWN(p, a) ((void *)((uintptr_t)(p) & ~(uintptr_t)((a) - 1)))
0108 /* Round pointer "p" up to the closest "a"-aligned address >= "p". */
0109 #define _Py_ALIGN_UP(p, a) ((void *)(((uintptr_t)(p) + \
0110         (uintptr_t)((a) - 1)) & ~(uintptr_t)((a) - 1)))
0111 /* Check if pointer "p" is aligned to "a"-bytes boundary. */
0112 #define _Py_IS_ALIGNED(p, a) (!((uintptr_t)(p) & (uintptr_t)((a) - 1)))
0113 
0114 /* Use this for unused arguments in a function definition to silence compiler
0115  * warnings. Example:
0116  *
0117  * int func(int a, int Py_UNUSED(b)) { return a; }
0118  */
0119 #if defined(__GNUC__) || defined(__clang__)
0120 #  define Py_UNUSED(name) _unused_ ## name __attribute__((unused))
0121 #else
0122 #  define Py_UNUSED(name) _unused_ ## name
0123 #endif
0124 
0125 #if defined(RANDALL_WAS_HERE)
0126 #  define Py_UNREACHABLE() \
0127     Py_FatalError( \
0128         "If you're seeing this, the code is in what I thought was\n" \
0129         "an unreachable state.\n\n" \
0130         "I could give you advice for what to do, but honestly, why\n" \
0131         "should you trust me?  I clearly screwed this up.  I'm writing\n" \
0132         "a message that should never appear, yet I know it will\n" \
0133         "probably appear someday.\n\n" \
0134         "On a deep level, I know I'm not up to this task.\n" \
0135         "I'm so sorry.\n" \
0136         "https://xkcd.com/2200")
0137 #elif defined(Py_DEBUG)
0138 #  define Py_UNREACHABLE() \
0139     Py_FatalError( \
0140         "We've reached an unreachable state. Anything is possible.\n" \
0141         "The limits were in our heads all along. Follow your dreams.\n" \
0142         "https://xkcd.com/2200")
0143 #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
0144 #  define Py_UNREACHABLE() __builtin_unreachable()
0145 #elif defined(__clang__) || defined(__INTEL_COMPILER)
0146 #  define Py_UNREACHABLE() __builtin_unreachable()
0147 #elif defined(_MSC_VER)
0148 #  define Py_UNREACHABLE() __assume(0)
0149 #else
0150 #  define Py_UNREACHABLE() \
0151     Py_FatalError("Unreachable C code path reached")
0152 #endif
0153 
0154 // Prevent using an expression as a l-value.
0155 // For example, "int x; _Py_RVALUE(x) = 1;" fails with a compiler error.
0156 #define _Py_RVALUE(EXPR) ((void)0, (EXPR))
0157 
0158 // Return non-zero if the type is signed, return zero if it's unsigned.
0159 // Use "<= 0" rather than "< 0" to prevent the compiler warning:
0160 // "comparison of unsigned expression in '< 0' is always false".
0161 #define _Py_IS_TYPE_SIGNED(type) ((type)(-1) <= 0)
0162 
0163 #endif /* Py_PYMACRO_H */