| 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 | // void* operator new[](std::size_t, std::nothrow_t const&); |
| 10 | |
| 11 | // Test that we can replace the operator by defining our own. |
| 12 | |
| 13 | // UNSUPPORTED: sanitizer-new-delete |
| 14 | // XFAIL: libcpp-no-vcruntime |
| 15 | |
| 16 | #include <new> |
| 17 | #include <cstddef> |
| 18 | #include <cstdlib> |
| 19 | #include <cassert> |
| 20 | |
| 21 | #include "test_macros.h" |
| 22 | |
| 23 | int new_nothrow_called = 0; |
| 24 | int delete_called = 0; |
| 25 | |
| 26 | void* operator new[](std::size_t s, std::nothrow_t const&) TEST_NOEXCEPT { |
| 27 | ++new_nothrow_called; |
| 28 | return std::malloc(s); |
| 29 | } |
| 30 | |
| 31 | void operator delete[](void* p) TEST_NOEXCEPT { |
| 32 | ++delete_called; |
| 33 | std::free(p); |
| 34 | } |
| 35 | |
| 36 | int main(int, char**) { |
| 37 | new_nothrow_called = delete_called = 0; |
| 38 | int* x = DoNotOptimize(new (std::nothrow) int[3]); |
| 39 | assert(x != nullptr); |
| 40 | ASSERT_WITH_OPERATOR_NEW_FALLBACKS(new_nothrow_called == 1); |
| 41 | |
| 42 | assert(delete_called == 0); |
| 43 | delete[] x; |
| 44 | ASSERT_WITH_OPERATOR_NEW_FALLBACKS(delete_called == 1); |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |