Merge -r 127928:132243 from trunk
[official-gcc.git] / libstdc++-v3 / testsuite / util / testsuite_allocator.h
blob6411568c26d4e3a1a943943de89445c4457f82a7
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.
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.
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.
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.
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
36 #ifndef _GLIBCXX_TESTSUITE_ALLOCATOR_H
37 #define _GLIBCXX_TESTSUITE_ALLOCATOR_H
39 #include <cstddef>
40 #include <tr1/unordered_map>
41 #include <cassert>
42 #include <bits/stl_move.h>
44 namespace
46 bool new_called = false;
47 bool delete_called = false;
50 namespace __gnu_test
52 class tracker_allocator_counter
54 public:
55 typedef std::size_t size_type;
57 static void*
58 allocate(size_type blocksize)
60 allocationCount_ += blocksize;
61 return ::operator new(blocksize);
64 static void
65 construct() { constructCount_++; }
67 static void
68 destroy() { destructCount_++; }
70 static void
71 deallocate(void* p, size_type blocksize)
73 ::operator delete(p);
74 deallocationCount_ += blocksize;
77 static size_type
78 get_allocation_count() { return allocationCount_; }
80 static size_type
81 get_deallocation_count() { return deallocationCount_; }
83 static int
84 get_construct_count() { return constructCount_; }
86 static int
87 get_destruct_count() { return destructCount_; }
89 static void
90 reset()
92 allocationCount_ = 0;
93 deallocationCount_ = 0;
94 constructCount_ = 0;
95 destructCount_ = 0;
98 private:
99 static size_type allocationCount_;
100 static size_type deallocationCount_;
101 static int constructCount_;
102 static int destructCount_;
105 // A simple basic allocator that just forwards to the
106 // tracker_allocator_counter to fulfill memory requests. This class
107 // is templated on the target object type, but tracker isn't.
108 template<class T>
109 class tracker_allocator
111 private:
112 typedef tracker_allocator_counter counter_type;
114 public:
115 typedef T value_type;
116 typedef T* pointer;
117 typedef const T* const_pointer;
118 typedef T& reference;
119 typedef const T& const_reference;
120 typedef std::size_t size_type;
121 typedef std::ptrdiff_t difference_type;
123 template<class U> struct rebind { typedef tracker_allocator<U> other; };
125 pointer
126 address(reference value) const
127 { return &value; }
129 const_pointer
130 address(const_reference value) const
131 { return &value; }
133 tracker_allocator() throw()
136 tracker_allocator(const tracker_allocator&) throw()
139 template<class U>
140 tracker_allocator(const tracker_allocator<U>&) throw()
143 ~tracker_allocator() throw()
146 size_type
147 max_size() const throw()
148 { return size_type(-1) / sizeof(T); }
150 pointer
151 allocate(size_type n, const void* = 0)
152 { return static_cast<pointer>(counter_type::allocate(n * sizeof(T))); }
154 void
155 construct(pointer p, const T& value)
157 ::new ((void *)p) T(value);
158 counter_type::construct();
161 #ifdef __GXX_EXPERIMENTAL_CXX0X__
162 template<typename... Args>
163 void
164 construct(pointer p, Args&&... args)
166 ::new((void *)p) T(std::forward<Args>(args)...);
167 counter_type::construct();
169 #endif
171 void
172 destroy(pointer p)
174 p->~T();
175 counter_type::destroy();
178 void
179 deallocate(pointer p, size_type num)
180 { counter_type::deallocate(p, num * sizeof(T)); }
183 template<class T1, class T2>
184 bool
185 operator==(const tracker_allocator<T1>&,
186 const tracker_allocator<T2>&) throw()
187 { return true; }
189 template<class T1, class T2>
190 bool
191 operator!=(const tracker_allocator<T1>&,
192 const tracker_allocator<T2>&) throw()
193 { return false; }
195 bool
196 check_construct_destroy(const char* tag, int expected_c, int expected_d);
198 template<typename Alloc, bool uses_global_new>
199 bool
200 check_new(Alloc a = Alloc())
202 bool test __attribute__((unused)) = true;
203 a.allocate(10);
204 test &= ( new_called == uses_global_new );
205 return test;
208 template<typename Alloc, bool uses_global_delete>
209 bool
210 check_delete(Alloc a = Alloc())
212 bool test __attribute__((unused)) = true;
213 typename Alloc::pointer p = a.allocate(10);
214 a.deallocate(p, 10);
215 test &= ( delete_called == uses_global_delete );
216 return test;
219 template<typename Alloc>
220 bool
221 check_deallocate_null()
223 // Let's not core here...
224 Alloc a;
225 a.deallocate(NULL, 1);
226 a.deallocate(NULL, 10);
227 return true;
230 template<typename Alloc>
231 bool
232 check_allocate_max_size()
234 Alloc a;
237 a.allocate(a.max_size() + 1);
239 catch(std::bad_alloc&)
241 return true;
243 catch(...)
245 throw;
247 throw;
251 // A simple allocator which can be constructed endowed of a given
252 // "personality" (an integer), queried in operator== to simulate the
253 // behavior of realworld "unequal" allocators (i.e., not exploiting
254 // the provision in 20.1.5/4, first bullet). A global unordered_map,
255 // filled at allocation time with (pointer, personality) pairs, is
256 // then consulted to enforce the requirements in Table 32 about
257 // deallocation vs allocator equality. Note that this allocator is
258 // swappable, not assignable, consistently with Option 3 of DR 431
259 // (see N1599).
260 struct uneq_allocator_base
262 typedef std::tr1::unordered_map<void*, int> map_type;
264 // Avoid static initialization troubles and/or bad interactions
265 // with tests linking testsuite_allocator.o and playing globally
266 // with operator new/delete.
267 static map_type&
268 get_map()
270 static map_type alloc_map;
271 return alloc_map;
275 template<typename Tp>
276 class uneq_allocator
277 : private uneq_allocator_base
279 public:
280 typedef size_t size_type;
281 typedef ptrdiff_t difference_type;
282 typedef Tp* pointer;
283 typedef const Tp* const_pointer;
284 typedef Tp& reference;
285 typedef const Tp& const_reference;
286 typedef Tp value_type;
288 template<typename Tp1>
289 struct rebind
290 { typedef uneq_allocator<Tp1> other; };
292 uneq_allocator() throw()
293 : personality(0) { }
295 uneq_allocator(int person) throw()
296 : personality(person) { }
298 template<typename Tp1>
299 uneq_allocator(const uneq_allocator<Tp1>& b) throw()
300 : personality(b.get_personality()) { }
302 int get_personality() const { return personality; }
304 pointer
305 address(reference x) const { return &x; }
307 const_pointer
308 address(const_reference x) const { return &x; }
310 pointer
311 allocate(size_type n, const void* = 0)
313 if (__builtin_expect(n > this->max_size(), false))
314 std::__throw_bad_alloc();
316 pointer p = static_cast<Tp*>(::operator new(n * sizeof(Tp)));
319 get_map().insert(map_type::value_type(reinterpret_cast<void*>(p),
320 personality));
322 catch(...)
324 ::operator delete(p);
325 __throw_exception_again;
327 return p;
330 void
331 deallocate(pointer p, size_type)
333 assert( p );
335 map_type::iterator it = get_map().find(reinterpret_cast<void*>(p));
336 assert( it != get_map().end() );
338 // Enforce requirements in Table 32 about deallocation vs
339 // allocator equality.
340 assert( it->second == personality );
342 get_map().erase(it);
343 ::operator delete(p);
346 size_type
347 max_size() const throw()
348 { return size_type(-1) / sizeof(Tp); }
350 void
351 construct(pointer p, const Tp& val)
352 { ::new((void *)p) Tp(val); }
354 #ifdef __GXX_EXPERIMENTAL_CXX0X__
355 template<typename... Args>
356 void
357 construct(pointer p, Args&&... args)
358 { ::new((void *)p) Tp(std::forward<Args>(args)...); }
359 #endif
361 void
362 destroy(pointer p) { p->~Tp(); }
364 private:
365 // Not assignable...
366 uneq_allocator&
367 operator=(const uneq_allocator&);
369 // ... yet swappable!
370 friend inline void
371 swap(uneq_allocator& a, uneq_allocator& b)
372 { std::swap(a.personality, b.personality); }
374 template<typename Tp1>
375 friend inline bool
376 operator==(const uneq_allocator& a, const uneq_allocator<Tp1>& b)
377 { return a.personality == b.personality; }
379 template<typename Tp1>
380 friend inline bool
381 operator!=(const uneq_allocator& a, const uneq_allocator<Tp1>& b)
382 { return !(a == b); }
384 int personality;
386 } // namespace __gnu_test
388 #endif // _GLIBCXX_TESTSUITE_ALLOCATOR_H