46 #ifndef _THROW_ALLOCATOR_H
47 #define _THROW_ALLOCATOR_H 1
58 #if __cplusplus >= 201103L
68 _GLIBCXX_BEGIN_NAMESPACE_VERSION
74 struct forced_error :
public std::exception
79 __throw_forced_error()
80 { _GLIBCXX_THROW_OR_ABORT(forced_error()); }
105 insert(
void* p,
size_t size)
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());
114 const_iterator found = map().find(p);
115 if (found != map().end())
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());
123 map().insert(make_entry(p, size));
127 erase(
void* p,
size_t size)
129 check_allocated(p, size);
135 check_allocated(
void* p,
size_t size)
137 const_iterator found = map().find(p);
138 if (found == map().end())
140 std::string error(
"annotate_base::check_allocated by value "
142 log_to_string(error, make_entry(p, size));
143 std::__throw_logic_error(error.c_str());
146 if (found->second.second != size)
148 std::string error(
"annotate_base::check_allocated by value "
149 "wrong-size erase!\n");
150 log_to_string(error, make_entry(p, size));
151 log_to_string(error, *found);
152 std::__throw_logic_error(error.c_str());
158 check_allocated(
size_t label)
160 const_iterator beg = map().begin();
161 const_iterator end = map().end();
165 if (beg->second.first == label)
166 log_to_string(found, *beg);
172 std::string error(
"annotate_base::check_allocated by label\n");
174 std::__throw_logic_error(error.c_str());
179 typedef std::pair<size_t, size_t> data_type;
180 typedef std::map<void*, data_type> map_type;
181 typedef map_type::value_type entry_type;
182 typedef map_type::const_iterator const_iterator;
183 typedef map_type::const_reference const_reference;
186 operator<<(std::ostream&,
const annotate_base&);
189 make_entry(
void* p,
size_t size)
190 {
return std::make_pair(p, data_type(get_label(), size)); }
193 log_to_string(
std::string& s, const_reference ref)
const
196 const char tab(
'\t');
198 unsigned long l =
static_cast<unsigned long>(ref.second.first);
199 __builtin_sprintf(buf,
"%lu", l);
203 l =
static_cast<unsigned long>(ref.second.second);
204 __builtin_sprintf(buf,
"%lu", l);
208 __builtin_sprintf(buf,
"%p", ref.first);
223 static map_type _S_map;
229 operator<<(std::ostream& os,
const annotate_base& __b)
232 typedef annotate_base base_type;
233 base_type::const_iterator beg = __b.map().begin();
234 base_type::const_iterator end = __b.map().end();
235 for (; beg != end; ++beg)
236 __b.log_to_string(error, *beg);
247 struct condition_base
249 virtual ~condition_base() { };
256 struct limit_condition :
public condition_base
264 const size_t _M_orig;
267 adjustor_base() : _M_orig(limit()) { }
270 ~adjustor_base() { set_limit(_M_orig); }
274 struct never_adjustor :
public adjustor_base
280 struct always_adjustor :
public adjustor_base
282 always_adjustor() { set_limit(count()); }
286 struct limit_adjustor :
public adjustor_base
288 limit_adjustor(
const size_t __l) { set_limit(__l); }
294 throw_conditionally()
296 if (count() == limit())
297 __throw_forced_error();
304 static size_t _S_count(0);
317 set_limit(
const size_t __l)
328 struct random_condition :
public condition_base
336 const double _M_orig;
339 adjustor_base() : _M_orig(probability()) { }
341 virtual ~adjustor_base()
342 { set_probability(_M_orig); }
346 struct group_adjustor :
public adjustor_base
348 group_adjustor(
size_t size)
349 { set_probability(1 - std::pow(
double(1 - probability()),
350 double(0.5 / (size + 1))));
355 struct never_adjustor :
public adjustor_base
357 never_adjustor() { set_probability(0); }
361 struct always_adjustor :
public adjustor_base
363 always_adjustor() { set_probability(1); }
373 set_probability(
double __p)
374 { probability() = __p; }
377 throw_conditionally()
379 if (generate() < probability())
380 __throw_forced_error();
384 seed(
unsigned long __s)
385 { engine().seed(__s); }
388 #if __cplusplus >= 201103L
389 typedef std::uniform_real_distribution<double> distribution_type;
390 typedef std::mt19937 engine_type;
392 typedef std::tr1::uniform_real<double> distribution_type;
393 typedef std::tr1::mt19937 engine_type;
399 #if __cplusplus >= 201103L
400 const distribution_type distribution(0, 1);
401 static auto generator = std::bind(distribution, engine());
404 typedef std::tr1::variate_generator<engine_type, distribution_type> gen_t;
405 distribution_type distribution(0, 1);
406 static gen_t generator(engine(), distribution);
409 double random = generator();
410 if (random < distribution.min() || random > distribution.max())
414 __s +=
"random number generated is: ";
416 __builtin_sprintf(buf,
"%f", random);
418 std::__throw_out_of_range(__s.c_str());
434 static engine_type _S_e;
446 template<
typename _Cond>
447 struct throw_value_base :
public _Cond
449 typedef _Cond condition_type;
451 using condition_type::throw_conditionally;
455 #ifndef _GLIBCXX_IS_AGGREGATE
456 throw_value_base() : _M_i(0)
457 { throw_conditionally(); }
459 throw_value_base(
const throw_value_base& __v) : _M_i(__v._M_i)
460 { throw_conditionally(); }
462 #if __cplusplus >= 201103L
464 throw_value_base(throw_value_base&&) =
default;
467 explicit throw_value_base(
const std::size_t __i) : _M_i(__i)
468 { throw_conditionally(); }
472 operator=(
const throw_value_base& __v)
474 throw_conditionally();
479 #if __cplusplus >= 201103L
482 operator=(throw_value_base&&) =
default;
488 throw_conditionally();
494 template<
typename _Cond>
496 swap(throw_value_base<_Cond>& __a, throw_value_base<_Cond>& __b)
498 typedef throw_value_base<_Cond> throw_value;
499 throw_value::throw_conditionally();
500 throw_value orig(__a);
506 template<
typename _Cond>
508 operator==(
const throw_value_base<_Cond>& __a,
509 const throw_value_base<_Cond>& __b)
511 typedef throw_value_base<_Cond> throw_value;
512 throw_value::throw_conditionally();
513 bool __ret = __a._M_i == __b._M_i;
517 template<
typename _Cond>
519 operator<(const throw_value_base<_Cond>& __a,
520 const throw_value_base<_Cond>& __b)
522 typedef throw_value_base<_Cond> throw_value;
523 throw_value::throw_conditionally();
524 bool __ret = __a._M_i < __b._M_i;
529 template<
typename _Cond>
530 inline throw_value_base<_Cond>
531 operator+(
const throw_value_base<_Cond>& __a,
532 const throw_value_base<_Cond>& __b)
534 typedef throw_value_base<_Cond> throw_value;
535 throw_value::throw_conditionally();
536 throw_value __ret(__a._M_i + __b._M_i);
540 template<
typename _Cond>
541 inline throw_value_base<_Cond>
542 operator-(
const throw_value_base<_Cond>& __a,
543 const throw_value_base<_Cond>& __b)
545 typedef throw_value_base<_Cond> throw_value;
546 throw_value::throw_conditionally();
547 throw_value __ret(__a._M_i - __b._M_i);
551 template<
typename _Cond>
552 inline throw_value_base<_Cond>
553 operator*(
const throw_value_base<_Cond>& __a,
554 const throw_value_base<_Cond>& __b)
556 typedef throw_value_base<_Cond> throw_value;
557 throw_value::throw_conditionally();
558 throw_value __ret(__a._M_i * __b._M_i);
564 struct throw_value_limit :
public throw_value_base<limit_condition>
566 typedef throw_value_base<limit_condition> base_type;
568 #ifndef _GLIBCXX_IS_AGGREGATE
569 throw_value_limit() { }
571 throw_value_limit(
const throw_value_limit& __other)
572 : base_type(__other._M_i) { }
574 #if __cplusplus >= 201103L
575 throw_value_limit(throw_value_limit&&) =
default;
578 explicit throw_value_limit(
const std::size_t __i) : base_type(__i) { }
582 operator=(
const throw_value_limit& __other)
584 base_type::operator=(__other);
588 #if __cplusplus >= 201103L
590 operator=(throw_value_limit&&) =
default;
595 struct throw_value_random :
public throw_value_base<random_condition>
597 typedef throw_value_base<random_condition> base_type;
599 #ifndef _GLIBCXX_IS_AGGREGATE
600 throw_value_random() { }
602 throw_value_random(
const throw_value_random& __other)
603 : base_type(__other._M_i) { }
605 #if __cplusplus >= 201103L
606 throw_value_random(throw_value_random&&) =
default;
609 explicit throw_value_random(
const std::size_t __i) : base_type(__i) { }
613 operator=(
const throw_value_random& __other)
615 base_type::operator=(__other);
619 #if __cplusplus >= 201103L
621 operator=(throw_value_random&&) =
default;
633 template<
typename _Tp,
typename _Cond>
634 class throw_allocator_base
635 :
public annotate_base,
public _Cond
638 typedef size_t size_type;
640 typedef _Tp value_type;
641 typedef value_type* pointer;
642 typedef const value_type* const_pointer;
643 typedef value_type& reference;
644 typedef const value_type& const_reference;
646 #if __cplusplus >= 201103L
653 typedef _Cond condition_type;
655 std::allocator<value_type> _M_allocator;
657 using condition_type::throw_conditionally;
661 max_size() const _GLIBCXX_USE_NOEXCEPT
662 {
return _M_allocator.max_size(); }
665 address(reference __x)
const _GLIBCXX_NOEXCEPT
666 {
return std::__addressof(__x); }
669 address(const_reference __x)
const _GLIBCXX_NOEXCEPT
670 {
return std::__addressof(__x); }
673 allocate(size_type __n, std::allocator<void>::const_pointer hint = 0)
675 if (__n > this->max_size())
676 std::__throw_bad_alloc();
678 throw_conditionally();
679 pointer
const a = _M_allocator.allocate(__n, hint);
680 insert(a,
sizeof(value_type) * __n);
684 #if __cplusplus >= 201103L
685 template<
typename _Up,
typename... _Args>
687 construct(_Up* __p, _Args&&... __args)
688 {
return _M_allocator.construct(__p, std::forward<_Args>(__args)...); }
690 template<
typename _Up>
693 { _M_allocator.destroy(__p); }
696 construct(pointer __p,
const value_type& val)
697 {
return _M_allocator.construct(__p, val); }
701 { _M_allocator.destroy(__p); }
705 deallocate(pointer __p, size_type __n)
707 erase(__p,
sizeof(value_type) * __n);
708 _M_allocator.deallocate(__p, __n);
712 check_allocated(pointer __p, size_type __n)
714 size_type __t =
sizeof(value_type) * __n;
715 annotate_base::check_allocated(__p, __t);
719 check_allocated(size_type __n)
720 { annotate_base::check_allocated(__n); }
723 template<
typename _Tp,
typename _Cond>
725 operator==(
const throw_allocator_base<_Tp, _Cond>&,
726 const throw_allocator_base<_Tp, _Cond>&)
729 template<
typename _Tp,
typename _Cond>
731 operator!=(
const throw_allocator_base<_Tp, _Cond>&,
732 const throw_allocator_base<_Tp, _Cond>&)
736 template<
typename _Tp>
737 struct throw_allocator_limit
738 :
public throw_allocator_base<_Tp, limit_condition>
740 template<
typename _Tp1>
742 {
typedef throw_allocator_limit<_Tp1> other; };
744 throw_allocator_limit() _GLIBCXX_USE_NOEXCEPT { }
746 throw_allocator_limit(
const throw_allocator_limit&)
747 _GLIBCXX_USE_NOEXCEPT { }
749 template<
typename _Tp1>
750 throw_allocator_limit(
const throw_allocator_limit<_Tp1>&)
751 _GLIBCXX_USE_NOEXCEPT { }
753 ~throw_allocator_limit() _GLIBCXX_USE_NOEXCEPT { }
757 template<
typename _Tp>
758 struct throw_allocator_random
759 :
public throw_allocator_base<_Tp, random_condition>
761 template<
typename _Tp1>
763 {
typedef throw_allocator_random<_Tp1> other; };
765 throw_allocator_random() _GLIBCXX_USE_NOEXCEPT { }
767 throw_allocator_random(
const throw_allocator_random&)
768 _GLIBCXX_USE_NOEXCEPT { }
770 template<
typename _Tp1>
771 throw_allocator_random(
const throw_allocator_random<_Tp1>&)
772 _GLIBCXX_USE_NOEXCEPT { }
774 ~throw_allocator_random() _GLIBCXX_USE_NOEXCEPT { }
777 _GLIBCXX_END_NAMESPACE_VERSION
780 #if __cplusplus >= 201103L
788 struct hash<__gnu_cxx::throw_value_limit>
789 :
public std::unary_function<__gnu_cxx::throw_value_limit, size_t>
792 operator()(
const __gnu_cxx::throw_value_limit& __val)
const
794 std::hash<std::size_t> __h;
795 size_t __result = __h(__val._M_i);
802 struct hash<__gnu_cxx::throw_value_random>
803 :
public std::unary_function<__gnu_cxx::throw_value_random, size_t>
806 operator()(
const __gnu_cxx::throw_value_random& __val)
const
808 std::hash<std::size_t> __h;
809 size_t __result = __h(__val._M_i);
bool operator==(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
basic_string< char > string
Definition: string:1153
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const basic_string< _CharT, _Traits, _Allocator > &__str)
Definition: string:1116
namespace std _GLIBCXX_VISIBILITY(default)
Definition: auto_ptr.h:36
_Safe_iterator< _Iterator, _Sequence > operator+(typename _Safe_iterator< _Iterator, _Sequence >::difference_type __n, const _Safe_iterator< _Iterator, _Sequence > &__i)
Definition: safe_iterator.h:712
_Safe_iterator< _IteratorL, _Sequence >::difference_type operator-(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:680
bool operator!=(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
const _Tp & max(const _Tp &__a, const _Tp &__b)
Equivalent to std::max.
Definition: base.h:150
__SIZE_TYPE__ size_t
Definition: stddef.h:212
std::tr1::integral_constant< int, 1 > true_type
Definition: type_utils.hpp:70
__PTRDIFF_TYPE__ ptrdiff_t
Definition: stddef.h:147
void swap(exception_ptr &__lhs, exception_ptr &__rhs)
Definition: exception_ptr.h:160