MAINTAINERS: Fix name order
[official-gcc.git] / libstdc++-v3 / testsuite / 18_support / new_nothrow.cc
blob551b71dfa58901f52b8a65c0bffe4fc28bbb17b8
1 // Copyright (C) 2018-2024 Free Software Foundation, Inc.
2 //
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)
7 // any later version.
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/>.
18 // { dg-do run }
19 // { dg-xfail-run-if "AIX operator new" { powerpc-ibm-aix* } }
20 // { dg-require-effective-target hosted }
22 #include <new>
23 #include <stdlib.h>
24 #include <testsuite_hooks.h>
26 // PR libstdc++/68210
28 struct MyBadAlloc: std::bad_alloc { };
30 static bool new_fail;
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++)
41 throw MyBadAlloc ();
44 #if __cplusplus >= 201103L
45 # define THROW_BAD_ALLOC noexcept(false)
46 # define NOEXCEPT noexcept
47 # else
48 # define THROW_BAD_ALLOC throw(std::bad_alloc)
49 # define NOEXCEPT throw()
50 #endif
52 void* operator new (size_t n) THROW_BAD_ALLOC
54 static size_t cntr;
56 ++new_called;
58 for ( ; ; ) {
59 if (void *p = new_fail ? 0 : malloc (n + sizeof n)) {
60 *static_cast<size_t*>(p) = ++cntr;
61 return static_cast<size_t*>(p) + 1;
64 if (std::new_handler h = std::set_new_handler (0)) {
65 std::set_new_handler (h);
66 h ();
68 else {
69 bad_alloc_thrown = true;
70 throw MyBadAlloc ();
75 void operator delete (void *p) NOEXCEPT
77 ++delete_called;
78 if (p)
79 free (static_cast<size_t*>(p) - 1);
82 void* operator new[] (size_t n) THROW_BAD_ALLOC
84 ++new_vec_called;
85 return operator new(n);
88 void operator delete[] (void *p) NOEXCEPT
90 ++delete_vec_called;
91 operator delete(p);
94 #if __cplusplus >= 201402L
95 void operator delete (void *p, std::size_t) noexcept
97 ::operator delete(p);
99 void operator delete[] (void *p, std::size_t) noexcept
101 ::operator delete[](p);
103 #endif
105 void init()
107 new_fail = false;
108 new_called = 0;
109 delete_called = 0;
110 new_vec_called = 0;
111 delete_vec_called = 0;
112 new_handler_called = 0;
113 std::set_new_handler (0);
116 void
117 test01()
119 init ();
121 void *p = operator new (1, std::nothrow);
123 VERIFY (p != 0);
124 VERIFY (1 == new_called);
125 VERIFY (0 == new_handler_called);
126 VERIFY (!bad_alloc_thrown);
128 operator delete (p, std::nothrow);
129 VERIFY( 1 == delete_called );
131 new_fail = true;
132 p = operator new (1, std::nothrow);
134 VERIFY (0 == p);
135 VERIFY (2 == new_called);
136 VERIFY (0 == new_handler_called);
137 VERIFY (bad_alloc_thrown);
139 new_fail = true;
140 bad_alloc_thrown = false;
141 std::set_new_handler (new_handler);
142 p = operator new (1, std::nothrow);
144 VERIFY (0 == p);
145 VERIFY (3 == new_called);
146 VERIFY (2 == new_handler_called);
147 VERIFY (!bad_alloc_thrown);
150 void
151 test02()
153 init ();
155 void *p = operator new[] (1, std::nothrow);
157 VERIFY (p != 0);
158 VERIFY (1 == new_called);
159 VERIFY (1 == new_vec_called);
160 VERIFY (0 == new_handler_called);
161 VERIFY (!bad_alloc_thrown);
163 operator delete[] (p, std::nothrow);
164 VERIFY( 1 == delete_called );
165 VERIFY( 1 == delete_vec_called );
167 new_fail = true;
168 p = operator new[] (1, std::nothrow);
170 VERIFY (0 == p);
171 VERIFY (2 == new_called);
172 VERIFY (2 == new_vec_called);
173 VERIFY (0 == new_handler_called);
174 VERIFY (bad_alloc_thrown);
176 new_fail = true;
177 bad_alloc_thrown = false;
178 std::set_new_handler (new_handler);
179 p = operator new[] (1, std::nothrow);
181 VERIFY (0 == p);
182 VERIFY (3 == new_called);
183 VERIFY (3 == new_vec_called);
184 VERIFY (2 == new_handler_called);
185 VERIFY (!bad_alloc_thrown);
189 int main()
191 test01();
192 test02();