| 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 |  | 
| Christian Heimes | 61dbb00 | 2013-01-06 16:41:56 +0100 | [diff] [blame] | 32 |    Written by Rusty Russell, public domain, http://ccodearchive.net/ | 
 | 33 |  | 
 | 34 |    Requires at GCC 3.1+ */ | 
 | 35 | #if (defined(__GNUC__) && !defined(__STRICT_ANSI__) && \ | 
| Christian Heimes | e0a2d12 | 2013-06-24 15:39:41 +0200 | [diff] [blame] | 36 |     (((__GNUC__ == 3) && (__GNU_MINOR__ >= 1)) || (__GNUC__ >= 4))) | 
| Victor Stinner | 573696a | 2011-09-29 12:12:39 +0200 | [diff] [blame] | 37 | /* Two gcc extensions. | 
 | 38 |    &a[0] degrades to a pointer: a different type from an array */ | 
| Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 39 | #define Py_ARRAY_LENGTH(array) \ | 
| Victor Stinner | 573696a | 2011-09-29 12:12:39 +0200 | [diff] [blame] | 40 |     (sizeof(array) / sizeof((array)[0]) \ | 
| Victor Stinner | f0ddadc | 2011-09-29 12:43:18 +0200 | [diff] [blame] | 41 |      + Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(typeof(array), \ | 
 | 42 |                                                           typeof(&(array)[0])))) | 
| Victor Stinner | 573696a | 2011-09-29 12:12:39 +0200 | [diff] [blame] | 43 | #else | 
 | 44 | #define Py_ARRAY_LENGTH(array) \ | 
 | 45 |     (sizeof(array) / sizeof((array)[0])) | 
 | 46 | #endif | 
| Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 47 |  | 
 | 48 |  | 
 | 49 | /* Define macros for inline documentation. */ | 
 | 50 | #define PyDoc_VAR(name) static char name[] | 
 | 51 | #define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str) | 
 | 52 | #ifdef WITH_DOC_STRINGS | 
 | 53 | #define PyDoc_STR(str) str | 
 | 54 | #else | 
 | 55 | #define PyDoc_STR(str) "" | 
 | 56 | #endif | 
 | 57 |  | 
| Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 58 | /* Below "a" is a power of 2. */ | 
 | 59 | /* Round down size "n" to be a multiple of "a". */ | 
 | 60 | #define _Py_SIZE_ROUND_DOWN(n, a) ((size_t)(n) & ~(size_t)((a) - 1)) | 
 | 61 | /* Round up size "n" to be a multiple of "a". */ | 
 | 62 | #define _Py_SIZE_ROUND_UP(n, a) (((size_t)(n) + \ | 
 | 63 |         (size_t)((a) - 1)) & ~(size_t)((a) - 1)) | 
 | 64 | /* Round pointer "p" down to the closest "a"-aligned address <= "p". */ | 
 | 65 | #define _Py_ALIGN_DOWN(p, a) ((void *)((Py_uintptr_t)(p) & ~(Py_uintptr_t)((a) - 1))) | 
 | 66 | /* Round pointer "p" up to the closest "a"-aligned address >= "p". */ | 
 | 67 | #define _Py_ALIGN_UP(p, a) ((void *)(((Py_uintptr_t)(p) + \ | 
 | 68 |         (Py_uintptr_t)((a) - 1)) & ~(Py_uintptr_t)((a) - 1))) | 
 | 69 | /* Check if pointer "p" is aligned to "a"-bytes boundary. */ | 
 | 70 | #define _Py_IS_ALIGNED(p, a) (!((Py_uintptr_t)(p) & (Py_uintptr_t)((a) - 1))) | 
 | 71 |  | 
| Victor Stinner | dfb866d | 2011-09-29 01:12:24 +0200 | [diff] [blame] | 72 | #endif /* Py_PYMACRO_H */ |