1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2012-2013. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/container for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_CONTAINER_DETAIL_ALLOCATOR_VERSION_TRAITS_HPP
12 #define BOOST_CONTAINER_DETAIL_ALLOCATOR_VERSION_TRAITS_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/container/detail/config_begin.hpp>
23 #include <boost/container/detail/workaround.hpp>
25 #include <boost/container/allocator_traits.hpp> //allocator_traits
26 #include <boost/container/throw_exception.hpp>
27 #include <boost/container/detail/multiallocation_chain.hpp> //multiallocation_chain
28 #include <boost/container/detail/version_type.hpp> //version_type
29 #include <boost/container/detail/allocation_type.hpp> //allocation_type
30 #include <boost/container/detail/mpl.hpp> //integral_constant
31 #include <boost/intrusive/pointer_traits.hpp> //pointer_traits
32 #include <boost/core/no_exceptions_support.hpp> //BOOST_TRY
38 template<class Allocator, unsigned Version = boost::container::dtl::version<Allocator>::value>
39 struct allocator_version_traits
41 typedef ::boost::container::dtl::integral_constant
42 <unsigned, Version> alloc_version;
44 typedef typename Allocator::multiallocation_chain multiallocation_chain;
46 typedef typename boost::container::allocator_traits<Allocator>::pointer pointer;
47 typedef typename boost::container::allocator_traits<Allocator>::size_type size_type;
49 //Node allocation interface
50 static pointer allocate_one(Allocator &a)
51 { return a.allocate_one(); }
53 static void deallocate_one(Allocator &a, const pointer &p)
54 { a.deallocate_one(p); }
56 static void allocate_individual(Allocator &a, size_type n, multiallocation_chain &m)
57 { return a.allocate_individual(n, m); }
59 static void deallocate_individual(Allocator &a, multiallocation_chain &holder)
60 { a.deallocate_individual(holder); }
62 static pointer allocation_command(Allocator &a, allocation_type command,
63 size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse)
64 { return a.allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse); }
67 template<class Allocator>
68 struct allocator_version_traits<Allocator, 1>
70 typedef ::boost::container::dtl::integral_constant
71 <unsigned, 1> alloc_version;
73 typedef typename boost::container::allocator_traits<Allocator>::pointer pointer;
74 typedef typename boost::container::allocator_traits<Allocator>::size_type size_type;
75 typedef typename boost::container::allocator_traits<Allocator>::value_type value_type;
77 typedef typename boost::intrusive::pointer_traits<pointer>::
78 template rebind_pointer<void>::type void_ptr;
79 typedef dtl::basic_multiallocation_chain
80 <void_ptr> multialloc_cached_counted;
81 typedef boost::container::dtl::
82 transform_multiallocation_chain
83 < multialloc_cached_counted, value_type> multiallocation_chain;
85 //Node allocation interface
86 static pointer allocate_one(Allocator &a)
87 { return a.allocate(1); }
89 static void deallocate_one(Allocator &a, const pointer &p)
90 { a.deallocate(p, 1); }
92 static void deallocate_individual(Allocator &a, multiallocation_chain &holder)
94 size_type n = holder.size();
95 typename multiallocation_chain::iterator it = holder.begin();
98 pointer p = boost::intrusive::pointer_traits<pointer>::pointer_to(*it);
104 struct allocate_individual_rollback
106 allocate_individual_rollback(Allocator &a, multiallocation_chain &chain)
107 : mr_a(a), mp_chain(&chain)
110 ~allocate_individual_rollback()
113 allocator_version_traits::deallocate_individual(mr_a, *mp_chain);
122 multiallocation_chain * mp_chain;
125 static void allocate_individual(Allocator &a, size_type n, multiallocation_chain &m)
127 allocate_individual_rollback rollback(a, m);
129 m.push_front(a.allocate(1));
134 static pointer allocation_command(Allocator &a, allocation_type command,
135 size_type, size_type &prefer_in_recvd_out_size, pointer &reuse)
137 pointer ret = pointer();
138 if(BOOST_UNLIKELY(!(command & allocate_new) && !(command & nothrow_allocation))){
139 throw_logic_error("version 1 allocator without allocate_new flag");
143 ret = a.allocate(prefer_in_recvd_out_size);
146 if(!(command & nothrow_allocation)){
158 } //namespace container {
159 } //namespace boost {
161 #include <boost/container/detail/config_end.hpp>
163 #endif // ! defined(BOOST_CONTAINER_DETAIL_ALLOCATOR_VERSION_TRAITS_HPP)