2 // Testing allocator for the C++ library testsuite.
4 // Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 // This file provides an test instrumentation allocator that can be
32 // used to verify allocation functionality of standard library
33 // containers. 2002.11.25 smw
35 #ifndef _GLIBCXX_TESTSUITE_ALLOCATOR_H
36 #define _GLIBCXX_TESTSUITE_ALLOCATOR_H
40 #include <tr1/unordered_map>
45 bool new_called = false;
46 bool delete_called = false;
51 class allocation_tracker
54 typedef std::size_t size_type;
57 allocate(size_type blocksize)
59 allocationTotal_ += blocksize;
60 return ::operator new(blocksize);
64 construct() { constructCount_++; }
67 destroy() { destructCount_++; }
70 deallocate(void* p, size_type blocksize)
73 deallocationTotal_ += blocksize;
77 allocationTotal() { return allocationTotal_; }
80 deallocationTotal() { return deallocationTotal_; }
83 constructCount() { return constructCount_; }
86 destructCount() { return destructCount_; }
92 deallocationTotal_ = 0;
98 static size_type allocationTotal_;
99 static size_type deallocationTotal_;
100 static int constructCount_;
101 static int destructCount_;
104 // A simple basic allocator that just forwards to the
105 // allocation_tracker to fulfill memory requests. This class is
106 // templated on the target object type, but tracker isn't.
111 typedef T value_type;
113 typedef const T* const_pointer;
114 typedef T& reference;
115 typedef const T& const_reference;
116 typedef std::size_t size_type;
117 typedef std::ptrdiff_t difference_type;
119 template<class U> struct rebind { typedef tracker_alloc<U> other; };
122 address(reference value) const
126 address(const_reference value) const
129 tracker_alloc() throw()
132 tracker_alloc(const tracker_alloc&) throw()
136 tracker_alloc(const tracker_alloc<U>&) throw()
139 ~tracker_alloc() throw()
143 max_size() const throw()
144 { return std::numeric_limits<std::size_t>::max() / sizeof(T); }
147 allocate(size_type n, const void* = 0)
149 return static_cast<pointer>(allocation_tracker::allocate(n * sizeof(T)));
153 construct(pointer p, const T& value)
156 allocation_tracker::construct();
163 allocation_tracker::destroy();
167 deallocate(pointer p, size_type num)
168 { allocation_tracker::deallocate(p, num * sizeof(T)); }
171 template<class T1, class T2>
173 operator==(const tracker_alloc<T1>&, const tracker_alloc<T2>&) throw()
176 template<class T1, class T2>
178 operator!=(const tracker_alloc<T1>&, const tracker_alloc<T2>&) throw()
182 check_construct_destroy(const char* tag, int expected_c, int expected_d);
184 template<typename Alloc, bool uses_global_new>
186 check_new(Alloc a = Alloc())
188 bool test __attribute__((unused)) = true;
190 test &= ( new_called == uses_global_new );
194 template<typename Alloc, bool uses_global_delete>
196 check_delete(Alloc a = Alloc())
198 bool test __attribute__((unused)) = true;
199 typename Alloc::pointer p = a.allocate(10);
201 test &= ( delete_called == uses_global_delete );
205 template<typename Alloc>
207 check_deallocate_null()
209 // Let's not core here...
211 a.deallocate(NULL, 1);
212 a.deallocate(NULL, 10);
216 template<typename Alloc>
218 check_allocate_max_size()
223 a.allocate(a.max_size() + 1);
225 catch(std::bad_alloc&)
237 // A simple allocator which can be constructed endowed of a given
238 // "personality" (an integer), queried in operator== to simulate the
239 // behavior of realworld "unequal" allocators (i.e., not exploiting
240 // the provision in 20.1.5/4, first bullet). A global unordered_map,
241 // filled at allocation time with (pointer, personality) pairs, is
242 // then consulted to enforce the requirements in Table 32 about
243 // deallocation vs allocator equality. Note that this allocator is
244 // swappable, not assignable, consistently with Option 3 of DR 431
246 struct uneq_allocator_base
248 typedef std::tr1::unordered_map<void*, int> map_type;
250 // Avoid static initialization troubles and/or bad interactions
251 // with tests linking testsuite_allocator.o and playing globally
252 // with operator new/delete.
256 static map_type alloc_map;
261 template<typename Tp>
263 : private uneq_allocator_base
266 typedef size_t size_type;
267 typedef ptrdiff_t difference_type;
269 typedef const Tp* const_pointer;
270 typedef Tp& reference;
271 typedef const Tp& const_reference;
272 typedef Tp value_type;
274 template<typename Tp1>
276 { typedef uneq_allocator<Tp1> other; };
278 uneq_allocator() throw()
281 uneq_allocator(int person) throw()
282 : personality(person) { }
284 template<typename Tp1>
285 uneq_allocator(const uneq_allocator<Tp1>& b) throw()
286 : personality(b.get_personality()) { }
288 int get_personality() const { return personality; }
291 address(reference x) const { return &x; }
294 address(const_reference x) const { return &x; }
297 allocate(size_type n, const void* = 0)
299 if (__builtin_expect(n > this->max_size(), false))
300 std::__throw_bad_alloc();
302 pointer p = static_cast<Tp*>(::operator new(n * sizeof(Tp)));
305 get_map().insert(map_type::value_type(reinterpret_cast<void*>(p),
310 ::operator delete(p);
311 __throw_exception_again;
317 deallocate(pointer p, size_type)
321 map_type::iterator it = get_map().find(reinterpret_cast<void*>(p));
322 assert( it != get_map().end() );
324 // Enforce requirements in Table 32 about deallocation vs
325 // allocator equality.
326 assert( it->second == personality );
329 ::operator delete(p);
333 max_size() const throw()
334 { return size_t(-1) / sizeof(Tp); }
337 construct(pointer p, const Tp& val)
338 { ::new(p) Tp(val); }
341 destroy(pointer p) { p->~Tp(); }
346 operator=(const uneq_allocator&);
348 // ... yet swappable!
350 swap(uneq_allocator& a, uneq_allocator& b)
351 { std::swap(a.personality, b.personality); }
353 template<typename Tp1>
355 operator==(const uneq_allocator& a, const uneq_allocator<Tp1>& b)
356 { return a.personality == b.personality; }
358 template<typename Tp1>
360 operator!=(const uneq_allocator& a, const uneq_allocator<Tp1>& b)
361 { return !(a == b); }
367 template<typename Tp>
368 class throw_allocator
371 typedef std::size_t size_type;
372 typedef std::ptrdiff_t difference_type;
374 typedef const Tp* const_pointer;
375 typedef Tp& reference;
376 typedef const Tp& const_reference;
377 typedef Tp value_type;
379 template<typename Tp1>
381 { typedef throw_allocator<Tp1> other; };
383 throw_allocator() throw()
384 : count(size_type(-1)) { }
386 throw_allocator(size_type c) throw()
389 template<typename Tp1>
390 throw_allocator(const throw_allocator<Tp1>& b) throw()
391 : count(b.get_count()) { }
393 size_type get_count() const { return count; }
396 address(reference x) const { return &x; }
399 address(const_reference x) const { return &x; }
402 allocate(size_type n, const void* = 0)
405 throw std::bad_alloc();
407 if (count != size_type(-1))
410 return static_cast<Tp*>(::operator new(n * sizeof(Tp)));
414 deallocate(pointer p, size_type)
415 { ::operator delete(p); }
418 max_size() const throw()
419 { return size_type(-1) / sizeof(Tp); }
422 construct(pointer p, const Tp& val)
423 { ::new(p) Tp(val); }
426 destroy(pointer p) { p->~Tp(); }
429 template<typename Tp1>
431 operator==(const throw_allocator&, const throw_allocator<Tp1>&)
434 template<typename Tp1>
436 operator!=(const throw_allocator&, const throw_allocator<Tp1>&)
441 }; // namespace __gnu_test
443 #endif // _GLIBCXX_TESTSUITE_ALLOCATOR_H