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 // test operator new replacement
     11 
     12 // UNSUPPORTED: asan, msan
     13 
     14 #include <new>
     15 #include <cstddef>
     16 #include <cstdlib>
     17 #include <cassert>
     18 #include <limits>
     19 
     20 int new_called = 0;
     21 
     22 void* operator new(std::size_t s) throw(std::bad_alloc)
     23 {
     24     ++new_called;
     25     return std::malloc(s);
     26 }
     27 
     28 void  operator delete(void* p) throw()
     29 {
     30     --new_called;
     31     std::free(p);
     32 }
     33 
     34 bool A_constructed = false;
     35 
     36 struct A
     37 {
     38     A() {A_constructed = true;}
     39     ~A() {A_constructed = false;}
     40 };
     41 
     42 int main()
     43 {
     44     A* ap = new A;
     45     assert(ap);
     46     assert(A_constructed);
     47     assert(new_called);
     48     delete ap;
     49     assert(!A_constructed);
     50     assert(!new_called);
     51 }
     52