Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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 | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Howard Hinnant | cf6dcc3 | 2010-08-22 00:31:12 +0000 | [diff] [blame] | 9 | |
Eric Fiselier | 72aab5f | 2014-11-04 05:11:41 +0000 | [diff] [blame] | 10 | // test operator new [] (nothrow) |
Eric Fiselier | 71915c1 | 2014-11-14 02:55:16 +0000 | [diff] [blame] | 11 | // NOTE: asan and msan will not call the new handler. |
Eric Fiselier | 07a4bec | 2015-03-10 20:46:04 +0000 | [diff] [blame] | 12 | // UNSUPPORTED: sanitizer-new-delete |
Eric Fiselier | 72aab5f | 2014-11-04 05:11:41 +0000 | [diff] [blame] | 13 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 14 | |
| 15 | #include <new> |
| 16 | #include <cstddef> |
| 17 | #include <cassert> |
| 18 | #include <limits> |
| 19 | |
| 20 | int new_handler_called = 0; |
| 21 | |
| 22 | void new_handler() |
| 23 | { |
| 24 | ++new_handler_called; |
| 25 | std::set_new_handler(0); |
| 26 | } |
| 27 | |
| 28 | int A_constructed = 0; |
| 29 | |
| 30 | struct A |
| 31 | { |
| 32 | A() {++A_constructed;} |
| 33 | ~A() {--A_constructed;} |
| 34 | }; |
| 35 | |
| 36 | int main() |
| 37 | { |
| 38 | std::set_new_handler(new_handler); |
| 39 | try |
| 40 | { |
Howard Hinnant | 5ce391e | 2013-01-16 17:56:06 +0000 | [diff] [blame] | 41 | void*volatile vp = operator new [] (std::numeric_limits<std::size_t>::max(), std::nothrow); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 42 | assert(new_handler_called == 1); |
| 43 | assert(vp == 0); |
| 44 | } |
| 45 | catch (...) |
| 46 | { |
| 47 | assert(false); |
| 48 | } |
| 49 | A* ap = new(std::nothrow) A[3]; |
| 50 | assert(ap); |
| 51 | assert(A_constructed == 3); |
| 52 | delete [] ap; |
| 53 | assert(A_constructed == 0); |
| 54 | } |