1 | //===----------------------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | // test sized operator delete replacement. |
10 | |
11 | // UNSUPPORTED: sanitizer-new-delete, c++03, c++11 |
12 | |
13 | // NOTE: Clang does not enable sized-deallocation in C++14 and beyond by |
14 | // default. It is only enabled when -fsized-deallocation is given. |
15 | // XFAIL: clang, apple-clang |
16 | |
17 | #include <new> |
18 | #include <cstddef> |
19 | #include <cstdlib> |
20 | #include <cassert> |
21 | |
22 | #include "test_macros.h" |
23 | |
24 | int unsized_delete_called = 0; |
25 | int unsized_delete_nothrow_called = 0; |
26 | int sized_delete_called = 0; |
27 | |
28 | void operator delete(void* p) TEST_NOEXCEPT |
29 | { |
30 | ++unsized_delete_called; |
31 | std::free(p); |
32 | } |
33 | |
34 | void operator delete(void* p, const std::nothrow_t&) TEST_NOEXCEPT |
35 | { |
36 | ++unsized_delete_nothrow_called; |
37 | std::free(p); |
38 | } |
39 | |
40 | void operator delete(void* p, std::size_t) TEST_NOEXCEPT |
41 | { |
42 | ++sized_delete_called; |
43 | std::free(p); |
44 | } |
45 | |
46 | int main(int, char**) |
47 | { |
48 | int *x = new int(42); |
49 | DoNotOptimize(x); |
50 | assert(0 == unsized_delete_called); |
51 | assert(0 == unsized_delete_nothrow_called); |
52 | assert(0 == sized_delete_called); |
53 | |
54 | delete x; |
55 | DoNotOptimize(x); |
56 | assert(0 == unsized_delete_called); |
57 | assert(1 == sized_delete_called); |
58 | assert(0 == unsized_delete_nothrow_called); |
59 | |
60 | return 0; |
61 | } |
62 | |