1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
12 // constexpr T* allocate(size_t n);
16 #include <cstddef> // for std::max_align_t
18 #include "test_macros.h"
19 #include "count_new.h"
22 #ifdef TEST_HAS_NO_ALIGNED_ALLOCATION
23 static const bool UsingAlignedNew
= false;
25 static const bool UsingAlignedNew
= true;
28 #ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
29 static const std::size_t MaxAligned
= __STDCPP_DEFAULT_NEW_ALIGNMENT__
;
31 static const std::size_t MaxAligned
= std::alignment_of
<std::max_align_t
>::value
;
34 static const std::size_t OverAligned
= MaxAligned
* 2;
37 template <std::size_t Align
>
38 struct TEST_ALIGNAS(Align
) AlignedType
{
40 static int constructed
;
41 AlignedType() { ++constructed
; }
42 AlignedType(AlignedType
const&) { ++constructed
; }
43 ~AlignedType() { --constructed
; }
45 template <std::size_t Align
>
46 int AlignedType
<Align
>::constructed
= 0;
49 template <std::size_t Align
>
51 typedef AlignedType
<Align
> T
;
53 globalMemCounter
.reset();
55 const bool IsOverAlignedType
= Align
> MaxAligned
;
56 const bool ExpectAligned
= IsOverAlignedType
&& UsingAlignedNew
;
58 assert(globalMemCounter
.checkOutstandingNewEq(0));
59 assert(T::constructed
== 0);
60 globalMemCounter
.last_new_size
= 0;
61 globalMemCounter
.last_new_align
= 0;
62 T
* ap
= a
.allocate(3);
64 assert(globalMemCounter
.checkOutstandingNewEq(1));
65 assert(globalMemCounter
.checkNewCalledEq(1));
66 assert(globalMemCounter
.checkAlignedNewCalledEq(ExpectAligned
));
67 assert(globalMemCounter
.checkLastNewSizeEq(3 * sizeof(T
)));
68 assert(globalMemCounter
.checkLastNewAlignEq(ExpectAligned
? Align
: 0));
69 assert(T::constructed
== 0);
70 globalMemCounter
.last_delete_align
= 0;
72 assert(globalMemCounter
.checkOutstandingNewEq(0));
73 assert(globalMemCounter
.checkDeleteCalledEq(1));
74 assert(globalMemCounter
.checkAlignedDeleteCalledEq(ExpectAligned
));
75 assert(globalMemCounter
.checkLastDeleteAlignEq(ExpectAligned
? Align
: 0));
76 assert(T::constructed
== 0);
81 template <std::size_t Align
>
82 constexpr bool test_aligned_constexpr() {
83 typedef AlignedType
<Align
> T
;
85 T
* ap
= a
.allocate(3);
92 int main(int, char**) {
98 test_aligned
<MaxAligned
>();
99 test_aligned
<OverAligned
>();
100 test_aligned
<OverAligned
* 2>();
102 #if TEST_STD_VER > 17
103 static_assert(test_aligned_constexpr
<1>());
104 static_assert(test_aligned_constexpr
<2>());
105 static_assert(test_aligned_constexpr
<4>());
106 static_assert(test_aligned_constexpr
<8>());
107 static_assert(test_aligned_constexpr
<16>());
108 static_assert(test_aligned_constexpr
<MaxAligned
>());
109 static_assert(test_aligned_constexpr
<OverAligned
>());
110 static_assert(test_aligned_constexpr
<OverAligned
* 2>());