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: no-aligned-allocation 14 15// test operator new nothrow by replacing only operator new 16 17#include <new> 18#include <cstddef> 19#include <cstdlib> 20#include <cassert> 21#include <limits> 22 23#include "test_macros.h" 24 25constexpr auto OverAligned = alignof(std::max_align_t) * 2; 26 27int A_constructed = 0; 28 29struct alignas(OverAligned) A 30{ 31 A() {++A_constructed;} 32 ~A() {--A_constructed;} 33}; 34 35int B_constructed = 0; 36 37struct B { 38 std::max_align_t member; 39 B() { ++B_constructed; } 40 ~B() { --B_constructed; } 41}; 42 43int new_called = 0; 44alignas(OverAligned) char Buff[OverAligned * 3]; 45 46void* operator new[](std::size_t s, std::align_val_t a) TEST_THROW_SPEC(std::bad_alloc) 47{ 48 assert(!new_called); 49 assert(s <= sizeof(Buff)); 50 assert(static_cast<std::size_t>(a) == OverAligned); 51 ++new_called; 52 return Buff; 53} 54 55void operator delete[](void* p, std::align_val_t a) TEST_NOEXCEPT 56{ 57 assert(p == Buff); 58 assert(static_cast<std::size_t>(a) == OverAligned); 59 assert(new_called); 60 --new_called; 61} 62 63int main() 64{ 65 { 66 A* ap = new (std::nothrow) A[2]; 67 assert(ap); 68 assert(A_constructed == 2); 69 assert(new_called); 70 delete [] ap; 71 assert(A_constructed == 0); 72 assert(!new_called); 73 } 74 { 75 B* bp = new (std::nothrow) B[2]; 76 assert(bp); 77 assert(B_constructed == 2); 78 assert(!new_called); 79 delete [] bp; 80 assert(!new_called); 81 assert(!B_constructed); 82 } 83} 84