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 = alignof(std::max_align_t) * 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 return DummyData; 57 } 58 59 void operator delete[](void* p, std::align_val_t) TEST_NOEXCEPT 60 { 61 assert(new_called == 1); 62 --new_called; 63 assert(p == DummyData); 64 } 65 66 67 int main() 68 { 69 { 70 A* ap = new A[3]; 71 assert(ap); 72 assert(A_constructed == 3); 73 assert(new_called); 74 delete [] ap; 75 assert(!A_constructed); 76 assert(!new_called); 77 } 78 { 79 B* bp = new B[3]; 80 assert(bp); 81 assert(B_constructed == 3); 82 assert(!new_called); 83 delete [] bp; 84 assert(!new_called); 85 } 86 } 87