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); 10 11 // Test that we can replace the operator by replacing the `operator new(std::size_t)` (the non-array version). 12 13 // UNSUPPORTED: sanitizer-new-delete 14 // XFAIL: libcpp-no-vcruntime 15 // XFAIL: LIBCXX-AIX-FIXME 16 17 #include <new> 18 #include <cstddef> 19 #include <cstdlib> 20 #include <cassert> 21 #include <limits> 22 23 #include "test_macros.h" 24 25 int new_called = 0; 26 int delete_called = 0; 27 28 TEST_WORKAROUND_BUG_109234844_WEAK operator new(std::size_t s)29void* operator new(std::size_t s) TEST_THROW_SPEC(std::bad_alloc) { 30 ++new_called; 31 void* ret = std::malloc(s); 32 if (!ret) { 33 std::abort(); // placate MSVC's unchecked malloc warning (assert() won't silence it) 34 } 35 return ret; 36 } 37 operator delete(void * p)38void operator delete(void* p) TEST_NOEXCEPT { 39 ++delete_called; 40 std::free(p); 41 } 42 main(int,char **)43int main(int, char**) { 44 new_called = delete_called = 0; 45 int* x = DoNotOptimize(new int[3]); 46 assert(x != nullptr); 47 ASSERT_WITH_OPERATOR_NEW_FALLBACKS(new_called == 1); 48 49 delete[] x; 50 ASSERT_WITH_OPERATOR_NEW_FALLBACKS(delete_called == 1); 51 52 return 0; 53 } 54