3 // Copyright (C) 2005-2025 Free Software Foundation, Inc.
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
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.
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.
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/>.
25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
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
36 /** @file ext/throw_allocator.h
37 * This file is a GNU extension to the Standard C++ Library.
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.
46 #ifndef _THROW_ALLOCATOR_H
47 #define _THROW_ALLOCATOR_H 1
49 #include <bits/requires_hosted.h> // GNU extensions are currently omitted
58 #include <bits/functexcept.h>
59 #include <bits/move.h>
60 #if __cplusplus >= 201103L
61 # include <functional>
64 # include <tr1/functional>
65 # include <tr1/random>
67 #include <ext/alloc_traits.h>
69 #if !__has_builtin(__builtin_sprintf)
73 namespace __gnu_cxx
_GLIBCXX_VISIBILITY(default)
75 _GLIBCXX_BEGIN_NAMESPACE_VERSION
78 * @brief Thrown by utilities for testing exception safety.
81 struct forced_error
: public std::exception
84 // Substitute for forced_error object when -fno-exceptions.
86 __throw_forced_error()
87 { _GLIBCXX_THROW_OR_ABORT(forced_error()); }
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.
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
;
123 insert(void* p
, size_t size
)
125 entry_type entry
= make_entry(p
, size
);
128 std::string
error("annotate_base::insert null insert!\n");
129 log_to_string(error
, entry
);
130 std::__throw_logic_error(error
.c_str());
133 std::pair
<map_alloc_type::iterator
, bool> inserted
134 = map_alloc().insert(entry
);
135 if (!inserted
.second
)
137 std::string
error("annotate_base::insert double insert!\n");
138 log_to_string(error
, entry
);
139 log_to_string(error
, *inserted
.first
);
140 std::__throw_logic_error(error
.c_str());
145 erase(void* p
, size_t size
)
146 { map_alloc().erase(check_allocated(p
, size
)); }
148 #if __cplusplus >= 201103L
150 insert_construct(void* p
)
154 std::string
error("annotate_base::insert_construct null!\n");
155 std::__throw_logic_error(error
.c_str());
158 auto inserted
= map_construct().insert(std::make_pair(p
, get_label()));
159 if (!inserted
.second
)
161 std::string
error("annotate_base::insert_construct double insert!\n");
162 log_to_string(error
, std::make_pair(p
, get_label()));
163 log_to_string(error
, *inserted
.first
);
164 std::__throw_logic_error(error
.c_str());
169 erase_construct(void* p
)
170 { map_construct().erase(check_constructed(p
)); }
173 // See if a particular address and allocation size has been saved.
174 inline map_alloc_type::iterator
175 check_allocated(void* p
, size_t size
)
177 map_alloc_type::iterator found
= map_alloc().find(p
);
178 if (found
== map_alloc().end())
180 std::string
error("annotate_base::check_allocated by value "
182 log_to_string(error
, make_entry(p
, size
));
183 std::__throw_logic_error(error
.c_str());
186 if (found
->second
.second
!= size
)
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());
198 // See if a given label has been allocated.
204 const_iterator beg
= map_alloc().begin();
205 const_iterator end
= map_alloc().end();
208 if (beg
->second
.first
== label
)
209 log_to_string(found
, *beg
);
214 #if __cplusplus >= 201103L
216 auto beg
= map_construct().begin();
217 auto end
= map_construct().end();
220 if (beg
->second
== label
)
221 log_to_string(found
, *beg
);
229 std::string
error("annotate_base::check by label\n");
231 std::__throw_logic_error(error
.c_str());
235 // See if there is anything left allocated or constructed.
241 const_iterator beg
= map_alloc().begin();
242 const_iterator end
= map_alloc().end();
245 log_to_string(found
, *beg
);
250 #if __cplusplus >= 201103L
252 auto beg
= map_construct().begin();
253 auto end
= map_construct().end();
256 log_to_string(found
, *beg
);
264 std::string
error("annotate_base::check \n");
266 std::__throw_logic_error(error
.c_str());
270 #if __cplusplus >= 201103L
271 inline map_construct_type::iterator
272 check_constructed(void* p
)
274 auto found
= map_construct().find(p
);
275 if (found
== map_construct().end())
277 std::string
error("annotate_base::check_constructed not "
279 log_to_string(error
, std::make_pair(p
, get_label()));
280 std::__throw_logic_error(error
.c_str());
287 check_constructed(size_t label
)
289 auto beg
= map_construct().begin();
290 auto end
= map_construct().end();
294 if (beg
->second
== label
)
295 log_to_string(found
, *beg
);
301 std::string
error("annotate_base::check_constructed by label\n");
303 std::__throw_logic_error(error
.c_str());
310 operator<<(std::ostream
&, const annotate_base
&);
313 make_entry(void* p
, size_t size
)
314 { return std::make_pair(p
, data_type(get_label(), size
)); }
317 log_to_string(std::string
& s
, const_reference ref
)
319 #if ! __has_builtin(__builtin_sprintf)
320 __typeof__(&std::sprintf
) __builtin_sprintf
= &std::sprintf
;
324 const char tab('\t');
326 unsigned long l
= static_cast<unsigned long>(ref
.second
.first
);
327 __builtin_sprintf(buf
, "%lu", l
);
331 l
= static_cast<unsigned long>(ref
.second
.second
);
332 __builtin_sprintf(buf
, "%lu", l
);
336 __builtin_sprintf(buf
, "%p", ref
.first
);
341 #if __cplusplus >= 201103L
343 log_to_string(std::string
& s
, const std::pair
<const void*, size_t>& ref
)
345 #if ! __has_builtin(__builtin_sprintf)
346 auto __builtin_sprintf
= &std::sprintf
;
350 const char tab('\t');
352 unsigned long l
= static_cast<unsigned long>(ref
.second
);
353 __builtin_sprintf(buf
, "%lu", l
);
357 __builtin_sprintf(buf
, "%p", ref
.first
);
366 static size_t _S_label(std::numeric_limits
<size_t>::max());
370 static map_alloc_type
&
373 static map_alloc_type _S_map
;
377 #if __cplusplus >= 201103L
378 static map_construct_type
&
381 static map_construct_type _S_map
;
388 operator<<(std::ostream
& os
, const annotate_base
& __b
)
391 typedef annotate_base base_type
;
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
);
398 #if __cplusplus >= 201103L
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
);
411 * @brief Base struct for condition policy.
413 * Requires a public member function with the signature
414 * void throw_conditionally()
416 struct condition_base
418 #if __cplusplus >= 201103L
419 condition_base() = default;
420 condition_base(const condition_base
&) = default;
421 condition_base
& operator=(const condition_base
&) = default;
423 virtual ~condition_base() { };
428 * @brief Base class for incremental control and throw.
430 struct limit_condition
: public condition_base
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.
438 const size_t _M_orig
;
441 adjustor_base() : _M_orig(limit()) { }
444 ~adjustor_base() { set_limit(_M_orig
); }
447 /// Never enter the condition.
448 struct never_adjustor
: public adjustor_base
450 never_adjustor() { set_limit(std::numeric_limits
<size_t>::max()); }
453 /// Always enter the condition.
454 struct always_adjustor
: public adjustor_base
456 always_adjustor() { set_limit(count()); }
459 /// Enter the nth condition.
460 struct limit_adjustor
: public adjustor_base
462 limit_adjustor(const size_t __l
) { set_limit(__l
); }
465 // Increment _S_count every time called.
466 // If _S_count matches the limit count, throw.
468 throw_conditionally()
470 if (count() == limit())
471 __throw_forced_error();
478 static size_t _S_count(0);
485 static size_t _S_limit(std::numeric_limits
<size_t>::max());
489 // Zero the throw counter, set limit to argument.
491 set_limit(const size_t __l
)
499 * @brief Base class for random probability control and throw.
501 struct random_condition
: public condition_base
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.
509 const double _M_orig
;
512 adjustor_base() : _M_orig(probability()) { }
514 virtual ~adjustor_base()
515 { set_probability(_M_orig
); }
519 struct group_adjustor
: public adjustor_base
521 group_adjustor(size_t size
)
522 { set_probability(1 - std::pow(double(1 - probability()),
523 double(0.5 / (size
+ 1))));
527 /// Never enter the condition.
528 struct never_adjustor
: public adjustor_base
530 never_adjustor() { set_probability(0); }
533 /// Always enter the condition.
534 struct always_adjustor
: public adjustor_base
536 always_adjustor() { set_probability(1); }
546 set_probability(double __p
)
547 { probability() = __p
; }
550 throw_conditionally()
552 if (generate() < probability())
553 __throw_forced_error();
557 seed(unsigned long __s
)
558 { engine().seed(__s
); }
561 #if __cplusplus >= 201103L
562 typedef std::uniform_real_distribution
<double> distribution_type
;
563 typedef std::mt19937 engine_type
;
565 typedef std::tr1::uniform_real
<double> distribution_type
;
566 typedef std::tr1::mt19937 engine_type
;
572 #if __cplusplus >= 201103L
573 const distribution_type
distribution(0, 1);
574 static auto generator
= std::bind(distribution
, engine());
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
);
582 #if ! __has_builtin(__builtin_sprintf)
583 __typeof__(&std::sprintf
) __builtin_sprintf
= &std::sprintf
;
586 double random
= generator();
587 if (random
< distribution
.min() || random
> distribution
.max())
589 std::string
__s("random_condition::generate");
591 __s
+= "random number generated is: ";
593 __builtin_sprintf(buf
, "%f", random
);
595 std::__throw_out_of_range(__s
.c_str());
611 static engine_type _S_e
;
617 * @brief Class with exception generation control. Intended to be
618 * used as a value_type in templatized code.
620 * Note: Destructor not allowed to throw.
622 template<typename _Cond
>
623 struct throw_value_base
: public _Cond
625 typedef _Cond condition_type
;
627 using condition_type::throw_conditionally
;
631 #ifndef _GLIBCXX_IS_AGGREGATE
632 throw_value_base() : _M_i(0)
633 { throw_conditionally(); }
635 throw_value_base(const throw_value_base
& __v
) : _M_i(__v
._M_i
)
636 { throw_conditionally(); }
638 #if __cplusplus >= 201103L
640 throw_value_base(throw_value_base
&&) = default;
643 explicit throw_value_base(const std::size_t __i
) : _M_i(__i
)
644 { throw_conditionally(); }
648 operator=(const throw_value_base
& __v
)
650 throw_conditionally();
655 #if __cplusplus >= 201103L
658 operator=(throw_value_base
&&) = default;
664 throw_conditionally();
670 template<typename _Cond
>
672 swap(throw_value_base
<_Cond
>& __a
, throw_value_base
<_Cond
>& __b
)
674 typedef throw_value_base
<_Cond
> throw_value
;
675 throw_value::throw_conditionally();
676 throw_value
orig(__a
);
681 // General instantiable types requirements.
682 template<typename _Cond
>
684 operator==(const throw_value_base
<_Cond
>& __a
,
685 const throw_value_base
<_Cond
>& __b
)
687 typedef throw_value_base
<_Cond
> throw_value
;
688 throw_value::throw_conditionally();
689 bool __ret
= __a
._M_i
== __b
._M_i
;
693 template<typename _Cond
>
695 operator<(const throw_value_base
<_Cond
>& __a
,
696 const throw_value_base
<_Cond
>& __b
)
698 typedef throw_value_base
<_Cond
> throw_value
;
699 throw_value::throw_conditionally();
700 bool __ret
= __a
._M_i
< __b
._M_i
;
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
)
710 typedef throw_value_base
<_Cond
> throw_value
;
711 throw_value::throw_conditionally();
712 throw_value
__ret(__a
._M_i
+ __b
._M_i
);
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
)
721 typedef throw_value_base
<_Cond
> throw_value
;
722 throw_value::throw_conditionally();
723 throw_value
__ret(__a
._M_i
- __b
._M_i
);
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
)
732 typedef throw_value_base
<_Cond
> throw_value
;
733 throw_value::throw_conditionally();
734 throw_value
__ret(__a
._M_i
* __b
._M_i
);
739 /// Type throwing via limit condition.
740 struct throw_value_limit
: public throw_value_base
<limit_condition
>
742 typedef throw_value_base
<limit_condition
> base_type
;
744 #ifndef _GLIBCXX_IS_AGGREGATE
745 throw_value_limit() { }
747 throw_value_limit(const throw_value_limit
& __other
)
748 : base_type(__other
._M_i
) { }
750 #if __cplusplus >= 201103L
751 throw_value_limit(throw_value_limit
&&) = default;
754 explicit throw_value_limit(const std::size_t __i
) : base_type(__i
) { }
758 operator=(const throw_value_limit
& __other
)
760 base_type::operator=(__other
);
764 #if __cplusplus >= 201103L
766 operator=(throw_value_limit
&&) = default;
770 /// Type throwing via random condition.
771 struct throw_value_random
: public throw_value_base
<random_condition
>
773 typedef throw_value_base
<random_condition
> base_type
;
775 #ifndef _GLIBCXX_IS_AGGREGATE
776 throw_value_random() { }
778 throw_value_random(const throw_value_random
& __other
)
779 : base_type(__other
._M_i
) { }
781 #if __cplusplus >= 201103L
782 throw_value_random(throw_value_random
&&) = default;
785 explicit throw_value_random(const std::size_t __i
) : base_type(__i
) { }
789 operator=(const throw_value_random
& __other
)
791 base_type::operator=(__other
);
795 #if __cplusplus >= 201103L
797 operator=(throw_value_random
&&) = default;
802 * @brief Allocator class with logging and exception generation control.
803 * Intended to be used as an allocator_type in templatized code.
804 * @ingroup allocators
806 * Note: Deallocate not allowed to throw.
808 template<typename _Tp
, typename _Cond
>
809 class throw_allocator_base
810 : public annotate_base
, public _Cond
813 typedef std::size_t size_type
;
814 typedef std::ptrdiff_t difference_type
;
815 typedef _Tp value_type
;
816 typedef value_type
* pointer
;
817 typedef const value_type
* const_pointer
;
818 typedef value_type
& reference
;
819 typedef const value_type
& const_reference
;
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
;
828 typedef _Cond condition_type
;
830 std::allocator
<value_type
> _M_allocator
;
832 typedef __gnu_cxx::__alloc_traits
<std::allocator
<value_type
> > traits
;
834 using condition_type::throw_conditionally
;
838 max_size() const _GLIBCXX_USE_NOEXCEPT
839 { return traits::max_size(_M_allocator
); }
842 address(reference __x
) const _GLIBCXX_NOEXCEPT
843 { return std::__addressof(__x
); }
846 address(const_reference __x
) const _GLIBCXX_NOEXCEPT
847 { return std::__addressof(__x
); }
849 _GLIBCXX_NODISCARD pointer
850 allocate(size_type __n
, const void* __hint
= 0)
852 if (__n
> this->max_size())
853 std::__throw_bad_alloc();
855 throw_conditionally();
856 pointer
const a
= traits::allocate(_M_allocator
, __n
, __hint
);
857 insert(a
, sizeof(value_type
) * __n
);
861 #if __cplusplus >= 201103L
862 template<typename _Up
, typename
... _Args
>
864 construct(_Up
* __p
, _Args
&&... __args
)
866 traits::construct(_M_allocator
, __p
, std::forward
<_Args
>(__args
)...);
867 insert_construct(__p
);
870 template<typename _Up
>
874 erase_construct(__p
);
875 traits::destroy(_M_allocator
, __p
);
879 construct(pointer __p
, const value_type
& __val
)
880 { return _M_allocator
.construct(__p
, __val
); }
884 { _M_allocator
.destroy(__p
); }
888 deallocate(pointer __p
, size_type __n
)
890 erase(__p
, sizeof(value_type
) * __n
);
891 _M_allocator
.deallocate(__p
, __n
);
895 check_allocated(pointer __p
, size_type __n
)
897 size_type __t
= sizeof(value_type
) * __n
;
898 annotate_base::check_allocated(__p
, __t
);
903 { annotate_base::check(__n
); }
906 template<typename _Tp
, typename _Cond
>
908 operator==(const throw_allocator_base
<_Tp
, _Cond
>&,
909 const throw_allocator_base
<_Tp
, _Cond
>&)
912 #if __cpp_impl_three_way_comparison < 201907L
913 template<typename _Tp
, typename _Cond
>
915 operator!=(const throw_allocator_base
<_Tp
, _Cond
>&,
916 const throw_allocator_base
<_Tp
, _Cond
>&)
920 /// Allocator throwing via limit condition.
921 template<typename _Tp
>
922 struct throw_allocator_limit
923 : public throw_allocator_base
<_Tp
, limit_condition
>
925 template<typename _Tp1
>
927 { typedef throw_allocator_limit
<_Tp1
> other
; };
929 throw_allocator_limit() _GLIBCXX_USE_NOEXCEPT
{ }
931 throw_allocator_limit(const throw_allocator_limit
&)
932 _GLIBCXX_USE_NOEXCEPT
{ }
934 template<typename _Tp1
>
935 throw_allocator_limit(const throw_allocator_limit
<_Tp1
>&)
936 _GLIBCXX_USE_NOEXCEPT
{ }
938 ~throw_allocator_limit() _GLIBCXX_USE_NOEXCEPT
{ }
940 #if __cplusplus >= 201103L
941 throw_allocator_limit
&
942 operator=(const throw_allocator_limit
&) = default;
946 /// Allocator throwing via random condition.
947 template<typename _Tp
>
948 struct throw_allocator_random
949 : public throw_allocator_base
<_Tp
, random_condition
>
951 template<typename _Tp1
>
953 { typedef throw_allocator_random
<_Tp1
> other
; };
955 throw_allocator_random() _GLIBCXX_USE_NOEXCEPT
{ }
957 throw_allocator_random(const throw_allocator_random
&)
958 _GLIBCXX_USE_NOEXCEPT
{ }
960 template<typename _Tp1
>
961 throw_allocator_random(const throw_allocator_random
<_Tp1
>&)
962 _GLIBCXX_USE_NOEXCEPT
{ }
964 ~throw_allocator_random() _GLIBCXX_USE_NOEXCEPT
{ }
966 #if __cplusplus >= 201103L
967 throw_allocator_random
&
968 operator=(const throw_allocator_random
&) = default;
972 _GLIBCXX_END_NAMESPACE_VERSION
975 #if __cplusplus >= 201103L
977 # include <bits/functional_hash.h>
979 namespace std
_GLIBCXX_VISIBILITY(default)
981 #pragma GCC diagnostic push
982 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
984 /// Explicit specialization of std::hash for __gnu_cxx::throw_value_limit.
986 struct hash
<__gnu_cxx::throw_value_limit
>
987 : public std::unary_function
<__gnu_cxx::throw_value_limit
, size_t>
990 operator()(const __gnu_cxx::throw_value_limit
& __val
) const
992 __gnu_cxx::throw_value_limit::throw_conditionally();
993 std::hash
<std::size_t> __h
;
994 size_t __result
= __h(__val
._M_i
);
999 /// Explicit specialization of std::hash for __gnu_cxx::throw_value_random.
1001 struct hash
<__gnu_cxx::throw_value_random
>
1002 : public std::unary_function
<__gnu_cxx::throw_value_random
, size_t>
1005 operator()(const __gnu_cxx::throw_value_random
& __val
) const
1007 __gnu_cxx::throw_value_random::throw_conditionally();
1008 std::hash
<std::size_t> __h
;
1009 size_t __result
= __h(__val
._M_i
);
1014 #pragma GCC diagnostic pop
1015 } // end namespace std