46 #ifndef _THROW_ALLOCATOR_H
47 #define _THROW_ALLOCATOR_H 1
58 #if __cplusplus >= 201103L
62 # include <tr1/functional>
63 # include <tr1/random>
67 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
69 _GLIBCXX_BEGIN_NAMESPACE_VERSION
80 __throw_forced_error()
95 typedef map_alloc_type::const_iterator const_iterator;
96 typedef map_alloc_type::const_reference const_reference;
97 #if __cplusplus >= 201103L
117 insert(
void* p,
size_t size)
122 std::string error(
"annotate_base::insert null insert!\n");
123 log_to_string(error, entry);
124 std::__throw_logic_error(error.
c_str());
128 = map_alloc().
insert(entry);
131 std::string error(
"annotate_base::insert double insert!\n");
132 log_to_string(error, entry);
133 log_to_string(error, *inserted.
first);
134 std::__throw_logic_error(error.
c_str());
139 erase(
void* p,
size_t size)
140 { map_alloc().
erase(check_allocated(p, size)); }
142 #if __cplusplus >= 201103L
144 insert_construct(
void* p)
148 std::string error(
"annotate_base::insert_construct null!\n");
149 std::__throw_logic_error(error.
c_str());
152 auto inserted = map_construct().
insert(std::make_pair(p, get_label()));
153 if (!inserted.second)
155 std::string error(
"annotate_base::insert_construct double insert!\n");
156 log_to_string(error, std::make_pair(p, get_label()));
157 log_to_string(error, *inserted.first);
158 std::__throw_logic_error(error.
c_str());
163 erase_construct(
void* p)
164 { map_construct().
erase(check_constructed(p)); }
168 inline map_alloc_type::iterator
169 check_allocated(
void* p,
size_t size)
171 map_alloc_type::iterator found = map_alloc().
find(p);
172 if (found == map_alloc().
end())
174 std::string error(
"annotate_base::check_allocated by value "
176 log_to_string(error, make_entry(p, size));
177 std::__throw_logic_error(error.
c_str());
180 if (found->second.second != size)
182 std::string error(
"annotate_base::check_allocated by value "
183 "wrong-size erase!\n");
184 log_to_string(error, make_entry(p, size));
185 log_to_string(error, *found);
186 std::__throw_logic_error(error.
c_str());
198 const_iterator beg = map_alloc().
begin();
199 const_iterator
end = map_alloc().
end();
202 if (beg->second.first == label)
203 log_to_string(found, *beg);
208 #if __cplusplus >= 201103L
210 auto beg = map_construct().
begin();
211 auto end = map_construct().
end();
214 if (beg->second == label)
215 log_to_string(found, *beg);
223 std::string error(
"annotate_base::check by label\n");
225 std::__throw_logic_error(error.
c_str());
235 const_iterator beg = map_alloc().
begin();
236 const_iterator
end = map_alloc().
end();
239 log_to_string(found, *beg);
244 #if __cplusplus >= 201103L
246 auto beg = map_construct().
begin();
247 auto end = map_construct().
end();
250 log_to_string(found, *beg);
260 std::__throw_logic_error(error.
c_str());
264 #if __cplusplus >= 201103L
265 inline map_construct_type::iterator
266 check_constructed(
void* p)
268 auto found = map_construct().
find(p);
269 if (found == map_construct().
end())
271 std::string error(
"annotate_base::check_constructed not "
273 log_to_string(error, std::make_pair(p, get_label()));
274 std::__throw_logic_error(error.
c_str());
281 check_constructed(
size_t label)
283 auto beg = map_construct().
begin();
284 auto end = map_construct().
end();
288 if (beg->second == label)
289 log_to_string(found, *beg);
295 std::string error(
"annotate_base::check_constructed by label\n");
297 std::__throw_logic_error(error.
c_str());
307 make_entry(
void* p,
size_t size)
308 {
return std::make_pair(p,
data_type(get_label(), size)); }
314 const char tab(
'\t');
316 unsigned long l =
static_cast<unsigned long>(ref.second.first);
317 __builtin_sprintf(buf,
"%lu", l);
321 l =
static_cast<unsigned long>(ref.second.second);
322 __builtin_sprintf(buf,
"%lu", l);
326 __builtin_sprintf(buf,
"%p", ref.first);
331 #if __cplusplus >= 201103L
336 const char tab(
'\t');
338 unsigned long l =
static_cast<unsigned long>(ref.
second);
339 __builtin_sprintf(buf,
"%lu", l);
343 __builtin_sprintf(buf,
"%p", ref.
first);
363 #if __cplusplus >= 201103L
379 base_type::const_iterator beg = __b.map_alloc().
begin();
380 base_type::const_iterator
end = __b.map_alloc().
end();
381 for (; beg !=
end; ++beg)
382 __b.log_to_string(error, *beg);
384 #if __cplusplus >= 201103L
386 auto beg = __b.map_construct().
begin();
387 auto end = __b.map_construct().
end();
388 for (; beg !=
end; ++beg)
389 __b.log_to_string(error, *beg);
404 #if __cplusplus >= 201103L
424 const size_t _M_orig;
427 adjustor_base() : _M_orig(limit()) { }
430 ~adjustor_base() { set_limit(_M_orig); }
454 throw_conditionally()
456 if (count() == limit())
457 __throw_forced_error();
464 static size_t _S_count(0);
477 set_limit(
const size_t __l)
484 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
496 const double _M_orig;
499 adjustor_base() : _M_orig(probability()) { }
501 virtual ~adjustor_base()
502 { set_probability(_M_orig); }
509 { set_probability(1 -
std::pow(
double(1 - probability()),
510 double(0.5 / (size + 1))));
533 set_probability(
double __p)
534 { probability() = __p; }
537 throw_conditionally()
539 if (generate() < probability())
540 __throw_forced_error();
544 seed(
unsigned long __s)
545 { engine().seed(__s); }
548 #if __cplusplus >= 201103L
552 typedef std::tr1::uniform_real<double> distribution_type;
559 #if __cplusplus >= 201103L
560 const distribution_type distribution(0, 1);
561 static auto generator =
std::bind(distribution, engine());
564 typedef std::tr1::variate_generator<engine_type, distribution_type> gen_t;
565 distribution_type distribution(0, 1);
566 static gen_t generator(engine(), distribution);
569 double random = generator();
570 if (random < distribution.min() || random > distribution.max())
574 __s +=
"random number generated is: ";
576 __builtin_sprintf(buf,
"%f", random);
578 std::__throw_out_of_range(__s.c_str());
594 static engine_type _S_e;
598 #endif // _GLIBCXX_USE_C99_STDINT_TR1
606 template<
typename _Cond>
609 typedef _Cond condition_type;
611 using condition_type::throw_conditionally;
615 #ifndef _GLIBCXX_IS_AGGREGATE
617 { throw_conditionally(); }
620 { throw_conditionally(); }
622 #if __cplusplus >= 201103L
628 { throw_conditionally(); }
634 throw_conditionally();
639 #if __cplusplus >= 201103L
648 throw_conditionally();
654 template<
typename _Cond>
659 throw_value::throw_conditionally();
660 throw_value orig(__a);
666 template<
typename _Cond>
668 operator==(
const throw_value_base<_Cond>& __a,
669 const throw_value_base<_Cond>& __b)
671 typedef throw_value_base<_Cond> throw_value;
672 throw_value::throw_conditionally();
673 bool __ret = __a._M_i == __b._M_i;
677 template<
typename _Cond>
679 operator<(
const throw_value_base<_Cond>& __a,
680 const throw_value_base<_Cond>& __b)
682 typedef throw_value_base<_Cond> throw_value;
683 throw_value::throw_conditionally();
684 bool __ret = __a._M_i < __b._M_i;
689 template<
typename _Cond>
690 inline throw_value_base<_Cond>
691 operator+(
const throw_value_base<_Cond>& __a,
692 const throw_value_base<_Cond>& __b)
694 typedef throw_value_base<_Cond> throw_value;
695 throw_value::throw_conditionally();
696 throw_value __ret(__a._M_i + __b._M_i);
700 template<
typename _Cond>
701 inline throw_value_base<_Cond>
702 operator-(
const throw_value_base<_Cond>& __a,
703 const throw_value_base<_Cond>& __b)
705 typedef throw_value_base<_Cond> throw_value;
706 throw_value::throw_conditionally();
707 throw_value __ret(__a._M_i - __b._M_i);
711 template<
typename _Cond>
712 inline throw_value_base<_Cond>
713 operator*(
const throw_value_base<_Cond>& __a,
714 const throw_value_base<_Cond>& __b)
716 typedef throw_value_base<_Cond> throw_value;
717 throw_value::throw_conditionally();
718 throw_value __ret(__a._M_i * __b._M_i);
728 #ifndef _GLIBCXX_IS_AGGREGATE
734 #if __cplusplus >= 201103L
744 base_type::operator=(__other);
748 #if __cplusplus >= 201103L
754 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
760 #ifndef _GLIBCXX_IS_AGGREGATE
766 #if __cplusplus >= 201103L
776 base_type::operator=(__other);
780 #if __cplusplus >= 201103L
785 #endif // _GLIBCXX_USE_C99_STDINT_TR1
794 template<
typename _Tp,
typename _Cond>
799 typedef std::size_t size_type;
800 typedef std::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;
807 #if __cplusplus >= 201103L
814 typedef _Cond condition_type;
820 using condition_type::throw_conditionally;
824 max_size()
const _GLIBCXX_USE_NOEXCEPT
828 address(reference __x)
const _GLIBCXX_NOEXCEPT
832 address(const_reference __x)
const _GLIBCXX_NOEXCEPT
835 _GLIBCXX_NODISCARD pointer
836 allocate(size_type __n,
const void* hint = 0)
838 if (__n > this->max_size())
839 std::__throw_bad_alloc();
841 throw_conditionally();
843 insert(a,
sizeof(value_type) * __n);
847 #if __cplusplus >= 201103L
848 template<
typename _Up,
typename... _Args>
850 construct(_Up* __p, _Args&&... __args)
852 traits::construct(_M_allocator, __p, std::forward<_Args>(__args)...);
853 insert_construct(__p);
856 template<
typename _Up>
860 erase_construct(__p);
861 traits::destroy(_M_allocator, __p);
865 construct(pointer __p,
const value_type& val)
866 {
return _M_allocator.construct(__p, val); }
870 { _M_allocator.destroy(__p); }
874 deallocate(pointer __p, size_type __n)
876 erase(__p,
sizeof(value_type) * __n);
877 _M_allocator.deallocate(__p, __n);
881 check_allocated(pointer __p, size_type __n)
883 size_type __t =
sizeof(value_type) * __n;
884 annotate_base::check_allocated(__p, __t);
889 { annotate_base::check(__n); }
892 template<
typename _Tp,
typename _Cond>
898 template<
typename _Tp,
typename _Cond>
900 operator!=(
const throw_allocator_base<_Tp, _Cond>&,
901 const throw_allocator_base<_Tp, _Cond>&)
905 template<
typename _Tp>
909 template<
typename _Tp1>
916 _GLIBCXX_USE_NOEXCEPT { }
918 template<
typename _Tp1>
920 _GLIBCXX_USE_NOEXCEPT { }
925 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
927 template<
typename _Tp>
931 template<
typename _Tp1>
938 _GLIBCXX_USE_NOEXCEPT { }
940 template<
typename _Tp1>
942 _GLIBCXX_USE_NOEXCEPT { }
946 #endif // _GLIBCXX_USE_C99_STDINT_TR1
948 _GLIBCXX_END_NAMESPACE_VERSION
951 #if __cplusplus >= 201103L
955 namespace std _GLIBCXX_VISIBILITY(default)
965 __gnu_cxx::throw_value_limit::throw_conditionally();
967 size_t __result = __h(__val._M_i);
972 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
981 __gnu_cxx::throw_value_random::throw_conditionally();
983 size_t __result = __h(__val._M_i);