libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

new_replace.pass.cpp (1186B)


      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 // test operator new replacement
     11 
     12 // UNSUPPORTED: sanitizer-new-delete
     13 
     14 #include <new>
     15 #include <cstddef>
     16 #include <cstdlib>
     17 #include <cassert>
     18 #include <limits>
     19 
     20 #include "test_macros.h"
     21 
     22 int new_called = 0;
     23 
     24 void* operator new(std::size_t s) TEST_THROW_SPEC(std::bad_alloc)
     25 {
     26     ++new_called;
     27     void* ret = std::malloc(s);
     28     if (!ret) std::abort(); // placate MSVC's unchecked malloc warning
     29     return ret;
     30 }
     31 
     32 void  operator delete(void* p) TEST_NOEXCEPT
     33 {
     34     --new_called;
     35     std::free(p);
     36 }
     37 
     38 bool A_constructed = false;
     39 
     40 struct A
     41 {
     42     A() {A_constructed = true;}
     43     ~A() {A_constructed = false;}
     44 };
     45 
     46 int main()
     47 {
     48     A *ap = new A;
     49     DoNotOptimize(ap);
     50     assert(ap);
     51     assert(A_constructed);
     52     assert(new_called);
     53     delete ap;
     54     DoNotOptimize(ap);
     55     assert(!A_constructed);
     56     assert(!new_called);
     57 }