3 // Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 2, or (at your option) any later
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
42 /** @file ext/vstring.h
43 * This file is a GNU extension to the Standard C++ Library.
45 * Contains an exception-throwing allocator, useful for testing
46 * exception safety. In addition, allocation addresses are stored and
51 * @file throw_allocator.h
54 #ifndef _THROW_ALLOCATOR_H
55 #define _THROW_ALLOCATOR_H 1
67 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
69 class twister_rand_gen
72 twister_rand_gen(unsigned int seed =
73 static_cast<unsigned int>(std::time(0)));
82 std::tr1::mt19937 _M_generator;
86 struct forced_exception_error : public std::exception
89 class throw_allocator_base
93 init(unsigned long seed);
96 set_throw_prob(double throw_prob);
107 struct group_throw_prob_adjustor
109 group_throw_prob_adjustor(size_t size)
110 : _M_throw_prob_orig(_S_throw_prob)
113 1 - std::pow(double(1 - _S_throw_prob), double(0.5 / (size + 1)));
116 ~group_throw_prob_adjustor()
117 { _S_throw_prob = _M_throw_prob_orig; }
120 const double _M_throw_prob_orig;
123 struct zero_throw_prob_adjustor
125 zero_throw_prob_adjustor() : _M_throw_prob_orig(_S_throw_prob)
126 { _S_throw_prob = 0; }
128 ~zero_throw_prob_adjustor()
129 { _S_throw_prob = _M_throw_prob_orig; }
132 const double _M_throw_prob_orig;
137 insert(void*, size_t);
140 erase(void*, size_t);
143 throw_conditionally();
145 // See if a particular address and size has been allocated by this
148 check_allocated(void*, size_t);
150 // See if a given label has been allocated by this allocator.
152 check_allocated(size_t);
155 typedef std::pair<size_t, size_t> alloc_data_type;
156 typedef std::map<void*, alloc_data_type> map_type;
157 typedef map_type::value_type entry_type;
158 typedef map_type::const_iterator const_iterator;
159 typedef map_type::const_reference const_reference;
162 operator<<(std::ostream&, const throw_allocator_base&);
165 make_entry(void*, size_t);
168 print_to_string(std::string&);
171 print_to_string(std::string&, const_reference);
173 static twister_rand_gen _S_g;
174 static map_type _S_map;
175 static double _S_throw_prob;
176 static size_t _S_label;
181 class throw_allocator : public throw_allocator_base
184 typedef size_t size_type;
185 typedef ptrdiff_t difference_type;
186 typedef T value_type;
187 typedef value_type* pointer;
188 typedef const value_type* const_pointer;
189 typedef value_type& reference;
190 typedef const value_type& const_reference;
196 typedef throw_allocator<U> other;
199 throw_allocator() throw() { }
201 throw_allocator(const throw_allocator&) throw() { }
204 throw_allocator(const throw_allocator<U>&) throw() { }
206 ~throw_allocator() throw() { }
209 max_size() const throw()
210 { return std::allocator<value_type>().max_size(); }
213 allocate(size_type __n, std::allocator<void>::const_pointer hint = 0)
215 if (__builtin_expect(__n > this->max_size(), false))
216 std::__throw_bad_alloc();
218 throw_conditionally();
219 value_type* const a = std::allocator<value_type>().allocate(__n, hint);
220 insert(a, sizeof(value_type) * __n);
225 construct(pointer __p, const T& val)
226 { return std::allocator<value_type>().construct(__p, val); }
230 { std::allocator<value_type>().destroy(__p); }
233 deallocate(pointer __p, size_type __n)
235 erase(__p, sizeof(value_type) * __n);
236 std::allocator<value_type>().deallocate(__p, __n);
240 check_allocated(pointer __p, size_type __n)
241 { throw_allocator_base::check_allocated(__p, sizeof(value_type) * __n); }
244 check_allocated(size_type label)
245 { throw_allocator_base::check_allocated(label); }
250 operator==(const throw_allocator<T>&, const throw_allocator<T>&)
255 operator!=(const throw_allocator<T>&, const throw_allocator<T>&)
259 operator<<(std::ostream& os, const throw_allocator_base& alloc)
262 throw_allocator_base::print_to_string(error);
267 // XXX Should be in .cc.
269 twister_rand_gen(unsigned int seed) : _M_generator(seed) { }
273 init(unsigned int seed)
274 { _M_generator.seed(seed); }
280 const double eng_min = _M_generator.min();
281 const double eng_range =
282 static_cast<const double>(_M_generator.max() - eng_min);
284 const double eng_res =
285 static_cast<const double>(_M_generator() - eng_min);
287 const double ret = eng_res / eng_range;
288 _GLIBCXX_DEBUG_ASSERT(ret >= 0 && ret <= 1);
292 twister_rand_gen throw_allocator_base::_S_g;
294 throw_allocator_base::map_type
295 throw_allocator_base::_S_map;
297 double throw_allocator_base::_S_throw_prob;
299 size_t throw_allocator_base::_S_label = 0;
301 throw_allocator_base::entry_type
302 throw_allocator_base::make_entry(void* p, size_t size)
303 { return std::make_pair(p, alloc_data_type(_S_label, size)); }
306 throw_allocator_base::init(unsigned long seed)
310 throw_allocator_base::set_throw_prob(double throw_prob)
311 { _S_throw_prob = throw_prob; }
314 throw_allocator_base::get_throw_prob()
315 { return _S_throw_prob; }
318 throw_allocator_base::set_label(size_t l)
322 throw_allocator_base::insert(void* p, size_t size)
324 const_iterator found_it = _S_map.find(p);
325 if (found_it != _S_map.end())
327 std::string error("throw_allocator_base::insert");
328 error += "double insert!";
330 print_to_string(error, make_entry(p, size));
331 print_to_string(error, *found_it);
332 throw std::logic_error(error);
334 _S_map.insert(make_entry(p, size));
338 throw_allocator_base::empty()
339 { return _S_map.empty(); }
342 throw_allocator_base::erase(void* p, size_t size)
344 check_allocated(p, size);
349 throw_allocator_base::check_allocated(void* p, size_t size)
351 const_iterator found_it = _S_map.find(p);
352 if (found_it == _S_map.end())
354 std::string error("throw_allocator_base::check_allocated by value ");
355 error += "null erase!";
357 print_to_string(error, make_entry(p, size));
358 throw std::logic_error(error);
361 if (found_it->second.second != size)
363 std::string error("throw_allocator_base::check_allocated by value ");
364 error += "wrong-size erase!";
366 print_to_string(error, make_entry(p, size));
367 print_to_string(error, *found_it);
368 throw std::logic_error(error);
373 throw_allocator_base::check_allocated(size_t label)
376 const_iterator it = _S_map.begin();
377 while (it != _S_map.end())
379 if (it->second.first == label)
380 print_to_string(found, *it);
386 std::string error("throw_allocator_base::check_allocated by label ");
389 throw std::logic_error(error);
394 throw_allocator_base::throw_conditionally()
396 if (_S_g.get_prob() < _S_throw_prob)
397 throw forced_exception_error();
401 throw_allocator_base::print_to_string(std::string& s)
403 const_iterator begin = throw_allocator_base::_S_map.begin();
404 const_iterator end = throw_allocator_base::_S_map.end();
405 for (; begin != end; ++begin)
406 print_to_string(s, *begin);
410 throw_allocator_base::print_to_string(std::string& s, const_reference ref)
413 const char tab('\t');
415 sprintf(buf, "%p", ref.first);
419 sprintf(buf, "%u", ref.second.first);
423 sprintf(buf, "%u", ref.second.second);
428 _GLIBCXX_END_NAMESPACE