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 // template <class... Args> void construct(pointer p, Args&&... args); 14 15 #include <memory> 16 #include <cassert> 17 18 #include "test_macros.h" 19 #include "count_new.hpp" 20 21 int A_constructed = 0; 22 23 struct A 24 { 25 int data; 26 A() {++A_constructed;} 27 28 A(const A&) {++A_constructed;} 29 30 explicit A(int) {++A_constructed;} 31 A(int, int*) {++A_constructed;} 32 33 ~A() {--A_constructed;} 34 }; 35 36 int move_only_constructed = 0; 37 38 #if TEST_STD_VER >= 11 39 class move_only 40 { 41 move_only(const move_only&) = delete; 42 move_only& operator=(const move_only&)= delete; 43 44 public: 45 move_only(move_only&&) {++move_only_constructed;} 46 move_only& operator=(move_only&&) {return *this;} 47 48 move_only() {++move_only_constructed;} 49 ~move_only() {--move_only_constructed;} 50 51 public: 52 int data; // unused other than to make sizeof(move_only) == sizeof(int). 53 // but public to suppress "-Wunused-private-field" 54 }; 55 #endif // TEST_STD_VER >= 11 56 57 int main() 58 { 59 globalMemCounter.reset(); 60 { 61 std::allocator<A> a; 62 assert(globalMemCounter.checkOutstandingNewEq(0)); 63 assert(A_constructed == 0); 64 65 globalMemCounter.last_new_size = 0; 66 A* ap = a.allocate(3); 67 assert(globalMemCounter.checkOutstandingNewEq(1)); 68 assert(globalMemCounter.checkLastNewSizeEq(3 * sizeof(int))); 69 assert(A_constructed == 0); 70 71 a.construct(ap); 72 assert(globalMemCounter.checkOutstandingNewEq(1)); 73 assert(A_constructed == 1); 74 75 a.destroy(ap); 76 assert(globalMemCounter.checkOutstandingNewEq(1)); 77 assert(A_constructed == 0); 78 79 a.construct(ap, A()); 80 assert(globalMemCounter.checkOutstandingNewEq(1)); 81 assert(A_constructed == 1); 82 83 a.destroy(ap); 84 assert(globalMemCounter.checkOutstandingNewEq(1)); 85 assert(A_constructed == 0); 86 87 a.construct(ap, 5); 88 assert(globalMemCounter.checkOutstandingNewEq(1)); 89 assert(A_constructed == 1); 90 91 a.destroy(ap); 92 assert(globalMemCounter.checkOutstandingNewEq(1)); 93 assert(A_constructed == 0); 94 95 a.construct(ap, 5, (int*)0); 96 assert(globalMemCounter.checkOutstandingNewEq(1)); 97 assert(A_constructed == 1); 98 99 a.destroy(ap); 100 assert(globalMemCounter.checkOutstandingNewEq(1)); 101 assert(A_constructed == 0); 102 103 a.deallocate(ap, 3); 104 assert(globalMemCounter.checkOutstandingNewEq(0)); 105 assert(A_constructed == 0); 106 } 107 #if TEST_STD_VER >= 11 108 { 109 std::allocator<move_only> a; 110 assert(globalMemCounter.checkOutstandingNewEq(0)); 111 assert(move_only_constructed == 0); 112 113 globalMemCounter.last_new_size = 0; 114 move_only* ap = a.allocate(3); 115 assert(globalMemCounter.checkOutstandingNewEq(1)); 116 assert(globalMemCounter.checkLastNewSizeEq(3 * sizeof(int))); 117 assert(move_only_constructed == 0); 118 119 a.construct(ap); 120 assert(globalMemCounter.checkOutstandingNewEq(1)); 121 assert(move_only_constructed == 1); 122 123 a.destroy(ap); 124 assert(globalMemCounter.checkOutstandingNewEq(1)); 125 assert(move_only_constructed == 0); 126 127 a.construct(ap, move_only()); 128 assert(globalMemCounter.checkOutstandingNewEq(1)); 129 assert(move_only_constructed == 1); 130 131 a.destroy(ap); 132 assert(globalMemCounter.checkOutstandingNewEq(1)); 133 assert(move_only_constructed == 0); 134 135 a.deallocate(ap, 3); 136 assert(globalMemCounter.checkOutstandingNewEq(0)); 137 assert(move_only_constructed == 0); 138 } 139 #endif 140 } 141