allocate.pass.cpp (3678B)
1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <memory> 11 12 // allocator: 13 // pointer allocate(size_type n, allocator<void>::const_pointer hint=0); 14 15 #include <memory> 16 #include <cassert> 17 #include <cstddef> // for std::max_align_t 18 #include <iostream> 19 20 #include "test_macros.h" 21 #include "count_new.hpp" 22 23 24 #ifdef TEST_HAS_NO_ALIGNED_ALLOCATION 25 static const bool UsingAlignedNew = false; 26 #else 27 static const bool UsingAlignedNew = true; 28 #endif 29 30 #ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__ 31 static const size_t MaxAligned = __STDCPP_DEFAULT_NEW_ALIGNMENT__; 32 #else 33 static const size_t MaxAligned = std::alignment_of<std::max_align_t>::value; 34 #endif 35 36 static const size_t OverAligned = MaxAligned * 2; 37 38 39 template <size_t Align> 40 struct TEST_ALIGNAS(Align) AlignedType { 41 char data; 42 static int constructed; 43 AlignedType() { ++constructed; } 44 AlignedType(AlignedType const&) { ++constructed; } 45 ~AlignedType() { --constructed; } 46 }; 47 template <size_t Align> 48 int AlignedType<Align>::constructed = 0; 49 50 51 template <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 assert(globalMemCounter.checkOutstandingNewEq(0)); 61 assert(T::constructed == 0); 62 globalMemCounter.last_new_size = 0; 63 globalMemCounter.last_new_align = 0; 64 T* ap = a.allocate(3); 65 DoNotOptimize(ap); 66 assert(globalMemCounter.checkOutstandingNewEq(1)); 67 assert(globalMemCounter.checkNewCalledEq(1)); 68 assert(globalMemCounter.checkAlignedNewCalledEq(ExpectAligned)); 69 assert(globalMemCounter.checkLastNewSizeEq(3 * sizeof(T))); 70 assert(globalMemCounter.checkLastNewAlignEq(ExpectAligned ? Align : 0)); 71 assert(T::constructed == 0); 72 globalMemCounter.last_delete_align = 0; 73 a.deallocate(ap, 3); 74 assert(globalMemCounter.checkOutstandingNewEq(0)); 75 assert(globalMemCounter.checkDeleteCalledEq(1)); 76 assert(globalMemCounter.checkAlignedDeleteCalledEq(ExpectAligned)); 77 assert(globalMemCounter.checkLastDeleteAlignEq(ExpectAligned ? Align : 0)); 78 assert(T::constructed == 0); 79 } 80 globalMemCounter.reset(); 81 { 82 globalMemCounter.last_new_size = 0; 83 globalMemCounter.last_new_align = 0; 84 T* volatile ap2 = a.allocate(11, (const void*)5); 85 DoNotOptimize(ap2); 86 assert(globalMemCounter.checkOutstandingNewEq(1)); 87 assert(globalMemCounter.checkNewCalledEq(1)); 88 assert(globalMemCounter.checkAlignedNewCalledEq(ExpectAligned)); 89 assert(globalMemCounter.checkLastNewSizeEq(11 * sizeof(T))); 90 assert(globalMemCounter.checkLastNewAlignEq(ExpectAligned ? Align : 0)); 91 assert(T::constructed == 0); 92 globalMemCounter.last_delete_align = 0; 93 a.deallocate(ap2, 11); 94 DoNotOptimize(ap2); 95 assert(globalMemCounter.checkOutstandingNewEq(0)); 96 assert(globalMemCounter.checkDeleteCalledEq(1)); 97 assert(globalMemCounter.checkAlignedDeleteCalledEq(ExpectAligned)); 98 assert(globalMemCounter.checkLastDeleteAlignEq(ExpectAligned ? Align : 0)); 99 assert(T::constructed == 0); 100 } 101 } 102 103 int main() { 104 test_aligned<1>(); 105 test_aligned<2>(); 106 test_aligned<4>(); 107 test_aligned<8>(); 108 test_aligned<16>(); 109 test_aligned<MaxAligned>(); 110 test_aligned<OverAligned>(); 111 test_aligned<OverAligned * 2>(); 112 }