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