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>
45 bool new_called
= false;
46 bool delete_called
= false;
51 class tracker_allocator_counter
54 typedef std::size_t size_type
;
57 allocate(size_type blocksize
)
59 allocationCount_
+= blocksize
;
60 return ::operator new(blocksize
);
64 construct() { constructCount_
++; }
67 destroy() { destructCount_
++; }
70 deallocate(void* p
, size_type blocksize
)
73 deallocationCount_
+= blocksize
;
77 get_allocation_count() { return allocationCount_
; }
80 get_deallocation_count() { return deallocationCount_
; }
83 get_construct_count() { return constructCount_
; }
86 get_destruct_count() { return destructCount_
; }
92 deallocationCount_
= 0;
98 static size_type allocationCount_
;
99 static size_type deallocationCount_
;
100 static int constructCount_
;
101 static int destructCount_
;
104 // A simple basic allocator that just forwards to the
105 // tracker_allocator_counter to fulfill memory requests. This class
106 // is templated on the target object type, but tracker isn't.
108 class tracker_allocator
111 typedef tracker_allocator_counter counter_type
;
114 typedef T value_type
;
116 typedef const T
* const_pointer
;
117 typedef T
& reference
;
118 typedef const T
& const_reference
;
119 typedef std::size_t size_type
;
120 typedef std::ptrdiff_t difference_type
;
122 template<class U
> struct rebind
{ typedef tracker_allocator
<U
> other
; };
125 address(reference value
) const
129 address(const_reference value
) const
132 tracker_allocator() throw()
135 tracker_allocator(const tracker_allocator
&) throw()
139 tracker_allocator(const tracker_allocator
<U
>&) throw()
142 ~tracker_allocator() throw()
146 max_size() const throw()
147 { return size_type(-1) / sizeof(T
); }
150 allocate(size_type n
, const void* = 0)
151 { return static_cast<pointer
>(counter_type::allocate(n
* sizeof(T
))); }
154 construct(pointer p
, const T
& value
)
157 counter_type::construct();
164 counter_type::destroy();
168 deallocate(pointer p
, size_type num
)
169 { counter_type::deallocate(p
, num
* sizeof(T
)); }
172 template<class T1
, class T2
>
174 operator==(const tracker_allocator
<T1
>&,
175 const tracker_allocator
<T2
>&) throw()
178 template<class T1
, class T2
>
180 operator!=(const tracker_allocator
<T1
>&,
181 const tracker_allocator
<T2
>&) throw()
185 check_construct_destroy(const char* tag
, int expected_c
, int expected_d
);
187 template<typename Alloc
, bool uses_global_new
>
189 check_new(Alloc a
= Alloc())
191 bool test
__attribute__((unused
)) = true;
193 test
&= ( new_called
== uses_global_new
);
197 template<typename Alloc
, bool uses_global_delete
>
199 check_delete(Alloc a
= Alloc())
201 bool test
__attribute__((unused
)) = true;
202 typename
Alloc::pointer p
= a
.allocate(10);
204 test
&= ( delete_called
== uses_global_delete
);
208 template<typename Alloc
>
210 check_deallocate_null()
212 // Let's not core here...
214 a
.deallocate(NULL
, 1);
215 a
.deallocate(NULL
, 10);
219 template<typename Alloc
>
221 check_allocate_max_size()
226 a
.allocate(a
.max_size() + 1);
228 catch(std::bad_alloc
&)
240 // A simple allocator which can be constructed endowed of a given
241 // "personality" (an integer), queried in operator== to simulate the
242 // behavior of realworld "unequal" allocators (i.e., not exploiting
243 // the provision in 20.1.5/4, first bullet). A global unordered_map,
244 // filled at allocation time with (pointer, personality) pairs, is
245 // then consulted to enforce the requirements in Table 32 about
246 // deallocation vs allocator equality. Note that this allocator is
247 // swappable, not assignable, consistently with Option 3 of DR 431
249 struct uneq_allocator_base
251 typedef std::tr1::unordered_map
<void*, int> map_type
;
253 // Avoid static initialization troubles and/or bad interactions
254 // with tests linking testsuite_allocator.o and playing globally
255 // with operator new/delete.
259 static map_type alloc_map
;
264 template<typename Tp
>
266 : private uneq_allocator_base
269 typedef size_t size_type
;
270 typedef ptrdiff_t difference_type
;
272 typedef const Tp
* const_pointer
;
273 typedef Tp
& reference
;
274 typedef const Tp
& const_reference
;
275 typedef Tp value_type
;
277 template<typename Tp1
>
279 { typedef uneq_allocator
<Tp1
> other
; };
281 uneq_allocator() throw()
284 uneq_allocator(int person
) throw()
285 : personality(person
) { }
287 template<typename Tp1
>
288 uneq_allocator(const uneq_allocator
<Tp1
>& b
) throw()
289 : personality(b
.get_personality()) { }
291 int get_personality() const { return personality
; }
294 address(reference x
) const { return &x
; }
297 address(const_reference x
) const { return &x
; }
300 allocate(size_type n
, const void* = 0)
302 if (__builtin_expect(n
> this->max_size(), false))
303 std::__throw_bad_alloc();
305 pointer p
= static_cast<Tp
*>(::operator new(n
* sizeof(Tp
)));
308 get_map().insert(map_type::value_type(reinterpret_cast<void*>(p
),
313 ::operator delete(p
);
314 __throw_exception_again
;
320 deallocate(pointer p
, size_type
)
324 map_type::iterator it
= get_map().find(reinterpret_cast<void*>(p
));
325 assert( it
!= get_map().end() );
327 // Enforce requirements in Table 32 about deallocation vs
328 // allocator equality.
329 assert( it
->second
== personality
);
332 ::operator delete(p
);
336 max_size() const throw()
337 { return size_type(-1) / sizeof(Tp
); }
340 construct(pointer p
, const Tp
& val
)
341 { ::new(p
) Tp(val
); }
344 destroy(pointer p
) { p
->~Tp(); }
349 operator=(const uneq_allocator
&);
351 // ... yet swappable!
353 swap(uneq_allocator
& a
, uneq_allocator
& b
)
354 { std::swap(a
.personality
, b
.personality
); }
356 template<typename Tp1
>
358 operator==(const uneq_allocator
& a
, const uneq_allocator
<Tp1
>& b
)
359 { return a
.personality
== b
.personality
; }
361 template<typename Tp1
>
363 operator!=(const uneq_allocator
& a
, const uneq_allocator
<Tp1
>& b
)
364 { return !(a
== b
); }
368 }; // namespace __gnu_test
370 #endif // _GLIBCXX_TESTSUITE_ALLOCATOR_H