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 sized operator [] delete calls the unsized operator [] delete.
     11 // When sized operator delete [] is not available (ex C++11) then the unsized
     12 // operator delete [] is called directly.
     13 
     14 // UNSUPPORTED: sanitizer-new-delete
     15 
     16 #include <new>
     17 #include <cstddef>
     18 #include <cstdlib>
     19 #include <cassert>
     20 
     21 #include "test_macros.h"
     22 
     23 int delete_called = 0;
     24 int delete_nothrow_called = 0;
     25 
     26 void operator delete[](void* p) TEST_NOEXCEPT
     27 {
     28     ++delete_called;
     29     std::free(p);
     30 }
     31 
     32 void operator delete[](void* p, const std::nothrow_t&) TEST_NOEXCEPT
     33 {
     34     ++delete_nothrow_called;
     35     std::free(p);
     36 }
     37 
     38 // NOTE: Use a class with a non-trivial destructor as the test type in order
     39 // to ensure the correct overload is called.
     40 // C++14 5.3.5 [expr.delete]p10
     41 // - If the type is complete and if, for the second alternative (delete array)
     42 //   only, the operand is a pointer to a class type with a non-trivial
     43 //   destructor or a (possibly multi-dimensional) array thereof, the function
     44 //   with two parameters is selected.
     45 // - Otherwise, it is unspecified which of the two deallocation functions is
     46 //   selected.
     47 struct A { ~A() {} };
     48 
     49 A *volatile x;
     50 
     51 int main()
     52 {
     53     x = new A[3];
     54     assert(0 == delete_called);
     55     assert(0 == delete_nothrow_called);
     56 
     57     delete [] x;
     58     assert(1 == delete_called);
     59     assert(0 == delete_nothrow_called);
     60 }
     61