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 // UNSUPPORTED: c++98, c++03, c++11, c++14 11 // UNSUPPORTED: sanitizer-new-delete 12 13 // NOTE: GCC doesn't provide the -faligned-allocation flag to test for 14 // XFAIL: no-aligned-allocation && !gcc 15 16 // test operator new replacement 17 18 #include <new> 19 #include <cstddef> 20 #include <cstdlib> 21 #include <cstdint> 22 #include <cassert> 23 #include <limits> 24 25 #include "test_macros.h" 26 27 constexpr auto OverAligned = __STDCPP_DEFAULT_NEW_ALIGNMENT__ * 2; 28 29 int A_constructed = 0; 30 31 struct alignas(OverAligned) A { 32 A() { ++A_constructed;} 33 ~A() { --A_constructed;} 34 }; 35 36 37 int B_constructed = 0; 38 39 struct alignas(std::max_align_t) B 40 { 41 std::max_align_t member; 42 B() { ++B_constructed;} 43 ~B() { --B_constructed;} 44 }; 45 46 int new_called = 0; 47 48 alignas(OverAligned) char DummyData[OverAligned * 4]; 49 50 void* operator new[](std::size_t s, std::align_val_t a) TEST_THROW_SPEC(std::bad_alloc) 51 { 52 assert(new_called == 0); // We already allocated 53 assert(s <= sizeof(DummyData)); 54 assert(static_cast<std::size_t>(a) == OverAligned); 55 ++new_called; 56 void *Ret = DummyData; 57 DoNotOptimize(Ret); 58 return Ret; 59 } 60 61 void operator delete[](void* p, std::align_val_t) TEST_NOEXCEPT 62 { 63 assert(new_called == 1); 64 --new_called; 65 assert(p == DummyData); 66 DoNotOptimize(p); 67 } 68 69 70 int main() 71 { 72 { 73 A* ap = new A[3]; 74 assert(ap); 75 assert(A_constructed == 3); 76 assert(new_called); 77 delete [] ap; 78 assert(!A_constructed); 79 assert(!new_called); 80 } 81 { 82 B* bp = new B[3]; 83 assert(bp); 84 assert(B_constructed == 3); 85 assert(!new_called); 86 delete [] bp; 87 assert(!new_called); 88 } 89 } 90