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[] replacement.
     11 
     12 // Note that sized delete operator definitions below are simply ignored
     13 // when sized deallocation is not supported, e.g., prior to C++14.
     14 
     15 // UNSUPPORTED: sanitizer-new-delete
     16 
     17 // NOTE: Only clang-3.7 and GCC 5.1 and greater support -fsized-deallocation.
     18 // REQUIRES: fsized-deallocation
     19 
     20 // RUN: %build -fsized-deallocation
     21 // RUN: %run
     22 
     23 #if !defined(__cpp_sized_deallocation)
     24 # error __cpp_sized_deallocation should be defined
     25 #endif
     26 
     27 #if !(__cpp_sized_deallocation >= 201309L)
     28 # error expected __cpp_sized_deallocation >= 201309L
     29 #endif
     30 
     31 #include <new>
     32 #include <cstddef>
     33 #include <cstdlib>
     34 #include <cassert>
     35 
     36 #include "test_macros.h"
     37 
     38 int unsized_delete_called = 0;
     39 int unsized_delete_nothrow_called = 0;
     40 int sized_delete_called = 0;
     41 
     42 void operator delete[](void* p) TEST_NOEXCEPT
     43 {
     44     ++unsized_delete_called;
     45     std::free(p);
     46 }
     47 
     48 void operator delete[](void* p, const std::nothrow_t&) TEST_NOEXCEPT
     49 {
     50     ++unsized_delete_nothrow_called;
     51     std::free(p);
     52 }
     53 
     54 void operator delete[](void* p, std::size_t) TEST_NOEXCEPT
     55 {
     56     ++sized_delete_called;
     57     std::free(p);
     58 }
     59 
     60 // NOTE: Use a class with a non-trivial destructor as the test type in order
     61 // to ensure the correct overload is called.
     62 // C++14 5.3.5 [expr.delete]p10
     63 // - If the type is complete and if, for the second alternative (delete array)
     64 //   only, the operand is a pointer to a class type with a non-trivial
     65 //   destructor or a (possibly multi-dimensional) array thereof, the function
     66 //   with two parameters is selected.
     67 // - Otherwise, it is unspecified which of the two deallocation functions is
     68 //   selected.
     69 struct A { ~A() {} };
     70 
     71 int main()
     72 {
     73     A* x = new A[3];
     74     assert(0 == unsized_delete_called);
     75     assert(0 == unsized_delete_nothrow_called);
     76     assert(0 == sized_delete_called);
     77 
     78     delete [] x;
     79     assert(0 == unsized_delete_called);
     80     assert(0 == unsized_delete_nothrow_called);
     81     assert(1 == sized_delete_called);
     82 }
     83