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 | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 21 | /* Argument must be a char or an int in [-128, 127] or [0, 255]. */ |
| 22 | #define Py_CHARMASK(c) ((unsigned char)((c) & 0xff)) |
| 23 | |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 24 | /* Assert a build-time dependency, as an expression. |
| 25 | |
| 26 | Your compile will fail if the condition isn't true, or can't be evaluated |
| 27 | by the compiler. This can be used in an expression: its value is 0. |
| 28 | |
| 29 | Example: |
| 30 | |
| 31 | #define foo_to_char(foo) \ |
| 32 | ((char *)(foo) \ |
Victor Stinner | f0ddadc | 2011-09-29 12:43:18 +0200 | [diff] [blame] | 33 | + Py_BUILD_ASSERT_EXPR(offsetof(struct foo, string) == 0)) |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 34 | |
Victor Stinner | 573696a | 2011-09-29 12:12:39 +0200 | [diff] [blame] | 35 | Written by Rusty Russell, public domain, http://ccodearchive.net/ */ |
Victor Stinner | f0ddadc | 2011-09-29 12:43:18 +0200 | [diff] [blame] | 36 | #define Py_BUILD_ASSERT_EXPR(cond) \ |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 37 | (sizeof(char [1 - 2*!(cond)]) - 1) |
| 38 | |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 39 | #define Py_BUILD_ASSERT(cond) do { \ |
| 40 | (void)Py_BUILD_ASSERT_EXPR(cond); \ |
| 41 | } while(0) |
| 42 | |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 43 | /* Get the number of elements in a visible array |
| 44 | |
| 45 | This does not work on pointers, or arrays declared as [], or function |
| 46 | parameters. With correct compiler support, such usage will cause a build |
Victor Stinner | f0ddadc | 2011-09-29 12:43:18 +0200 | [diff] [blame] | 47 | error (see Py_BUILD_ASSERT_EXPR). |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 48 | |
Christian Heimes | 61dbb00 | 2013-01-06 16:41:56 +0100 | [diff] [blame] | 49 | Written by Rusty Russell, public domain, http://ccodearchive.net/ |
| 50 | |
| 51 | Requires at GCC 3.1+ */ |
| 52 | #if (defined(__GNUC__) && !defined(__STRICT_ANSI__) && \ |
Christian Heimes | e0a2d12 | 2013-06-24 15:39:41 +0200 | [diff] [blame] | 53 | (((__GNUC__ == 3) && (__GNU_MINOR__ >= 1)) || (__GNUC__ >= 4))) |
Victor Stinner | 573696a | 2011-09-29 12:12:39 +0200 | [diff] [blame] | 54 | /* Two gcc extensions. |
| 55 | &a[0] degrades to a pointer: a different type from an array */ |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 56 | #define Py_ARRAY_LENGTH(array) \ |
Victor Stinner | 573696a | 2011-09-29 12:12:39 +0200 | [diff] [blame] | 57 | (sizeof(array) / sizeof((array)[0]) \ |
Victor Stinner | f0ddadc | 2011-09-29 12:43:18 +0200 | [diff] [blame] | 58 | + Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(typeof(array), \ |
| 59 | typeof(&(array)[0])))) |
Victor Stinner | 573696a | 2011-09-29 12:12:39 +0200 | [diff] [blame] | 60 | #else |
| 61 | #define Py_ARRAY_LENGTH(array) \ |
| 62 | (sizeof(array) / sizeof((array)[0])) |
| 63 | #endif |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 64 | |
| 65 | |
| 66 | /* Define macros for inline documentation. */ |
| 67 | #define PyDoc_VAR(name) static char name[] |
| 68 | #define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str) |
| 69 | #ifdef WITH_DOC_STRINGS |
| 70 | #define PyDoc_STR(str) str |
| 71 | #else |
| 72 | #define PyDoc_STR(str) "" |
| 73 | #endif |
| 74 | |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 75 | /* Below "a" is a power of 2. */ |
| 76 | /* Round down size "n" to be a multiple of "a". */ |
| 77 | #define _Py_SIZE_ROUND_DOWN(n, a) ((size_t)(n) & ~(size_t)((a) - 1)) |
| 78 | /* Round up size "n" to be a multiple of "a". */ |
| 79 | #define _Py_SIZE_ROUND_UP(n, a) (((size_t)(n) + \ |
| 80 | (size_t)((a) - 1)) & ~(size_t)((a) - 1)) |
| 81 | /* Round pointer "p" down to the closest "a"-aligned address <= "p". */ |
| 82 | #define _Py_ALIGN_DOWN(p, a) ((void *)((Py_uintptr_t)(p) & ~(Py_uintptr_t)((a) - 1))) |
| 83 | /* Round pointer "p" up to the closest "a"-aligned address >= "p". */ |
| 84 | #define _Py_ALIGN_UP(p, a) ((void *)(((Py_uintptr_t)(p) + \ |
| 85 | (Py_uintptr_t)((a) - 1)) & ~(Py_uintptr_t)((a) - 1))) |
| 86 | /* Check if pointer "p" is aligned to "a"-bytes boundary. */ |
| 87 | #define _Py_IS_ALIGNED(p, a) (!((Py_uintptr_t)(p) & (Py_uintptr_t)((a) - 1))) |
| 88 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 89 | #ifdef __GNUC__ |
| 90 | #define Py_UNUSED(name) _unused_ ## name __attribute__((unused)) |
| 91 | #else |
| 92 | #define Py_UNUSED(name) _unused_ ## name |
| 93 | #endif |
| 94 | |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 95 | #endif /* Py_PYMACRO_H */ |