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