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 // test operator new[] replacement by replacing only operator new
     11 
     12 // UNSUPPORTED: asan, msan
     13 
     14 #include <new>
     15 #include <cstddef>
     16 #include <cstdlib>
     17 #include <cassert>
     18 #include <limits>
     19 
     20 volatile 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 int A_constructed = 0;
     35 
     36 struct A
     37 {
     38     A() {++A_constructed;}
     39     ~A() {--A_constructed;}
     40 };
     41 
     42 int main()
     43 {
     44     A* ap = new A[3];
     45     assert(ap);
     46     assert(A_constructed == 3);
     47     assert(new_called == 1);
     48     delete [] ap;
     49     assert(A_constructed == 0);
     50     assert(new_called == 0);
     51 }
     52