summaryrefslogtreecommitdiff
path: root/Include/pymacro.h
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2012-09-20 20:56:47 +0200
committerAntoine Pitrou <solipsis@pitrou.net>2012-09-20 20:56:47 +0200
commitca8aa4acf6755dd012706e1e38fb737ae83ab5c6 (patch)
tree310b20a536a99dd80657e2d22e07bb1466d0a895 /Include/pymacro.h
parent1c47222a256f2977dcbb36c05dce7a5ae8e6ae06 (diff)
downloadcpython-git-ca8aa4acf6755dd012706e1e38fb737ae83ab5c6.tar.gz
Issue #15144: Fix possible integer overflow when handling pointers as integer values, by using Py_uintptr_t instead of size_t.
Patch by Serhiy Storchaka.
Diffstat (limited to 'Include/pymacro.h')
-rw-r--r--Include/pymacro.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/Include/pymacro.h b/Include/pymacro.h
index 1dc0c61653..ce1cbefb73 100644
--- a/Include/pymacro.h
+++ b/Include/pymacro.h
@@ -52,4 +52,18 @@
#define PyDoc_STR(str) ""
#endif
+/* Below "a" is a power of 2. */
+/* Round down size "n" to be a multiple of "a". */
+#define _Py_SIZE_ROUND_DOWN(n, a) ((size_t)(n) & ~(size_t)((a) - 1))
+/* Round up size "n" to be a multiple of "a". */
+#define _Py_SIZE_ROUND_UP(n, a) (((size_t)(n) + \
+ (size_t)((a) - 1)) & ~(size_t)((a) - 1))
+/* Round pointer "p" down to the closest "a"-aligned address <= "p". */
+#define _Py_ALIGN_DOWN(p, a) ((void *)((Py_uintptr_t)(p) & ~(Py_uintptr_t)((a) - 1)))
+/* Round pointer "p" up to the closest "a"-aligned address >= "p". */
+#define _Py_ALIGN_UP(p, a) ((void *)(((Py_uintptr_t)(p) + \
+ (Py_uintptr_t)((a) - 1)) & ~(Py_uintptr_t)((a) - 1)))
+/* Check if pointer "p" is aligned to "a"-bytes boundary. */
+#define _Py_IS_ALIGNED(p, a) (!((Py_uintptr_t)(p) & (Py_uintptr_t)((a) - 1)))
+
#endif /* Py_PYMACRO_H */