]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/ext/throw_allocator.h
Remove conflict marker.
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / throw_allocator.h
CommitLineData
a86151e1
BK
1// -*- C++ -*-
2
113008b5 3// Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
a86151e1
BK
4//
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
9// version.
10
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.
15
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.
20
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
29// Public License.
30
31// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32
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
40// warranty.
41
78a53887
BK
42/** @file ext/vstring.h
43 * This file is a GNU extension to the Standard C++ Library.
44 *
45 * Contains an exception-throwing allocator, useful for testing
46 * exception safety. In addition, allocation addresses are stored and
47 * sanity checked.
48 */
49
a86151e1 50/**
78a53887 51 * @file throw_allocator.h
a86151e1
BK
52 */
53
54#ifndef _THROW_ALLOCATOR_H
55#define _THROW_ALLOCATOR_H 1
56
57#include <cmath>
67a7356b 58#include <ctime>
a86151e1
BK
59#include <map>
60#include <set>
61#include <string>
62#include <ostream>
63#include <stdexcept>
64#include <utility>
65#include <tr1/random>
66
67_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
68
69 class twister_rand_gen
70 {
71 public:
72 twister_rand_gen(unsigned int seed =
73 static_cast<unsigned int>(std::time(0)));
74
75 void
76 init(unsigned int);
77
78 double
79 get_prob();
80
81 private:
82 std::tr1::mt19937 _M_generator;
83 };
84
85
86 struct forced_exception_error : public std::exception
87 { };
88
89 class throw_allocator_base
90 {
91 public:
92 void
93 init(unsigned long seed);
94
95 static void
96 set_throw_prob(double throw_prob);
97
98 static double
99 get_throw_prob();
100
101 static void
102 set_label(size_t l);
103
104 static bool
105 empty();
106
107 struct group_throw_prob_adjustor
108 {
109 group_throw_prob_adjustor(size_t size)
110 : _M_throw_prob_orig(_S_throw_prob)
111 {
112 _S_throw_prob =
113008b5 113 1 - std::pow(double(1 - _S_throw_prob), double(0.5 / (size + 1)));
a86151e1
BK
114 }
115
116 ~group_throw_prob_adjustor()
117 { _S_throw_prob = _M_throw_prob_orig; }
118
119 private:
120 const double _M_throw_prob_orig;
121 };
122
123 struct zero_throw_prob_adjustor
124 {
125 zero_throw_prob_adjustor() : _M_throw_prob_orig(_S_throw_prob)
126 { _S_throw_prob = 0; }
127
128 ~zero_throw_prob_adjustor()
129 { _S_throw_prob = _M_throw_prob_orig; }
130
131 private:
132 const double _M_throw_prob_orig;
133 };
134
135 protected:
136 static void
137 insert(void*, size_t);
138
139 static void
140 erase(void*, size_t);
141
142 static void
143 throw_conditionally();
144
3441f106
BK
145 // See if a particular address and size has been allocated by this
146 // allocator.
a86151e1 147 static void
3441f106 148 check_allocated(void*, size_t);
a86151e1 149
3441f106 150 // See if a given label has been allocated by this allocator.
a86151e1 151 static void
3441f106 152 check_allocated(size_t);
a86151e1
BK
153
154 private:
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;
160
161 friend std::ostream&
162 operator<<(std::ostream&, const throw_allocator_base&);
163
164 static entry_type
165 make_entry(void*, size_t);
166
167 static void
168 print_to_string(std::string&);
169
170 static void
171 print_to_string(std::string&, const_reference);
172
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;
177 };
178
179
180 template<typename T>
181 class throw_allocator : public throw_allocator_base
182 {
183 public:
3441f106
BK
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;
191
a86151e1
BK
192
193 template<typename U>
194 struct rebind
195 {
196 typedef throw_allocator<U> other;
197 };
198
199 throw_allocator() throw() { }
200
3441f106 201 throw_allocator(const throw_allocator&) throw() { }
a86151e1 202
3441f106 203 template<typename U>
a86151e1
BK
204 throw_allocator(const throw_allocator<U>&) throw() { }
205
206 ~throw_allocator() throw() { }
207
208 size_type
209 max_size() const throw()
3441f106 210 { return std::allocator<value_type>().max_size(); }
a86151e1
BK
211
212 pointer
213 allocate(size_type num, std::allocator<void>::const_pointer hint = 0)
214 {
215 throw_conditionally();
3441f106
BK
216 value_type* const a = std::allocator<value_type>().allocate(num, hint);
217 insert(a, sizeof(value_type) * num);
a86151e1
BK
218 return a;
219 }
220
221 void
222 construct(pointer p, const T& val)
3441f106 223 { return std::allocator<value_type>().construct(p, val); }
a86151e1
BK
224
225 void
226 destroy(pointer p)
3441f106 227 { std::allocator<value_type>().destroy(p); }
a86151e1
BK
228
229 void
230 deallocate(pointer p, size_type num)
231 {
3441f106
BK
232 erase(p, sizeof(value_type) * num);
233 std::allocator<value_type>().deallocate(p, num);
a86151e1
BK
234 }
235
236 void
237 check_allocated(pointer p, size_type num)
3441f106
BK
238 { throw_allocator_base::check_allocated(p, sizeof(value_type) * num); }
239
240 void
241 check_allocated(size_type label)
242 { throw_allocator_base::check_allocated(label); }
a86151e1
BK
243 };
244
245 template<typename T>
246 inline bool
247 operator==(const throw_allocator<T>&, const throw_allocator<T>&)
248 { return true; }
249
250 template<typename T>
251 inline bool
252 operator!=(const throw_allocator<T>&, const throw_allocator<T>&)
253 { return false; }
254
255 std::ostream&
256 operator<<(std::ostream& os, const throw_allocator_base& alloc)
257 {
258 std::string error;
259 throw_allocator_base::print_to_string(error);
260 os << error;
261 return os;
262 }
263
264 // XXX Should be in .cc.
265 twister_rand_gen::
266 twister_rand_gen(unsigned int seed) : _M_generator(seed) { }
267
268 void
269 twister_rand_gen::
270 init(unsigned int seed)
271 { _M_generator.seed(seed); }
272
273 double
274 twister_rand_gen::
275 get_prob()
276 {
277 const double eng_min = _M_generator.min();
278 const double eng_range =
279 static_cast<const double>(_M_generator.max() - eng_min);
280
281 const double eng_res =
282 static_cast<const double>(_M_generator() - eng_min);
283
284 const double ret = eng_res / eng_range;
285 _GLIBCXX_DEBUG_ASSERT(ret >= 0 && ret <= 1);
286 return ret;
287 }
288
289 twister_rand_gen throw_allocator_base::_S_g;
290
291 throw_allocator_base::map_type
292 throw_allocator_base::_S_map;
293
294 double throw_allocator_base::_S_throw_prob;
295
296 size_t throw_allocator_base::_S_label = 0;
297
298 throw_allocator_base::entry_type
299 throw_allocator_base::make_entry(void* p, size_t size)
300 { return std::make_pair(p, alloc_data_type(_S_label, size)); }
301
302 void
303 throw_allocator_base::init(unsigned long seed)
304 { _S_g.init(seed); }
305
306 void
307 throw_allocator_base::set_throw_prob(double throw_prob)
308 { _S_throw_prob = throw_prob; }
309
310 double
311 throw_allocator_base::get_throw_prob()
312 { return _S_throw_prob; }
313
314 void
315 throw_allocator_base::set_label(size_t l)
316 { _S_label = l; }
317
318 void
319 throw_allocator_base::insert(void* p, size_t size)
320 {
321 const_iterator found_it = _S_map.find(p);
322 if (found_it != _S_map.end())
323 {
324 std::string error("throw_allocator_base::insert");
325 error += "double insert!";
326 error += '\n';
327 print_to_string(error, make_entry(p, size));
328 print_to_string(error, *found_it);
329 throw std::logic_error(error);
330 }
331 _S_map.insert(make_entry(p, size));
332 }
333
334 bool
335 throw_allocator_base::empty()
336 { return _S_map.empty(); }
337
338 void
339 throw_allocator_base::erase(void* p, size_t size)
340 {
341 check_allocated(p, size);
342 _S_map.erase(p);
343 }
344
345 void
346 throw_allocator_base::check_allocated(void* p, size_t size)
347 {
348 const_iterator found_it = _S_map.find(p);
349 if (found_it == _S_map.end())
350 {
3441f106 351 std::string error("throw_allocator_base::check_allocated by value ");
a86151e1
BK
352 error += "null erase!";
353 error += '\n';
354 print_to_string(error, make_entry(p, size));
355 throw std::logic_error(error);
356 }
357
358 if (found_it->second.second != size)
359 {
3441f106 360 std::string error("throw_allocator_base::check_allocated by value ");
a86151e1
BK
361 error += "wrong-size erase!";
362 error += '\n';
363 print_to_string(error, make_entry(p, size));
364 print_to_string(error, *found_it);
365 throw std::logic_error(error);
366 }
367 }
368
3441f106
BK
369 void
370 throw_allocator_base::check_allocated(size_t label)
371 {
372 std::string found;
373 const_iterator it = _S_map.begin();
374 while (it != _S_map.end())
375 {
376 if (it->second.first == label)
377 print_to_string(found, *it);
378 ++it;
379 }
380
381 if (!found.empty())
382 {
383 std::string error("throw_allocator_base::check_allocated by label ");
384 error += '\n';
385 error += found;
386 throw std::logic_error(error);
387 }
388 }
389
a86151e1
BK
390 void
391 throw_allocator_base::throw_conditionally()
392 {
393 if (_S_g.get_prob() < _S_throw_prob)
394 throw forced_exception_error();
395 }
396
397 void
398 throw_allocator_base::print_to_string(std::string& s)
399 {
3441f106
BK
400 const_iterator begin = throw_allocator_base::_S_map.begin();
401 const_iterator end = throw_allocator_base::_S_map.end();
402 for (; begin != end; ++begin)
403 print_to_string(s, *begin);
a86151e1
BK
404 }
405
406 void
407 throw_allocator_base::print_to_string(std::string& s, const_reference ref)
408 {
3441f106
BK
409 char buf[40];
410 const char tab('\t');
411 s += "address: ";
412 sprintf(buf, "%p", ref.first);
413 s += buf;
414 s += tab;
415 s += "label: ";
416 sprintf(buf, "%u", ref.second.first);
417 s += buf;
418 s += tab;
419 s += "size: ";
420 sprintf(buf, "%u", ref.second.second);
421 s += buf;
a86151e1
BK
422 s += '\n';
423 }
424
425_GLIBCXX_END_NAMESPACE
426
427#endif