Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 1 | #ifndef Py_PYMACRO_H |
| 2 | #define Py_PYMACRO_H |
| 3 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4 | /* Minimum value between x and y */ |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 5 | #define Py_MIN(x, y) (((x) > (y)) ? (y) : (x)) |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 6 | |
| 7 | /* Maximum value between x and y */ |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 8 | #define Py_MAX(x, y) (((x) > (y)) ? (x) : (y)) |
| 9 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 10 | /* Absolute value of the number x */ |
| 11 | #define Py_ABS(x) ((x) < 0 ? -(x) : (x)) |
| 12 | |
| 13 | #define _Py_XSTRINGIFY(x) #x |
| 14 | |
| 15 | /* Convert the argument to a string. For example, Py_STRINGIFY(123) is replaced |
| 16 | with "123" by the preprocessor. Defines are also replaced by their value. |
| 17 | For example Py_STRINGIFY(__LINE__) is replaced by the line number, not |
| 18 | by "__LINE__". */ |
| 19 | #define Py_STRINGIFY(x) _Py_XSTRINGIFY(x) |
| 20 | |
Victor Stinner | 98ee9d5 | 2016-09-08 09:33:56 -0700 | [diff] [blame] | 21 | /* Get the size of a structure member in bytes */ |
| 22 | #define Py_MEMBER_SIZE(type, member) sizeof(((type *)0)->member) |
| 23 | |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 24 | /* Argument must be a char or an int in [-128, 127] or [0, 255]. */ |
| 25 | #define Py_CHARMASK(c) ((unsigned char)((c) & 0xff)) |
| 26 | |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 27 | /* Assert a build-time dependency, as an expression. |
| 28 | |
| 29 | Your compile will fail if the condition isn't true, or can't be evaluated |
| 30 | by the compiler. This can be used in an expression: its value is 0. |
| 31 | |
| 32 | Example: |
| 33 | |
| 34 | #define foo_to_char(foo) \ |
| 35 | ((char *)(foo) \ |
Victor Stinner | f0ddadc | 2011-09-29 12:43:18 +0200 | [diff] [blame] | 36 | + Py_BUILD_ASSERT_EXPR(offsetof(struct foo, string) == 0)) |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 37 | |
Victor Stinner | 573696a | 2011-09-29 12:12:39 +0200 | [diff] [blame] | 38 | Written by Rusty Russell, public domain, http://ccodearchive.net/ */ |
Victor Stinner | f0ddadc | 2011-09-29 12:43:18 +0200 | [diff] [blame] | 39 | #define Py_BUILD_ASSERT_EXPR(cond) \ |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 40 | (sizeof(char [1 - 2*!(cond)]) - 1) |
| 41 | |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 42 | #define Py_BUILD_ASSERT(cond) do { \ |
| 43 | (void)Py_BUILD_ASSERT_EXPR(cond); \ |
| 44 | } while(0) |
| 45 | |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 46 | /* Get the number of elements in a visible array |
| 47 | |
| 48 | This does not work on pointers, or arrays declared as [], or function |
| 49 | parameters. With correct compiler support, such usage will cause a build |
Victor Stinner | f0ddadc | 2011-09-29 12:43:18 +0200 | [diff] [blame] | 50 | error (see Py_BUILD_ASSERT_EXPR). |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 51 | |
Christian Heimes | 61dbb00 | 2013-01-06 16:41:56 +0100 | [diff] [blame] | 52 | Written by Rusty Russell, public domain, http://ccodearchive.net/ |
| 53 | |
| 54 | Requires at GCC 3.1+ */ |
| 55 | #if (defined(__GNUC__) && !defined(__STRICT_ANSI__) && \ |
Niklas Fiekas | 83371f4 | 2017-03-29 06:58:01 +0200 | [diff] [blame] | 56 | (((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)) || (__GNUC__ >= 4))) |
Victor Stinner | 573696a | 2011-09-29 12:12:39 +0200 | [diff] [blame] | 57 | /* Two gcc extensions. |
| 58 | &a[0] degrades to a pointer: a different type from an array */ |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 59 | #define Py_ARRAY_LENGTH(array) \ |
Victor Stinner | 573696a | 2011-09-29 12:12:39 +0200 | [diff] [blame] | 60 | (sizeof(array) / sizeof((array)[0]) \ |
Victor Stinner | f0ddadc | 2011-09-29 12:43:18 +0200 | [diff] [blame] | 61 | + Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(typeof(array), \ |
| 62 | typeof(&(array)[0])))) |
Victor Stinner | 573696a | 2011-09-29 12:12:39 +0200 | [diff] [blame] | 63 | #else |
| 64 | #define Py_ARRAY_LENGTH(array) \ |
| 65 | (sizeof(array) / sizeof((array)[0])) |
| 66 | #endif |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 67 | |
| 68 | |
| 69 | /* Define macros for inline documentation. */ |
Inada Naoki | 926b0cb | 2019-04-17 08:39:46 +0900 | [diff] [blame] | 70 | #define PyDoc_VAR(name) static const char name[] |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 71 | #define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str) |
| 72 | #ifdef WITH_DOC_STRINGS |
| 73 | #define PyDoc_STR(str) str |
| 74 | #else |
| 75 | #define PyDoc_STR(str) "" |
| 76 | #endif |
| 77 | |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 78 | /* Below "a" is a power of 2. */ |
| 79 | /* Round down size "n" to be a multiple of "a". */ |
| 80 | #define _Py_SIZE_ROUND_DOWN(n, a) ((size_t)(n) & ~(size_t)((a) - 1)) |
| 81 | /* Round up size "n" to be a multiple of "a". */ |
| 82 | #define _Py_SIZE_ROUND_UP(n, a) (((size_t)(n) + \ |
| 83 | (size_t)((a) - 1)) & ~(size_t)((a) - 1)) |
| 84 | /* Round pointer "p" down to the closest "a"-aligned address <= "p". */ |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 85 | #define _Py_ALIGN_DOWN(p, a) ((void *)((uintptr_t)(p) & ~(uintptr_t)((a) - 1))) |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 86 | /* Round pointer "p" up to the closest "a"-aligned address >= "p". */ |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 87 | #define _Py_ALIGN_UP(p, a) ((void *)(((uintptr_t)(p) + \ |
| 88 | (uintptr_t)((a) - 1)) & ~(uintptr_t)((a) - 1))) |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 89 | /* Check if pointer "p" is aligned to "a"-bytes boundary. */ |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 90 | #define _Py_IS_ALIGNED(p, a) (!((uintptr_t)(p) & (uintptr_t)((a) - 1))) |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 91 | |
Victor Stinner | b3a9843 | 2019-05-24 15:16:08 +0200 | [diff] [blame] | 92 | /* Use this for unused arguments in a function definition to silence compiler |
| 93 | * warnings. Example: |
| 94 | * |
| 95 | * int func(int a, int Py_UNUSED(b)) { return a; } |
| 96 | */ |
| 97 | #if defined(__GNUC__) || defined(__clang__) |
| 98 | # define Py_UNUSED(name) _unused_ ## name __attribute__((unused)) |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 99 | #else |
Victor Stinner | b3a9843 | 2019-05-24 15:16:08 +0200 | [diff] [blame] | 100 | # define Py_UNUSED(name) _unused_ ## name |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 101 | #endif |
| 102 | |
Zachary Ware | 3ab6147 | 2019-09-12 13:35:48 +0100 | [diff] [blame] | 103 | #if defined(RANDALL_WAS_HERE) |
Serhiy Storchaka | eebaa9b | 2020-03-09 20:49:52 +0200 | [diff] [blame] | 104 | # define Py_UNREACHABLE() \ |
Victor Stinner | b154258 | 2019-09-20 23:10:16 +0200 | [diff] [blame] | 105 | Py_FatalError( \ |
| 106 | "If you're seeing this, the code is in what I thought was\n" \ |
| 107 | "an unreachable state.\n\n" \ |
| 108 | "I could give you advice for what to do, but honestly, why\n" \ |
| 109 | "should you trust me? I clearly screwed this up. I'm writing\n" \ |
| 110 | "a message that should never appear, yet I know it will\n" \ |
| 111 | "probably appear someday.\n\n" \ |
| 112 | "On a deep level, I know I'm not up to this task.\n" \ |
| 113 | "I'm so sorry.\n" \ |
| 114 | "https://xkcd.com/2200") |
Zachary Ware | 3ab6147 | 2019-09-12 13:35:48 +0100 | [diff] [blame] | 115 | #elif defined(Py_DEBUG) |
Serhiy Storchaka | eebaa9b | 2020-03-09 20:49:52 +0200 | [diff] [blame] | 116 | # define Py_UNREACHABLE() \ |
Victor Stinner | b154258 | 2019-09-20 23:10:16 +0200 | [diff] [blame] | 117 | Py_FatalError( \ |
| 118 | "We've reached an unreachable state. Anything is possible.\n" \ |
| 119 | "The limits were in our heads all along. Follow your dreams.\n" \ |
| 120 | "https://xkcd.com/2200") |
Dong-hee Na | 24ba3b0 | 2020-09-29 05:41:23 +0900 | [diff] [blame] | 121 | #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)) |
| 122 | # define Py_UNREACHABLE() __builtin_unreachable() |
| 123 | #elif defined(__clang__) || defined(__INTEL_COMPILER) |
Serhiy Storchaka | eebaa9b | 2020-03-09 20:49:52 +0200 | [diff] [blame] | 124 | # define Py_UNREACHABLE() __builtin_unreachable() |
| 125 | #elif defined(_MSC_VER) |
| 126 | # define Py_UNREACHABLE() __assume(0) |
Zachary Ware | 3ab6147 | 2019-09-12 13:35:48 +0100 | [diff] [blame] | 127 | #else |
Serhiy Storchaka | eebaa9b | 2020-03-09 20:49:52 +0200 | [diff] [blame] | 128 | # define Py_UNREACHABLE() \ |
Victor Stinner | b154258 | 2019-09-20 23:10:16 +0200 | [diff] [blame] | 129 | Py_FatalError("Unreachable C code path reached") |
Zachary Ware | 3ab6147 | 2019-09-12 13:35:48 +0100 | [diff] [blame] | 130 | #endif |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 131 | |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 132 | #endif /* Py_PYMACRO_H */ |