Skip to content

Commit aade0ec

Browse files
committed
Fix the guaranteed alignment of memory returned by malloc/new on Darwin
The guaranteed alignment is 16 bytes on Darwin. rdar://73431623 Differential Revision: https://reviews.llvm.org/D95910
1 parent 781a1b1 commit aade0ec

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

clang/lib/Basic/TargetInfo.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,12 @@ TargetInfo::TargetInfo(const llvm::Triple &T) : TargetOpts(), Triple(T) {
6767
// From the glibc documentation, on GNU systems, malloc guarantees 16-byte
6868
// alignment on 64-bit systems and 8-byte alignment on 32-bit systems. See
6969
// https://www.gnu.org/software/libc/manual/html_node/Malloc-Examples.html.
70-
// This alignment guarantee also applies to Windows and Android.
70+
// This alignment guarantee also applies to Windows and Android. On Darwin,
71+
// the alignment is 16 bytes on both 64-bit and 32-bit systems.
7172
if (T.isGNUEnvironment() || T.isWindowsMSVCEnvironment() || T.isAndroid())
7273
NewAlign = Triple.isArch64Bit() ? 128 : Triple.isArch32Bit() ? 64 : 0;
74+
else if (T.isOSDarwin())
75+
NewAlign = 128;
7376
else
7477
NewAlign = 0; // Infer from basic type alignment.
7578
HalfWidth = 16;

clang/test/Preprocessor/init-aarch64.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,9 @@
532532
// AARCH64-DARWIN: #define __WINT_WIDTH__ 32
533533
// AARCH64-DARWIN: #define __aarch64__ 1
534534

535+
// RUN: %clang_cc1 -E -dM -triple=aarch64-apple-ios7.0 -x c++ < /dev/null | FileCheck -match-full-lines -check-prefix AARCH64-DARWIN-CXX %s
536+
// AARCH64-DARWIN-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 16UL
537+
535538
// RUN: %clang_cc1 -E -dM -ffreestanding -triple=aarch64-windows-msvc < /dev/null | FileCheck -match-full-lines -check-prefix AARCH64-MSVC %s
536539

537540
// AARCH64-MSVC: #define _INTEGRAL_MAX_BITS 64

clang/test/Preprocessor/init-arm.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@
196196
// ARM:#define __arm 1
197197
// ARM:#define __arm__ 1
198198

199+
// RUN: %clang_cc1 -E -dM -triple=armv7-apple-ios7.0 -x c++ < /dev/null | FileCheck -match-full-lines -check-prefix ARM-DARWIN-CXX %s
200+
// ARM-DARWIN-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 16UL
201+
199202
// RUN: %clang_cc1 -dM -ffreestanding -triple arm-none-none -target-abi apcs-gnu -E /dev/null -o - | FileCheck -match-full-lines -check-prefix ARM-APCS-GNU %s
200203
// ARM-APCS-GNU: #define __INTPTR_TYPE__ int
201204
// ARM-APCS-GNU: #define __PTRDIFF_TYPE__ int

0 commit comments

Comments
 (0)