44 #ifndef _THROW_ALLOCATOR_H
45 #define _THROW_ALLOCATOR_H 1
61 class twister_rand_gen
64 std::tr1::mt19937 _M_generator;
67 twister_rand_gen(
unsigned int s = static_cast<unsigned int>(std::time(0)));
85 __throw_forced_exception_error()
99 init(
unsigned long seed);
102 set_throw_prob(
double throw_prob);
113 struct group_throw_prob_adjustor
115 group_throw_prob_adjustor(
size_t size) : _M_throw_prob_orig(_S_throw_prob)
118 1 - std::pow(
double(1 - _S_throw_prob),
double(0.5 / (size + 1)));
121 ~group_throw_prob_adjustor()
122 { _S_throw_prob = _M_throw_prob_orig; }
125 const double _M_throw_prob_orig;
128 struct zero_throw_prob_adjustor
130 zero_throw_prob_adjustor() : _M_throw_prob_orig(_S_throw_prob)
131 { _S_throw_prob = 0; }
133 ~zero_throw_prob_adjustor()
134 { _S_throw_prob = _M_throw_prob_orig; }
137 const double _M_throw_prob_orig;
142 insert(
void*,
size_t);
145 erase(
void*,
size_t);
148 throw_conditionally();
153 check_allocated(
void*,
size_t);
157 check_allocated(
size_t);
163 typedef map_type::const_iterator const_iterator;
164 typedef map_type::const_reference const_reference;
170 make_entry(
void*,
size_t);
178 static twister_rand_gen _S_g;
179 static map_type _S_map;
180 static double _S_throw_prob;
181 static size_t _S_label;
192 typedef size_t size_type;
193 typedef ptrdiff_t difference_type;
194 typedef T value_type;
195 typedef value_type* pointer;
196 typedef const value_type* const_pointer;
197 typedef value_type& reference;
198 typedef const value_type& const_reference;
217 max_size()
const throw()
223 if (__builtin_expect(__n > this->max_size(),
false))
224 std::__throw_bad_alloc();
226 throw_conditionally();
228 insert(a,
sizeof(value_type) * __n);
233 construct(pointer __p,
const T& val)
236 #ifdef __GXX_EXPERIMENTAL_CXX0X__
237 template<
typename... _Args>
239 construct(pointer __p, _Args&&... __args)
242 construct(__p, std::forward<_Args>(__args)...);
251 deallocate(pointer __p, size_type __n)
253 erase(__p,
sizeof(value_type) * __n);
258 check_allocated(pointer __p, size_type __n)
259 { throw_allocator_base::check_allocated(__p,
sizeof(value_type) * __n); }
262 check_allocated(size_type label)
263 { throw_allocator_base::check_allocated(label); }
273 operator!=(
const throw_allocator<T>&,
const throw_allocator<T>&)
277 operator<<(std::ostream& os,
const throw_allocator_base& alloc)
280 throw_allocator_base::print_to_string(error);
287 twister_rand_gen(
unsigned int seed) : _M_generator(seed) { }
291 init(
unsigned int seed)
292 { _M_generator.seed(seed); }
298 const double min = _M_generator.min();
299 const double res =
static_cast<const double>(_M_generator() -
min);
300 const double range =
static_cast<const double>(_M_generator.max() -
min);
301 const double ret = res / range;
302 _GLIBCXX_DEBUG_ASSERT(ret >= 0 && ret <= 1);
306 twister_rand_gen throw_allocator_base::_S_g;
308 throw_allocator_base::map_type
309 throw_allocator_base::_S_map;
311 double throw_allocator_base::_S_throw_prob;
313 size_t throw_allocator_base::_S_label = 0;
315 throw_allocator_base::entry_type
316 throw_allocator_base::make_entry(
void* p,
size_t size)
317 {
return std::make_pair(p, alloc_data_type(_S_label, size)); }
320 throw_allocator_base::init(
unsigned long seed)
324 throw_allocator_base::set_throw_prob(
double throw_prob)
325 { _S_throw_prob = throw_prob; }
328 throw_allocator_base::get_throw_prob()
329 {
return _S_throw_prob; }
332 throw_allocator_base::set_label(
size_t l)
336 throw_allocator_base::insert(
void* p,
size_t size)
338 const_iterator found_it = _S_map.
find(p);
339 if (found_it != _S_map.
end())
342 error +=
"double insert!";
344 print_to_string(error, make_entry(p, size));
345 print_to_string(error, *found_it);
346 std::__throw_logic_error(error.c_str());
348 _S_map.
insert(make_entry(p, size));
352 throw_allocator_base::empty()
353 {
return _S_map.
empty(); }
356 throw_allocator_base::erase(
void* p,
size_t size)
358 check_allocated(p, size);
363 throw_allocator_base::check_allocated(
void* p,
size_t size)
365 const_iterator found_it = _S_map.
find(p);
366 if (found_it == _S_map.
end())
368 std::string error(
"throw_allocator_base::check_allocated by value ");
369 error +=
"null erase!";
371 print_to_string(error, make_entry(p, size));
372 std::__throw_logic_error(error.c_str());
375 if (found_it->second.second != size)
377 std::string error(
"throw_allocator_base::check_allocated by value ");
378 error +=
"wrong-size erase!";
380 print_to_string(error, make_entry(p, size));
381 print_to_string(error, *found_it);
382 std::__throw_logic_error(error.c_str());
387 throw_allocator_base::check_allocated(
size_t label)
390 const_iterator it = _S_map.
begin();
391 while (it != _S_map.
end())
393 if (it->second.first == label)
395 print_to_string(found, *it);
402 std::string error(
"throw_allocator_base::check_allocated by label ");
405 std::__throw_logic_error(error.c_str());
410 throw_allocator_base::throw_conditionally()
412 if (_S_g.get_prob() < _S_throw_prob)
413 __throw_forced_exception_error();
417 throw_allocator_base::print_to_string(
std::string& s)
419 const_iterator begin = throw_allocator_base::_S_map.begin();
420 const_iterator end = throw_allocator_base::_S_map.end();
421 for (; begin != end; ++begin)
422 print_to_string(s, *begin);
426 throw_allocator_base::print_to_string(
std::string& s, const_reference ref)
429 const char tab(
'\t');
431 __builtin_sprintf(buf,
"%p", ref.first);
435 unsigned long l =
static_cast<unsigned long>(ref.second.first);
436 __builtin_sprintf(buf,
"%lu", l);
440 l =
static_cast<unsigned long>(ref.second.second);
441 __builtin_sprintf(buf,
"%lu", l);
446 _GLIBCXX_END_NAMESPACE
Thown by throw_allocator.
The "standard" allocator, as per [20.4].Further details: http://gcc.gnu.org/onlinedocs/libstdc++/manu...
iterator find(const key_type &__x)
Tries to locate an element in a map.
A standard container made up of (key,value) pairs, which can be retrieved based on a key...
Allocator class with logging and exception control.
pair holds two objects of arbitrary type.
GNU extensions for public use.
basic_ostream< _CharT, _Traits > & operator<<(basic_ostream< _CharT, _Traits > &__os, const basic_string< _CharT, _Traits, _Alloc > &__str)
Write string to a stream.
void erase(iterator __position)
Erases an element from a map.
std::pair< iterator, bool > insert(const value_type &__x)
Attempts to insert a std::pair into the map.
const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.