2 // Testing allocator for the C++ library testsuite.
4 // Copyright (C) 2002-2016 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 3, 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 COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
22 // This file provides an test instrumentation allocator that can be
23 // used to verify allocation functionality of standard library
24 // containers. 2002.11.25 smw
26 #ifndef _GLIBCXX_TESTSUITE_ALLOCATOR_H
27 #define _GLIBCXX_TESTSUITE_ALLOCATOR_H
29 #include <tr1/unordered_map>
30 #include <bits/move.h>
31 #include <ext/pointer.h>
32 #include <ext/alloc_traits.h>
33 #include <testsuite_hooks.h>
37 class tracker_allocator_counter
40 typedef std::size_t size_type
;
43 allocate(size_type blocksize
)
44 { allocationCount_
+= blocksize
; }
47 construct() { ++constructCount_
; }
50 destroy() { ++destructCount_
; }
53 deallocate(size_type blocksize
)
54 { deallocationCount_
+= blocksize
; }
57 get_allocation_count() { return allocationCount_
; }
60 get_deallocation_count() { return deallocationCount_
; }
63 get_construct_count() { return constructCount_
; }
66 get_destruct_count() { return destructCount_
; }
72 deallocationCount_
= 0;
78 static size_type allocationCount_
;
79 static size_type deallocationCount_
;
80 static int constructCount_
;
81 static int destructCount_
;
84 // Helper to detect inconsistency between type used to instantiate an
85 // allocator and the underlying allocator value_type.
86 template<typename T
, typename Alloc
,
87 typename
= typename
Alloc::value_type
>
88 struct check_consistent_alloc_value_type
;
90 template<typename T
, typename Alloc
>
91 struct check_consistent_alloc_value_type
<T
, Alloc
, T
>
92 { typedef T value_type
; };
94 // An allocator facade that intercepts allocate/deallocate/construct/destroy
95 // calls and track them through the tracker_allocator_counter class. This
96 // class is templated on the target object type, but tracker isn't.
97 template<typename T
, typename Alloc
= std::allocator
<T
> >
98 class tracker_allocator
: public Alloc
101 typedef tracker_allocator_counter counter_type
;
103 typedef __gnu_cxx::__alloc_traits
<Alloc
> AllocTraits
;
107 check_consistent_alloc_value_type
<T
, Alloc
>::value_type value_type
;
108 typedef typename
AllocTraits::pointer pointer
;
109 typedef typename
AllocTraits::size_type size_type
;
114 typedef tracker_allocator
<U
,
115 typename
AllocTraits::template rebind
<U
>::other
> other
;
118 #if __cplusplus >= 201103L
119 tracker_allocator() = default;
120 tracker_allocator(const tracker_allocator
&) = default;
121 tracker_allocator(tracker_allocator
&&) = default;
122 tracker_allocator
& operator=(const tracker_allocator
&) = default;
123 tracker_allocator
& operator=(tracker_allocator
&&) = default;
125 // Perfect forwarding constructor.
126 template<typename
... _Args
>
127 tracker_allocator(_Args
&&... __args
)
128 : Alloc(std::forward
<_Args
>(__args
)...)
134 tracker_allocator(const tracker_allocator
&)
142 tracker_allocator(const tracker_allocator
<U
,
143 typename
AllocTraits::template rebind
<U
>::other
>& alloc
)
144 _GLIBCXX_USE_NOEXCEPT
149 allocate(size_type n
, const void* = 0)
151 pointer p
= AllocTraits::allocate(*this, n
);
152 counter_type::allocate(n
* sizeof(T
));
156 #if __cplusplus >= 201103L
157 template<typename U
, typename
... Args
>
159 construct(U
* p
, Args
&&... args
)
161 AllocTraits::construct(*this, p
, std::forward
<Args
>(args
)...);
162 counter_type::construct();
169 AllocTraits::destroy(*this, p
);
170 counter_type::destroy();
174 construct(pointer p
, const T
& value
)
176 AllocTraits::construct(*this, p
, value
);
177 counter_type::construct();
183 AllocTraits::destroy(*this, p
);
184 counter_type::destroy();
189 deallocate(pointer p
, size_type num
)
191 counter_type::deallocate(num
* sizeof(T
));
192 AllocTraits::deallocate(*this, p
, num
);
195 // Implement swap for underlying allocators that might need it.
197 swap(tracker_allocator
& a
, tracker_allocator
& b
)
207 template<class T1
, class Alloc1
, class T2
, class Alloc2
>
209 operator==(const tracker_allocator
<T1
, Alloc1
>& lhs
,
210 const tracker_allocator
<T2
, Alloc2
>& rhs
) throw()
212 const Alloc1
& alloc1
= lhs
;
213 const Alloc2
& alloc2
= rhs
;
214 return alloc1
== alloc2
;
217 template<class T1
, class Alloc1
, class T2
, class Alloc2
>
219 operator!=(const tracker_allocator
<T1
, Alloc1
>& lhs
,
220 const tracker_allocator
<T2
, Alloc2
>& rhs
) throw()
221 { return !(lhs
== rhs
); }
224 check_construct_destroy(const char* tag
, int expected_c
, int expected_d
);
226 template<typename Alloc
>
228 check_deallocate_null()
230 // Let's not core here...
237 template<typename Alloc
>
239 check_allocate_max_size()
244 a
.allocate(a
.max_size() + 1);
246 catch(std::bad_alloc
&)
257 // A simple allocator which can be constructed endowed of a given
258 // "personality" (an integer), queried in operator== to simulate the
259 // behavior of realworld "unequal" allocators (i.e., not exploiting
260 // the provision in 20.1.5/4, first bullet). A global unordered_map,
261 // filled at allocation time with (pointer, personality) pairs, is
262 // then consulted to enforce the requirements in Table 32 about
263 // deallocation vs allocator equality. Note that this allocator is
264 // swappable, not copy assignable, consistently with Option 3 of DR 431
266 struct uneq_allocator_base
268 typedef std::tr1::unordered_map
<void*, int> map_type
;
270 // Avoid static initialization troubles and/or bad interactions
271 // with tests linking testsuite_allocator.o and playing globally
272 // with operator new/delete.
276 static map_type alloc_map
;
281 template<typename Tp
, typename Alloc
= std::allocator
<Tp
> >
283 : private uneq_allocator_base
,
286 typedef __gnu_cxx::__alloc_traits
<Alloc
> AllocTraits
;
288 Alloc
& base() { return *this; }
289 const Alloc
& base() const { return *this; }
290 void swap_base(Alloc
& b
) { swap(b
, this->base()); }
293 typedef typename check_consistent_alloc_value_type
<Tp
, Alloc
>::value_type
295 typedef typename
AllocTraits::size_type size_type
;
296 typedef typename
AllocTraits::pointer pointer
;
298 #if __cplusplus >= 201103L
299 typedef std::true_type propagate_on_container_swap
;
302 template<typename Tp1
>
305 typedef uneq_allocator
<Tp1
,
306 typename
AllocTraits::template rebind
<Tp1
>::other
> other
;
309 uneq_allocator() _GLIBCXX_USE_NOEXCEPT
312 uneq_allocator(int person
) _GLIBCXX_USE_NOEXCEPT
313 : personality(person
) { }
315 #if __cplusplus >= 201103L
316 uneq_allocator(const uneq_allocator
&) = default;
317 uneq_allocator(uneq_allocator
&&) = default;
320 template<typename Tp1
>
321 uneq_allocator(const uneq_allocator
<Tp1
,
322 typename
AllocTraits::template rebind
<Tp1
>::other
>& b
)
323 _GLIBCXX_USE_NOEXCEPT
324 : personality(b
.get_personality()) { }
326 ~uneq_allocator() _GLIBCXX_USE_NOEXCEPT
329 int get_personality() const { return personality
; }
332 allocate(size_type n
, const void* hint
= 0)
334 pointer p
= AllocTraits::allocate(*this, n
);
338 get_map().insert(map_type::value_type(reinterpret_cast<void*>(p
),
343 AllocTraits::deallocate(*this, p
, n
);
344 __throw_exception_again
;
351 deallocate(pointer p
, size_type n
)
353 bool test
__attribute__((unused
)) = true;
357 map_type::iterator it
= get_map().find(reinterpret_cast<void*>(p
));
358 VERIFY( it
!= get_map().end() );
360 // Enforce requirements in Table 32 about deallocation vs
361 // allocator equality.
362 VERIFY( it
->second
== personality
);
365 AllocTraits::deallocate(*this, p
, n
);
368 #if __cplusplus >= 201103L
369 // Not copy assignable...
371 operator=(const uneq_allocator
&) = delete;
373 // ... but still moveable if base allocator is.
375 operator=(uneq_allocator
&&) = default;
380 operator=(const uneq_allocator
&);
384 // ... yet swappable!
386 swap(uneq_allocator
& a
, uneq_allocator
& b
)
388 std::swap(a
.personality
, b
.personality
);
392 template<typename Tp1
>
394 operator==(const uneq_allocator
& a
,
395 const uneq_allocator
<Tp1
,
396 typename
AllocTraits::template rebind
<Tp1
>::other
>& b
)
397 { return a
.personality
== b
.personality
; }
399 template<typename Tp1
>
401 operator!=(const uneq_allocator
& a
,
402 const uneq_allocator
<Tp1
,
403 typename
AllocTraits::template rebind
<Tp1
>::other
>& b
)
404 { return !(a
== b
); }
409 #if __cplusplus >= 201103L
410 // An uneq_allocator which can be used to test allocator propagation.
411 template<typename Tp
, bool Propagate
, typename Alloc
= std::allocator
<Tp
>>
412 class propagating_allocator
: public uneq_allocator
<Tp
, Alloc
>
414 typedef __gnu_cxx::__alloc_traits
<Alloc
> AllocTraits
;
416 typedef uneq_allocator
<Tp
, Alloc
> base_alloc
;
417 base_alloc
& base() { return *this; }
418 const base_alloc
& base() const { return *this; }
419 void swap_base(base_alloc
& b
) { swap(b
, this->base()); }
421 typedef std::integral_constant
<bool, Propagate
> trait_type
;
424 // default allocator_traits::rebind_alloc would select
425 // uneq_allocator::rebind so we must define rebind here
426 template<typename Up
>
429 typedef propagating_allocator
<Up
, Propagate
,
430 typename
AllocTraits::template rebind
<Up
>::other
> other
;
433 propagating_allocator(int i
) noexcept
437 template<typename Up
>
438 propagating_allocator(const propagating_allocator
<Up
, Propagate
,
439 typename
AllocTraits::template rebind
<Up
>::other
>& a
)
444 propagating_allocator() noexcept
= default;
446 propagating_allocator(const propagating_allocator
&) noexcept
= default;
448 propagating_allocator
&
449 operator=(const propagating_allocator
& a
) noexcept
451 static_assert(Propagate
, "assigning propagating_allocator<T, true>");
452 propagating_allocator(a
).swap_base(*this);
457 propagating_allocator
&
458 operator=(const propagating_allocator
<Tp
, P2
, Alloc
>& a
) noexcept
460 static_assert(P2
, "assigning propagating_allocator<T, true>");
461 propagating_allocator(a
).swap_base(*this);
465 // postcondition: a.get_personality() == 0
466 propagating_allocator(propagating_allocator
&& a
) noexcept
470 // postcondition: a.get_personality() == 0
471 propagating_allocator
&
472 operator=(propagating_allocator
&& a
) noexcept
474 propagating_allocator(std::move(a
)).swap_base(*this);
478 typedef trait_type propagate_on_container_copy_assignment
;
479 typedef trait_type propagate_on_container_move_assignment
;
480 typedef trait_type propagate_on_container_swap
;
482 propagating_allocator
select_on_container_copy_construction() const
483 { return Propagate
? *this : propagating_allocator(); }
486 // Class template supporting the minimal interface that satisfies the
487 // Allocator requirements, from example in [allocator.requirements]
489 struct SimpleAllocator
491 typedef Tp value_type
;
493 SimpleAllocator() noexcept
{ }
496 SimpleAllocator(const SimpleAllocator
<T
>&) { }
498 Tp
*allocate(std::size_t n
)
499 { return std::allocator
<Tp
>().allocate(n
); }
501 void deallocate(Tp
*p
, std::size_t n
)
502 { std::allocator
<Tp
>().deallocate(p
, n
); }
505 template <class T
, class U
>
506 bool operator==(const SimpleAllocator
<T
>&, const SimpleAllocator
<U
>&)
508 template <class T
, class U
>
509 bool operator!=(const SimpleAllocator
<T
>&, const SimpleAllocator
<U
>&)
514 template<typename Tp
>
515 struct ExplicitConsAlloc
: std::allocator
<Tp
>
517 ExplicitConsAlloc() { }
519 template<typename Up
>
521 ExplicitConsAlloc(const ExplicitConsAlloc
<Up
>&) { }
523 template<typename Up
>
525 { typedef ExplicitConsAlloc
<Up
> other
; };
528 #if __cplusplus >= 201103L
529 template<typename Tp
>
530 class CustomPointerAlloc
: public std::allocator
<Tp
>
532 template<typename Up
, typename Sp
= __gnu_cxx::_Std_pointer_impl
<Up
>>
533 using Ptr
= __gnu_cxx::_Pointer_adapter
<Sp
>;
536 CustomPointerAlloc() = default;
538 template<typename Up
>
539 CustomPointerAlloc(const CustomPointerAlloc
<Up
>&) { }
541 template<typename Up
>
543 { typedef CustomPointerAlloc
<Up
> other
; };
545 typedef Ptr
<Tp
> pointer
;
546 typedef Ptr
<const Tp
> const_pointer
;
547 typedef Ptr
<void> void_pointer
;
548 typedef Ptr
<const void> const_void_pointer
;
550 pointer
allocate(std::size_t n
, pointer
= {})
551 { return pointer(std::allocator
<Tp
>::allocate(n
)); }
553 void deallocate(pointer p
, std::size_t n
)
554 { std::allocator
<Tp
>::deallocate(std::addressof(*p
), n
); }
557 // Utility for use as CRTP base class of custom pointer types
558 template<typename Derived
, typename T
>
561 typedef T element_type
;
563 // typedefs for iterator_traits
564 typedef T value_type
;
565 typedef std::ptrdiff_t difference_type
;
566 typedef std::random_access_iterator_tag iterator_category
;
567 typedef Derived pointer
;
568 typedef T
& reference
;
572 explicit PointerBase(T
* p
= nullptr) : value(p
) { }
574 template<typename D
, typename U
,
575 typename
= decltype(static_cast<T
*>(std::declval
<U
*>()))>
576 PointerBase(const PointerBase
<D
, U
>& p
) : value(p
.value
) { }
578 T
& operator*() const { return *value
; }
579 T
* operator->() const { return value
; }
580 T
& operator[](difference_type n
) const { return value
[n
]; }
582 Derived
& operator++() { ++value
; return derived(); }
583 Derived
operator++(int) { Derived
tmp(derived()); ++value
; return tmp
; }
584 Derived
& operator--() { --value
; return derived(); }
585 Derived
operator--(int) { Derived
tmp(derived()); --value
; return tmp
; }
587 Derived
& operator+=(difference_type n
) { value
+= n
; return derived(); }
588 Derived
& operator-=(difference_type n
) { value
-= n
; return derived(); }
590 explicit operator bool() const { return value
!= nullptr; }
593 operator+(difference_type n
) const
595 Derived
p(derived());
600 operator-(difference_type n
) const
602 Derived
p(derived());
607 Derived
& derived() { return static_cast<Derived
&>(*this); }
610 template<typename D
, typename T
>
611 std::ptrdiff_t operator-(PointerBase
<D
, T
> l
, PointerBase
<D
, T
> r
)
612 { return l
.value
- r
.value
; }
614 template<typename D
, typename T
>
615 bool operator==(PointerBase
<D
, T
> l
, PointerBase
<D
, T
> r
)
616 { return l
.value
== r
.value
; }
618 template<typename D
, typename T
>
619 bool operator!=(PointerBase
<D
, T
> l
, PointerBase
<D
, T
> r
)
620 { return l
.value
!= r
.value
; }
622 // implementation for void specializations
624 struct PointerBase_void
626 typedef T element_type
;
628 // typedefs for iterator_traits
629 typedef T value_type
;
630 typedef std::ptrdiff_t difference_type
;
631 typedef std::random_access_iterator_tag iterator_category
;
635 explicit PointerBase_void(T
* p
= nullptr) : value(p
) { }
637 template<typename D
, typename U
,
638 typename
= decltype(static_cast<T
*>(std::declval
<U
*>()))>
639 PointerBase_void(const PointerBase
<D
, U
>& p
) : value(p
.value
) { }
641 explicit operator bool() const { return value
!= nullptr; }
644 template<typename Derived
>
645 struct PointerBase
<Derived
, void> : PointerBase_void
<void>
647 using PointerBase_void::PointerBase_void
;
648 typedef Derived pointer
;
651 template<typename Derived
>
652 struct PointerBase
<Derived
, const void> : PointerBase_void
<const void>
654 using PointerBase_void::PointerBase_void
;
655 typedef Derived pointer
;
659 } // namespace __gnu_test
661 #endif // _GLIBCXX_TESTSUITE_ALLOCATOR_H