Skip to content

Commit

Permalink
align allocations and PyGC_Head to 16 bytes on 64-bit platforms
Browse files Browse the repository at this point in the history
Combination of two upstream commits:
- gh#python/cpython@1b85f4e (3.7.17)
- gh#python/cpython@8766cb7 (3.7.17)

Fixes: bpo#27987
Fixes: rh#1923658
Patch: align-allocations-PyGC_Head.patch
  • Loading branch information
methane authored and mcepl committed Apr 5, 2024
1 parent 2ab4878 commit 869c3f7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Include/objimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,11 @@ typedef union _gc_head {
union _gc_head *gc_prev;
Py_ssize_t gc_refs;
} gc;
double dummy; /* force worst-case alignment */
long double dummy; /* force worst-case alignment */
// malloc returns memory block aligned for any built-in types and
// long double is the largest standard C type.
// On amd64 linux, long double requires 16 byte alignment.
// See bpo-27987 for more discussion.
} PyGC_Head;

extern PyGC_Head *_PyGC_generation0;
Expand Down
6 changes: 6 additions & 0 deletions Objects/obmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -650,8 +650,14 @@ static int running_on_valgrind = -1;
*
* You shouldn't change this unless you know what you are doing.
*/

#if SIZEOF_VOID_P > 4
#define ALIGNMENT 16 /* must be 2^N */
#define ALIGNMENT_SHIFT 4
#else
#define ALIGNMENT 8 /* must be 2^N */
#define ALIGNMENT_SHIFT 3
#endif

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

0 comments on commit 869c3f7

Please sign in to comment.