Issue 31912: PyMem_Malloc() should guarantee alignof(max_align_t)

Created on 2017-10-31 18:01 by skrah, last changed 2022-04-11 14:58 by admin.

Messages (4) msg305320 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2017-10-31 18:01
This is related to #27987 and #20064 and perhaps the pymalloc patch
from #18835.

I think PyMem_Malloc() should guarantee alignof(max_align_t).

It actually did before the "#define PYMEM_FUNCS PYOBJ_FUNCS" optimization,
so we have a sort of regression here.
msg305322 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-10-31 18:07
> I think PyMem_Malloc() should guarantee alignof(max_align_t).

Do you mean the C++ std::max_align_t? Does C99 have something like that?

The Linux malloc() manual page says:

"The malloc() and calloc() functions return a pointer to  the  allocated memory,  which  is  suitably  aligned for any built-in type."

But I don't know the list of C built-in types.
msg305324 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2017-10-31 18:14
> Do you mean the C++ std::max_align_t? Does C99 have something like that?
> 
> The Linux malloc() manual page says:
> 
> "The malloc() and calloc() functions return a pointer to  the  allocated memory,  which  is  suitably  aligned for any built-in type."

C11 has max_align_t, but also for C99 "any builtin type" means 16 byte alignment
for long double on x64, so malloc() and calloc() are required to align 16 bytes
with -std=c99 (and earlier).

max_align_t is just a shorthand to express the concept.
msg311322 - (view) Author: Florian Weimer (fweimer) Date: 2018-01-31 10:55
max_align_t is a bit of a kitchen sink and will specify larger and larger alignment in the future, e.g. 32-byte alignment for a complex _Float128 type (a pair of two _Float128 variables).  The alignment is also not generally useful for allocations whose size is smaller than the alignment.  (Many mallocs do not follow the C standard and do not provide 16-byte alignment when 8 bytes are allocated, although alignof(max_align_t) is 16.)