]>
Commit | Line | Data |
---|---|---|
a86151e1 BK |
1 | // -*- C++ -*- |
2 | ||
a945c346 | 3 | // Copyright (C) 2005-2024 Free Software Foundation, Inc. |
a86151e1 BK |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the terms | |
7 | // of the GNU General Public License as published by the Free Software | |
748086b7 | 8 | // Foundation; either version 3, or (at your option) any later |
a86151e1 BK |
9 | // version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, but | |
12 | // WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | // General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
a86151e1 | 19 | |
748086b7 JJ |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
a86151e1 BK |
24 | |
25 | // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. | |
26 | ||
27 | // Permission to use, copy, modify, sell, and distribute this software | |
28 | // is hereby granted without fee, provided that the above copyright | |
29 | // notice appears in all copies, and that both that copyright notice | |
30 | // and this permission notice appear in supporting documentation. None | |
31 | // of the above authors, nor IBM Haifa Research Laboratories, make any | |
32 | // representation about the suitability of this software for any | |
33 | // purpose. It is provided "as is" without express or implied | |
34 | // warranty. | |
35 | ||
65be6ddd | 36 | /** @file ext/throw_allocator.h |
78a53887 BK |
37 | * This file is a GNU extension to the Standard C++ Library. |
38 | * | |
861de21e BK |
39 | * Contains two exception-generating types (throw_value, throw_allocator) |
40 | * intended to be used as value and allocator types while testing | |
41 | * exception safety in templatized containers and algorithms. The | |
42 | * allocator has additional log and debug features. The exception | |
43 | * generated is of type forced_exception_error. | |
78a53887 BK |
44 | */ |
45 | ||
a86151e1 BK |
46 | #ifndef _THROW_ALLOCATOR_H |
47 | #define _THROW_ALLOCATOR_H 1 | |
48 | ||
18f176d0 AA |
49 | #include <bits/requires_hosted.h> // GNU extensions are currently omitted |
50 | ||
a86151e1 | 51 | #include <cmath> |
67a7356b | 52 | #include <ctime> |
a86151e1 | 53 | #include <map> |
a86151e1 BK |
54 | #include <string> |
55 | #include <ostream> | |
56 | #include <stdexcept> | |
57 | #include <utility> | |
949d9ae1 | 58 | #include <bits/functexcept.h> |
ca0f8fd1 | 59 | #include <bits/move.h> |
734f5023 | 60 | #if __cplusplus >= 201103L |
5580c6e7 BK |
61 | # include <functional> |
62 | # include <random> | |
63 | #else | |
64 | # include <tr1/functional> | |
65 | # include <tr1/random> | |
66 | #endif | |
2cae56bd | 67 | #include <ext/alloc_traits.h> |
a86151e1 | 68 | |
96d9670e JW |
69 | #if !__has_builtin(__builtin_sprintf) |
70 | # include <cstdio> | |
71 | #endif | |
72 | ||
12ffa228 BK |
73 | namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) |
74 | { | |
75 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
a86151e1 | 76 | |
5580c6e7 | 77 | /** |
f2ce64b5 | 78 | * @brief Thrown by utilities for testing exception safety. |
5b9daa7e BK |
79 | * @ingroup exceptions |
80 | */ | |
861de21e | 81 | struct forced_error : public std::exception |
a86151e1 BK |
82 | { }; |
83 | ||
861de21e | 84 | // Substitute for forced_error object when -fno-exceptions. |
949d9ae1 | 85 | inline void |
861de21e | 86 | __throw_forced_error() |
54ba39f5 | 87 | { _GLIBCXX_THROW_OR_ABORT(forced_error()); } |
861de21e BK |
88 | |
89 | /** | |
90 | * @brief Base class for checking address and label information | |
91 | * about allocations. Create a std::map between the allocated | |
92 | * address (void*) and a datum for annotations, which are a pair of | |
93 | * numbers corresponding to label and allocated size. | |
94 | */ | |
5580c6e7 | 95 | struct annotate_base |
a86151e1 | 96 | { |
0da48248 FD |
97 | private: |
98 | typedef std::pair<size_t, size_t> data_type; | |
99 | typedef std::map<void*, data_type> map_alloc_type; | |
100 | typedef map_alloc_type::value_type entry_type; | |
101 | typedef map_alloc_type::const_iterator const_iterator; | |
102 | typedef map_alloc_type::const_reference const_reference; | |
103 | #if __cplusplus >= 201103L | |
104 | typedef std::map<void*, size_t> map_construct_type; | |
105 | #endif | |
106 | ||
107 | public: | |
5580c6e7 BK |
108 | annotate_base() |
109 | { | |
110 | label(); | |
0462b6aa | 111 | map_alloc(); |
5580c6e7 | 112 | } |
861de21e | 113 | |
5580c6e7 BK |
114 | static void |
115 | set_label(size_t l) | |
116 | { label() = l; } | |
117 | ||
118 | static size_t | |
119 | get_label() | |
120 | { return label(); } | |
121 | ||
a86151e1 | 122 | void |
5580c6e7 BK |
123 | insert(void* p, size_t size) |
124 | { | |
0da48248 | 125 | entry_type entry = make_entry(p, size); |
8fc81078 | 126 | if (!p) |
5580c6e7 | 127 | { |
861de21e | 128 | std::string error("annotate_base::insert null insert!\n"); |
0da48248 | 129 | log_to_string(error, entry); |
5580c6e7 BK |
130 | std::__throw_logic_error(error.c_str()); |
131 | } | |
a86151e1 | 132 | |
0da48248 FD |
133 | std::pair<map_alloc_type::iterator, bool> inserted |
134 | = map_alloc().insert(entry); | |
135 | if (!inserted.second) | |
5580c6e7 | 136 | { |
861de21e | 137 | std::string error("annotate_base::insert double insert!\n"); |
0da48248 FD |
138 | log_to_string(error, entry); |
139 | log_to_string(error, *inserted.first); | |
5580c6e7 BK |
140 | std::__throw_logic_error(error.c_str()); |
141 | } | |
5580c6e7 | 142 | } |
a86151e1 | 143 | |
5580c6e7 BK |
144 | void |
145 | erase(void* p, size_t size) | |
0da48248 | 146 | { map_alloc().erase(check_allocated(p, size)); } |
a86151e1 | 147 | |
0462b6aa FD |
148 | #if __cplusplus >= 201103L |
149 | void | |
150 | insert_construct(void* p) | |
151 | { | |
152 | if (!p) | |
153 | { | |
154 | std::string error("annotate_base::insert_construct null!\n"); | |
155 | std::__throw_logic_error(error.c_str()); | |
156 | } | |
157 | ||
0da48248 FD |
158 | auto inserted = map_construct().insert(std::make_pair(p, get_label())); |
159 | if (!inserted.second) | |
0462b6aa FD |
160 | { |
161 | std::string error("annotate_base::insert_construct double insert!\n"); | |
162 | log_to_string(error, std::make_pair(p, get_label())); | |
0da48248 | 163 | log_to_string(error, *inserted.first); |
0462b6aa FD |
164 | std::__throw_logic_error(error.c_str()); |
165 | } | |
0462b6aa FD |
166 | } |
167 | ||
168 | void | |
169 | erase_construct(void* p) | |
0da48248 | 170 | { map_construct().erase(check_constructed(p)); } |
0462b6aa FD |
171 | #endif |
172 | ||
861de21e | 173 | // See if a particular address and allocation size has been saved. |
0da48248 | 174 | inline map_alloc_type::iterator |
5580c6e7 BK |
175 | check_allocated(void* p, size_t size) |
176 | { | |
0da48248 | 177 | map_alloc_type::iterator found = map_alloc().find(p); |
0462b6aa | 178 | if (found == map_alloc().end()) |
5580c6e7 BK |
179 | { |
180 | std::string error("annotate_base::check_allocated by value " | |
181 | "null erase!\n"); | |
182 | log_to_string(error, make_entry(p, size)); | |
183 | std::__throw_logic_error(error.c_str()); | |
184 | } | |
861de21e | 185 | |
5580c6e7 BK |
186 | if (found->second.second != size) |
187 | { | |
188 | std::string error("annotate_base::check_allocated by value " | |
189 | "wrong-size erase!\n"); | |
190 | log_to_string(error, make_entry(p, size)); | |
191 | log_to_string(error, *found); | |
192 | std::__throw_logic_error(error.c_str()); | |
193 | } | |
0da48248 FD |
194 | |
195 | return found; | |
5580c6e7 | 196 | } |
a86151e1 | 197 | |
5580c6e7 BK |
198 | // See if a given label has been allocated. |
199 | inline void | |
0462b6aa FD |
200 | check(size_t label) |
201 | { | |
202 | std::string found; | |
203 | { | |
204 | const_iterator beg = map_alloc().begin(); | |
205 | const_iterator end = map_alloc().end(); | |
206 | while (beg != end) | |
207 | { | |
208 | if (beg->second.first == label) | |
209 | log_to_string(found, *beg); | |
210 | ++beg; | |
211 | } | |
212 | } | |
213 | ||
214 | #if __cplusplus >= 201103L | |
215 | { | |
216 | auto beg = map_construct().begin(); | |
217 | auto end = map_construct().end(); | |
218 | while (beg != end) | |
219 | { | |
220 | if (beg->second == label) | |
221 | log_to_string(found, *beg); | |
222 | ++beg; | |
223 | } | |
224 | } | |
225 | #endif | |
226 | ||
227 | if (!found.empty()) | |
228 | { | |
229 | std::string error("annotate_base::check by label\n"); | |
230 | error += found; | |
231 | std::__throw_logic_error(error.c_str()); | |
232 | } | |
233 | } | |
234 | ||
235 | // See if there is anything left allocated or constructed. | |
236 | inline static void | |
237 | check() | |
238 | { | |
239 | std::string found; | |
240 | { | |
241 | const_iterator beg = map_alloc().begin(); | |
242 | const_iterator end = map_alloc().end(); | |
243 | while (beg != end) | |
244 | { | |
245 | log_to_string(found, *beg); | |
246 | ++beg; | |
247 | } | |
248 | } | |
249 | ||
250 | #if __cplusplus >= 201103L | |
251 | { | |
252 | auto beg = map_construct().begin(); | |
253 | auto end = map_construct().end(); | |
254 | while (beg != end) | |
255 | { | |
256 | log_to_string(found, *beg); | |
257 | ++beg; | |
258 | } | |
259 | } | |
260 | #endif | |
261 | ||
262 | if (!found.empty()) | |
263 | { | |
264 | std::string error("annotate_base::check \n"); | |
265 | error += found; | |
266 | std::__throw_logic_error(error.c_str()); | |
267 | } | |
268 | } | |
269 | ||
270 | #if __cplusplus >= 201103L | |
0da48248 | 271 | inline map_construct_type::iterator |
0462b6aa FD |
272 | check_constructed(void* p) |
273 | { | |
274 | auto found = map_construct().find(p); | |
275 | if (found == map_construct().end()) | |
276 | { | |
277 | std::string error("annotate_base::check_constructed not " | |
278 | "constructed!\n"); | |
279 | log_to_string(error, std::make_pair(p, get_label())); | |
280 | std::__throw_logic_error(error.c_str()); | |
281 | } | |
0da48248 FD |
282 | |
283 | return found; | |
0462b6aa FD |
284 | } |
285 | ||
286 | inline void | |
287 | check_constructed(size_t label) | |
a86151e1 | 288 | { |
0462b6aa FD |
289 | auto beg = map_construct().begin(); |
290 | auto end = map_construct().end(); | |
5580c6e7 BK |
291 | std::string found; |
292 | while (beg != end) | |
293 | { | |
0462b6aa | 294 | if (beg->second == label) |
5580c6e7 BK |
295 | log_to_string(found, *beg); |
296 | ++beg; | |
297 | } | |
861de21e | 298 | |
5580c6e7 BK |
299 | if (!found.empty()) |
300 | { | |
0462b6aa | 301 | std::string error("annotate_base::check_constructed by label\n"); |
5580c6e7 BK |
302 | error += found; |
303 | std::__throw_logic_error(error.c_str()); | |
304 | } | |
305 | } | |
0462b6aa | 306 | #endif |
5580c6e7 BK |
307 | |
308 | private: | |
5580c6e7 BK |
309 | friend std::ostream& |
310 | operator<<(std::ostream&, const annotate_base&); | |
a86151e1 | 311 | |
5580c6e7 BK |
312 | entry_type |
313 | make_entry(void* p, size_t size) | |
314 | { return std::make_pair(p, data_type(get_label(), size)); } | |
315 | ||
0462b6aa FD |
316 | static void |
317 | log_to_string(std::string& s, const_reference ref) | |
5580c6e7 | 318 | { |
96d9670e JW |
319 | #if ! __has_builtin(__builtin_sprintf) |
320 | __typeof__(&std::sprintf) __builtin_sprintf = &std::sprintf; | |
321 | #endif | |
322 | ||
5580c6e7 BK |
323 | char buf[40]; |
324 | const char tab('\t'); | |
325 | s += "label: "; | |
326 | unsigned long l = static_cast<unsigned long>(ref.second.first); | |
327 | __builtin_sprintf(buf, "%lu", l); | |
328 | s += buf; | |
329 | s += tab; | |
330 | s += "size: "; | |
331 | l = static_cast<unsigned long>(ref.second.second); | |
332 | __builtin_sprintf(buf, "%lu", l); | |
333 | s += buf; | |
334 | s += tab; | |
335 | s += "address: "; | |
336 | __builtin_sprintf(buf, "%p", ref.first); | |
337 | s += buf; | |
338 | s += '\n'; | |
339 | } | |
340 | ||
0462b6aa FD |
341 | #if __cplusplus >= 201103L |
342 | static void | |
343 | log_to_string(std::string& s, const std::pair<const void*, size_t>& ref) | |
344 | { | |
96d9670e JW |
345 | #if ! __has_builtin(__builtin_sprintf) |
346 | auto __builtin_sprintf = &std::sprintf; | |
347 | #endif | |
348 | ||
0462b6aa FD |
349 | char buf[40]; |
350 | const char tab('\t'); | |
351 | s += "label: "; | |
352 | unsigned long l = static_cast<unsigned long>(ref.second); | |
353 | __builtin_sprintf(buf, "%lu", l); | |
354 | s += buf; | |
355 | s += tab; | |
356 | s += "address: "; | |
357 | __builtin_sprintf(buf, "%p", ref.first); | |
358 | s += buf; | |
359 | s += '\n'; | |
360 | } | |
361 | #endif | |
362 | ||
5580c6e7 BK |
363 | static size_t& |
364 | label() | |
365 | { | |
861de21e BK |
366 | static size_t _S_label(std::numeric_limits<size_t>::max()); |
367 | return _S_label; | |
5580c6e7 BK |
368 | } |
369 | ||
0462b6aa FD |
370 | static map_alloc_type& |
371 | map_alloc() | |
5580c6e7 | 372 | { |
0462b6aa | 373 | static map_alloc_type _S_map; |
861de21e | 374 | return _S_map; |
5580c6e7 | 375 | } |
0462b6aa FD |
376 | |
377 | #if __cplusplus >= 201103L | |
378 | static map_construct_type& | |
379 | map_construct() | |
380 | { | |
381 | static map_construct_type _S_map; | |
382 | return _S_map; | |
383 | } | |
384 | #endif | |
5580c6e7 BK |
385 | }; |
386 | ||
387 | inline std::ostream& | |
388 | operator<<(std::ostream& os, const annotate_base& __b) | |
389 | { | |
390 | std::string error; | |
391 | typedef annotate_base base_type; | |
0462b6aa FD |
392 | { |
393 | base_type::const_iterator beg = __b.map_alloc().begin(); | |
394 | base_type::const_iterator end = __b.map_alloc().end(); | |
395 | for (; beg != end; ++beg) | |
396 | __b.log_to_string(error, *beg); | |
397 | } | |
398 | #if __cplusplus >= 201103L | |
399 | { | |
400 | auto beg = __b.map_construct().begin(); | |
401 | auto end = __b.map_construct().end(); | |
402 | for (; beg != end; ++beg) | |
403 | __b.log_to_string(error, *beg); | |
404 | } | |
405 | #endif | |
5580c6e7 BK |
406 | return os << error; |
407 | } | |
408 | ||
861de21e BK |
409 | |
410 | /** | |
411 | * @brief Base struct for condition policy. | |
412 | * | |
413 | * Requires a public member function with the signature | |
414 | * void throw_conditionally() | |
415 | */ | |
416 | struct condition_base | |
417 | { | |
f07c2237 JM |
418 | #if __cplusplus >= 201103L |
419 | condition_base() = default; | |
420 | condition_base(const condition_base&) = default; | |
421 | condition_base& operator=(const condition_base&) = default; | |
422 | #endif | |
861de21e BK |
423 | virtual ~condition_base() { }; |
424 | }; | |
425 | ||
426 | ||
427 | /** | |
428 | * @brief Base class for incremental control and throw. | |
429 | */ | |
430 | struct limit_condition : public condition_base | |
5580c6e7 | 431 | { |
861de21e BK |
432 | // Scope-level adjustor objects: set limit for throw at the |
433 | // beginning of a scope block, and restores to previous limit when | |
434 | // object is destroyed on exiting the block. | |
5580c6e7 BK |
435 | struct adjustor_base |
436 | { | |
a86151e1 | 437 | private: |
861de21e | 438 | const size_t _M_orig; |
5580c6e7 BK |
439 | |
440 | public: | |
861de21e BK |
441 | adjustor_base() : _M_orig(limit()) { } |
442 | ||
443 | virtual | |
444 | ~adjustor_base() { set_limit(_M_orig); } | |
445 | }; | |
446 | ||
447 | /// Never enter the condition. | |
448 | struct never_adjustor : public adjustor_base | |
449 | { | |
450 | never_adjustor() { set_limit(std::numeric_limits<size_t>::max()); } | |
451 | }; | |
452 | ||
453 | /// Always enter the condition. | |
454 | struct always_adjustor : public adjustor_base | |
455 | { | |
456 | always_adjustor() { set_limit(count()); } | |
457 | }; | |
458 | ||
459 | /// Enter the nth condition. | |
460 | struct limit_adjustor : public adjustor_base | |
461 | { | |
462 | limit_adjustor(const size_t __l) { set_limit(__l); } | |
463 | }; | |
464 | ||
465 | // Increment _S_count every time called. | |
466 | // If _S_count matches the limit count, throw. | |
467 | static void | |
468 | throw_conditionally() | |
469 | { | |
470 | if (count() == limit()) | |
471 | __throw_forced_error(); | |
472 | ++count(); | |
473 | } | |
474 | ||
475 | static size_t& | |
476 | count() | |
477 | { | |
478 | static size_t _S_count(0); | |
479 | return _S_count; | |
480 | } | |
481 | ||
482 | static size_t& | |
483 | limit() | |
484 | { | |
485 | static size_t _S_limit(std::numeric_limits<size_t>::max()); | |
486 | return _S_limit; | |
487 | } | |
488 | ||
489 | // Zero the throw counter, set limit to argument. | |
490 | static void | |
491 | set_limit(const size_t __l) | |
492 | { | |
493 | limit() = __l; | |
494 | count() = 0; | |
495 | } | |
496 | }; | |
497 | ||
861de21e BK |
498 | /** |
499 | * @brief Base class for random probability control and throw. | |
500 | */ | |
501 | struct random_condition : public condition_base | |
502 | { | |
503 | // Scope-level adjustor objects: set probability for throw at the | |
504 | // beginning of a scope block, and restores to previous | |
505 | // probability when object is destroyed on exiting the block. | |
506 | struct adjustor_base | |
507 | { | |
508 | private: | |
509 | const double _M_orig; | |
510 | ||
511 | public: | |
512 | adjustor_base() : _M_orig(probability()) { } | |
5580c6e7 BK |
513 | |
514 | virtual ~adjustor_base() | |
861de21e | 515 | { set_probability(_M_orig); } |
a86151e1 BK |
516 | }; |
517 | ||
861de21e | 518 | /// Group condition. |
5580c6e7 | 519 | struct group_adjustor : public adjustor_base |
a86151e1 | 520 | { |
5580c6e7 | 521 | group_adjustor(size_t size) |
861de21e | 522 | { set_probability(1 - std::pow(double(1 - probability()), |
5580c6e7 BK |
523 | double(0.5 / (size + 1)))); |
524 | } | |
525 | }; | |
a86151e1 | 526 | |
861de21e | 527 | /// Never enter the condition. |
5580c6e7 BK |
528 | struct never_adjustor : public adjustor_base |
529 | { | |
530 | never_adjustor() { set_probability(0); } | |
531 | }; | |
a86151e1 | 532 | |
861de21e | 533 | /// Always enter the condition. |
5580c6e7 BK |
534 | struct always_adjustor : public adjustor_base |
535 | { | |
536 | always_adjustor() { set_probability(1); } | |
a86151e1 BK |
537 | }; |
538 | ||
861de21e | 539 | random_condition() |
5580c6e7 BK |
540 | { |
541 | probability(); | |
542 | engine(); | |
543 | } | |
a86151e1 BK |
544 | |
545 | static void | |
5580c6e7 BK |
546 | set_probability(double __p) |
547 | { probability() = __p; } | |
a86151e1 | 548 | |
861de21e | 549 | static void |
5580c6e7 BK |
550 | throw_conditionally() |
551 | { | |
861de21e BK |
552 | if (generate() < probability()) |
553 | __throw_forced_error(); | |
5580c6e7 | 554 | } |
a86151e1 | 555 | |
5580c6e7 BK |
556 | void |
557 | seed(unsigned long __s) | |
558 | { engine().seed(__s); } | |
a86151e1 BK |
559 | |
560 | private: | |
734f5023 | 561 | #if __cplusplus >= 201103L |
5580c6e7 BK |
562 | typedef std::uniform_real_distribution<double> distribution_type; |
563 | typedef std::mt19937 engine_type; | |
564 | #else | |
565 | typedef std::tr1::uniform_real<double> distribution_type; | |
566 | typedef std::tr1::mt19937 engine_type; | |
567 | #endif | |
a86151e1 | 568 | |
861de21e | 569 | static double |
5580c6e7 BK |
570 | generate() |
571 | { | |
734f5023 | 572 | #if __cplusplus >= 201103L |
5580c6e7 BK |
573 | const distribution_type distribution(0, 1); |
574 | static auto generator = std::bind(distribution, engine()); | |
575 | #else | |
576 | // Use variate_generator to get normalized results. | |
577 | typedef std::tr1::variate_generator<engine_type, distribution_type> gen_t; | |
578 | distribution_type distribution(0, 1); | |
579 | static gen_t generator(engine(), distribution); | |
580 | #endif | |
a86151e1 | 581 | |
96d9670e JW |
582 | #if ! __has_builtin(__builtin_sprintf) |
583 | __typeof__(&std::sprintf) __builtin_sprintf = &std::sprintf; | |
584 | #endif | |
585 | ||
5580c6e7 BK |
586 | double random = generator(); |
587 | if (random < distribution.min() || random > distribution.max()) | |
588 | { | |
861de21e | 589 | std::string __s("random_condition::generate"); |
5580c6e7 BK |
590 | __s += "\n"; |
591 | __s += "random number generated is: "; | |
592 | char buf[40]; | |
593 | __builtin_sprintf(buf, "%f", random); | |
594 | __s += buf; | |
595 | std::__throw_out_of_range(__s.c_str()); | |
596 | } | |
a86151e1 | 597 | |
5580c6e7 BK |
598 | return random; |
599 | } | |
a86151e1 | 600 | |
5580c6e7 BK |
601 | static double& |
602 | probability() | |
603 | { | |
861de21e BK |
604 | static double _S_p; |
605 | return _S_p; | |
5580c6e7 | 606 | } |
a86151e1 | 607 | |
5580c6e7 BK |
608 | static engine_type& |
609 | engine() | |
610 | { | |
861de21e BK |
611 | static engine_type _S_e; |
612 | return _S_e; | |
5580c6e7 | 613 | } |
a86151e1 | 614 | }; |
861de21e | 615 | |
5580c6e7 | 616 | /** |
861de21e BK |
617 | * @brief Class with exception generation control. Intended to be |
618 | * used as a value_type in templatized code. | |
619 | * | |
620 | * Note: Destructor not allowed to throw. | |
621 | */ | |
622 | template<typename _Cond> | |
623 | struct throw_value_base : public _Cond | |
624 | { | |
625 | typedef _Cond condition_type; | |
626 | ||
627 | using condition_type::throw_conditionally; | |
628 | ||
629 | std::size_t _M_i; | |
630 | ||
3c0f2830 | 631 | #ifndef _GLIBCXX_IS_AGGREGATE |
861de21e BK |
632 | throw_value_base() : _M_i(0) |
633 | { throw_conditionally(); } | |
634 | ||
3c0f2830 | 635 | throw_value_base(const throw_value_base& __v) : _M_i(__v._M_i) |
861de21e BK |
636 | { throw_conditionally(); } |
637 | ||
734f5023 | 638 | #if __cplusplus >= 201103L |
51bf1e80 FD |
639 | // Shall not throw. |
640 | throw_value_base(throw_value_base&&) = default; | |
641 | #endif | |
642 | ||
3c0f2830 | 643 | explicit throw_value_base(const std::size_t __i) : _M_i(__i) |
861de21e | 644 | { throw_conditionally(); } |
3c0f2830 | 645 | #endif |
861de21e BK |
646 | |
647 | throw_value_base& | |
648 | operator=(const throw_value_base& __v) | |
649 | { | |
650 | throw_conditionally(); | |
651 | _M_i = __v._M_i; | |
652 | return *this; | |
653 | } | |
654 | ||
734f5023 | 655 | #if __cplusplus >= 201103L |
51bf1e80 FD |
656 | // Shall not throw. |
657 | throw_value_base& | |
658 | operator=(throw_value_base&&) = default; | |
659 | #endif | |
660 | ||
861de21e BK |
661 | throw_value_base& |
662 | operator++() | |
663 | { | |
664 | throw_conditionally(); | |
665 | ++_M_i; | |
666 | return *this; | |
667 | } | |
668 | }; | |
669 | ||
670 | template<typename _Cond> | |
671 | inline void | |
672 | swap(throw_value_base<_Cond>& __a, throw_value_base<_Cond>& __b) | |
673 | { | |
674 | typedef throw_value_base<_Cond> throw_value; | |
675 | throw_value::throw_conditionally(); | |
676 | throw_value orig(__a); | |
677 | __a = __b; | |
678 | __b = orig; | |
679 | } | |
680 | ||
681 | // General instantiable types requirements. | |
682 | template<typename _Cond> | |
683 | inline bool | |
684 | operator==(const throw_value_base<_Cond>& __a, | |
685 | const throw_value_base<_Cond>& __b) | |
686 | { | |
687 | typedef throw_value_base<_Cond> throw_value; | |
688 | throw_value::throw_conditionally(); | |
689 | bool __ret = __a._M_i == __b._M_i; | |
690 | return __ret; | |
691 | } | |
692 | ||
693 | template<typename _Cond> | |
694 | inline bool | |
695 | operator<(const throw_value_base<_Cond>& __a, | |
696 | const throw_value_base<_Cond>& __b) | |
697 | { | |
698 | typedef throw_value_base<_Cond> throw_value; | |
699 | throw_value::throw_conditionally(); | |
700 | bool __ret = __a._M_i < __b._M_i; | |
701 | return __ret; | |
702 | } | |
703 | ||
704 | // Numeric algorithms instantiable types requirements. | |
705 | template<typename _Cond> | |
706 | inline throw_value_base<_Cond> | |
707 | operator+(const throw_value_base<_Cond>& __a, | |
708 | const throw_value_base<_Cond>& __b) | |
709 | { | |
710 | typedef throw_value_base<_Cond> throw_value; | |
711 | throw_value::throw_conditionally(); | |
712 | throw_value __ret(__a._M_i + __b._M_i); | |
713 | return __ret; | |
714 | } | |
715 | ||
716 | template<typename _Cond> | |
717 | inline throw_value_base<_Cond> | |
718 | operator-(const throw_value_base<_Cond>& __a, | |
719 | const throw_value_base<_Cond>& __b) | |
720 | { | |
721 | typedef throw_value_base<_Cond> throw_value; | |
722 | throw_value::throw_conditionally(); | |
723 | throw_value __ret(__a._M_i - __b._M_i); | |
724 | return __ret; | |
725 | } | |
726 | ||
727 | template<typename _Cond> | |
728 | inline throw_value_base<_Cond> | |
729 | operator*(const throw_value_base<_Cond>& __a, | |
730 | const throw_value_base<_Cond>& __b) | |
731 | { | |
732 | typedef throw_value_base<_Cond> throw_value; | |
733 | throw_value::throw_conditionally(); | |
734 | throw_value __ret(__a._M_i * __b._M_i); | |
735 | return __ret; | |
736 | } | |
737 | ||
3c0f2830 | 738 | |
861de21e BK |
739 | /// Type throwing via limit condition. |
740 | struct throw_value_limit : public throw_value_base<limit_condition> | |
741 | { | |
742 | typedef throw_value_base<limit_condition> base_type; | |
743 | ||
3c0f2830 | 744 | #ifndef _GLIBCXX_IS_AGGREGATE |
861de21e BK |
745 | throw_value_limit() { } |
746 | ||
747 | throw_value_limit(const throw_value_limit& __other) | |
748 | : base_type(__other._M_i) { } | |
749 | ||
734f5023 | 750 | #if __cplusplus >= 201103L |
51bf1e80 FD |
751 | throw_value_limit(throw_value_limit&&) = default; |
752 | #endif | |
753 | ||
861de21e | 754 | explicit throw_value_limit(const std::size_t __i) : base_type(__i) { } |
3c0f2830 | 755 | #endif |
51bf1e80 FD |
756 | |
757 | throw_value_limit& | |
758 | operator=(const throw_value_limit& __other) | |
759 | { | |
760 | base_type::operator=(__other); | |
761 | return *this; | |
762 | } | |
763 | ||
734f5023 | 764 | #if __cplusplus >= 201103L |
51bf1e80 FD |
765 | throw_value_limit& |
766 | operator=(throw_value_limit&&) = default; | |
767 | #endif | |
861de21e BK |
768 | }; |
769 | ||
770 | /// Type throwing via random condition. | |
771 | struct throw_value_random : public throw_value_base<random_condition> | |
772 | { | |
773 | typedef throw_value_base<random_condition> base_type; | |
774 | ||
3c0f2830 | 775 | #ifndef _GLIBCXX_IS_AGGREGATE |
861de21e BK |
776 | throw_value_random() { } |
777 | ||
778 | throw_value_random(const throw_value_random& __other) | |
779 | : base_type(__other._M_i) { } | |
780 | ||
734f5023 | 781 | #if __cplusplus >= 201103L |
51bf1e80 FD |
782 | throw_value_random(throw_value_random&&) = default; |
783 | #endif | |
861de21e BK |
784 | |
785 | explicit throw_value_random(const std::size_t __i) : base_type(__i) { } | |
3c0f2830 | 786 | #endif |
51bf1e80 FD |
787 | |
788 | throw_value_random& | |
789 | operator=(const throw_value_random& __other) | |
790 | { | |
791 | base_type::operator=(__other); | |
792 | return *this; | |
793 | } | |
794 | ||
734f5023 | 795 | #if __cplusplus >= 201103L |
51bf1e80 FD |
796 | throw_value_random& |
797 | operator=(throw_value_random&&) = default; | |
798 | #endif | |
861de21e | 799 | }; |
861de21e BK |
800 | |
801 | /** | |
802 | * @brief Allocator class with logging and exception generation control. | |
803 | * Intended to be used as an allocator_type in templatized code. | |
5b9daa7e | 804 | * @ingroup allocators |
861de21e BK |
805 | * |
806 | * Note: Deallocate not allowed to throw. | |
5b9daa7e | 807 | */ |
861de21e BK |
808 | template<typename _Tp, typename _Cond> |
809 | class throw_allocator_base | |
810 | : public annotate_base, public _Cond | |
a86151e1 BK |
811 | { |
812 | public: | |
84a2f4b3 JW |
813 | typedef std::size_t size_type; |
814 | typedef std::ptrdiff_t difference_type; | |
861de21e | 815 | typedef _Tp value_type; |
3441f106 BK |
816 | typedef value_type* pointer; |
817 | typedef const value_type* const_pointer; | |
818 | typedef value_type& reference; | |
819 | typedef const value_type& const_reference; | |
820 | ||
1b5dc776 JW |
821 | #if __cplusplus >= 201103L |
822 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
823 | // 2103. std::allocator propagate_on_container_move_assignment | |
824 | typedef std::true_type propagate_on_container_move_assignment; | |
825 | #endif | |
826 | ||
5580c6e7 | 827 | private: |
861de21e | 828 | typedef _Cond condition_type; |
a86151e1 | 829 | |
861de21e | 830 | std::allocator<value_type> _M_allocator; |
a86151e1 | 831 | |
2cae56bd JW |
832 | typedef __gnu_cxx::__alloc_traits<std::allocator<value_type> > traits; |
833 | ||
861de21e | 834 | using condition_type::throw_conditionally; |
a86151e1 | 835 | |
861de21e | 836 | public: |
a86151e1 | 837 | size_type |
7d9cb054 | 838 | max_size() const _GLIBCXX_USE_NOEXCEPT |
2cae56bd | 839 | { return traits::max_size(_M_allocator); } |
a86151e1 | 840 | |
882b3d5c | 841 | pointer |
7d9cb054 PC |
842 | address(reference __x) const _GLIBCXX_NOEXCEPT |
843 | { return std::__addressof(__x); } | |
882b3d5c PC |
844 | |
845 | const_pointer | |
7d9cb054 PC |
846 | address(const_reference __x) const _GLIBCXX_NOEXCEPT |
847 | { return std::__addressof(__x); } | |
882b3d5c | 848 | |
d715f554 | 849 | _GLIBCXX_NODISCARD pointer |
b81b017e | 850 | allocate(size_type __n, const void* __hint = 0) |
a86151e1 | 851 | { |
e762c6f4 | 852 | if (__n > this->max_size()) |
b8637750 BK |
853 | std::__throw_bad_alloc(); |
854 | ||
a86151e1 | 855 | throw_conditionally(); |
b81b017e | 856 | pointer const a = traits::allocate(_M_allocator, __n, __hint); |
b8637750 | 857 | insert(a, sizeof(value_type) * __n); |
a86151e1 BK |
858 | return a; |
859 | } | |
860 | ||
734f5023 | 861 | #if __cplusplus >= 201103L |
45ba8f9f JW |
862 | template<typename _Up, typename... _Args> |
863 | void | |
864 | construct(_Up* __p, _Args&&... __args) | |
0462b6aa | 865 | { |
2cae56bd | 866 | traits::construct(_M_allocator, __p, std::forward<_Args>(__args)...); |
0462b6aa FD |
867 | insert_construct(__p); |
868 | } | |
45ba8f9f JW |
869 | |
870 | template<typename _Up> | |
871 | void | |
872 | destroy(_Up* __p) | |
0462b6aa FD |
873 | { |
874 | erase_construct(__p); | |
2cae56bd | 875 | traits::destroy(_M_allocator, __p); |
0462b6aa | 876 | } |
45ba8f9f | 877 | #else |
a86151e1 | 878 | void |
b81b017e JW |
879 | construct(pointer __p, const value_type& __val) |
880 | { return _M_allocator.construct(__p, __val); } | |
a86151e1 BK |
881 | |
882 | void | |
b8637750 | 883 | destroy(pointer __p) |
5580c6e7 | 884 | { _M_allocator.destroy(__p); } |
45ba8f9f | 885 | #endif |
a86151e1 BK |
886 | |
887 | void | |
b8637750 | 888 | deallocate(pointer __p, size_type __n) |
a86151e1 | 889 | { |
b8637750 | 890 | erase(__p, sizeof(value_type) * __n); |
5580c6e7 | 891 | _M_allocator.deallocate(__p, __n); |
a86151e1 BK |
892 | } |
893 | ||
894 | void | |
b8637750 | 895 | check_allocated(pointer __p, size_type __n) |
5580c6e7 BK |
896 | { |
897 | size_type __t = sizeof(value_type) * __n; | |
898 | annotate_base::check_allocated(__p, __t); | |
899 | } | |
3441f106 | 900 | |
861de21e | 901 | void |
0462b6aa FD |
902 | check(size_type __n) |
903 | { annotate_base::check(__n); } | |
861de21e | 904 | }; |
a86151e1 | 905 | |
861de21e | 906 | template<typename _Tp, typename _Cond> |
a86151e1 | 907 | inline bool |
861de21e BK |
908 | operator==(const throw_allocator_base<_Tp, _Cond>&, |
909 | const throw_allocator_base<_Tp, _Cond>&) | |
a86151e1 BK |
910 | { return true; } |
911 | ||
596676d6 | 912 | #if __cpp_impl_three_way_comparison < 201907L |
861de21e | 913 | template<typename _Tp, typename _Cond> |
a86151e1 | 914 | inline bool |
861de21e BK |
915 | operator!=(const throw_allocator_base<_Tp, _Cond>&, |
916 | const throw_allocator_base<_Tp, _Cond>&) | |
a86151e1 | 917 | { return false; } |
596676d6 | 918 | #endif |
a86151e1 | 919 | |
861de21e BK |
920 | /// Allocator throwing via limit condition. |
921 | template<typename _Tp> | |
922 | struct throw_allocator_limit | |
923 | : public throw_allocator_base<_Tp, limit_condition> | |
924 | { | |
925 | template<typename _Tp1> | |
926 | struct rebind | |
927 | { typedef throw_allocator_limit<_Tp1> other; }; | |
928 | ||
7d9cb054 | 929 | throw_allocator_limit() _GLIBCXX_USE_NOEXCEPT { } |
861de21e | 930 | |
7d9cb054 PC |
931 | throw_allocator_limit(const throw_allocator_limit&) |
932 | _GLIBCXX_USE_NOEXCEPT { } | |
861de21e BK |
933 | |
934 | template<typename _Tp1> | |
7d9cb054 PC |
935 | throw_allocator_limit(const throw_allocator_limit<_Tp1>&) |
936 | _GLIBCXX_USE_NOEXCEPT { } | |
861de21e | 937 | |
7d9cb054 | 938 | ~throw_allocator_limit() _GLIBCXX_USE_NOEXCEPT { } |
eb6b71b8 JW |
939 | |
940 | #if __cplusplus >= 201103L | |
941 | throw_allocator_limit& | |
942 | operator=(const throw_allocator_limit&) = default; | |
943 | #endif | |
861de21e BK |
944 | }; |
945 | ||
946 | /// Allocator throwing via random condition. | |
947 | template<typename _Tp> | |
948 | struct throw_allocator_random | |
949 | : public throw_allocator_base<_Tp, random_condition> | |
950 | { | |
951 | template<typename _Tp1> | |
952 | struct rebind | |
953 | { typedef throw_allocator_random<_Tp1> other; }; | |
954 | ||
7d9cb054 | 955 | throw_allocator_random() _GLIBCXX_USE_NOEXCEPT { } |
861de21e | 956 | |
7d9cb054 PC |
957 | throw_allocator_random(const throw_allocator_random&) |
958 | _GLIBCXX_USE_NOEXCEPT { } | |
861de21e BK |
959 | |
960 | template<typename _Tp1> | |
7d9cb054 PC |
961 | throw_allocator_random(const throw_allocator_random<_Tp1>&) |
962 | _GLIBCXX_USE_NOEXCEPT { } | |
861de21e | 963 | |
7d9cb054 | 964 | ~throw_allocator_random() _GLIBCXX_USE_NOEXCEPT { } |
eb6b71b8 JW |
965 | |
966 | #if __cplusplus >= 201103L | |
967 | throw_allocator_random& | |
968 | operator=(const throw_allocator_random&) = default; | |
969 | #endif | |
861de21e BK |
970 | }; |
971 | ||
12ffa228 BK |
972 | _GLIBCXX_END_NAMESPACE_VERSION |
973 | } // namespace | |
a86151e1 | 974 | |
734f5023 | 975 | #if __cplusplus >= 201103L |
861de21e BK |
976 | |
977 | # include <bits/functional_hash.h> | |
978 | ||
12ffa228 | 979 | namespace std _GLIBCXX_VISIBILITY(default) |
861de21e | 980 | { |
de196e5d JW |
981 | #pragma GCC diagnostic push |
982 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" | |
983 | ||
861de21e BK |
984 | /// Explicit specialization of std::hash for __gnu_cxx::throw_value_limit. |
985 | template<> | |
986 | struct hash<__gnu_cxx::throw_value_limit> | |
987 | : public std::unary_function<__gnu_cxx::throw_value_limit, size_t> | |
988 | { | |
989 | size_t | |
714902c8 | 990 | operator()(const __gnu_cxx::throw_value_limit& __val) const |
861de21e | 991 | { |
0462b6aa | 992 | __gnu_cxx::throw_value_limit::throw_conditionally(); |
6e687a9a RW |
993 | std::hash<std::size_t> __h; |
994 | size_t __result = __h(__val._M_i); | |
861de21e BK |
995 | return __result; |
996 | } | |
997 | }; | |
998 | ||
0462b6aa | 999 | /// Explicit specialization of std::hash for __gnu_cxx::throw_value_random. |
861de21e BK |
1000 | template<> |
1001 | struct hash<__gnu_cxx::throw_value_random> | |
1002 | : public std::unary_function<__gnu_cxx::throw_value_random, size_t> | |
1003 | { | |
1004 | size_t | |
714902c8 | 1005 | operator()(const __gnu_cxx::throw_value_random& __val) const |
861de21e | 1006 | { |
0462b6aa | 1007 | __gnu_cxx::throw_value_random::throw_conditionally(); |
6e687a9a RW |
1008 | std::hash<std::size_t> __h; |
1009 | size_t __result = __h(__val._M_i); | |
861de21e BK |
1010 | return __result; |
1011 | } | |
1012 | }; | |
de196e5d JW |
1013 | |
1014 | #pragma GCC diagnostic pop | |
861de21e BK |
1015 | } // end namespace std |
1016 | #endif | |
1017 | ||
5580c6e7 | 1018 | #endif |