1 // Copyright (C) 2018-2024 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* } }
20 // { dg-require-effective-target hosted }
24 #include <testsuite_hooks.h>
28 struct MyBadAlloc
: std::bad_alloc
{ };
31 static bool bad_alloc_thrown
;
32 static unsigned new_called
;
33 static unsigned delete_called
;
34 static unsigned new_vec_called
;
35 static unsigned delete_vec_called
;
36 static unsigned new_handler_called
;
38 static void new_handler ()
40 if (new_handler_called
++)
44 void* operator new (size_t n
)
51 if (void *p
= new_fail
? 0 : malloc (n
+ sizeof n
)) {
52 *static_cast<size_t*>(p
) = ++cntr
;
53 return static_cast<size_t*>(p
) + 1;
56 if (std::new_handler h
= std::set_new_handler (0)) {
57 std::set_new_handler (h
);
61 bad_alloc_thrown
= true;
67 #if __cplusplus >= 201103L
68 #define NOEXCEPT noexcept
73 void operator delete (void *p
) NOEXCEPT
77 free (static_cast<size_t*>(p
) - 1);
80 void* operator new[] (size_t n
)
83 return operator new(n
);
86 void operator delete[] (void *p
) NOEXCEPT
92 #if __cplusplus >= 201402L
93 void operator delete (void *p
, std::size_t) noexcept
97 void operator delete[] (void *p
, std::size_t) noexcept
99 ::operator delete[](p
);
109 delete_vec_called
= 0;
110 new_handler_called
= 0;
111 std::set_new_handler (0);
119 void *p
= operator new (1, std::nothrow
);
122 VERIFY (1 == new_called
);
123 VERIFY (0 == new_handler_called
);
124 VERIFY (!bad_alloc_thrown
);
126 operator delete (p
, std::nothrow
);
127 VERIFY( 1 == delete_called
);
130 p
= operator new (1, std::nothrow
);
133 VERIFY (2 == new_called
);
134 VERIFY (0 == new_handler_called
);
135 VERIFY (bad_alloc_thrown
);
138 bad_alloc_thrown
= false;
139 std::set_new_handler (new_handler
);
140 p
= operator new (1, std::nothrow
);
143 VERIFY (3 == new_called
);
144 VERIFY (2 == new_handler_called
);
145 VERIFY (!bad_alloc_thrown
);
153 void *p
= operator new[] (1, std::nothrow
);
156 VERIFY (1 == new_called
);
157 VERIFY (1 == new_vec_called
);
158 VERIFY (0 == new_handler_called
);
159 VERIFY (!bad_alloc_thrown
);
161 operator delete[] (p
, std::nothrow
);
162 VERIFY( 1 == delete_called
);
163 VERIFY( 1 == delete_vec_called
);
166 p
= operator new[] (1, std::nothrow
);
169 VERIFY (2 == new_called
);
170 VERIFY (2 == new_vec_called
);
171 VERIFY (0 == new_handler_called
);
172 VERIFY (bad_alloc_thrown
);
175 bad_alloc_thrown
= false;
176 std::set_new_handler (new_handler
);
177 p
= operator new[] (1, std::nothrow
);
180 VERIFY (3 == new_called
);
181 VERIFY (3 == new_vec_called
);
182 VERIFY (2 == new_handler_called
);
183 VERIFY (!bad_alloc_thrown
);