blob: 3f6f5dce61286ca14ceba5c096636c43d733bad4 [file] [log] [blame]
Victor Stinnerdfb866d2011-09-29 01:12:24 +02001#ifndef Py_PYMACRO_H
2#define Py_PYMACRO_H
3
Victor Stinner45e8e2f2014-05-14 17:24:35 +02004/* Minimum value between x and y */
Victor Stinnerdfb866d2011-09-29 01:12:24 +02005#define Py_MIN(x, y) (((x) > (y)) ? (y) : (x))
Victor Stinner45e8e2f2014-05-14 17:24:35 +02006
7/* Maximum value between x and y */
Victor Stinnerdfb866d2011-09-29 01:12:24 +02008#define Py_MAX(x, y) (((x) > (y)) ? (x) : (y))
9
Victor Stinner45e8e2f2014-05-14 17:24:35 +020010/* 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 Stinnerdfb866d2011-09-29 01:12:24 +020021/* 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 Stinnerdfb866d2011-09-29 01:12:24 +020024/* 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 Stinnerf0ddadc2011-09-29 12:43:18 +020033 + Py_BUILD_ASSERT_EXPR(offsetof(struct foo, string) == 0))
Victor Stinnerdfb866d2011-09-29 01:12:24 +020034
Victor Stinner573696a2011-09-29 12:12:39 +020035 Written by Rusty Russell, public domain, http://ccodearchive.net/ */
Victor Stinnerf0ddadc2011-09-29 12:43:18 +020036#define Py_BUILD_ASSERT_EXPR(cond) \
Victor Stinnerdfb866d2011-09-29 01:12:24 +020037 (sizeof(char [1 - 2*!(cond)]) - 1)
38
Victor Stinnerdfb866d2011-09-29 01:12:24 +020039/* Get the number of elements in a visible array
40
41 This does not work on pointers, or arrays declared as [], or function
42 parameters. With correct compiler support, such usage will cause a build
Victor Stinnerf0ddadc2011-09-29 12:43:18 +020043 error (see Py_BUILD_ASSERT_EXPR).
Victor Stinnerdfb866d2011-09-29 01:12:24 +020044
Christian Heimes61dbb002013-01-06 16:41:56 +010045 Written by Rusty Russell, public domain, http://ccodearchive.net/
46
47 Requires at GCC 3.1+ */
48#if (defined(__GNUC__) && !defined(__STRICT_ANSI__) && \
Christian Heimese0a2d122013-06-24 15:39:41 +020049 (((__GNUC__ == 3) && (__GNU_MINOR__ >= 1)) || (__GNUC__ >= 4)))
Victor Stinner573696a2011-09-29 12:12:39 +020050/* Two gcc extensions.
51 &a[0] degrades to a pointer: a different type from an array */
Victor Stinnerdfb866d2011-09-29 01:12:24 +020052#define Py_ARRAY_LENGTH(array) \
Victor Stinner573696a2011-09-29 12:12:39 +020053 (sizeof(array) / sizeof((array)[0]) \
Victor Stinnerf0ddadc2011-09-29 12:43:18 +020054 + Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(typeof(array), \
55 typeof(&(array)[0]))))
Victor Stinner573696a2011-09-29 12:12:39 +020056#else
57#define Py_ARRAY_LENGTH(array) \
58 (sizeof(array) / sizeof((array)[0]))
59#endif
Victor Stinnerdfb866d2011-09-29 01:12:24 +020060
61
62/* Define macros for inline documentation. */
63#define PyDoc_VAR(name) static char name[]
64#define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str)
65#ifdef WITH_DOC_STRINGS
66#define PyDoc_STR(str) str
67#else
68#define PyDoc_STR(str) ""
69#endif
70
Antoine Pitrouca8aa4a2012-09-20 20:56:47 +020071/* Below "a" is a power of 2. */
72/* Round down size "n" to be a multiple of "a". */
73#define _Py_SIZE_ROUND_DOWN(n, a) ((size_t)(n) & ~(size_t)((a) - 1))
74/* Round up size "n" to be a multiple of "a". */
75#define _Py_SIZE_ROUND_UP(n, a) (((size_t)(n) + \
76 (size_t)((a) - 1)) & ~(size_t)((a) - 1))
77/* Round pointer "p" down to the closest "a"-aligned address <= "p". */
78#define _Py_ALIGN_DOWN(p, a) ((void *)((Py_uintptr_t)(p) & ~(Py_uintptr_t)((a) - 1)))
79/* Round pointer "p" up to the closest "a"-aligned address >= "p". */
80#define _Py_ALIGN_UP(p, a) ((void *)(((Py_uintptr_t)(p) + \
81 (Py_uintptr_t)((a) - 1)) & ~(Py_uintptr_t)((a) - 1)))
82/* Check if pointer "p" is aligned to "a"-bytes boundary. */
83#define _Py_IS_ALIGNED(p, a) (!((Py_uintptr_t)(p) & (Py_uintptr_t)((a) - 1)))
84
Larry Hastings3cceb382014-01-04 11:09:09 -080085#ifdef __GNUC__
86#define Py_UNUSED(name) _unused_ ## name __attribute__((unused))
87#else
88#define Py_UNUSED(name) _unused_ ## name
89#endif
90
Victor Stinnerdfb866d2011-09-29 01:12:24 +020091#endif /* Py_PYMACRO_H */