]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/18_support/new_nothrow.cc
1 // Copyright (C) 2018-2020 Free Software Foundation, Inc.
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
19 // { dg-xfail-run-if "AIX operator new" { powerpc-ibm-aix* } }
23 #include <testsuite_hooks.h>
27 struct MyBadAlloc
: std::bad_alloc
{ };
30 static bool bad_alloc_thrown
;
31 static unsigned new_called
;
32 static unsigned delete_called
;
33 static unsigned new_vec_called
;
34 static unsigned delete_vec_called
;
35 static unsigned new_handler_called
;
37 static void new_handler ()
39 if (new_handler_called
++)
43 void* operator new (size_t n
)
50 if (void *p
= new_fail
? 0 : malloc (n
+ sizeof n
)) {
51 *static_cast<size_t*>(p
) = ++cntr
;
52 return static_cast<size_t*>(p
) + 1;
55 if (std::new_handler h
= std::set_new_handler (0)) {
56 std::set_new_handler (h
);
60 bad_alloc_thrown
= true;
66 void operator delete (void *p
)
70 free (static_cast<size_t*>(p
) - 1);
73 void* operator new[] (size_t n
)
76 return operator new(n
);
79 void operator delete[] (void *p
)
85 #if __cplusplus >= 201402L
86 void operator delete (void *p
, std::size_t)
90 void operator delete[] (void *p
, std::size_t)
92 ::operator delete[](p
);
102 delete_vec_called
= 0;
103 new_handler_called
= 0;
104 std::set_new_handler (0);
112 void *p
= operator new (1, std::nothrow
);
115 VERIFY (1 == new_called
);
116 VERIFY (0 == new_handler_called
);
117 VERIFY (!bad_alloc_thrown
);
119 operator delete (p
, std::nothrow
);
120 VERIFY( 1 == delete_called
);
123 p
= operator new (1, std::nothrow
);
126 VERIFY (2 == new_called
);
127 VERIFY (0 == new_handler_called
);
128 VERIFY (bad_alloc_thrown
);
131 bad_alloc_thrown
= false;
132 std::set_new_handler (new_handler
);
133 p
= operator new (1, std::nothrow
);
136 VERIFY (3 == new_called
);
137 VERIFY (2 == new_handler_called
);
138 VERIFY (!bad_alloc_thrown
);
146 void *p
= operator new[] (1, std::nothrow
);
149 VERIFY (1 == new_called
);
150 VERIFY (1 == new_vec_called
);
151 VERIFY (0 == new_handler_called
);
152 VERIFY (!bad_alloc_thrown
);
154 operator delete[] (p
, std::nothrow
);
155 VERIFY( 1 == delete_called
);
156 VERIFY( 1 == delete_vec_called
);
159 p
= operator new[] (1, std::nothrow
);
162 VERIFY (2 == new_called
);
163 VERIFY (2 == new_vec_called
);
164 VERIFY (0 == new_handler_called
);
165 VERIFY (bad_alloc_thrown
);
168 bad_alloc_thrown
= false;
169 std::set_new_handler (new_handler
);
170 p
= operator new[] (1, std::nothrow
);
173 VERIFY (3 == new_called
);
174 VERIFY (3 == new_vec_called
);
175 VERIFY (2 == new_handler_called
);
176 VERIFY (!bad_alloc_thrown
);