| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // <memory> |
| 10 | |
| 11 | // allocator: |
| 12 | // T* allocate(size_t n, const void* hint); |
| 13 | |
| 14 | // Removed in C++20, deprecated in C++17. |
| 15 | |
| 16 | // REQUIRES: c++03 || c++11 || c++14 || c++17 |
| 17 | // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS |
| 18 | |
| 19 | #include <memory> |
| 20 | #include <cassert> |
| 21 | #include <cstddef> // for std::max_align_t |
| 22 | |
| 23 | #include "test_macros.h" |
| 24 | #include "count_new.h" |
| 25 | |
| 26 | #ifdef TEST_HAS_NO_ALIGNED_ALLOCATION |
| 27 | static const bool UsingAlignedNew = false; |
| 28 | #else |
| 29 | static const bool UsingAlignedNew = true; |
| 30 | #endif |
| 31 | |
| 32 | #ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__ |
| 33 | static const std::size_t MaxAligned = __STDCPP_DEFAULT_NEW_ALIGNMENT__; |
| 34 | #else |
| 35 | static const std::size_t MaxAligned = std::alignment_of<std::max_align_t>::value; |
| 36 | #endif |
| 37 | |
| 38 | static const std::size_t OverAligned = MaxAligned * 2; |
| 39 | |
| 40 | template <std::size_t Align> |
| 41 | struct TEST_ALIGNAS(Align) AlignedType { |
| 42 | char data; |
| 43 | static int constructed; |
| 44 | AlignedType() { ++constructed; } |
| 45 | AlignedType(AlignedType const&) { ++constructed; } |
| 46 | ~AlignedType() { --constructed; } |
| 47 | }; |
| 48 | template <std::size_t Align> |
| 49 | int AlignedType<Align>::constructed = 0; |
| 50 | |
| 51 | template <std::size_t Align> |
| 52 | void test_aligned() { |
| 53 | typedef AlignedType<Align> T; |
| 54 | T::constructed = 0; |
| 55 | globalMemCounter.reset(); |
| 56 | std::allocator<T> a; |
| 57 | const bool IsOverAlignedType = Align > MaxAligned; |
| 58 | const bool ExpectAligned = IsOverAlignedType && UsingAlignedNew; |
| 59 | { |
| 60 | globalMemCounter.last_new_size = 0; |
| 61 | globalMemCounter.last_new_align = 0; |
| 62 | T* ap2 = a.allocate(n: 11, (const void*)5); |
| 63 | DoNotOptimize(ap2); |
| 64 | assert(globalMemCounter.checkOutstandingNewEq(1)); |
| 65 | assert(globalMemCounter.checkNewCalledEq(1)); |
| 66 | assert(globalMemCounter.checkAlignedNewCalledEq(ExpectAligned)); |
| 67 | assert(globalMemCounter.checkLastNewSizeEq(11 * sizeof(T))); |
| 68 | assert(globalMemCounter.checkLastNewAlignEq(ExpectAligned ? Align : 0)); |
| 69 | assert(T::constructed == 0); |
| 70 | globalMemCounter.last_delete_align = 0; |
| 71 | a.deallocate(p: ap2, t: 11); |
| 72 | DoNotOptimize(ap2); |
| 73 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
| 74 | assert(globalMemCounter.checkDeleteCalledEq(1)); |
| 75 | assert(globalMemCounter.checkAlignedDeleteCalledEq(ExpectAligned)); |
| 76 | assert(globalMemCounter.checkLastDeleteAlignEq(ExpectAligned ? Align : 0)); |
| 77 | assert(T::constructed == 0); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | int main(int, char**) { |
| 82 | test_aligned<1>(); |
| 83 | test_aligned<2>(); |
| 84 | test_aligned<4>(); |
| 85 | test_aligned<8>(); |
| 86 | test_aligned<16>(); |
| 87 | test_aligned<MaxAligned>(); |
| 88 | test_aligned<OverAligned>(); |
| 89 | test_aligned<OverAligned * 2>(); |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |