8000 Work around lack of alignof & max_align_t · python/cpython@291731b · GitHub
[go: up one dir, main page]

Skip to content

Commit 291731b

Browse files
committed
Work around lack of alignof & max_align_t
1 parent 439de5d commit 291731b

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Modules/_testcapi/heaptype_relative.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,13 @@ _PyTestCapi_Init_HeaptypeRelative(PyObject *m) {
262262
return -1;
263263
}
264264

265+
#ifdef __alignof_is_defined
266+
// C11
265267
PyModule_AddIntConstant(m, "alignof_max_align_t", alignof(max_align_t));
268+
#else
269+
// if alignof and max_align_t is unavailable, skip the alignment tests
270+
PyModule_AddIntConstant(m, "alignof_max_align_t", 1);
271+
#endif
266272

267273
return 0;
268274
}

Objects/typeobject.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3553,10 +3553,22 @@ static const PySlot_Offset pyslot_offsets[] = {
35533553
/* Align up to the nearest multiple of alignof(max_align_t)
35543554
* (like _Py_ALIGN_UP, but for a size rather than pointer)
35553555
*/
3556+
#if __alignof_is_defined
3557+
// C11
3558+
#define MAX_ALIGN alignof(max_align_t)
3559+
#else
3560+
// workaround for MSVC
3561+
typedef union {
3562+
intmax_t x;
3563+
long double y;
3564+
void *z;
3565+
void (*f)();
3566+
} _max_align_t_wannabe;
3567+
#define MAX_ALIGN _Alignof(_max_align_t_wannabe)
3568+
#endif
35563569
static Py_ssize_t
35573570
_align_up(Py_ssize_t size) {
3558-
const Py_ssize_t alignment = alignof(max_align_t);
3559-
return (size + alignment - 1) & ~(alignment - 1);
3571+
return (size + MAX_ALIGN - 1) & ~(MAX_ALIGN - 1);
35603572
}
35613573

35623574
/* Given a PyType_FromMetaclass `bases` argument (NULL, type, or tuple of

0 commit comments

Comments
 (0)
0