|
29 | 29 | using namespace swift; |
30 | 30 |
|
31 | 31 | #if defined(__APPLE__) |
32 | | -// Apple malloc is always 16-byte aligned. |
33 | | -# define MALLOC_ALIGN_MASK 15 |
34 | | - |
35 | | -#elif defined(__linux__) |
36 | | -// Linux malloc is 16-byte aligned on 64-bit, and 8-byte aligned on 32-bit. |
37 | | -# if defined(__LP64) |
38 | | -# define MALLOC_ALIGN_MASK 15 |
39 | | -# else |
40 | | -# define MALLOC_ALIGN_MASK 7 |
41 | | -# endif |
42 | | - |
43 | | -#elif defined(_WIN64) |
44 | | -// Windows malloc is 16-byte aligned on 64-bit and 8-byte aligned on 32-bit. |
45 | | -# define MALLOC_ALIGN_MASK 15 |
46 | | -#elif defined(_WIN32) |
47 | | -# define MALLOC_ALIGN_MASK 7 |
| 32 | +/// On Apple platforms, \c malloc() is always 16-byte aligned. |
| 33 | +static constexpr size_t MALLOC_ALIGN_MASK = 15; |
48 | 34 |
|
| 35 | +#elif defined(__linux__) || defined(_WIN32) |
| 36 | +/// On Linux and Windows, \c malloc() returns 16-byte aligned pointers on 64-bit |
| 37 | +/// and 8-byte aligned pointers on 32-bit. |
| 38 | +#if defined(__LP64) || defined(_WIN64) |
| 39 | +static constexpr size_t MALLOC_ALIGN_MASK = 15; |
49 | 40 | #else |
50 | | -// Unknown alignment, but the standard requires alignment suitable for the largest |
51 | | -// standard types. |
52 | | -# define MALLOC_ALIGN_MASK std::max(alignof(void *), alignof(double)) |
| 41 | +static constexpr size_t MALLOC_ALIGN_MASK = 7; |
| 42 | +#endif |
53 | 43 |
|
| 44 | +#else |
| 45 | +/// This platform's \c malloc() constraints are unknown, so fall back to a value |
| 46 | +/// derived from \c std::max_align_t that will be sufficient, but is not |
| 47 | +/// necessarily optimal. |
| 48 | +/// |
| 49 | +/// The C and C++ standards defined \c max_align_t as a type whose alignment is |
| 50 | +/// at least that of every scalar type. It is the lower bound for the alignment |
| 51 | +/// of any pointer returned from \c malloc(). |
| 52 | +static constexpr size_t MALLOC_ALIGN_MASK = alignof(std::max_align_t) - 1; |
54 | 53 | #endif |
55 | 54 |
|
56 | 55 | // This assert ensures that manually allocated memory always uses the |
|
0 commit comments