3 // Copyright (C) 2005, 2006 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
43 * @file throw_allocator.h Contains an exception-throwing allocator
44 * useful for testing exception safety. In addition, allocation
45 * addresses are stored and sanity checked.
48 #ifndef _THROW_ALLOCATOR_H
49 #define _THROW_ALLOCATOR_H 1
60 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx
)
62 class twister_rand_gen
65 twister_rand_gen(unsigned int seed
=
66 static_cast<unsigned int>(std::time(0)));
75 std::tr1::mt19937 _M_generator
;
79 struct forced_exception_error
: public std::exception
82 class throw_allocator_base
86 init(unsigned long seed
);
89 set_throw_prob(double throw_prob
);
100 struct group_throw_prob_adjustor
102 group_throw_prob_adjustor(size_t size
)
103 : _M_throw_prob_orig(_S_throw_prob
)
106 1 - ::pow(double(1 - _S_throw_prob
), double(0.5 / (size
+ 1)));
109 ~group_throw_prob_adjustor()
110 { _S_throw_prob
= _M_throw_prob_orig
; }
113 const double _M_throw_prob_orig
;
116 struct zero_throw_prob_adjustor
118 zero_throw_prob_adjustor() : _M_throw_prob_orig(_S_throw_prob
)
119 { _S_throw_prob
= 0; }
121 ~zero_throw_prob_adjustor()
122 { _S_throw_prob
= _M_throw_prob_orig
; }
125 const double _M_throw_prob_orig
;
130 insert(void*, size_t);
133 erase(void*, size_t);
136 throw_conditionally();
139 assert_allocatod(const void*, size_t);
142 check_allocated(void*, size_t);
145 typedef std::pair
<size_t, size_t> alloc_data_type
;
146 typedef std::map
<void*, alloc_data_type
> map_type
;
147 typedef map_type::value_type entry_type
;
148 typedef map_type::const_iterator const_iterator
;
149 typedef map_type::const_reference const_reference
;
152 operator<<(std::ostream
&, const throw_allocator_base
&);
155 make_entry(void*, size_t);
158 print_to_string(std::string
&);
161 print_to_string(std::string
&, const_reference
);
163 static twister_rand_gen _S_g
;
164 static map_type _S_map
;
165 static double _S_throw_prob
;
166 static size_t _S_label
;
171 class throw_allocator
: public throw_allocator_base
174 typedef size_t size_type
;
175 typedef ptrdiff_t difference_type
;
177 typedef const T
* const_pointer
;
178 typedef T
& reference
;
179 typedef const T
& const_reference
;
180 typedef T value_type
;
185 typedef throw_allocator
<U
> other
;
188 throw_allocator() throw() { }
190 throw_allocator(const throw_allocator
<T
>&) throw() { }
193 throw_allocator(const throw_allocator
<U
>&) throw() { }
195 ~throw_allocator() throw() { }
198 max_size() const throw()
199 { return std::allocator
<T
>().max_size(); }
202 allocate(size_type num
, std::allocator
<void>::const_pointer hint
= 0)
204 throw_conditionally();
205 T
* const a
= std::allocator
<T
>().allocate(num
, hint
);
206 insert(a
, sizeof(T
) * num
);
211 construct(pointer p
, const T
& val
)
212 { return std::allocator
<T
>().construct(p
, val
); }
216 { std::allocator
<T
>().destroy(p
); }
219 deallocate(pointer p
, size_type num
)
221 erase(p
, sizeof(T
) * num
);
222 std::allocator
<T
>().deallocate(p
, num
);
226 check_allocated(pointer p
, size_type num
)
227 { throw_allocator_base::check_allocated(p
, sizeof(T
) * num
); }
232 operator==(const throw_allocator
<T
>&, const throw_allocator
<T
>&)
237 operator!=(const throw_allocator
<T
>&, const throw_allocator
<T
>&)
241 operator<<(std::ostream
& os
, const throw_allocator_base
& alloc
)
244 throw_allocator_base::print_to_string(error
);
249 // XXX Should be in .cc.
251 twister_rand_gen(unsigned int seed
) : _M_generator(seed
) { }
255 init(unsigned int seed
)
256 { _M_generator
.seed(seed
); }
262 const double eng_min
= _M_generator
.min();
263 const double eng_range
=
264 static_cast<const double>(_M_generator
.max() - eng_min
);
266 const double eng_res
=
267 static_cast<const double>(_M_generator() - eng_min
);
269 const double ret
= eng_res
/ eng_range
;
270 _GLIBCXX_DEBUG_ASSERT(ret
>= 0 && ret
<= 1);
274 twister_rand_gen
throw_allocator_base::_S_g
;
276 throw_allocator_base::map_type
277 throw_allocator_base::_S_map
;
279 double throw_allocator_base::_S_throw_prob
;
281 size_t throw_allocator_base::_S_label
= 0;
283 throw_allocator_base::entry_type
284 throw_allocator_base::make_entry(void* p
, size_t size
)
285 { return std::make_pair(p
, alloc_data_type(_S_label
, size
)); }
288 throw_allocator_base::init(unsigned long seed
)
292 throw_allocator_base::set_throw_prob(double throw_prob
)
293 { _S_throw_prob
= throw_prob
; }
296 throw_allocator_base::get_throw_prob()
297 { return _S_throw_prob
; }
300 throw_allocator_base::set_label(size_t l
)
304 throw_allocator_base::insert(void* p
, size_t size
)
306 const_iterator found_it
= _S_map
.find(p
);
307 if (found_it
!= _S_map
.end())
309 std::string
error("throw_allocator_base::insert");
310 error
+= "double insert!";
312 print_to_string(error
, make_entry(p
, size
));
313 print_to_string(error
, *found_it
);
314 throw std::logic_error(error
);
316 _S_map
.insert(make_entry(p
, size
));
320 throw_allocator_base::empty()
321 { return _S_map
.empty(); }
324 throw_allocator_base::erase(void* p
, size_t size
)
326 check_allocated(p
, size
);
331 throw_allocator_base::check_allocated(void* p
, size_t size
)
333 const_iterator found_it
= _S_map
.find(p
);
334 if (found_it
== _S_map
.end())
336 std::string
error("throw_allocator_base::check_allocated");
337 error
+= "null erase!";
339 print_to_string(error
, make_entry(p
, size
));
340 throw std::logic_error(error
);
343 if (found_it
->second
.second
!= size
)
345 std::string
error("throw_allocator_base::check_allocated");
346 error
+= "wrong-size erase!";
348 print_to_string(error
, make_entry(p
, size
));
349 print_to_string(error
, *found_it
);
350 throw std::logic_error(error
);
355 throw_allocator_base::throw_conditionally()
357 if (_S_g
.get_prob() < _S_throw_prob
)
358 throw forced_exception_error();
362 throw_allocator_base::print_to_string(std::string
& s
)
364 const_iterator it
= throw_allocator_base::_S_map
.begin();
365 const_iterator end_it
= throw_allocator_base::_S_map
.end();
366 for (; it
!= end_it
; ++it
)
367 print_to_string(s
, *it
);
372 throw_allocator_base::print_to_string(std::string
& s
, const_reference ref
)
374 s
+= reinterpret_cast<const unsigned long>(ref
.first
);
376 s
+= ref
.second
.first
;
378 s
+= ref
.second
.second
;
382 _GLIBCXX_END_NAMESPACE