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 12 // asan and msan will not call the new handler. 13 // UNSUPPORTED: sanitizer-new-delete 14 15 // FIXME change this to XFAIL. 16 // UNSUPPORTED: no-aligned-allocation && !gcc 17 18 // XFAIL: with_system_cxx_lib=macosx10.12 19 // XFAIL: with_system_cxx_lib=macosx10.11 20 // XFAIL: with_system_cxx_lib=macosx10.10 21 // XFAIL: with_system_cxx_lib=macosx10.9 22 // XFAIL: with_system_cxx_lib=macosx10.7 23 // XFAIL: with_system_cxx_lib=macosx10.8 24 25 26 // On Windows libc++ doesn't provide its own definitions for new/delete 27 // but instead depends on the ones in VCRuntime. However VCRuntime does not 28 // yet provide aligned new/delete definitions so this test fails to link. 29 // XFAIL: LIBCXX-WINDOWS-FIXME 30 31 // test operator new 32 33 #include <new> 34 #include <cstddef> 35 #include <cassert> 36 #include <cstdint> 37 #include <limits> 38 39 #include "test_macros.h" 40 41 constexpr auto OverAligned = __STDCPP_DEFAULT_NEW_ALIGNMENT__ * 2; 42 43 int new_handler_called = 0; 44 45 void my_new_handler() 46 { 47 ++new_handler_called; 48 std::set_new_handler(0); 49 } 50 51 int A_constructed = 0; 52 53 struct alignas(OverAligned) A 54 { 55 A() { ++A_constructed;} 56 ~A() { --A_constructed;} 57 }; 58 59 void test_throw_max_size() { 60 #ifndef TEST_HAS_NO_EXCEPTIONS 61 std::set_new_handler(my_new_handler); 62 try 63 { 64 void* vp = operator new[] (std::numeric_limits<std::size_t>::max(), 65 static_cast<std::align_val_t>(32)); 66 ((void)vp); 67 assert(false); 68 } 69 catch (std::bad_alloc&) 70 { 71 assert(new_handler_called == 1); 72 } 73 catch (...) 74 { 75 assert(false); 76 } 77 #endif 78 } 79 80 int main() 81 { 82 { 83 A* ap = new A[2]; 84 assert(ap); 85 assert(reinterpret_cast<std::uintptr_t>(ap) % OverAligned == 0); 86 assert(A_constructed == 2); 87 delete [] ap; 88 assert(A_constructed == 0); 89 } 90 { 91 test_throw_max_size(); 92 } 93 } 94