On 11/1/20 8:37 AM, Liu Hao wrote:
在 2020/11/1 10:31, JonY via Mingw-w64-public 写道:typedef struct { long long __max_align_ll __attribute__((__aligned__(__alignof__(long long)))); long double __max_align_ld __attribute__((__aligned__(__alignof__(long double)))); + /* _Float128 is defined as a basic type, so max_align_t must be + sufficiently aligned for it. This code must work in C++, so we + use __float128 here; that is only available on some + architectures, but only on i386 is extra alignment needed for + __float128. */ +/* compiler probably knows __float128 if __SIZEOF_FLOAT128__ is defined */ +#if defined(__i386__) && defined (__SIZEOF_FLOAT128__) + __float128 __max_align_f128 __attribute__((__aligned__(__alignof(__float128)))); +#endif } max_align_t;This is not correct AFAICT. I will attach some direct quotes from the C17 standard draft [1] [2] below, which is shared between C and C++. In the case of C++ there are some library functions which take `alignof(std::max_align_t)` as default arguments which would introduce even more incompatibility. [1] http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2176.pdf [2] https://files.lhmouse.com/standards/ISO%20C%20N2176.pdf7.22.3 Memory management functions 1 The order and contiguity of storage allocated by successive calls to the aligned_alloc, calloc, malloc, and realloc functions is unspecified. The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object with a fundamental alignment requirement and then used to access such an object or an array of such objects in the space allocated (until the space is explicitly deallocated).This paragraph requires that a non-null return value of `malloc()` shall be aligned to at least 'fundamental alignment'. With 32-bit MSVCRT this value is `8` i.e. `2 * sizeof(void*)`.6.2.8 Alignment of objects 2 A fundamental alignment is a valid alignment less than or equal to _Alignof (max_align_t).Your patch increases `_Alignof(max_align_t)` to `16`, so `16` (if it is valid anyway; I don't see why it shouldn't be) is a 'fundamental alignment'. But the `malloc()` function cannot guarantee such alignment, so it is not correct to do so.3 An extended alignment is represented by an alignment greater than _Alignof (max_align_t). It is implementation-defined whether any extended alignments are supported and the storage durations for which they are supported.On the other hand, `__float128` may exist on 32-bit Windows with an 'extended alignment'. Extended alignments have stricter requirements basing on their the storage durations. E.g. a `static`, `_Thread_local` or automatic object may be supported, but `malloc()`'d memory may not. Anyway there is no need to increase `_Alignof(max_align_t)`.
I see, thanks for the heads up, so the mingw-w64 definition is correct and should not be increased without wrapping malloc.
OpenPGP_signature
Description: OpenPGP digital signature
_______________________________________________ Mingw-w64-public mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
