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 operator new[] replacement by replacing only operator new 10 11 // UNSUPPORTED: sanitizer-new-delete 12 // XFAIL: libcpp-no-vcruntime 13 // XFAIL: LIBCXX-AIX-FIXME 14 15 #include <new> 16 #include <cstddef> 17 #include <cstdlib> 18 #include <cassert> 19 #include <limits> 20 21 #include "test_macros.h" 22 23 int new_called = 0; 24 operator new(std::size_t s)25void* operator new(std::size_t s) TEST_THROW_SPEC(std::bad_alloc) 26 { 27 ++new_called; 28 void* ret = std::malloc(s); 29 if (!ret) std::abort(); // placate MSVC's unchecked malloc warning 30 return ret; 31 } 32 operator delete(void * p)33void operator delete(void* p) TEST_NOEXCEPT 34 { 35 --new_called; 36 std::free(p); 37 } 38 39 int A_constructed = 0; 40 41 struct A 42 { AA43 A() {++A_constructed;} ~AA44 ~A() {--A_constructed;} 45 }; 46 main(int,char **)47int main(int, char**) 48 { 49 new_called = 0; 50 A *ap = new A[3]; 51 DoNotOptimize(ap); 52 assert(ap); 53 assert(A_constructed == 3); 54 ASSERT_WITH_OPERATOR_NEW_FALLBACKS(new_called == 1); 55 delete [] ap; 56 DoNotOptimize(ap); 57 assert(A_constructed == 0); 58 ASSERT_WITH_OPERATOR_NEW_FALLBACKS(new_called == 0); 59 60 return 0; 61 } 62