class.c (check_bases): Propagate non-literality.
[official-gcc.git] / libstdc++-v3 / testsuite / util / testsuite_allocator.h
blobcb481b2a7f93c1d02891b6c9747b25a27d310f69
1 // -*- C++ -*-
2 // Testing allocator for the C++ library testsuite.
3 //
4 // Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 3, 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 COPYING3. If not see
20 // <http://www.gnu.org/licenses/>.
23 // This file provides an test instrumentation allocator that can be
24 // used to verify allocation functionality of standard library
25 // containers. 2002.11.25 smw
27 #ifndef _GLIBCXX_TESTSUITE_ALLOCATOR_H
28 #define _GLIBCXX_TESTSUITE_ALLOCATOR_H
30 #include <tr1/unordered_map>
31 #include <cassert>
32 #include <bits/move.h>
34 namespace __gnu_test
36 class tracker_allocator_counter
38 public:
39 typedef std::size_t size_type;
41 static void*
42 allocate(size_type blocksize)
44 allocationCount_ += blocksize;
45 return ::operator new(blocksize);
48 static void
49 construct() { constructCount_++; }
51 static void
52 destroy() { destructCount_++; }
54 static void
55 deallocate(void* p, size_type blocksize)
57 ::operator delete(p);
58 deallocationCount_ += blocksize;
61 static size_type
62 get_allocation_count() { return allocationCount_; }
64 static size_type
65 get_deallocation_count() { return deallocationCount_; }
67 static int
68 get_construct_count() { return constructCount_; }
70 static int
71 get_destruct_count() { return destructCount_; }
73 static void
74 reset()
76 allocationCount_ = 0;
77 deallocationCount_ = 0;
78 constructCount_ = 0;
79 destructCount_ = 0;
82 private:
83 static size_type allocationCount_;
84 static size_type deallocationCount_;
85 static int constructCount_;
86 static int destructCount_;
89 // A simple basic allocator that just forwards to the
90 // tracker_allocator_counter to fulfill memory requests. This class
91 // is templated on the target object type, but tracker isn't.
92 template<class T>
93 class tracker_allocator
95 private:
96 typedef tracker_allocator_counter counter_type;
98 public:
99 typedef T value_type;
100 typedef T* pointer;
101 typedef const T* const_pointer;
102 typedef T& reference;
103 typedef const T& const_reference;
104 typedef std::size_t size_type;
105 typedef std::ptrdiff_t difference_type;
107 template<class U> struct rebind { typedef tracker_allocator<U> other; };
109 pointer
110 address(reference value) const
111 { return &value; }
113 const_pointer
114 address(const_reference value) const
115 { return &value; }
117 tracker_allocator() throw()
120 tracker_allocator(const tracker_allocator&) throw()
123 template<class U>
124 tracker_allocator(const tracker_allocator<U>&) throw()
127 ~tracker_allocator() throw()
130 size_type
131 max_size() const throw()
132 { return size_type(-1) / sizeof(T); }
134 pointer
135 allocate(size_type n, const void* = 0)
136 { return static_cast<pointer>(counter_type::allocate(n * sizeof(T))); }
138 void
139 construct(pointer p, const T& value)
141 ::new ((void *)p) T(value);
142 counter_type::construct();
145 #ifdef __GXX_EXPERIMENTAL_CXX0X__
146 template<typename... Args>
147 void
148 construct(pointer p, Args&&... args)
150 ::new((void *)p) T(std::forward<Args>(args)...);
151 counter_type::construct();
153 #endif
155 void
156 destroy(pointer p)
158 p->~T();
159 counter_type::destroy();
162 void
163 deallocate(pointer p, size_type num)
164 { counter_type::deallocate(p, num * sizeof(T)); }
167 template<class T1, class T2>
168 bool
169 operator==(const tracker_allocator<T1>&,
170 const tracker_allocator<T2>&) throw()
171 { return true; }
173 template<class T1, class T2>
174 bool
175 operator!=(const tracker_allocator<T1>&,
176 const tracker_allocator<T2>&) throw()
177 { return false; }
179 bool
180 check_construct_destroy(const char* tag, int expected_c, int expected_d);
182 template<typename Alloc>
183 bool
184 check_deallocate_null()
186 // Let's not core here...
187 Alloc a;
188 a.deallocate(0, 1);
189 a.deallocate(0, 10);
190 return true;
193 template<typename Alloc>
194 bool
195 check_allocate_max_size()
197 Alloc a;
200 a.allocate(a.max_size() + 1);
202 catch(std::bad_alloc&)
204 return true;
206 catch(...)
208 throw;
210 throw;
214 // A simple allocator which can be constructed endowed of a given
215 // "personality" (an integer), queried in operator== to simulate the
216 // behavior of realworld "unequal" allocators (i.e., not exploiting
217 // the provision in 20.1.5/4, first bullet). A global unordered_map,
218 // filled at allocation time with (pointer, personality) pairs, is
219 // then consulted to enforce the requirements in Table 32 about
220 // deallocation vs allocator equality. Note that this allocator is
221 // swappable, not assignable, consistently with Option 3 of DR 431
222 // (see N1599).
223 struct uneq_allocator_base
225 typedef std::tr1::unordered_map<void*, int> map_type;
227 // Avoid static initialization troubles and/or bad interactions
228 // with tests linking testsuite_allocator.o and playing globally
229 // with operator new/delete.
230 static map_type&
231 get_map()
233 static map_type alloc_map;
234 return alloc_map;
238 template<typename Tp>
239 class uneq_allocator
240 : private uneq_allocator_base
242 public:
243 typedef std::size_t size_type;
244 typedef std::ptrdiff_t difference_type;
245 typedef Tp* pointer;
246 typedef const Tp* const_pointer;
247 typedef Tp& reference;
248 typedef const Tp& const_reference;
249 typedef Tp value_type;
251 template<typename Tp1>
252 struct rebind
253 { typedef uneq_allocator<Tp1> other; };
255 uneq_allocator() throw()
256 : personality(0) { }
258 uneq_allocator(int person) throw()
259 : personality(person) { }
261 template<typename Tp1>
262 uneq_allocator(const uneq_allocator<Tp1>& b) throw()
263 : personality(b.get_personality()) { }
265 int get_personality() const { return personality; }
267 pointer
268 address(reference x) const { return &x; }
270 const_pointer
271 address(const_reference x) const { return &x; }
273 pointer
274 allocate(size_type n, const void* = 0)
276 if (__builtin_expect(n > this->max_size(), false))
277 std::__throw_bad_alloc();
279 pointer p = static_cast<Tp*>(::operator new(n * sizeof(Tp)));
282 get_map().insert(map_type::value_type(reinterpret_cast<void*>(p),
283 personality));
285 catch(...)
287 ::operator delete(p);
288 __throw_exception_again;
290 return p;
293 void
294 deallocate(pointer p, size_type)
296 assert( p );
298 map_type::iterator it = get_map().find(reinterpret_cast<void*>(p));
299 assert( it != get_map().end() );
301 // Enforce requirements in Table 32 about deallocation vs
302 // allocator equality.
303 assert( it->second == personality );
305 get_map().erase(it);
306 ::operator delete(p);
309 size_type
310 max_size() const throw()
311 { return size_type(-1) / sizeof(Tp); }
313 void
314 construct(pointer p, const Tp& val)
315 { ::new((void *)p) Tp(val); }
317 #ifdef __GXX_EXPERIMENTAL_CXX0X__
318 template<typename... Args>
319 void
320 construct(pointer p, Args&&... args)
321 { ::new((void *)p) Tp(std::forward<Args>(args)...); }
322 #endif
324 void
325 destroy(pointer p) { p->~Tp(); }
327 private:
328 // Not assignable...
329 uneq_allocator&
330 operator=(const uneq_allocator&);
332 // ... yet swappable!
333 friend inline void
334 swap(uneq_allocator& a, uneq_allocator& b)
335 { std::swap(a.personality, b.personality); }
337 template<typename Tp1>
338 friend inline bool
339 operator==(const uneq_allocator& a, const uneq_allocator<Tp1>& b)
340 { return a.personality == b.personality; }
342 template<typename Tp1>
343 friend inline bool
344 operator!=(const uneq_allocator& a, const uneq_allocator<Tp1>& b)
345 { return !(a == b); }
347 int personality;
349 } // namespace __gnu_test
351 #endif // _GLIBCXX_TESTSUITE_ALLOCATOR_H