46 #ifndef _THROW_ALLOCATOR_H
47 #define _THROW_ALLOCATOR_H 1
60 #if __cplusplus >= 201103L
64 # include <tr1/functional>
65 # include <tr1/random>
69 #if !__has_builtin(__builtin_sprintf)
73 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
75 _GLIBCXX_BEGIN_NAMESPACE_VERSION
86 __throw_forced_error()
101 typedef map_alloc_type::const_iterator const_iterator;
102 typedef map_alloc_type::const_reference const_reference;
103 #if __cplusplus >= 201103L
123 insert(
void* p,
size_t 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());
134 = map_alloc().
insert(entry);
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)); }
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());
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());
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);
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());
313 make_entry(
void* p,
size_t size)
314 {
return std::make_pair(p,
data_type(get_label(), size)); }
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
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);
377 #if __cplusplus >= 201103L
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);
418 #if __cplusplus >= 201103L
438 const size_t _M_orig;
441 adjustor_base() : _M_orig(limit()) { }
444 ~adjustor_base() { set_limit(_M_orig); }
468 throw_conditionally()
470 if (count() == limit())
471 __throw_forced_error();
478 static size_t _S_count(0);
491 set_limit(
const size_t __l)
498 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
510 const double _M_orig;
513 adjustor_base() : _M_orig(probability()) { }
515 virtual ~adjustor_base()
516 { set_probability(_M_orig); }
523 { set_probability(1 -
std::pow(
double(1 - probability()),
524 double(0.5 / (size + 1))));
547 set_probability(
double __p)
548 { probability() = __p; }
551 throw_conditionally()
553 if (generate() < probability())
554 __throw_forced_error();
558 seed(
unsigned long __s)
559 { engine().seed(__s); }
562 #if __cplusplus >= 201103L
566 typedef std::tr1::uniform_real<double> distribution_type;
573 #if __cplusplus >= 201103L
574 const distribution_type distribution(0, 1);
575 static auto generator =
std::bind(distribution, engine());
578 typedef std::tr1::variate_generator<engine_type, distribution_type> gen_t;
579 distribution_type distribution(0, 1);
580 static gen_t generator(engine(), distribution);
583 #if ! __has_builtin(__builtin_sprintf)
584 __typeof__(&std::sprintf) __builtin_sprintf = &std::sprintf;
587 double random = generator();
588 if (random < distribution.min() || random > distribution.max())
592 __s +=
"random number generated is: ";
594 __builtin_sprintf(buf,
"%f", random);
596 std::__throw_out_of_range(__s.c_str());
612 static engine_type _S_e;
616 #endif // _GLIBCXX_USE_C99_STDINT_TR1
624 template<
typename _Cond>
627 typedef _Cond condition_type;
629 using condition_type::throw_conditionally;
633 #ifndef _GLIBCXX_IS_AGGREGATE
635 { throw_conditionally(); }
638 { throw_conditionally(); }
640 #if __cplusplus >= 201103L
646 { throw_conditionally(); }
652 throw_conditionally();
657 #if __cplusplus >= 201103L
666 throw_conditionally();
672 template<
typename _Cond>
677 throw_value::throw_conditionally();
678 throw_value orig(__a);
684 template<
typename _Cond>
686 operator==(
const throw_value_base<_Cond>& __a,
687 const throw_value_base<_Cond>& __b)
689 typedef throw_value_base<_Cond> throw_value;
690 throw_value::throw_conditionally();
691 bool __ret = __a._M_i == __b._M_i;
695 template<
typename _Cond>
697 operator<(
const throw_value_base<_Cond>& __a,
698 const throw_value_base<_Cond>& __b)
700 typedef throw_value_base<_Cond> throw_value;
701 throw_value::throw_conditionally();
702 bool __ret = __a._M_i < __b._M_i;
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)
712 typedef throw_value_base<_Cond> throw_value;
713 throw_value::throw_conditionally();
714 throw_value __ret(__a._M_i + __b._M_i);
718 template<
typename _Cond>
719 inline throw_value_base<_Cond>
720 operator-(
const throw_value_base<_Cond>& __a,
721 const throw_value_base<_Cond>& __b)
723 typedef throw_value_base<_Cond> throw_value;
724 throw_value::throw_conditionally();
725 throw_value __ret(__a._M_i - __b._M_i);
729 template<
typename _Cond>
730 inline throw_value_base<_Cond>
731 operator*(
const throw_value_base<_Cond>& __a,
732 const throw_value_base<_Cond>& __b)
734 typedef throw_value_base<_Cond> throw_value;
735 throw_value::throw_conditionally();
736 throw_value __ret(__a._M_i * __b._M_i);
746 #ifndef _GLIBCXX_IS_AGGREGATE
752 #if __cplusplus >= 201103L
762 base_type::operator=(__other);
766 #if __cplusplus >= 201103L
772 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
778 #ifndef _GLIBCXX_IS_AGGREGATE
784 #if __cplusplus >= 201103L
794 base_type::operator=(__other);
798 #if __cplusplus >= 201103L
803 #endif // _GLIBCXX_USE_C99_STDINT_TR1
812 template<
typename _Tp,
typename _Cond>
817 typedef std::size_t size_type;
818 typedef std::ptrdiff_t difference_type;
819 typedef _Tp value_type;
820 typedef value_type* pointer;
821 typedef const value_type* const_pointer;
822 typedef value_type& reference;
823 typedef const value_type& const_reference;
825 #if __cplusplus >= 201103L
832 typedef _Cond condition_type;
838 using condition_type::throw_conditionally;
842 max_size()
const _GLIBCXX_USE_NOEXCEPT
846 address(reference __x)
const _GLIBCXX_NOEXCEPT
850 address(const_reference __x)
const _GLIBCXX_NOEXCEPT
853 _GLIBCXX_NODISCARD pointer
854 allocate(size_type __n,
const void* __hint = 0)
856 if (__n > this->max_size())
857 std::__throw_bad_alloc();
859 throw_conditionally();
861 insert(a,
sizeof(value_type) * __n);
865 #if __cplusplus >= 201103L
866 template<
typename _Up,
typename... _Args>
868 construct(_Up* __p, _Args&&... __args)
870 traits::construct(_M_allocator, __p, std::forward<_Args>(__args)...);
871 insert_construct(__p);
874 template<
typename _Up>
878 erase_construct(__p);
879 traits::destroy(_M_allocator, __p);
883 construct(pointer __p,
const value_type& __val)
884 {
return _M_allocator.construct(__p, __val); }
888 { _M_allocator.destroy(__p); }
892 deallocate(pointer __p, size_type __n)
894 erase(__p,
sizeof(value_type) * __n);
895 _M_allocator.deallocate(__p, __n);
899 check_allocated(pointer __p, size_type __n)
901 size_type __t =
sizeof(value_type) * __n;
902 annotate_base::check_allocated(__p, __t);
907 { annotate_base::check(__n); }
910 template<
typename _Tp,
typename _Cond>
916 #if __cpp_impl_three_way_comparison < 201907L
917 template<
typename _Tp,
typename _Cond>
919 operator!=(
const throw_allocator_base<_Tp, _Cond>&,
920 const throw_allocator_base<_Tp, _Cond>&)
925 template<
typename _Tp>
929 template<
typename _Tp1>
936 _GLIBCXX_USE_NOEXCEPT { }
938 template<
typename _Tp1>
940 _GLIBCXX_USE_NOEXCEPT { }
944 #if __cplusplus >= 201103L
950 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
952 template<
typename _Tp>
956 template<
typename _Tp1>
963 _GLIBCXX_USE_NOEXCEPT { }
965 template<
typename _Tp1>
967 _GLIBCXX_USE_NOEXCEPT { }
971 #if __cplusplus >= 201103L
976 #endif // _GLIBCXX_USE_C99_STDINT_TR1
978 _GLIBCXX_END_NAMESPACE_VERSION
981 #if __cplusplus >= 201103L
985 namespace std _GLIBCXX_VISIBILITY(default)
987 #pragma GCC diagnostic push
988 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
998 __gnu_cxx::throw_value_limit::throw_conditionally();
1000 size_t __result = __h(__val._M_i);
1005 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
1014 __gnu_cxx::throw_value_random::throw_conditionally();
1016 size_t __result = __h(__val._M_i);
1022 #pragma GCC diagnostic pop