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 10 11 // UNSUPPORTED: sanitizer-new-delete 12 13 #include <new> 14 #include <cstddef> 15 #include <cstdlib> 16 #include <cassert> 17 #include <limits> 18 19 #include "test_macros.h" 20 21 int new_called = 0; 22 operator new(std::size_t s)23void* operator new(std::size_t s) TEST_THROW_SPEC(std::bad_alloc) 24 { 25 ++new_called; 26 void* ret = std::malloc(s); 27 if (!ret) std::abort(); // placate MSVC's unchecked malloc warning 28 return ret; 29 } 30 operator delete(void * p)31void operator delete(void* p) TEST_NOEXCEPT 32 { 33 --new_called; 34 std::free(p); 35 } 36 37 bool A_constructed = false; 38 39 struct A 40 { AA41 A() {A_constructed = true;} ~AA42 ~A() {A_constructed = false;} 43 }; 44 main(int,char **)45int main(int, char**) 46 { 47 new_called = 0; 48 A *ap = new A; 49 DoNotOptimize(ap); 50 assert(ap); 51 assert(A_constructed); 52 assert(new_called); 53 delete ap; 54 DoNotOptimize(ap); 55 assert(!A_constructed); 56 assert(!new_called); 57 58 return 0; 59 } 60