2 // Copyright 2006-2011 Daniel James.
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #if !defined(BOOST_UNORDERED_TEST_CXX11_ALLOCATOR_HEADER)
7 #define BOOST_UNORDERED_TEST_CXX11_ALLOCATOR_HEADER
9 #include <boost/config.hpp>
10 #include <boost/limits.hpp>
13 #include "../helpers/fwd.hpp"
14 #include "../helpers/memory.hpp"
17 struct allocator_false
21 is_select_on_copy = 0,
22 is_propagate_on_swap = 0,
23 is_propagate_on_assign = 0,
24 is_propagate_on_move = 0,
29 struct allocator_flags_all
33 is_select_on_copy = 1,
34 is_propagate_on_swap = 1,
35 is_propagate_on_assign = 1,
36 is_propagate_on_move = 1,
41 struct select_copy : allocator_false
48 struct propagate_swap : allocator_false
52 is_propagate_on_swap = 1
55 struct propagate_assign : allocator_false
59 is_propagate_on_assign = 1
62 struct propagate_move : allocator_false
66 is_propagate_on_move = 1
70 struct no_select_copy : allocator_flags_all
77 struct no_propagate_swap : allocator_flags_all
81 is_propagate_on_swap = 0
84 struct no_propagate_assign : allocator_flags_all
88 is_propagate_on_assign = 0
91 struct no_propagate_move : allocator_flags_all
95 is_propagate_on_move = 0
99 template <typename Flag> struct swap_allocator_base
101 struct propagate_on_container_swap
105 value = Flag::is_propagate_on_swap
110 template <typename Flag> struct assign_allocator_base
112 struct propagate_on_container_copy_assignment
116 value = Flag::is_propagate_on_assign
121 template <typename Flag> struct move_allocator_base
123 struct propagate_on_container_move_assignment
127 value = Flag::is_propagate_on_move
133 // boostinspect:nounnamed
134 bool force_equal_allocator_value = false;
137 struct force_equal_allocator
141 explicit force_equal_allocator(bool value)
142 : old_value_(force_equal_allocator_value)
144 force_equal_allocator_value = value;
147 ~force_equal_allocator() { force_equal_allocator_value = old_value_; }
150 template <typename T> struct cxx11_allocator_base
155 typedef std::size_t size_type;
156 typedef std::ptrdiff_t difference_type;
158 typedef T const* const_pointer;
159 typedef T& reference;
160 typedef T const& const_reference;
161 typedef T value_type;
163 explicit cxx11_allocator_base(int t) : tag_(t), selected_(0)
165 detail::tracker.allocator_ref();
168 template <typename Y>
169 cxx11_allocator_base(cxx11_allocator_base<Y> const& x)
170 : tag_(x.tag_), selected_(x.selected_)
172 detail::tracker.allocator_ref();
175 cxx11_allocator_base(cxx11_allocator_base const& x)
176 : tag_(x.tag_), selected_(x.selected_)
178 detail::tracker.allocator_ref();
181 ~cxx11_allocator_base() { detail::tracker.allocator_unref(); }
183 pointer address(reference r) { return pointer(&r); }
185 const_pointer address(const_reference r) { return const_pointer(&r); }
187 pointer allocate(size_type n)
189 pointer ptr(static_cast<T*>(::operator new(n * sizeof(T))));
190 detail::tracker.track_allocate((void*)ptr, n, sizeof(T), tag_);
194 pointer allocate(size_type n, void const*)
196 pointer ptr(static_cast<T*>(::operator new(n * sizeof(T))));
197 detail::tracker.track_allocate((void*)ptr, n, sizeof(T), tag_);
201 void deallocate(pointer p, size_type n)
203 // Only checking tags when propagating swap.
204 // Note that tags will be tested
205 // properly in the normal allocator.
206 detail::tracker.track_deallocate(
207 (void*)p, n, sizeof(T), tag_, !force_equal_allocator_value);
208 ::operator delete((void*)p);
211 void construct(T* p, T const& t)
213 detail::tracker.track_construct((void*)p, sizeof(T), tag_);
217 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
218 template <typename... Args>
219 void construct(T* p, BOOST_FWD_REF(Args)... args)
221 detail::tracker.track_construct((void*)p, sizeof(T), tag_);
222 new (p) T(boost::forward<Args>(args)...);
228 detail::tracker.track_destroy((void*)p, sizeof(T), tag_);
232 size_type max_size() const
234 return (std::numeric_limits<size_type>::max)();
238 template <typename T, typename Flags = propagate_swap, typename Enable = void>
239 struct cxx11_allocator;
241 template <typename T, typename Flags>
242 struct cxx11_allocator<T, Flags,
243 typename boost::disable_if_c<Flags::is_select_on_copy>::type>
244 : public cxx11_allocator_base<T>,
245 public swap_allocator_base<Flags>,
246 public assign_allocator_base<Flags>,
247 public move_allocator_base<Flags>,
250 #if BOOST_WORKAROUND(BOOST_GCC_VERSION, < 402000)
251 template <typename U> struct rebind
253 typedef cxx11_allocator<U, Flags> other;
257 explicit cxx11_allocator(int t = 0) : cxx11_allocator_base<T>(t) {}
259 template <typename Y>
260 cxx11_allocator(cxx11_allocator<Y, Flags> const& x)
261 : cxx11_allocator_base<T>(x)
265 cxx11_allocator(cxx11_allocator const& x) : cxx11_allocator_base<T>(x) {}
267 // When not propagating swap, allocators are always equal
268 // to avoid undefined behaviour.
269 bool operator==(cxx11_allocator const& x) const
271 return force_equal_allocator_value || (this->tag_ == x.tag_);
274 bool operator!=(cxx11_allocator const& x) const { return !(*this == x); }
277 template <typename T, typename Flags>
278 struct cxx11_allocator<T, Flags,
279 typename boost::enable_if_c<Flags::is_select_on_copy>::type>
280 : public cxx11_allocator_base<T>,
281 public swap_allocator_base<Flags>,
282 public assign_allocator_base<Flags>,
283 public move_allocator_base<Flags>,
286 cxx11_allocator select_on_container_copy_construction() const
288 cxx11_allocator tmp(*this);
293 #if BOOST_WORKAROUND(BOOST_GCC_VERSION, < 402000)
294 template <typename U> struct rebind
296 typedef cxx11_allocator<U, Flags> other;
300 explicit cxx11_allocator(int t = 0) : cxx11_allocator_base<T>(t) {}
302 template <typename Y>
303 cxx11_allocator(cxx11_allocator<Y, Flags> const& x)
304 : cxx11_allocator_base<T>(x)
308 cxx11_allocator(cxx11_allocator const& x) : cxx11_allocator_base<T>(x) {}
310 // When not propagating swap, allocators are always equal
311 // to avoid undefined behaviour.
312 bool operator==(cxx11_allocator const& x) const
314 return force_equal_allocator_value || (this->tag_ == x.tag_);
317 bool operator!=(cxx11_allocator const& x) const { return !(*this == x); }
320 template <typename T, typename Flags>
321 bool equivalent_impl(cxx11_allocator<T, Flags> const& x,
322 cxx11_allocator<T, Flags> const& y, test::derived_type)
324 return x.tag_ == y.tag_;
327 // Function to check how many times an allocator has been selected,
328 // return 0 for other allocators.
330 struct convert_from_anything
332 template <typename T> convert_from_anything(T const&) {}
335 inline int selected_count(convert_from_anything) { return 0; }
337 template <typename T, typename Flags>
338 int selected_count(cxx11_allocator<T, Flags> const& x)