Skip to content

Change the definition of MALLOC_ALIGN_MASK so it should always be correct on all platforms. #58495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions stdlib/public/runtime/Heap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,27 @@
using namespace swift;

#if defined(__APPLE__)
// Apple malloc is always 16-byte aligned.
# define MALLOC_ALIGN_MASK 15

#elif defined(__linux__)
// Linux malloc is 16-byte aligned on 64-bit, and 8-byte aligned on 32-bit.
# if defined(__LP64)
# define MALLOC_ALIGN_MASK 15
# else
# define MALLOC_ALIGN_MASK 7
# endif

#elif defined(_WIN64)
// Windows malloc is 16-byte aligned on 64-bit and 8-byte aligned on 32-bit.
# define MALLOC_ALIGN_MASK 15
#elif defined(_WIN32)
# define MALLOC_ALIGN_MASK 7
/// On Apple platforms, \c malloc() is always 16-byte aligned.
static constexpr size_t MALLOC_ALIGN_MASK = 15;

#elif defined(__linux__) || defined(_WIN32)
/// On Linux and Windows, \c malloc() returns 16-byte aligned pointers on 64-bit
/// and 8-byte aligned pointers on 32-bit.
#if defined(__LP64) || defined(_WIN64)
static constexpr size_t MALLOC_ALIGN_MASK = 15;
#else
// Unknown alignment, but the standard requires alignment suitable for the largest
// standard types.
# define MALLOC_ALIGN_MASK std::max(alignof(void *), alignof(double))
static constexpr size_t MALLOC_ALIGN_MASK = 7;
#endif

#else
/// This platform's \c malloc() constraints are unknown, so fall back to a value
/// derived from \c std::max_align_t that will be sufficient, but is not
/// necessarily optimal.
///
/// The C and C++ standards defined \c max_align_t as a type whose alignment is
/// at least that of every scalar type. It is the lower bound for the alignment
/// of any pointer returned from \c malloc().
static constexpr size_t MALLOC_ALIGN_MASK = alignof(std::max_align_t) - 1;
#endif

// This assert ensures that manually allocated memory always uses the
Expand Down