Skip to content

Commit 961e253

Browse files
methanemcepl
authored andcommitted
00358: align allocations and PyGC_Head to 16 bytes on 64-bit platforms
Upstream bug: https://bugs.python.org/issue27987 Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1923658 Combination of two upstream commits: - 1b85f4e - 8766cb7
1 parent 4def1bc commit 961e253

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

Include/objimpl.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,11 @@ typedef union _gc_head {
255255
union _gc_head *gc_prev;
256256
Py_ssize_t gc_refs;
257257
} gc;
258-
double dummy; /* force worst-case alignment */
258+
long double dummy; /* force worst-case alignment */
259+
// malloc returns memory block aligned for any built-in types and
260+
// long double is the largest standard C type.
261+
// On amd64 linux, long double requires 16 byte alignment.
262+
// See bpo-27987 for more discussion.
259263
} PyGC_Head;
260264

261265
extern PyGC_Head *_PyGC_generation0;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``PyGC_Head`` structure is aligned to ``long double``. This is needed to
2+
ensure GC-ed objects are aligned properly. Patch by Inada Naoki.

Objects/obmalloc.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,14 @@ static int running_on_valgrind = -1;
650650
*
651651
* You shouldn't change this unless you know what you are doing.
652652
*/
653+
654+
#if SIZEOF_VOID_P > 4
655+
#define ALIGNMENT 16 /* must be 2^N */
656+
#define ALIGNMENT_SHIFT 4
657+
#else
653658
#define ALIGNMENT 8 /* must be 2^N */
654659
#define ALIGNMENT_SHIFT 3
660+
#endif
655661

656662
/* Return the number of bytes in size class I, as a uint. */
657663
#define INDEX2SIZE(I) (((uint)(I) + 1) << ALIGNMENT_SHIFT)

0 commit comments

Comments
 (0)