blob: 1dc0c61653c981679ca7d7f61c42c37f4ba7e3fb [file] [log] [blame]
Victor Stinnerdfb866d2011-09-29 01:12:24 +02001#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 Stinnerf0ddadc2011-09-29 12:43:18 +020020 + Py_BUILD_ASSERT_EXPR(offsetof(struct foo, string) == 0))
Victor Stinnerdfb866d2011-09-29 01:12:24 +020021
Victor Stinner573696a2011-09-29 12:12:39 +020022 Written by Rusty Russell, public domain, http://ccodearchive.net/ */
Victor Stinnerf0ddadc2011-09-29 12:43:18 +020023#define Py_BUILD_ASSERT_EXPR(cond) \
Victor Stinnerdfb866d2011-09-29 01:12:24 +020024 (sizeof(char [1 - 2*!(cond)]) - 1)
25
Victor Stinnerdfb866d2011-09-29 01:12:24 +020026/* 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 Stinnerf0ddadc2011-09-29 12:43:18 +020030 error (see Py_BUILD_ASSERT_EXPR).
Victor Stinnerdfb866d2011-09-29 01:12:24 +020031
Victor Stinner573696a2011-09-29 12:12:39 +020032 Written by Rusty Russell, public domain, http://ccodearchive.net/ */
33#if defined(__GNUC__)
34/* Two gcc extensions.
35 &a[0] degrades to a pointer: a different type from an array */
Victor Stinnerdfb866d2011-09-29 01:12:24 +020036#define Py_ARRAY_LENGTH(array) \
Victor Stinner573696a2011-09-29 12:12:39 +020037 (sizeof(array) / sizeof((array)[0]) \
Victor Stinnerf0ddadc2011-09-29 12:43:18 +020038 + Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(typeof(array), \
39 typeof(&(array)[0]))))
Victor Stinner573696a2011-09-29 12:12:39 +020040#else
41#define Py_ARRAY_LENGTH(array) \
42 (sizeof(array) / sizeof((array)[0]))
43#endif
Victor Stinnerdfb866d2011-09-29 01:12:24 +020044
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
55#endif /* Py_PYMACRO_H */