Skip to content

Commit 40bc80a

Browse files
committed
Change the definition of MALLOC_ALIGN_MASK so it should always be correct on all platforms.
1 parent f652a9d commit 40bc80a

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

stdlib/public/runtime/Heap.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,27 @@
2929
using namespace swift;
3030

3131
#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;
4834

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;
4940
#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
5343

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;
5453
#endif
5554

5655
// This assert ensures that manually allocated memory always uses the

0 commit comments

Comments
 (0)