Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 1 | #ifndef Py_PYMACRO_H |
| 2 | #define Py_PYMACRO_H |
| 3 | |
| 4 | #define Py_MIN(x, y) (((x) > (y)) ? (y) : (x)) |
| 5 | #define Py_MAX(x, y) (((x) > (y)) ? (x) : (y)) |
| 6 | |
| 7 | /* Argument must be a char or an int in [-128, 127] or [0, 255]. */ |
| 8 | #define Py_CHARMASK(c) ((unsigned char)((c) & 0xff)) |
| 9 | |
| 10 | |
| 11 | /* Assert a build-time dependency, as an expression. |
| 12 | |
| 13 | Your compile will fail if the condition isn't true, or can't be evaluated |
| 14 | by the compiler. This can be used in an expression: its value is 0. |
| 15 | |
| 16 | Example: |
| 17 | |
| 18 | #define foo_to_char(foo) \ |
| 19 | ((char *)(foo) \ |
Victor Stinner | f0ddadc | 2011-09-29 12:43:18 +0200 | [diff] [blame] | 20 | + Py_BUILD_ASSERT_EXPR(offsetof(struct foo, string) == 0)) |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 21 | |
Victor Stinner | 573696a | 2011-09-29 12:12:39 +0200 | [diff] [blame] | 22 | Written by Rusty Russell, public domain, http://ccodearchive.net/ */ |
Victor Stinner | f0ddadc | 2011-09-29 12:43:18 +0200 | [diff] [blame] | 23 | #define Py_BUILD_ASSERT_EXPR(cond) \ |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 24 | (sizeof(char [1 - 2*!(cond)]) - 1) |
| 25 | |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 26 | /* Get the number of elements in a visible array |
| 27 | |
| 28 | This does not work on pointers, or arrays declared as [], or function |
| 29 | parameters. With correct compiler support, such usage will cause a build |
Victor Stinner | f0ddadc | 2011-09-29 12:43:18 +0200 | [diff] [blame] | 30 | error (see Py_BUILD_ASSERT_EXPR). |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 31 | |
Victor Stinner | 573696a | 2011-09-29 12:12:39 +0200 | [diff] [blame] | 32 | Written by Rusty Russell, public domain, http://ccodearchive.net/ */ |
Christian Heimes | fd0ddab | 2012-09-23 16:15:01 +0200 | [diff] [blame] | 33 | #if (defined(__GNUC__) && !defined(__STRICT_ANSI__)) |
Victor Stinner | 573696a | 2011-09-29 12:12:39 +0200 | [diff] [blame] | 34 | /* Two gcc extensions. |
| 35 | &a[0] degrades to a pointer: a different type from an array */ |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 36 | #define Py_ARRAY_LENGTH(array) \ |
Victor Stinner | 573696a | 2011-09-29 12:12:39 +0200 | [diff] [blame] | 37 | (sizeof(array) / sizeof((array)[0]) \ |
Victor Stinner | f0ddadc | 2011-09-29 12:43:18 +0200 | [diff] [blame] | 38 | + Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(typeof(array), \ |
| 39 | typeof(&(array)[0])))) |
Victor Stinner | 573696a | 2011-09-29 12:12:39 +0200 | [diff] [blame] | 40 | #else |
| 41 | #define Py_ARRAY_LENGTH(array) \ |
| 42 | (sizeof(array) / sizeof((array)[0])) |
| 43 | #endif |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 44 | |
| 45 | |
| 46 | /* Define macros for inline documentation. */ |
| 47 | #define PyDoc_VAR(name) static char name[] |
| 48 | #define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str) |
| 49 | #ifdef WITH_DOC_STRINGS |
| 50 | #define PyDoc_STR(str) str |
| 51 | #else |
| 52 | #define PyDoc_STR(str) "" |
| 53 | #endif |
| 54 | |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 55 | /* Below "a" is a power of 2. */ |
| 56 | /* Round down size "n" to be a multiple of "a". */ |
| 57 | #define _Py_SIZE_ROUND_DOWN(n, a) ((size_t)(n) & ~(size_t)((a) - 1)) |
| 58 | /* Round up size "n" to be a multiple of "a". */ |
| 59 | #define _Py_SIZE_ROUND_UP(n, a) (((size_t)(n) + \ |
| 60 | (size_t)((a) - 1)) & ~(size_t)((a) - 1)) |
| 61 | /* Round pointer "p" down to the closest "a"-aligned address <= "p". */ |
| 62 | #define _Py_ALIGN_DOWN(p, a) ((void *)((Py_uintptr_t)(p) & ~(Py_uintptr_t)((a) - 1))) |
| 63 | /* Round pointer "p" up to the closest "a"-aligned address >= "p". */ |
| 64 | #define _Py_ALIGN_UP(p, a) ((void *)(((Py_uintptr_t)(p) + \ |
| 65 | (Py_uintptr_t)((a) - 1)) & ~(Py_uintptr_t)((a) - 1))) |
| 66 | /* Check if pointer "p" is aligned to "a"-bytes boundary. */ |
| 67 | #define _Py_IS_ALIGNED(p, a) (!((Py_uintptr_t)(p) & (Py_uintptr_t)((a) - 1))) |
| 68 | |
Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 69 | #endif /* Py_PYMACRO_H */ |