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: c++14, c++1z
16 // UNSUPPORTED: sanitizer-new-delete
17
18 #include <new>
19 #include <cstddef>
20 #include <cstdlib>
21 #include <cassert>
22
23 int unsized_delete_called = 0;
24 int unsized_delete_nothrow_called = 0;
25 int sized_delete_called = 0;
26
operator delete[](void * p)27 void operator delete[](void* p) throw()
28 {
29 ++unsized_delete_called;
30 std::free(p);
31 }
32
operator delete[](void * p,const std::nothrow_t &)33 void operator delete[](void* p, const std::nothrow_t&) throw()
34 {
35 ++unsized_delete_nothrow_called;
36 std::free(p);
37 }
38
operator delete[](void * p,std::size_t)39 void operator delete[](void* p, std::size_t) throw()
40 {
41 ++sized_delete_called;
42 std::free(p);
43 }
44
45 // NOTE: Use a class with a non-trivial destructor as the test type in order
46 // to ensure the correct overload is called.
47 // C++14 5.3.5 [expr.delete]p10
48 // - If the type is complete and if, for the second alternative (delete array)
49 // only, the operand is a pointer to a class type with a non-trivial
50 // destructor or a (possibly multi-dimensional) array thereof, the function
51 // with two parameters is selected.
52 // - Otherwise, it is unspecified which of the two deallocation functions is
53 // selected.
~AA54 struct A { ~A() {} };
55
main()56 int main()
57 {
58
59 A* x = new A[3];
60 assert(0 == unsized_delete_called);
61 assert(0 == unsized_delete_nothrow_called);
62 assert(0 == sized_delete_called);
63
64 delete [] x;
65 assert(1 == unsized_delete_called);
66 assert(0 == sized_delete_called);
67 assert(0 == unsized_delete_nothrow_called);
68 }
69