2 // Testing allocator for the C++ library testsuite.
4 // Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007
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 2, 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 COPYING. If not, write to the Free
20 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
32 // This file provides an test instrumentation allocator that can be
33 // used to verify allocation functionality of standard library
34 // containers. 2002.11.25 smw
36 #ifndef _GLIBCXX_TESTSUITE_ALLOCATOR_H
37 #define _GLIBCXX_TESTSUITE_ALLOCATOR_H
40 #include <tr1/unordered_map>
42 #include <bits/stl_move.h>
46 bool new_called
= false;
47 bool delete_called
= false;
52 class tracker_allocator_counter
55 typedef std::size_t size_type
;
58 allocate(size_type blocksize
)
60 allocationCount_
+= blocksize
;
61 return ::operator new(blocksize
);
65 construct() { constructCount_
++; }
68 destroy() { destructCount_
++; }
71 deallocate(void* p
, size_type blocksize
)
74 deallocationCount_
+= blocksize
;
78 get_allocation_count() { return allocationCount_
; }
81 get_deallocation_count() { return deallocationCount_
; }
84 get_construct_count() { return constructCount_
; }
87 get_destruct_count() { return destructCount_
; }
93 deallocationCount_
= 0;
99 static size_type allocationCount_
;
100 static size_type deallocationCount_
;
101 static int constructCount_
;
102 static int destructCount_
;
105 // A simple basic allocator that just forwards to the
106 // tracker_allocator_counter to fulfill memory requests. This class
107 // is templated on the target object type, but tracker isn't.
109 class tracker_allocator
112 typedef tracker_allocator_counter counter_type
;
115 typedef T value_type
;
117 typedef const T
* const_pointer
;
118 typedef T
& reference
;
119 typedef const T
& const_reference
;
120 typedef std::size_t size_type
;
121 typedef std::ptrdiff_t difference_type
;
123 template<class U
> struct rebind
{ typedef tracker_allocator
<U
> other
; };
126 address(reference value
) const
130 address(const_reference value
) const
133 tracker_allocator() throw()
136 tracker_allocator(const tracker_allocator
&) throw()
140 tracker_allocator(const tracker_allocator
<U
>&) throw()
143 ~tracker_allocator() throw()
147 max_size() const throw()
148 { return size_type(-1) / sizeof(T
); }
151 allocate(size_type n
, const void* = 0)
152 { return static_cast<pointer
>(counter_type::allocate(n
* sizeof(T
))); }
155 construct(pointer p
, const T
& value
)
157 ::new ((void *)p
) T(value
);
158 counter_type::construct();
161 #ifdef __GXX_EXPERIMENTAL_CXX0X__
162 template<typename
... Args
>
164 construct(pointer p
, Args
&&... args
)
166 ::new((void *)p
) T(std::forward
<Args
>(args
)...);
167 counter_type::construct();
175 counter_type::destroy();
179 deallocate(pointer p
, size_type num
)
180 { counter_type::deallocate(p
, num
* sizeof(T
)); }
183 template<class T1
, class T2
>
185 operator==(const tracker_allocator
<T1
>&,
186 const tracker_allocator
<T2
>&) throw()
189 template<class T1
, class T2
>
191 operator!=(const tracker_allocator
<T1
>&,
192 const tracker_allocator
<T2
>&) throw()
196 check_construct_destroy(const char* tag
, int expected_c
, int expected_d
);
198 template<typename Alloc
, bool uses_global_new
>
200 check_new(Alloc a
= Alloc())
202 bool test
__attribute__((unused
)) = true;
204 test
&= ( new_called
== uses_global_new
);
208 template<typename Alloc
, bool uses_global_delete
>
210 check_delete(Alloc a
= Alloc())
212 bool test
__attribute__((unused
)) = true;
213 typename
Alloc::pointer p
= a
.allocate(10);
215 test
&= ( delete_called
== uses_global_delete
);
219 template<typename Alloc
>
221 check_deallocate_null()
223 // Let's not core here...
225 a
.deallocate(NULL
, 1);
226 a
.deallocate(NULL
, 10);
230 template<typename Alloc
>
232 check_allocate_max_size()
237 a
.allocate(a
.max_size() + 1);
239 catch(std::bad_alloc
&)
251 // A simple allocator which can be constructed endowed of a given
252 // "personality" (an integer), queried in operator== to simulate the
253 // behavior of realworld "unequal" allocators (i.e., not exploiting
254 // the provision in 20.1.5/4, first bullet). A global unordered_map,
255 // filled at allocation time with (pointer, personality) pairs, is
256 // then consulted to enforce the requirements in Table 32 about
257 // deallocation vs allocator equality. Note that this allocator is
258 // swappable, not assignable, consistently with Option 3 of DR 431
260 struct uneq_allocator_base
262 typedef std::tr1::unordered_map
<void*, int> map_type
;
264 // Avoid static initialization troubles and/or bad interactions
265 // with tests linking testsuite_allocator.o and playing globally
266 // with operator new/delete.
270 static map_type alloc_map
;
275 template<typename Tp
>
277 : private uneq_allocator_base
280 typedef size_t size_type
;
281 typedef ptrdiff_t difference_type
;
283 typedef const Tp
* const_pointer
;
284 typedef Tp
& reference
;
285 typedef const Tp
& const_reference
;
286 typedef Tp value_type
;
288 template<typename Tp1
>
290 { typedef uneq_allocator
<Tp1
> other
; };
292 uneq_allocator() throw()
295 uneq_allocator(int person
) throw()
296 : personality(person
) { }
298 template<typename Tp1
>
299 uneq_allocator(const uneq_allocator
<Tp1
>& b
) throw()
300 : personality(b
.get_personality()) { }
302 int get_personality() const { return personality
; }
305 address(reference x
) const { return &x
; }
308 address(const_reference x
) const { return &x
; }
311 allocate(size_type n
, const void* = 0)
313 if (__builtin_expect(n
> this->max_size(), false))
314 std::__throw_bad_alloc();
316 pointer p
= static_cast<Tp
*>(::operator new(n
* sizeof(Tp
)));
319 get_map().insert(map_type::value_type(reinterpret_cast<void*>(p
),
324 ::operator delete(p
);
325 __throw_exception_again
;
331 deallocate(pointer p
, size_type
)
335 map_type::iterator it
= get_map().find(reinterpret_cast<void*>(p
));
336 assert( it
!= get_map().end() );
338 // Enforce requirements in Table 32 about deallocation vs
339 // allocator equality.
340 assert( it
->second
== personality
);
343 ::operator delete(p
);
347 max_size() const throw()
348 { return size_type(-1) / sizeof(Tp
); }
351 construct(pointer p
, const Tp
& val
)
352 { ::new((void *)p
) Tp(val
); }
354 #ifdef __GXX_EXPERIMENTAL_CXX0X__
355 template<typename
... Args
>
357 construct(pointer p
, Args
&&... args
)
358 { ::new((void *)p
) Tp(std::forward
<Args
>(args
)...); }
362 destroy(pointer p
) { p
->~Tp(); }
367 operator=(const uneq_allocator
&);
369 // ... yet swappable!
371 swap(uneq_allocator
& a
, uneq_allocator
& b
)
372 { std::swap(a
.personality
, b
.personality
); }
374 template<typename Tp1
>
376 operator==(const uneq_allocator
& a
, const uneq_allocator
<Tp1
>& b
)
377 { return a
.personality
== b
.personality
; }
379 template<typename Tp1
>
381 operator!=(const uneq_allocator
& a
, const uneq_allocator
<Tp1
>& b
)
382 { return !(a
== b
); }
386 } // namespace __gnu_test
388 #endif // _GLIBCXX_TESTSUITE_ALLOCATOR_H