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 turn this into an XFAIL 16// UNSUPPORTED: no-aligned-allocation 17 18// test operator new (nothrow) 19 20#include <new> 21#include <cstddef> 22#include <cstdint> 23#include <cassert> 24#include <limits> 25 26#include "test_macros.h" 27 28constexpr auto OverAligned = alignof(std::max_align_t) * 2; 29 30int new_handler_called = 0; 31 32void my_new_handler() 33{ 34 ++new_handler_called; 35 std::set_new_handler(0); 36} 37 38int A_constructed = 0; 39 40struct alignas(OverAligned) A 41{ 42 A() { ++A_constructed; } 43 ~A() { --A_constructed; } 44}; 45 46void test_max_alloc() { 47 std::set_new_handler(my_new_handler); 48 auto do_test = []() { 49 void* vp = operator new [](std::numeric_limits<std::size_t>::max(), 50 std::align_val_t(OverAligned), 51 std::nothrow); 52 assert(new_handler_called == 1); 53 assert(vp == 0); 54 }; 55#ifndef TEST_HAS_NO_EXCEPTIONS 56 try 57 { 58 do_test(); 59 } 60 catch (...) 61 { 62 assert(false); 63 } 64#else 65 do_test(); 66#endif 67} 68 69int main() 70{ 71 { 72 A* ap = new(std::nothrow) A[3]; 73 assert(ap); 74 assert(reinterpret_cast<std::uintptr_t>(ap) % OverAligned == 0); 75 assert(A_constructed == 3); 76 delete [] ap; 77 assert(!A_constructed); 78 } 79 { 80 test_max_alloc(); 81 } 82} 83