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 class tracker_allocator_counter
40 typedef std::size_t size_type;
43 allocate(size_type blocksize)
45 allocationCount_ += blocksize;
46 return ::operator new(blocksize);
50 construct() { constructCount_++; }
53 destroy() { destructCount_++; }
56 deallocate(void* p, size_type blocksize)
59 deallocationCount_ += blocksize;
63 get_allocation_count() { return allocationCount_; }
66 get_deallocation_count() { return deallocationCount_; }
69 get_construct_count() { return constructCount_; }
72 get_destruct_count() { return destructCount_; }
78 deallocationCount_ = 0;
84 static size_type allocationCount_;
85 static size_type deallocationCount_;
86 static int constructCount_;
87 static int destructCount_;
90 // A simple basic allocator that just forwards to the
91 // tracker_allocator_counter to fulfill memory requests. This class
92 // is templated on the target object type, but tracker isn't.
94 class tracker_allocator
97 typedef tracker_allocator_counter counter_type;
100 typedef T value_type;
102 typedef const T* const_pointer;
103 typedef T& reference;
104 typedef const T& const_reference;
105 typedef std::size_t size_type;
106 typedef std::ptrdiff_t difference_type;
108 template<class U> struct rebind { typedef tracker_allocator<U> other; };
111 address(reference value) const
115 address(const_reference value) const
118 tracker_allocator() throw()
121 tracker_allocator(const tracker_allocator&) throw()
125 tracker_allocator(const tracker_allocator<U>&) throw()
128 ~tracker_allocator() throw()
132 max_size() const throw()
133 { return size_type(-1) / sizeof(T); }
136 allocate(size_type n, const void* = 0)
137 { return static_cast<pointer>(counter_type::allocate(n * sizeof(T))); }
140 construct(pointer p, const T& value)
142 ::new ((void *)p) T(value);
143 counter_type::construct();
146 #ifdef __GXX_EXPERIMENTAL_CXX0X__
147 template<typename... Args>
149 construct(pointer p, Args&&... args)
151 ::new((void *)p) T(std::forward<Args>(args)...);
152 counter_type::construct();
160 counter_type::destroy();
164 deallocate(pointer p, size_type num)
165 { counter_type::deallocate(p, num * sizeof(T)); }
168 template<class T1, class T2>
170 operator==(const tracker_allocator<T1>&,
171 const tracker_allocator<T2>&) throw()
174 template<class T1, class T2>
176 operator!=(const tracker_allocator<T1>&,
177 const tracker_allocator<T2>&) throw()
181 check_construct_destroy(const char* tag, int expected_c, int expected_d);
183 template<typename Alloc>
185 check_deallocate_null()
187 // Let's not core here...
189 a.deallocate(NULL, 1);
190 a.deallocate(NULL, 10);
194 template<typename Alloc>
196 check_allocate_max_size()
201 a.allocate(a.max_size() + 1);
203 catch(std::bad_alloc&)
215 // A simple allocator which can be constructed endowed of a given
216 // "personality" (an integer), queried in operator== to simulate the
217 // behavior of realworld "unequal" allocators (i.e., not exploiting
218 // the provision in 20.1.5/4, first bullet). A global unordered_map,
219 // filled at allocation time with (pointer, personality) pairs, is
220 // then consulted to enforce the requirements in Table 32 about
221 // deallocation vs allocator equality. Note that this allocator is
222 // swappable, not assignable, consistently with Option 3 of DR 431
224 struct uneq_allocator_base
226 typedef std::tr1::unordered_map<void*, int> map_type;
228 // Avoid static initialization troubles and/or bad interactions
229 // with tests linking testsuite_allocator.o and playing globally
230 // with operator new/delete.
234 static map_type alloc_map;
239 template<typename Tp>
241 : private uneq_allocator_base
244 typedef size_t size_type;
245 typedef ptrdiff_t difference_type;
247 typedef const Tp* const_pointer;
248 typedef Tp& reference;
249 typedef const Tp& const_reference;
250 typedef Tp value_type;
252 template<typename Tp1>
254 { typedef uneq_allocator<Tp1> other; };
256 uneq_allocator() throw()
259 uneq_allocator(int person) throw()
260 : personality(person) { }
262 template<typename Tp1>
263 uneq_allocator(const uneq_allocator<Tp1>& b) throw()
264 : personality(b.get_personality()) { }
266 int get_personality() const { return personality; }
269 address(reference x) const { return &x; }
272 address(const_reference x) const { return &x; }
275 allocate(size_type n, const void* = 0)
277 if (__builtin_expect(n > this->max_size(), false))
278 std::__throw_bad_alloc();
280 pointer p = static_cast<Tp*>(::operator new(n * sizeof(Tp)));
283 get_map().insert(map_type::value_type(reinterpret_cast<void*>(p),
288 ::operator delete(p);
289 __throw_exception_again;
295 deallocate(pointer p, size_type)
299 map_type::iterator it = get_map().find(reinterpret_cast<void*>(p));
300 assert( it != get_map().end() );
302 // Enforce requirements in Table 32 about deallocation vs
303 // allocator equality.
304 assert( it->second == personality );
307 ::operator delete(p);
311 max_size() const throw()
312 { return size_type(-1) / sizeof(Tp); }
315 construct(pointer p, const Tp& val)
316 { ::new((void *)p) Tp(val); }
318 #ifdef __GXX_EXPERIMENTAL_CXX0X__
319 template<typename... Args>
321 construct(pointer p, Args&&... args)
322 { ::new((void *)p) Tp(std::forward<Args>(args)...); }
326 destroy(pointer p) { p->~Tp(); }
331 operator=(const uneq_allocator&);
333 // ... yet swappable!
335 swap(uneq_allocator& a, uneq_allocator& b)
336 { std::swap(a.personality, b.personality); }
338 template<typename Tp1>
340 operator==(const uneq_allocator& a, const uneq_allocator<Tp1>& b)
341 { return a.personality == b.personality; }
343 template<typename Tp1>
345 operator!=(const uneq_allocator& a, const uneq_allocator<Tp1>& b)
346 { return !(a == b); }
350 } // namespace __gnu_test
352 #endif // _GLIBCXX_TESTSUITE_ALLOCATOR_H