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 // XFAIL: with_system_cxx_lib=macosx10.12 14 // XFAIL: with_system_cxx_lib=macosx10.11 15 // XFAIL: with_system_cxx_lib=macosx10.10 16 // XFAIL: with_system_cxx_lib=macosx10.9 17 // XFAIL: with_system_cxx_lib=macosx10.7 18 // XFAIL: with_system_cxx_lib=macosx10.8 19 20 // NOTE: gcc doesn't provide -faligned-allocation flag to test for 21 // XFAIL: no-aligned-allocation && !gcc 22 23 // On Windows libc++ doesn't provide its own definitions for new/delete 24 // but instead depends on the ones in VCRuntime. However VCRuntime does not 25 // yet provide aligned new/delete definitions so this test fails. 26 // XFAIL: LIBCXX-WINDOWS-FIXME 27 28 // test operator new nothrow by replacing only operator new 29 30 #include <new> 31 #include <cstddef> 32 #include <cstdlib> 33 #include <cassert> 34 #include <limits> 35 36 #include "test_macros.h" 37 38 constexpr auto OverAligned = alignof(std::max_align_t) * 2; 39 40 bool A_constructed = false; 41 42 struct alignas(OverAligned) A 43 { 44 A() {A_constructed = true;} 45 ~A() {A_constructed = false;} 46 }; 47 48 bool B_constructed = false; 49 50 struct B { 51 std::max_align_t member; 52 B() { B_constructed = true; } 53 ~B() { B_constructed = false; } 54 }; 55 56 int new_called = 0; 57 alignas(OverAligned) char Buff[OverAligned * 2]; 58 59 void* operator new(std::size_t s, std::align_val_t a) TEST_THROW_SPEC(std::bad_alloc) 60 { 61 assert(!new_called); 62 assert(s <= sizeof(Buff)); 63 assert(static_cast<std::size_t>(a) == OverAligned); 64 ++new_called; 65 return Buff; 66 } 67 68 void operator delete(void* p, std::align_val_t a) TEST_NOEXCEPT 69 { 70 assert(p == Buff); 71 assert(static_cast<std::size_t>(a) == OverAligned); 72 assert(new_called); 73 --new_called; 74 } 75 76 77 int main() 78 { 79 { 80 A* ap = new (std::nothrow) A; 81 assert(ap); 82 assert(A_constructed); 83 assert(new_called); 84 delete ap; 85 assert(!A_constructed); 86 assert(!new_called); 87 } 88 { 89 B* bp = new (std::nothrow) B; 90 assert(bp); 91 assert(B_constructed); 92 assert(!new_called); 93 delete bp; 94 assert(!new_called); 95 assert(!B_constructed); 96 } 97 } 98