1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <memory> 11 12 // shared_ptr 13 14 // template<class D> shared_ptr(nullptr_t, D d); 15 16 // UNSUPPORTED: sanitizer-new-delete 17 18 #include <memory> 19 #include <cassert> 20 #include <new> 21 #include <cstdlib> 22 #include "../test_deleter.h" 23 24 struct A 25 { 26 static int count; 27 AA28 A() {++count;} AA29 A(const A&) {++count;} ~AA30 ~A() {--count;} 31 }; 32 33 int A::count = 0; 34 35 bool throw_next = false; 36 operator new(std::size_t s)37void* operator new(std::size_t s) throw(std::bad_alloc) 38 { 39 if (throw_next) 40 throw std::bad_alloc(); 41 return std::malloc(s); 42 } 43 operator delete(void * p)44void operator delete(void* p) throw() 45 { 46 std::free(p); 47 } 48 main()49int main() 50 { 51 throw_next = true; 52 try 53 { 54 std::shared_ptr<A> p(nullptr, test_deleter<A>(3)); 55 assert(false); 56 } 57 catch (std::bad_alloc&) 58 { 59 assert(A::count == 0); 60 assert(test_deleter<A>::count == 0); 61 assert(test_deleter<A>::dealloc_count == 1); 62 } 63 } 64