c_locale.h (__convert_from_v): Only switch to the "C" locale if the current one isn...
[gcc.git] / libstdc++-v3 / testsuite / util / testsuite_allocator.h
1 // -*- C++ -*-
2 // Testing allocator for the C++ library testsuite.
3 //
4 // Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option)
11 // any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING. If not, write to the Free
20 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // USA.
22 //
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
31
32 // This file provides an test instrumentation allocator that can be
33 // used to verify allocation functionality of standard library
34 // containers. 2002.11.25 smw
35
36 #ifndef _GLIBCXX_TESTSUITE_ALLOCATOR_H
37 #define _GLIBCXX_TESTSUITE_ALLOCATOR_H
38
39 #include <cstddef>
40 #include <tr1/unordered_map>
41 #include <cassert>
42
43 namespace
44 {
45 bool new_called = false;
46 bool delete_called = false;
47 };
48
49 namespace __gnu_test
50 {
51 class tracker_allocator_counter
52 {
53 public:
54 typedef std::size_t size_type;
55
56 static void*
57 allocate(size_type blocksize)
58 {
59 allocationCount_ += blocksize;
60 return ::operator new(blocksize);
61 }
62
63 static void
64 construct() { constructCount_++; }
65
66 static void
67 destroy() { destructCount_++; }
68
69 static void
70 deallocate(void* p, size_type blocksize)
71 {
72 ::operator delete(p);
73 deallocationCount_ += blocksize;
74 }
75
76 static size_type
77 get_allocation_count() { return allocationCount_; }
78
79 static size_type
80 get_deallocation_count() { return deallocationCount_; }
81
82 static int
83 get_construct_count() { return constructCount_; }
84
85 static int
86 get_destruct_count() { return destructCount_; }
87
88 static void
89 reset()
90 {
91 allocationCount_ = 0;
92 deallocationCount_ = 0;
93 constructCount_ = 0;
94 destructCount_ = 0;
95 }
96
97 private:
98 static size_type allocationCount_;
99 static size_type deallocationCount_;
100 static int constructCount_;
101 static int destructCount_;
102 };
103
104 // A simple basic allocator that just forwards to the
105 // tracker_allocator_counter to fulfill memory requests. This class
106 // is templated on the target object type, but tracker isn't.
107 template<class T>
108 class tracker_allocator
109 {
110 private:
111 typedef tracker_allocator_counter counter_type;
112
113 public:
114 typedef T value_type;
115 typedef T* pointer;
116 typedef const T* const_pointer;
117 typedef T& reference;
118 typedef const T& const_reference;
119 typedef std::size_t size_type;
120 typedef std::ptrdiff_t difference_type;
121
122 template<class U> struct rebind { typedef tracker_allocator<U> other; };
123
124 pointer
125 address(reference value) const
126 { return &value; }
127
128 const_pointer
129 address(const_reference value) const
130 { return &value; }
131
132 tracker_allocator() throw()
133 { }
134
135 tracker_allocator(const tracker_allocator&) throw()
136 { }
137
138 template<class U>
139 tracker_allocator(const tracker_allocator<U>&) throw()
140 { }
141
142 ~tracker_allocator() throw()
143 { }
144
145 size_type
146 max_size() const throw()
147 { return size_type(-1) / sizeof(T); }
148
149 pointer
150 allocate(size_type n, const void* = 0)
151 { return static_cast<pointer>(counter_type::allocate(n * sizeof(T))); }
152
153 void
154 construct(pointer p, const T& value)
155 {
156 new (p) T(value);
157 counter_type::construct();
158 }
159
160 void
161 destroy(pointer p)
162 {
163 p->~T();
164 counter_type::destroy();
165 }
166
167 void
168 deallocate(pointer p, size_type num)
169 { counter_type::deallocate(p, num * sizeof(T)); }
170 };
171
172 template<class T1, class T2>
173 bool
174 operator==(const tracker_allocator<T1>&,
175 const tracker_allocator<T2>&) throw()
176 { return true; }
177
178 template<class T1, class T2>
179 bool
180 operator!=(const tracker_allocator<T1>&,
181 const tracker_allocator<T2>&) throw()
182 { return false; }
183
184 bool
185 check_construct_destroy(const char* tag, int expected_c, int expected_d);
186
187 template<typename Alloc, bool uses_global_new>
188 bool
189 check_new(Alloc a = Alloc())
190 {
191 bool test __attribute__((unused)) = true;
192 a.allocate(10);
193 test &= ( new_called == uses_global_new );
194 return test;
195 }
196
197 template<typename Alloc, bool uses_global_delete>
198 bool
199 check_delete(Alloc a = Alloc())
200 {
201 bool test __attribute__((unused)) = true;
202 typename Alloc::pointer p = a.allocate(10);
203 a.deallocate(p, 10);
204 test &= ( delete_called == uses_global_delete );
205 return test;
206 }
207
208 template<typename Alloc>
209 bool
210 check_deallocate_null()
211 {
212 // Let's not core here...
213 Alloc a;
214 a.deallocate(NULL, 1);
215 a.deallocate(NULL, 10);
216 return true;
217 }
218
219 template<typename Alloc>
220 bool
221 check_allocate_max_size()
222 {
223 Alloc a;
224 try
225 {
226 a.allocate(a.max_size() + 1);
227 }
228 catch(std::bad_alloc&)
229 {
230 return true;
231 }
232 catch(...)
233 {
234 throw;
235 }
236 throw;
237 }
238
239
240 // A simple allocator which can be constructed endowed of a given
241 // "personality" (an integer), queried in operator== to simulate the
242 // behavior of realworld "unequal" allocators (i.e., not exploiting
243 // the provision in 20.1.5/4, first bullet). A global unordered_map,
244 // filled at allocation time with (pointer, personality) pairs, is
245 // then consulted to enforce the requirements in Table 32 about
246 // deallocation vs allocator equality. Note that this allocator is
247 // swappable, not assignable, consistently with Option 3 of DR 431
248 // (see N1599).
249 struct uneq_allocator_base
250 {
251 typedef std::tr1::unordered_map<void*, int> map_type;
252
253 // Avoid static initialization troubles and/or bad interactions
254 // with tests linking testsuite_allocator.o and playing globally
255 // with operator new/delete.
256 static map_type&
257 get_map()
258 {
259 static map_type alloc_map;
260 return alloc_map;
261 }
262 };
263
264 template<typename Tp>
265 class uneq_allocator
266 : private uneq_allocator_base
267 {
268 public:
269 typedef size_t size_type;
270 typedef ptrdiff_t difference_type;
271 typedef Tp* pointer;
272 typedef const Tp* const_pointer;
273 typedef Tp& reference;
274 typedef const Tp& const_reference;
275 typedef Tp value_type;
276
277 template<typename Tp1>
278 struct rebind
279 { typedef uneq_allocator<Tp1> other; };
280
281 uneq_allocator() throw()
282 : personality(0) { }
283
284 uneq_allocator(int person) throw()
285 : personality(person) { }
286
287 template<typename Tp1>
288 uneq_allocator(const uneq_allocator<Tp1>& b) throw()
289 : personality(b.get_personality()) { }
290
291 int get_personality() const { return personality; }
292
293 pointer
294 address(reference x) const { return &x; }
295
296 const_pointer
297 address(const_reference x) const { return &x; }
298
299 pointer
300 allocate(size_type n, const void* = 0)
301 {
302 if (__builtin_expect(n > this->max_size(), false))
303 std::__throw_bad_alloc();
304
305 pointer p = static_cast<Tp*>(::operator new(n * sizeof(Tp)));
306 try
307 {
308 get_map().insert(map_type::value_type(reinterpret_cast<void*>(p),
309 personality));
310 }
311 catch(...)
312 {
313 ::operator delete(p);
314 __throw_exception_again;
315 }
316 return p;
317 }
318
319 void
320 deallocate(pointer p, size_type)
321 {
322 assert( p );
323
324 map_type::iterator it = get_map().find(reinterpret_cast<void*>(p));
325 assert( it != get_map().end() );
326
327 // Enforce requirements in Table 32 about deallocation vs
328 // allocator equality.
329 assert( it->second == personality );
330
331 get_map().erase(it);
332 ::operator delete(p);
333 }
334
335 size_type
336 max_size() const throw()
337 { return size_type(-1) / sizeof(Tp); }
338
339 void
340 construct(pointer p, const Tp& val)
341 { ::new(p) Tp(val); }
342
343 void
344 destroy(pointer p) { p->~Tp(); }
345
346 private:
347 // Not assignable...
348 uneq_allocator&
349 operator=(const uneq_allocator&);
350
351 // ... yet swappable!
352 friend inline void
353 swap(uneq_allocator& a, uneq_allocator& b)
354 { std::swap(a.personality, b.personality); }
355
356 template<typename Tp1>
357 friend inline bool
358 operator==(const uneq_allocator& a, const uneq_allocator<Tp1>& b)
359 { return a.personality == b.personality; }
360
361 template<typename Tp1>
362 friend inline bool
363 operator!=(const uneq_allocator& a, const uneq_allocator<Tp1>& b)
364 { return !(a == b); }
365
366 int personality;
367 };
368 }; // namespace __gnu_test
369
370 #endif // _GLIBCXX_TESTSUITE_ALLOCATOR_H