Home | History | Annotate | Download | only in new.delete.array
      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 
     25 constexpr auto OverAligned = alignof(std::max_align_t) * 2;
     26 
     27 int A_constructed = 0;
     28 
     29 struct alignas(OverAligned) A
     30 {
     31     A() {++A_constructed;}
     32     ~A() {--A_constructed;}
     33 };
     34 
     35 int B_constructed = 0;
     36 
     37 struct B {
     38   std::max_align_t member;
     39   B() { ++B_constructed; }
     40   ~B() { --B_constructed; }
     41 };
     42 
     43 int new_called = 0;
     44 alignas(OverAligned) char Buff[OverAligned * 3];
     45 
     46 void* 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 
     55 void  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 
     63 int 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