46#ifndef _THROW_ALLOCATOR_H
47#define _THROW_ALLOCATOR_H 1
58#if __cplusplus >= 201103L
62# include <tr1/functional>
67#if !__has_builtin(__builtin_sprintf)
71namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
73_GLIBCXX_BEGIN_NAMESPACE_VERSION
84 __throw_forced_error()
98 typedef map_alloc_type::value_type entry_type;
99 typedef map_alloc_type::const_iterator const_iterator;
100 typedef map_alloc_type::const_reference const_reference;
101#if __cplusplus >= 201103L
121 insert(
void* p,
size_t size)
123 entry_type entry = make_entry(p, size);
126 std::string error(
"annotate_base::insert null insert!\n");
127 log_to_string(error, entry);
128 std::__throw_logic_error(error.
c_str());
132 = map_alloc().insert(entry);
135 std::string error(
"annotate_base::insert double insert!\n");
136 log_to_string(error, entry);
137 log_to_string(error, *inserted.
first);
138 std::__throw_logic_error(error.
c_str());
143 erase(
void* p,
size_t size)
144 { map_alloc().erase(check_allocated(p, size)); }
146#if __cplusplus >= 201103L
148 insert_construct(
void* p)
152 std::string error(
"annotate_base::insert_construct null!\n");
153 std::__throw_logic_error(error.
c_str());
156 auto inserted = map_construct().insert(
std::make_pair(p, get_label()));
157 if (!inserted.second)
159 std::string error(
"annotate_base::insert_construct double insert!\n");
161 log_to_string(error, *inserted.first);
162 std::__throw_logic_error(error.
c_str());
167 erase_construct(
void* p)
168 { map_construct().erase(check_constructed(p)); }
172 inline map_alloc_type::iterator
173 check_allocated(
void* p,
size_t size)
175 map_alloc_type::iterator found = map_alloc().find(p);
176 if (found == map_alloc().end())
178 std::string error(
"annotate_base::check_allocated by value "
180 log_to_string(error, make_entry(p, size));
181 std::__throw_logic_error(error.
c_str());
184 if (found->second.second != size)
186 std::string error(
"annotate_base::check_allocated by value "
187 "wrong-size erase!\n");
188 log_to_string(error, make_entry(p, size));
189 log_to_string(error, *found);
190 std::__throw_logic_error(error.
c_str());
202 const_iterator beg = map_alloc().begin();
203 const_iterator end = map_alloc().end();
206 if (beg->second.first == label)
207 log_to_string(found, *beg);
212#if __cplusplus >= 201103L
214 auto beg = map_construct().begin();
215 auto end = map_construct().end();
218 if (beg->second == label)
219 log_to_string(found, *beg);
227 std::string error(
"annotate_base::check by label\n");
229 std::__throw_logic_error(error.
c_str());
239 const_iterator beg = map_alloc().begin();
240 const_iterator end = map_alloc().end();
243 log_to_string(found, *beg);
248#if __cplusplus >= 201103L
250 auto beg = map_construct().begin();
251 auto end = map_construct().end();
254 log_to_string(found, *beg);
264 std::__throw_logic_error(error.
c_str());
268#if __cplusplus >= 201103L
269 inline map_construct_type::iterator
270 check_constructed(
void* p)
272 auto found = map_construct().find(p);
273 if (found == map_construct().end())
275 std::string error(
"annotate_base::check_constructed not "
278 std::__throw_logic_error(error.
c_str());
285 check_constructed(
size_t label)
287 auto beg = map_construct().begin();
288 auto end = map_construct().end();
292 if (beg->second == label)
293 log_to_string(found, *beg);
299 std::string error(
"annotate_base::check_constructed by label\n");
301 std::__throw_logic_error(error.
c_str());
311 make_entry(
void* p,
size_t size)
317#if ! __has_builtin(__builtin_sprintf)
318 __typeof__(&std::sprintf) __builtin_sprintf = &std::sprintf;
322 const char tab(
'\t');
324 unsigned long l =
static_cast<unsigned long>(ref.second.first);
325 __builtin_sprintf(buf,
"%lu", l);
329 l =
static_cast<unsigned long>(ref.second.second);
330 __builtin_sprintf(buf,
"%lu", l);
334 __builtin_sprintf(buf,
"%p", ref.first);
339#if __cplusplus >= 201103L
343#if ! __has_builtin(__builtin_sprintf)
344 auto __builtin_sprintf = &std::sprintf;
348 const char tab(
'\t');
350 unsigned long l =
static_cast<unsigned long>(ref.second);
351 __builtin_sprintf(buf,
"%lu", l);
355 __builtin_sprintf(buf,
"%p", ref.first);
368 static map_alloc_type&
371 static map_alloc_type _S_map;
375#if __cplusplus >= 201103L
376 static map_construct_type&
379 static map_construct_type _S_map;
391 base_type::const_iterator beg = __b.map_alloc().
begin();
392 base_type::const_iterator end = __b.map_alloc().
end();
393 for (; beg != end; ++beg)
394 __b.log_to_string(error, *beg);
396#if __cplusplus >= 201103L
398 auto beg = __b.map_construct().
begin();
399 auto end = __b.map_construct().
end();
400 for (; beg != end; ++beg)
401 __b.log_to_string(error, *beg);
414 struct condition_base
416#if __cplusplus >= 201103L
417 condition_base() =
default;
418 condition_base(
const condition_base&) =
default;
419 condition_base& operator=(
const condition_base&) =
default;
421 virtual ~condition_base() { };
436 const size_t _M_orig;
439 adjustor_base() : _M_orig(limit()) { }
442 ~adjustor_base() { set_limit(_M_orig); }
446 struct never_adjustor :
public adjustor_base
452 struct always_adjustor :
public adjustor_base
454 always_adjustor() { set_limit(count()); }
458 struct limit_adjustor :
public adjustor_base
460 limit_adjustor(
const size_t __l) { set_limit(__l); }
466 throw_conditionally()
468 if (count() == limit())
469 __throw_forced_error();
476 static size_t _S_count(0);
489 set_limit(
const size_t __l)
496#ifdef _GLIBCXX_USE_C99_STDINT_TR1
500 struct random_condition :
public condition_base
508 const double _M_orig;
511 adjustor_base() : _M_orig(probability()) { }
513 virtual ~adjustor_base()
514 { set_probability(_M_orig); }
518 struct group_adjustor :
public adjustor_base
520 group_adjustor(
size_t size)
521 { set_probability(1 -
std::pow(
double(1 - probability()),
522 double(0.5 / (size + 1))));
527 struct never_adjustor :
public adjustor_base
529 never_adjustor() { set_probability(0); }
533 struct always_adjustor :
public adjustor_base
535 always_adjustor() { set_probability(1); }
545 set_probability(
double __p)
546 { probability() = __p; }
549 throw_conditionally()
551 if (generate() < probability())
552 __throw_forced_error();
556 seed(
unsigned long __s)
557 { engine().seed(__s); }
560#if __cplusplus >= 201103L
561 typedef std::uniform_real_distribution<double> distribution_type;
564 typedef std::tr1::uniform_real<double> distribution_type;
565 typedef std::tr1::mt19937 engine_type;
571#if __cplusplus >= 201103L
572 const distribution_type distribution(0, 1);
573 static auto generator =
std::bind(distribution, engine());
576 typedef std::tr1::variate_generator<engine_type, distribution_type> gen_t;
577 distribution_type distribution(0, 1);
578 static gen_t generator(engine(), distribution);
581#if ! __has_builtin(__builtin_sprintf)
582 __typeof__(&std::sprintf) __builtin_sprintf = &std::sprintf;
585 double random = generator();
586 if (random < distribution.min() || random > distribution.max())
590 __s +=
"random number generated is: ";
592 __builtin_sprintf(buf,
"%f", random);
594 std::__throw_out_of_range(__s.c_str());
610 static engine_type _S_e;
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>
675 throw_value::throw_conditionally();
676 throw_value orig(__a);
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>
699 throw_value::throw_conditionally();
700 bool __ret = __a._M_i < __b._M_i;
705 template<
typename _Cond>
711 throw_value::throw_conditionally();
712 throw_value __ret(__a._M_i + __b._M_i);
716 template<
typename _Cond>
722 throw_value::throw_conditionally();
723 throw_value __ret(__a._M_i - __b._M_i);
727 template<
typename _Cond>
733 throw_value::throw_conditionally();
734 throw_value __ret(__a._M_i * __b._M_i);
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#ifdef _GLIBCXX_USE_C99_STDINT_TR1
772 struct throw_value_random :
public throw_value_base<random_condition>
774 typedef throw_value_base<random_condition> base_type;
776#ifndef _GLIBCXX_IS_AGGREGATE
777 throw_value_random() { }
779 throw_value_random(
const throw_value_random& __other)
780 : base_type(__other._M_i) { }
782#if __cplusplus >= 201103L
783 throw_value_random(throw_value_random&&) =
default;
786 explicit throw_value_random(
const std::size_t __i) : base_type(__i) { }
790 operator=(
const throw_value_random& __other)
792 base_type::operator=(__other);
796#if __cplusplus >= 201103L
798 operator=(throw_value_random&&) =
default;
810 template<
typename _Tp,
typename _Cond>
812 :
public annotate_base,
public _Cond
815 typedef std::size_t size_type;
816 typedef std::ptrdiff_t difference_type;
817 typedef _Tp value_type;
818 typedef value_type* pointer;
819 typedef const value_type* const_pointer;
820 typedef value_type& reference;
821 typedef const value_type& const_reference;
823#if __cplusplus >= 201103L
830 typedef _Cond condition_type;
836 using condition_type::throw_conditionally;
840 max_size()
const _GLIBCXX_USE_NOEXCEPT
844 address(reference __x)
const _GLIBCXX_NOEXCEPT
848 address(const_reference __x)
const _GLIBCXX_NOEXCEPT
851 _GLIBCXX_NODISCARD pointer
852 allocate(size_type __n,
const void* __hint = 0)
854 if (__n > this->max_size())
855 std::__throw_bad_alloc();
857 throw_conditionally();
859 insert(a,
sizeof(value_type) * __n);
863#if __cplusplus >= 201103L
864 template<
typename _Up,
typename... _Args>
866 construct(_Up* __p, _Args&&... __args)
869 insert_construct(__p);
872 template<
typename _Up>
876 erase_construct(__p);
877 traits::destroy(_M_allocator, __p);
881 construct(pointer __p,
const value_type& __val)
882 {
return _M_allocator.construct(__p, __val); }
886 { _M_allocator.destroy(__p); }
890 deallocate(pointer __p, size_type __n)
892 erase(__p,
sizeof(value_type) * __n);
893 _M_allocator.deallocate(__p, __n);
897 check_allocated(pointer __p, size_type __n)
899 size_type __t =
sizeof(value_type) * __n;
900 annotate_base::check_allocated(__p, __t);
905 { annotate_base::check(__n); }
908 template<
typename _Tp,
typename _Cond>
914#if __cpp_impl_three_way_comparison < 201907L
915 template<
typename _Tp,
typename _Cond>
917 operator!=(
const throw_allocator_base<_Tp, _Cond>&,
918 const throw_allocator_base<_Tp, _Cond>&)
923 template<
typename _Tp>
924 struct throw_allocator_limit
927 template<
typename _Tp1>
929 {
typedef throw_allocator_limit<_Tp1>
other; };
931 throw_allocator_limit() _GLIBCXX_USE_NOEXCEPT { }
933 throw_allocator_limit(
const throw_allocator_limit&)
934 _GLIBCXX_USE_NOEXCEPT { }
936 template<
typename _Tp1>
937 throw_allocator_limit(
const throw_allocator_limit<_Tp1>&)
938 _GLIBCXX_USE_NOEXCEPT { }
940 ~throw_allocator_limit() _GLIBCXX_USE_NOEXCEPT { }
942#if __cplusplus >= 201103L
943 throw_allocator_limit&
944 operator=(
const throw_allocator_limit&) =
default;
948#ifdef _GLIBCXX_USE_C99_STDINT_TR1
950 template<
typename _Tp>
951 struct throw_allocator_random
954 template<
typename _Tp1>
956 {
typedef throw_allocator_random<_Tp1>
other; };
958 throw_allocator_random() _GLIBCXX_USE_NOEXCEPT { }
960 throw_allocator_random(
const throw_allocator_random&)
961 _GLIBCXX_USE_NOEXCEPT { }
963 template<
typename _Tp1>
964 throw_allocator_random(
const throw_allocator_random<_Tp1>&)
965 _GLIBCXX_USE_NOEXCEPT { }
967 ~throw_allocator_random() _GLIBCXX_USE_NOEXCEPT { }
969#if __cplusplus >= 201103L
970 throw_allocator_random&
971 operator=(
const throw_allocator_random&) =
default;
976_GLIBCXX_END_NAMESPACE_VERSION
979#if __cplusplus >= 201103L
983namespace std _GLIBCXX_VISIBILITY(default)
985#pragma GCC diagnostic push
986#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
996 __gnu_cxx::throw_value_limit::throw_conditionally();
998 size_t __result = __h(__val._M_i);
1003#ifdef _GLIBCXX_USE_C99_STDINT_TR1
1012 __gnu_cxx::throw_value_random::throw_conditionally();
1014 size_t __result = __h(__val._M_i);
1020#pragma GCC diagnostic pop
complex< _Tp > pow(const complex< _Tp > &, int)
Return x to the y'th power.
basic_ostream< char > ostream
Base class for char output streams.
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
constexpr pair< typename __decay_and_strip< _T1 >::__type, typename __decay_and_strip< _T2 >::__type > make_pair(_T1 &&__x, _T2 &&__y)
A convenience wrapper for creating a pair from two objects.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
basic_string< char > string
A string of char.
constexpr _Bind_helper< __is_socketlike< _Func >::value, _Func, _BoundArgs... >::type bind(_Func &&__f, _BoundArgs &&... __args)
Function template for std::bind.
mersenne_twister_engine< uint_fast32_t, 32, 624, 397, 31, 0x9908b0dfUL, 11, 0xffffffffUL, 7, 0x9d2c5680UL, 15, 0xefc60000UL, 18, 1812433253UL > mt19937
ISO C++ entities toplevel namespace is std.
GNU extensions for public use.
static constexpr _Tp max() noexcept
Primary class template hash.
The standard allocator, as per C++03 [20.4.1].
constexpr const _CharT * c_str() const noexcept
Return const pointer to null-terminated contents.
constexpr bool empty() const noexcept
Base class for all library exceptions.
Struct holding two objects of arbitrary type.
_T1 first
The first member.
_T2 second
The second member.
A standard container made up of (key,value) pairs, which can be retrieved based on a key,...
iterator begin() noexcept
Uniform interface to C++98 and C++11 allocators.
static constexpr pointer allocate(std::allocator< value_type > &__a, size_type __n)
static constexpr size_type max_size(const std::allocator< value_type > &__a) noexcept
Thrown by utilities for testing exception safety.
Base class for checking address and label information about allocations. Create a std::map between th...
Base class for incremental control and throw.
Class with exception generation control. Intended to be used as a value_type in templatized code.
Type throwing via limit condition.
Type throwing via random condition.
Allocator class with logging and exception generation control. Intended to be used as an allocator_ty...