2 // Testing allocator for the C++ library testsuite.
4 // Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 // Free Software Foundation, Inc.
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)
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
31 #include <tr1/unordered_map>
33 #include <bits/move.h>
37 bool new_called = false;
38 bool delete_called = false;
43 class tracker_allocator_counter
46 typedef std::size_t size_type;
49 allocate(size_type blocksize)
51 allocationCount_ += blocksize;
52 return ::operator new(blocksize);
56 construct() { constructCount_++; }
59 destroy() { destructCount_++; }
62 deallocate(void* p, size_type blocksize)
65 deallocationCount_ += blocksize;
69 get_allocation_count() { return allocationCount_; }
72 get_deallocation_count() { return deallocationCount_; }
75 get_construct_count() { return constructCount_; }
78 get_destruct_count() { return destructCount_; }
84 deallocationCount_ = 0;
90 static size_type allocationCount_;
91 static size_type deallocationCount_;
92 static int constructCount_;
93 static int destructCount_;
96 // A simple basic allocator that just forwards to the
97 // tracker_allocator_counter to fulfill memory requests. This class
98 // is templated on the target object type, but tracker isn't.
100 class tracker_allocator
103 typedef tracker_allocator_counter counter_type;
106 typedef T value_type;
108 typedef const T* const_pointer;
109 typedef T& reference;
110 typedef const T& const_reference;
111 typedef std::size_t size_type;
112 typedef std::ptrdiff_t difference_type;
114 template<class U> struct rebind { typedef tracker_allocator<U> other; };
117 address(reference value) const
121 address(const_reference value) const
124 tracker_allocator() throw()
127 tracker_allocator(const tracker_allocator&) throw()
131 tracker_allocator(const tracker_allocator<U>&) throw()
134 ~tracker_allocator() throw()
138 max_size() const throw()
139 { return size_type(-1) / sizeof(T); }
142 allocate(size_type n, const void* = 0)
143 { return static_cast<pointer>(counter_type::allocate(n * sizeof(T))); }
146 construct(pointer p, const T& value)
148 ::new ((void *)p) T(value);
149 counter_type::construct();
152 #ifdef __GXX_EXPERIMENTAL_CXX0X__
153 template<typename... Args>
155 construct(pointer p, Args&&... args)
157 ::new((void *)p) T(std::forward<Args>(args)...);
158 counter_type::construct();
166 counter_type::destroy();
170 deallocate(pointer p, size_type num)
171 { counter_type::deallocate(p, num * sizeof(T)); }
174 template<class T1, class T2>
176 operator==(const tracker_allocator<T1>&,
177 const tracker_allocator<T2>&) throw()
180 template<class T1, class T2>
182 operator!=(const tracker_allocator<T1>&,
183 const tracker_allocator<T2>&) throw()
187 check_construct_destroy(const char* tag, int expected_c, int expected_d);
189 template<typename Alloc, bool uses_global_new>
191 check_new(Alloc a = Alloc())
193 bool test __attribute__((unused)) = true;
195 test &= ( new_called == uses_global_new );
199 template<typename Alloc, bool uses_global_delete>
201 check_delete(Alloc a = Alloc())
203 bool test __attribute__((unused)) = true;
204 typename Alloc::pointer p = a.allocate(10);
206 test &= ( delete_called == uses_global_delete );
210 template<typename Alloc>
212 check_deallocate_null()
214 // Let's not core here...
216 a.deallocate(NULL, 1);
217 a.deallocate(NULL, 10);
221 template<typename Alloc>
223 check_allocate_max_size()
228 a.allocate(a.max_size() + 1);
230 catch(std::bad_alloc&)
242 // A simple allocator which can be constructed endowed of a given
243 // "personality" (an integer), queried in operator== to simulate the
244 // behavior of realworld "unequal" allocators (i.e., not exploiting
245 // the provision in 20.1.5/4, first bullet). A global unordered_map,
246 // filled at allocation time with (pointer, personality) pairs, is
247 // then consulted to enforce the requirements in Table 32 about
248 // deallocation vs allocator equality. Note that this allocator is
249 // swappable, not assignable, consistently with Option 3 of DR 431
251 struct uneq_allocator_base
253 typedef std::tr1::unordered_map<void*, int> map_type;
255 // Avoid static initialization troubles and/or bad interactions
256 // with tests linking testsuite_allocator.o and playing globally
257 // with operator new/delete.
261 static map_type alloc_map;
266 template<typename Tp>
268 : private uneq_allocator_base
271 typedef size_t size_type;
272 typedef ptrdiff_t difference_type;
274 typedef const Tp* const_pointer;
275 typedef Tp& reference;
276 typedef const Tp& const_reference;
277 typedef Tp value_type;
279 template<typename Tp1>
281 { typedef uneq_allocator<Tp1> other; };
283 uneq_allocator() throw()
286 uneq_allocator(int person) throw()
287 : personality(person) { }
289 template<typename Tp1>
290 uneq_allocator(const uneq_allocator<Tp1>& b) throw()
291 : personality(b.get_personality()) { }
293 int get_personality() const { return personality; }
296 address(reference x) const { return &x; }
299 address(const_reference x) const { return &x; }
302 allocate(size_type n, const void* = 0)
304 if (__builtin_expect(n > this->max_size(), false))
305 std::__throw_bad_alloc();
307 pointer p = static_cast<Tp*>(::operator new(n * sizeof(Tp)));
310 get_map().insert(map_type::value_type(reinterpret_cast<void*>(p),
315 ::operator delete(p);
316 __throw_exception_again;
322 deallocate(pointer p, size_type)
326 map_type::iterator it = get_map().find(reinterpret_cast<void*>(p));
327 assert( it != get_map().end() );
329 // Enforce requirements in Table 32 about deallocation vs
330 // allocator equality.
331 assert( it->second == personality );
334 ::operator delete(p);
338 max_size() const throw()
339 { return size_type(-1) / sizeof(Tp); }
342 construct(pointer p, const Tp& val)
343 { ::new((void *)p) Tp(val); }
345 #ifdef __GXX_EXPERIMENTAL_CXX0X__
346 template<typename... Args>
348 construct(pointer p, Args&&... args)
349 { ::new((void *)p) Tp(std::forward<Args>(args)...); }
353 destroy(pointer p) { p->~Tp(); }
358 operator=(const uneq_allocator&);
360 // ... yet swappable!
362 swap(uneq_allocator& a, uneq_allocator& b)
363 { std::swap(a.personality, b.personality); }
365 template<typename Tp1>
367 operator==(const uneq_allocator& a, const uneq_allocator<Tp1>& b)
368 { return a.personality == b.personality; }
370 template<typename Tp1>
372 operator!=(const uneq_allocator& a, const uneq_allocator<Tp1>& b)
373 { return !(a == b); }
377 } // namespace __gnu_test
379 #endif // _GLIBCXX_TESTSUITE_ALLOCATOR_H