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 Y, class D> shared_ptr(Y* p, D d);
15 
16 #include <memory>
17 #include <cassert>
18 #include <new>
19 #include <cstdlib>
20 #include "../test_deleter.h"
21 
22 struct A
23 {
24     static int count;
25 
AA26     A() {++count;}
AA27     A(const A&) {++count;}
~AA28     ~A() {--count;}
29 };
30 
31 int A::count = 0;
32 
33 bool throw_next = false;
34 
operator new(std::size_t s)35 void* operator new(std::size_t s) throw(std::bad_alloc)
36 {
37     if (throw_next)
38         throw std::bad_alloc();
39     return std::malloc(s);
40 }
41 
operator delete(void * p)42 void  operator delete(void* p) throw()
43 {
44     std::free(p);
45 }
46 
main()47 int main()
48 {
49     A* ptr = new A;
50     throw_next = true;
51     try
52     {
53         std::shared_ptr<A> p(ptr, test_deleter<A>(3));
54         assert(false);
55     }
56     catch (std::bad_alloc&)
57     {
58         assert(A::count == 0);
59         assert(test_deleter<A>::count == 0);
60         assert(test_deleter<A>::dealloc_count == 1);
61     }
62 }
63