2 // Testing allocator for the C++ library testsuite.
4 // Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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
30 #include <tr1/unordered_map>
32 #include <bits/move.h>
36 class tracker_allocator_counter
39 typedef std::size_t size_type
;
42 allocate(size_type blocksize
)
44 allocationCount_
+= blocksize
;
45 return ::operator new(blocksize
);
49 construct() { constructCount_
++; }
52 destroy() { destructCount_
++; }
55 deallocate(void* p
, size_type blocksize
)
58 deallocationCount_
+= blocksize
;
62 get_allocation_count() { return allocationCount_
; }
65 get_deallocation_count() { return deallocationCount_
; }
68 get_construct_count() { return constructCount_
; }
71 get_destruct_count() { return destructCount_
; }
77 deallocationCount_
= 0;
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.
93 class tracker_allocator
96 typedef tracker_allocator_counter counter_type
;
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
; };
110 address(reference value
) const
114 address(const_reference value
) const
117 tracker_allocator() throw()
120 tracker_allocator(const tracker_allocator
&) throw()
124 tracker_allocator(const tracker_allocator
<U
>&) throw()
127 ~tracker_allocator() throw()
131 max_size() const throw()
132 { return size_type(-1) / sizeof(T
); }
135 allocate(size_type n
, const void* = 0)
136 { return static_cast<pointer
>(counter_type::allocate(n
* sizeof(T
))); }
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
>
148 construct(pointer p
, Args
&&... args
)
150 ::new((void *)p
) T(std::forward
<Args
>(args
)...);
151 counter_type::construct();
159 counter_type::destroy();
163 deallocate(pointer p
, size_type num
)
164 { counter_type::deallocate(p
, num
* sizeof(T
)); }
167 template<class T1
, class T2
>
169 operator==(const tracker_allocator
<T1
>&,
170 const tracker_allocator
<T2
>&) throw()
173 template<class T1
, class T2
>
175 operator!=(const tracker_allocator
<T1
>&,
176 const tracker_allocator
<T2
>&) throw()
180 check_construct_destroy(const char* tag
, int expected_c
, int expected_d
);
182 template<typename Alloc
>
184 check_deallocate_null()
186 // Let's not core here...
193 template<typename Alloc
>
195 check_allocate_max_size()
200 a
.allocate(a
.max_size() + 1);
202 catch(std::bad_alloc
&)
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
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.
233 static map_type alloc_map
;
238 template<typename Tp
>
240 : private uneq_allocator_base
243 typedef std::size_t size_type
;
244 typedef std::ptrdiff_t difference_type
;
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
>
253 { typedef uneq_allocator
<Tp1
> other
; };
255 uneq_allocator() throw()
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
; }
268 address(reference x
) const { return &x
; }
271 address(const_reference x
) const { return &x
; }
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
),
287 ::operator delete(p
);
288 __throw_exception_again
;
294 deallocate(pointer p
, size_type
)
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
);
306 ::operator delete(p
);
310 max_size() const throw()
311 { return size_type(-1) / sizeof(Tp
); }
314 construct(pointer p
, const Tp
& val
)
315 { ::new((void *)p
) Tp(val
); }
317 #ifdef __GXX_EXPERIMENTAL_CXX0X__
318 template<typename
... Args
>
320 construct(pointer p
, Args
&&... args
)
321 { ::new((void *)p
) Tp(std::forward
<Args
>(args
)...); }
325 destroy(pointer p
) { p
->~Tp(); }
330 operator=(const uneq_allocator
&);
332 // ... yet swappable!
334 swap(uneq_allocator
& a
, uneq_allocator
& b
)
335 { std::swap(a
.personality
, b
.personality
); }
337 template<typename Tp1
>
339 operator==(const uneq_allocator
& a
, const uneq_allocator
<Tp1
>& b
)
340 { return a
.personality
== b
.personality
; }
342 template<typename Tp1
>
344 operator!=(const uneq_allocator
& a
, const uneq_allocator
<Tp1
>& b
)
345 { return !(a
== b
); }
349 } // namespace __gnu_test
351 #endif // _GLIBCXX_TESTSUITE_ALLOCATOR_H