Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <memory> |
| 11 | |
| 12 | // template<class Y> explicit shared_ptr(Y* p); |
| 13 | |
Eric Fiselier | 07a4bec | 2015-03-10 20:46:04 +0000 | [diff] [blame] | 14 | // UNSUPPORTED: sanitizer-new-delete |
Eric Fiselier | 72aab5f | 2014-11-04 05:11:41 +0000 | [diff] [blame] | 15 | |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 16 | #include <memory> |
| 17 | #include <new> |
| 18 | #include <cstdlib> |
| 19 | #include <cassert> |
| 20 | |
| 21 | struct A |
| 22 | { |
| 23 | static int count; |
| 24 | |
| 25 | A() {++count;} |
| 26 | A(const A&) {++count;} |
| 27 | ~A() {--count;} |
| 28 | }; |
| 29 | |
| 30 | int A::count = 0; |
| 31 | |
| 32 | bool throw_next = false; |
| 33 | |
| 34 | void* operator new(std::size_t s) throw(std::bad_alloc) |
| 35 | { |
| 36 | if (throw_next) |
| 37 | throw std::bad_alloc(); |
| 38 | return std::malloc(s); |
| 39 | } |
| 40 | |
| 41 | void operator delete(void* p) throw() |
| 42 | { |
| 43 | std::free(p); |
| 44 | } |
| 45 | |
| 46 | int main() |
| 47 | { |
| 48 | { |
| 49 | A* ptr = new A; |
| 50 | throw_next = true; |
| 51 | assert(A::count == 1); |
| 52 | try |
| 53 | { |
| 54 | std::shared_ptr<A> p(ptr); |
| 55 | assert(false); |
| 56 | } |
| 57 | catch (std::bad_alloc&) |
| 58 | { |
| 59 | assert(A::count == 0); |
| 60 | } |
| 61 | } |
| 62 | } |