Debian Codegraph @

libstdc++-v3/testsuite/18_support/new_nothrow.cc

raw
1 // Copyright (C) 2018-2025 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.
8
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.
13
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/>.
17
18 // { dg-do run }
19 // { dg-xfail-run-if "AIX operator new" { powerpc-ibm-aix* } }
20 // { dg-require-effective-target hosted }
21
22 #include <new>
23 #include <stdlib.h>
24 #include <testsuite_hooks.h>
25
26 // PR libstdc++/68210
27
28 struct MyBadAlloc: std::bad_alloc { };
29
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;
37
38 static void new_handler ()
39 {
40 if (new_handler_called++)
41 throw MyBadAlloc ();
42 }
43
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
51
52 void* operator new (size_t n) THROW_BAD_ALLOC
53 {
54 static size_t cntr;
55
56 ++new_called;
57
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;
62 }
63
64 if (std::new_handler h = std::set_new_handler (0)) {
65 std::set_new_handler (h);
66 h ();
67 }
68 else {
69 bad_alloc_thrown = true;
70 throw MyBadAlloc ();
71 }
72 }
73 }
74
75 void operator delete (void *p) NOEXCEPT
76 {
77 ++delete_called;
78 if (p)
79 free (static_cast<size_t*>(p) - 1);
80 }
81
82 void* operator new[] (size_t n) THROW_BAD_ALLOC
83 {
84 ++new_vec_called;
85 return operator new(n);
86 }
87
88 void operator delete[] (void *p) NOEXCEPT
89 {
90 ++delete_vec_called;
91 operator delete(p);
92 }
93
94 #if __cplusplus >= 201402L
95 void operator delete (void *p, std::size_t) noexcept
96 {
97 ::operator delete(p);
98 }
99 void operator delete[] (void *p, std::size_t) noexcept
100 {
101 ::operator delete[](p);
102 }
103 #endif
104
105 void init()
106 {
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);
114 }
115
116 void
117 test01()
118 {
119 init ();
120
121 void *p = operator new (1, std::nothrow);
122
123 VERIFY (p != 0);
124 VERIFY (1 == new_called);
125 VERIFY (0 == new_handler_called);
126 VERIFY (!bad_alloc_thrown);
127
128 operator delete (p, std::nothrow);
129 VERIFY( 1 == delete_called );
130
131 new_fail = true;
132 p = operator new (1, std::nothrow);
133
134 VERIFY (0 == p);
135 VERIFY (2 == new_called);
136 VERIFY (0 == new_handler_called);
137 VERIFY (bad_alloc_thrown);
138
139 new_fail = true;
140 bad_alloc_thrown = false;
141 std::set_new_handler (new_handler);
142 p = operator new (1, std::nothrow);
143
144 VERIFY (0 == p);
145 VERIFY (3 == new_called);
146 VERIFY (2 == new_handler_called);
147 VERIFY (!bad_alloc_thrown);
148 }
149
150 void
151 test02()
152 {
153 init ();
154
155 void *p = operator new[] (1, std::nothrow);
156
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);
162
163 operator delete[] (p, std::nothrow);
164 VERIFY( 1 == delete_called );
165 VERIFY( 1 == delete_vec_called );
166
167 new_fail = true;
168 p = operator new[] (1, std::nothrow);
169
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);
175
176 new_fail = true;
177 bad_alloc_thrown = false;
178 std::set_new_handler (new_handler);
179 p = operator new[] (1, std::nothrow);
180
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);
186 }
187
188
189 int main()
190 {
191 test01();
192 test02();
193 }