Some libstdc++ fixes for -Wdeprecated-copy.
[gcc.git] / libstdc++-v3 / include / ext / throw_allocator.h
1 // -*- C++ -*-
2
3 // Copyright (C) 2005-2018 Free Software Foundation, Inc.
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
8 // Foundation; either version 3, or (at your option) any later
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
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.
19
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/>.
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
36 /** @file ext/throw_allocator.h
37 * This file is a GNU extension to the Standard C++ Library.
38 *
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.
44 */
45
46 #ifndef _THROW_ALLOCATOR_H
47 #define _THROW_ALLOCATOR_H 1
48
49 #include <cmath>
50 #include <ctime>
51 #include <map>
52 #include <string>
53 #include <ostream>
54 #include <stdexcept>
55 #include <utility>
56 #include <bits/functexcept.h>
57 #include <bits/move.h>
58 #if __cplusplus >= 201103L
59 # include <functional>
60 # include <random>
61 #else
62 # include <tr1/functional>
63 # include <tr1/random>
64 #endif
65
66 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
67 {
68 _GLIBCXX_BEGIN_NAMESPACE_VERSION
69
70 /**
71 * @brief Thown by exception safety machinery.
72 * @ingroup exceptions
73 */
74 struct forced_error : public std::exception
75 { };
76
77 // Substitute for forced_error object when -fno-exceptions.
78 inline void
79 __throw_forced_error()
80 { _GLIBCXX_THROW_OR_ABORT(forced_error()); }
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 */
88 struct annotate_base
89 {
90 annotate_base()
91 {
92 label();
93 map_alloc();
94 }
95
96 static void
97 set_label(size_t l)
98 { label() = l; }
99
100 static size_t
101 get_label()
102 { return label(); }
103
104 void
105 insert(void* p, size_t size)
106 {
107 if (!p)
108 {
109 std::string error("annotate_base::insert null insert!\n");
110 log_to_string(error, make_entry(p, size));
111 std::__throw_logic_error(error.c_str());
112 }
113
114 const_iterator found = map_alloc().find(p);
115 if (found != map_alloc().end())
116 {
117 std::string error("annotate_base::insert double insert!\n");
118 log_to_string(error, make_entry(p, size));
119 log_to_string(error, *found);
120 std::__throw_logic_error(error.c_str());
121 }
122
123 map_alloc().insert(make_entry(p, size));
124 }
125
126 void
127 erase(void* p, size_t size)
128 {
129 check_allocated(p, size);
130 map_alloc().erase(p);
131 }
132
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
163 // See if a particular address and allocation size has been saved.
164 inline void
165 check_allocated(void* p, size_t size)
166 {
167 const_iterator found = map_alloc().find(p);
168 if (found == map_alloc().end())
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 }
175
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 }
185
186 // See if a given label has been allocated.
187 inline void
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)
274 {
275 auto beg = map_construct().begin();
276 auto end = map_construct().end();
277 std::string found;
278 while (beg != end)
279 {
280 if (beg->second == label)
281 log_to_string(found, *beg);
282 ++beg;
283 }
284
285 if (!found.empty())
286 {
287 std::string error("annotate_base::check_constructed by label\n");
288 error += found;
289 std::__throw_logic_error(error.c_str());
290 }
291 }
292 #endif
293
294 private:
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
303
304 friend std::ostream&
305 operator<<(std::ostream&, const annotate_base&);
306
307 entry_type
308 make_entry(void* p, size_t size)
309 { return std::make_pair(p, data_type(get_label(), size)); }
310
311 static void
312 log_to_string(std::string& s, const_reference ref)
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
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
350 static size_t&
351 label()
352 {
353 static size_t _S_label(std::numeric_limits<size_t>::max());
354 return _S_label;
355 }
356
357 static map_alloc_type&
358 map_alloc()
359 {
360 static map_alloc_type _S_map;
361 return _S_map;
362 }
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
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;
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
393 return os << error;
394 }
395
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 #if __cplusplus >= 201103L
406 condition_base() = default;
407 condition_base(const condition_base&) = default;
408 condition_base& operator=(const condition_base&) = default;
409 #endif
410 virtual ~condition_base() { };
411 };
412
413
414 /**
415 * @brief Base class for incremental control and throw.
416 */
417 struct limit_condition : public condition_base
418 {
419 // Scope-level adjustor objects: set limit for throw at the
420 // beginning of a scope block, and restores to previous limit when
421 // object is destroyed on exiting the block.
422 struct adjustor_base
423 {
424 private:
425 const size_t _M_orig;
426
427 public:
428 adjustor_base() : _M_orig(limit()) { }
429
430 virtual
431 ~adjustor_base() { set_limit(_M_orig); }
432 };
433
434 /// Never enter the condition.
435 struct never_adjustor : public adjustor_base
436 {
437 never_adjustor() { set_limit(std::numeric_limits<size_t>::max()); }
438 };
439
440 /// Always enter the condition.
441 struct always_adjustor : public adjustor_base
442 {
443 always_adjustor() { set_limit(count()); }
444 };
445
446 /// Enter the nth condition.
447 struct limit_adjustor : public adjustor_base
448 {
449 limit_adjustor(const size_t __l) { set_limit(__l); }
450 };
451
452 // Increment _S_count every time called.
453 // If _S_count matches the limit count, throw.
454 static void
455 throw_conditionally()
456 {
457 if (count() == limit())
458 __throw_forced_error();
459 ++count();
460 }
461
462 static size_t&
463 count()
464 {
465 static size_t _S_count(0);
466 return _S_count;
467 }
468
469 static size_t&
470 limit()
471 {
472 static size_t _S_limit(std::numeric_limits<size_t>::max());
473 return _S_limit;
474 }
475
476 // Zero the throw counter, set limit to argument.
477 static void
478 set_limit(const size_t __l)
479 {
480 limit() = __l;
481 count() = 0;
482 }
483 };
484
485
486 /**
487 * @brief Base class for random probability control and throw.
488 */
489 struct random_condition : public condition_base
490 {
491 // Scope-level adjustor objects: set probability for throw at the
492 // beginning of a scope block, and restores to previous
493 // probability when object is destroyed on exiting the block.
494 struct adjustor_base
495 {
496 private:
497 const double _M_orig;
498
499 public:
500 adjustor_base() : _M_orig(probability()) { }
501
502 virtual ~adjustor_base()
503 { set_probability(_M_orig); }
504 };
505
506 /// Group condition.
507 struct group_adjustor : public adjustor_base
508 {
509 group_adjustor(size_t size)
510 { set_probability(1 - std::pow(double(1 - probability()),
511 double(0.5 / (size + 1))));
512 }
513 };
514
515 /// Never enter the condition.
516 struct never_adjustor : public adjustor_base
517 {
518 never_adjustor() { set_probability(0); }
519 };
520
521 /// Always enter the condition.
522 struct always_adjustor : public adjustor_base
523 {
524 always_adjustor() { set_probability(1); }
525 };
526
527 random_condition()
528 {
529 probability();
530 engine();
531 }
532
533 static void
534 set_probability(double __p)
535 { probability() = __p; }
536
537 static void
538 throw_conditionally()
539 {
540 if (generate() < probability())
541 __throw_forced_error();
542 }
543
544 void
545 seed(unsigned long __s)
546 { engine().seed(__s); }
547
548 private:
549 #if __cplusplus >= 201103L
550 typedef std::uniform_real_distribution<double> distribution_type;
551 typedef std::mt19937 engine_type;
552 #else
553 typedef std::tr1::uniform_real<double> distribution_type;
554 typedef std::tr1::mt19937 engine_type;
555 #endif
556
557 static double
558 generate()
559 {
560 #if __cplusplus >= 201103L
561 const distribution_type distribution(0, 1);
562 static auto generator = std::bind(distribution, engine());
563 #else
564 // Use variate_generator to get normalized results.
565 typedef std::tr1::variate_generator<engine_type, distribution_type> gen_t;
566 distribution_type distribution(0, 1);
567 static gen_t generator(engine(), distribution);
568 #endif
569
570 double random = generator();
571 if (random < distribution.min() || random > distribution.max())
572 {
573 std::string __s("random_condition::generate");
574 __s += "\n";
575 __s += "random number generated is: ";
576 char buf[40];
577 __builtin_sprintf(buf, "%f", random);
578 __s += buf;
579 std::__throw_out_of_range(__s.c_str());
580 }
581
582 return random;
583 }
584
585 static double&
586 probability()
587 {
588 static double _S_p;
589 return _S_p;
590 }
591
592 static engine_type&
593 engine()
594 {
595 static engine_type _S_e;
596 return _S_e;
597 }
598 };
599
600
601 /**
602 * @brief Class with exception generation control. Intended to be
603 * used as a value_type in templatized code.
604 *
605 * Note: Destructor not allowed to throw.
606 */
607 template<typename _Cond>
608 struct throw_value_base : public _Cond
609 {
610 typedef _Cond condition_type;
611
612 using condition_type::throw_conditionally;
613
614 std::size_t _M_i;
615
616 #ifndef _GLIBCXX_IS_AGGREGATE
617 throw_value_base() : _M_i(0)
618 { throw_conditionally(); }
619
620 throw_value_base(const throw_value_base& __v) : _M_i(__v._M_i)
621 { throw_conditionally(); }
622
623 #if __cplusplus >= 201103L
624 // Shall not throw.
625 throw_value_base(throw_value_base&&) = default;
626 #endif
627
628 explicit throw_value_base(const std::size_t __i) : _M_i(__i)
629 { throw_conditionally(); }
630 #endif
631
632 throw_value_base&
633 operator=(const throw_value_base& __v)
634 {
635 throw_conditionally();
636 _M_i = __v._M_i;
637 return *this;
638 }
639
640 #if __cplusplus >= 201103L
641 // Shall not throw.
642 throw_value_base&
643 operator=(throw_value_base&&) = default;
644 #endif
645
646 throw_value_base&
647 operator++()
648 {
649 throw_conditionally();
650 ++_M_i;
651 return *this;
652 }
653 };
654
655 template<typename _Cond>
656 inline void
657 swap(throw_value_base<_Cond>& __a, throw_value_base<_Cond>& __b)
658 {
659 typedef throw_value_base<_Cond> throw_value;
660 throw_value::throw_conditionally();
661 throw_value orig(__a);
662 __a = __b;
663 __b = orig;
664 }
665
666 // General instantiable types requirements.
667 template<typename _Cond>
668 inline bool
669 operator==(const throw_value_base<_Cond>& __a,
670 const throw_value_base<_Cond>& __b)
671 {
672 typedef throw_value_base<_Cond> throw_value;
673 throw_value::throw_conditionally();
674 bool __ret = __a._M_i == __b._M_i;
675 return __ret;
676 }
677
678 template<typename _Cond>
679 inline bool
680 operator<(const throw_value_base<_Cond>& __a,
681 const throw_value_base<_Cond>& __b)
682 {
683 typedef throw_value_base<_Cond> throw_value;
684 throw_value::throw_conditionally();
685 bool __ret = __a._M_i < __b._M_i;
686 return __ret;
687 }
688
689 // Numeric algorithms instantiable types requirements.
690 template<typename _Cond>
691 inline throw_value_base<_Cond>
692 operator+(const throw_value_base<_Cond>& __a,
693 const throw_value_base<_Cond>& __b)
694 {
695 typedef throw_value_base<_Cond> throw_value;
696 throw_value::throw_conditionally();
697 throw_value __ret(__a._M_i + __b._M_i);
698 return __ret;
699 }
700
701 template<typename _Cond>
702 inline throw_value_base<_Cond>
703 operator-(const throw_value_base<_Cond>& __a,
704 const throw_value_base<_Cond>& __b)
705 {
706 typedef throw_value_base<_Cond> throw_value;
707 throw_value::throw_conditionally();
708 throw_value __ret(__a._M_i - __b._M_i);
709 return __ret;
710 }
711
712 template<typename _Cond>
713 inline throw_value_base<_Cond>
714 operator*(const throw_value_base<_Cond>& __a,
715 const throw_value_base<_Cond>& __b)
716 {
717 typedef throw_value_base<_Cond> throw_value;
718 throw_value::throw_conditionally();
719 throw_value __ret(__a._M_i * __b._M_i);
720 return __ret;
721 }
722
723
724 /// Type throwing via limit condition.
725 struct throw_value_limit : public throw_value_base<limit_condition>
726 {
727 typedef throw_value_base<limit_condition> base_type;
728
729 #ifndef _GLIBCXX_IS_AGGREGATE
730 throw_value_limit() { }
731
732 throw_value_limit(const throw_value_limit& __other)
733 : base_type(__other._M_i) { }
734
735 #if __cplusplus >= 201103L
736 throw_value_limit(throw_value_limit&&) = default;
737 #endif
738
739 explicit throw_value_limit(const std::size_t __i) : base_type(__i) { }
740 #endif
741
742 throw_value_limit&
743 operator=(const throw_value_limit& __other)
744 {
745 base_type::operator=(__other);
746 return *this;
747 }
748
749 #if __cplusplus >= 201103L
750 throw_value_limit&
751 operator=(throw_value_limit&&) = default;
752 #endif
753 };
754
755 /// Type throwing via random condition.
756 struct throw_value_random : public throw_value_base<random_condition>
757 {
758 typedef throw_value_base<random_condition> base_type;
759
760 #ifndef _GLIBCXX_IS_AGGREGATE
761 throw_value_random() { }
762
763 throw_value_random(const throw_value_random& __other)
764 : base_type(__other._M_i) { }
765
766 #if __cplusplus >= 201103L
767 throw_value_random(throw_value_random&&) = default;
768 #endif
769
770 explicit throw_value_random(const std::size_t __i) : base_type(__i) { }
771 #endif
772
773 throw_value_random&
774 operator=(const throw_value_random& __other)
775 {
776 base_type::operator=(__other);
777 return *this;
778 }
779
780 #if __cplusplus >= 201103L
781 throw_value_random&
782 operator=(throw_value_random&&) = default;
783 #endif
784 };
785
786
787 /**
788 * @brief Allocator class with logging and exception generation control.
789 * Intended to be used as an allocator_type in templatized code.
790 * @ingroup allocators
791 *
792 * Note: Deallocate not allowed to throw.
793 */
794 template<typename _Tp, typename _Cond>
795 class throw_allocator_base
796 : public annotate_base, public _Cond
797 {
798 public:
799 typedef size_t size_type;
800 typedef ptrdiff_t difference_type;
801 typedef _Tp value_type;
802 typedef value_type* pointer;
803 typedef const value_type* const_pointer;
804 typedef value_type& reference;
805 typedef const value_type& const_reference;
806
807 #if __cplusplus >= 201103L
808 // _GLIBCXX_RESOLVE_LIB_DEFECTS
809 // 2103. std::allocator propagate_on_container_move_assignment
810 typedef std::true_type propagate_on_container_move_assignment;
811 #endif
812
813 private:
814 typedef _Cond condition_type;
815
816 std::allocator<value_type> _M_allocator;
817
818 using condition_type::throw_conditionally;
819
820 public:
821 size_type
822 max_size() const _GLIBCXX_USE_NOEXCEPT
823 { return _M_allocator.max_size(); }
824
825 pointer
826 address(reference __x) const _GLIBCXX_NOEXCEPT
827 { return std::__addressof(__x); }
828
829 const_pointer
830 address(const_reference __x) const _GLIBCXX_NOEXCEPT
831 { return std::__addressof(__x); }
832
833 pointer
834 allocate(size_type __n, std::allocator<void>::const_pointer hint = 0)
835 {
836 if (__n > this->max_size())
837 std::__throw_bad_alloc();
838
839 throw_conditionally();
840 pointer const a = _M_allocator.allocate(__n, hint);
841 insert(a, sizeof(value_type) * __n);
842 return a;
843 }
844
845 #if __cplusplus >= 201103L
846 template<typename _Up, typename... _Args>
847 void
848 construct(_Up* __p, _Args&&... __args)
849 {
850 _M_allocator.construct(__p, std::forward<_Args>(__args)...);
851 insert_construct(__p);
852 }
853
854 template<typename _Up>
855 void
856 destroy(_Up* __p)
857 {
858 erase_construct(__p);
859 _M_allocator.destroy(__p);
860 }
861 #else
862 void
863 construct(pointer __p, const value_type& val)
864 { return _M_allocator.construct(__p, val); }
865
866 void
867 destroy(pointer __p)
868 { _M_allocator.destroy(__p); }
869 #endif
870
871 void
872 deallocate(pointer __p, size_type __n)
873 {
874 erase(__p, sizeof(value_type) * __n);
875 _M_allocator.deallocate(__p, __n);
876 }
877
878 void
879 check_allocated(pointer __p, size_type __n)
880 {
881 size_type __t = sizeof(value_type) * __n;
882 annotate_base::check_allocated(__p, __t);
883 }
884
885 void
886 check(size_type __n)
887 { annotate_base::check(__n); }
888 };
889
890 template<typename _Tp, typename _Cond>
891 inline bool
892 operator==(const throw_allocator_base<_Tp, _Cond>&,
893 const throw_allocator_base<_Tp, _Cond>&)
894 { return true; }
895
896 template<typename _Tp, typename _Cond>
897 inline bool
898 operator!=(const throw_allocator_base<_Tp, _Cond>&,
899 const throw_allocator_base<_Tp, _Cond>&)
900 { return false; }
901
902 /// Allocator throwing via limit condition.
903 template<typename _Tp>
904 struct throw_allocator_limit
905 : public throw_allocator_base<_Tp, limit_condition>
906 {
907 template<typename _Tp1>
908 struct rebind
909 { typedef throw_allocator_limit<_Tp1> other; };
910
911 throw_allocator_limit() _GLIBCXX_USE_NOEXCEPT { }
912
913 throw_allocator_limit(const throw_allocator_limit&)
914 _GLIBCXX_USE_NOEXCEPT { }
915
916 template<typename _Tp1>
917 throw_allocator_limit(const throw_allocator_limit<_Tp1>&)
918 _GLIBCXX_USE_NOEXCEPT { }
919
920 ~throw_allocator_limit() _GLIBCXX_USE_NOEXCEPT { }
921 };
922
923 /// Allocator throwing via random condition.
924 template<typename _Tp>
925 struct throw_allocator_random
926 : public throw_allocator_base<_Tp, random_condition>
927 {
928 template<typename _Tp1>
929 struct rebind
930 { typedef throw_allocator_random<_Tp1> other; };
931
932 throw_allocator_random() _GLIBCXX_USE_NOEXCEPT { }
933
934 throw_allocator_random(const throw_allocator_random&)
935 _GLIBCXX_USE_NOEXCEPT { }
936
937 template<typename _Tp1>
938 throw_allocator_random(const throw_allocator_random<_Tp1>&)
939 _GLIBCXX_USE_NOEXCEPT { }
940
941 ~throw_allocator_random() _GLIBCXX_USE_NOEXCEPT { }
942 };
943
944 _GLIBCXX_END_NAMESPACE_VERSION
945 } // namespace
946
947 #if __cplusplus >= 201103L
948
949 # include <bits/functional_hash.h>
950
951 namespace std _GLIBCXX_VISIBILITY(default)
952 {
953 /// Explicit specialization of std::hash for __gnu_cxx::throw_value_limit.
954 template<>
955 struct hash<__gnu_cxx::throw_value_limit>
956 : public std::unary_function<__gnu_cxx::throw_value_limit, size_t>
957 {
958 size_t
959 operator()(const __gnu_cxx::throw_value_limit& __val) const
960 {
961 __gnu_cxx::throw_value_limit::throw_conditionally();
962 std::hash<std::size_t> __h;
963 size_t __result = __h(__val._M_i);
964 return __result;
965 }
966 };
967
968 /// Explicit specialization of std::hash for __gnu_cxx::throw_value_random.
969 template<>
970 struct hash<__gnu_cxx::throw_value_random>
971 : public std::unary_function<__gnu_cxx::throw_value_random, size_t>
972 {
973 size_t
974 operator()(const __gnu_cxx::throw_value_random& __val) const
975 {
976 __gnu_cxx::throw_value_random::throw_conditionally();
977 std::hash<std::size_t> __h;
978 size_t __result = __h(__val._M_i);
979 return __result;
980 }
981 };
982 } // end namespace std
983 #endif
984
985 #endif