1
//////////////////////////////////////////////////////////////////////////////
3
// (C) Copyright Ion Gaztanaga 2012-2012. 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
18
#include <boost/container/detail/config_begin.hpp>
19
#include <boost/container/detail/workaround.hpp>
20
#include <boost/container/allocator_traits.hpp> //allocator_traits
21
#include <boost/container/throw_exception.hpp>
22
#include <boost/container/detail/multiallocation_chain.hpp> //multiallocation_chain
23
#include <boost/container/detail/version_type.hpp> //version_type
24
#include <boost/container/detail/allocation_type.hpp> //allocation_type
25
#include <boost/container/detail/mpl.hpp> //integral_constant
26
#include <boost/intrusive/pointer_traits.hpp> //pointer_traits
27
#include <utility> //pair
28
#include <boost/detail/no_exceptions_support.hpp> //BOOST_TRY
32
namespace container_detail {
34
template<class Allocator, unsigned Version = boost::container::container_detail::version<Allocator>::value>
35
struct allocator_version_traits
37
typedef ::boost::container::container_detail::integral_constant
38
<unsigned, Version> alloc_version;
40
typedef typename Allocator::multiallocation_chain multiallocation_chain;
42
typedef typename boost::container::allocator_traits<Allocator>::pointer pointer;
43
typedef typename boost::container::allocator_traits<Allocator>::size_type size_type;
45
//Node allocation interface
46
static pointer allocate_one(Allocator &a)
47
{ return a.allocate_one(); }
49
static void deallocate_one(Allocator &a, const pointer &p)
50
{ a.deallocate_one(p); }
52
static void allocate_individual(Allocator &a, size_type n, multiallocation_chain &m)
53
{ return a.allocate_individual(n, m); }
55
static void deallocate_individual(Allocator &a, multiallocation_chain &holder)
56
{ a.deallocate_individual(holder); }
58
static std::pair<pointer, bool>
59
allocation_command(Allocator &a, allocation_type command,
60
size_type limit_size, size_type preferred_size,
61
size_type &received_size, const pointer &reuse)
63
return a.allocation_command
64
(command, limit_size, preferred_size, received_size, reuse);
68
template<class Allocator>
69
struct allocator_version_traits<Allocator, 1>
71
typedef ::boost::container::container_detail::integral_constant
72
<unsigned, 1> alloc_version;
74
typedef typename boost::container::allocator_traits<Allocator>::pointer pointer;
75
typedef typename boost::container::allocator_traits<Allocator>::size_type size_type;
76
typedef typename boost::container::allocator_traits<Allocator>::value_type value_type;
78
typedef typename boost::intrusive::pointer_traits<pointer>::
79
template rebind_pointer<void>::type void_ptr;
80
typedef container_detail::basic_multiallocation_chain
81
<void_ptr> multialloc_cached_counted;
82
typedef boost::container::container_detail::
83
transform_multiallocation_chain
84
< multialloc_cached_counted, value_type> multiallocation_chain;
86
//Node allocation interface
87
static pointer allocate_one(Allocator &a)
88
{ return a.allocate(1); }
90
static void deallocate_one(Allocator &a, const pointer &p)
91
{ a.deallocate(p, 1); }
93
static void deallocate_individual(Allocator &a, multiallocation_chain &holder)
95
size_type n = holder.size();
96
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 std::pair<pointer, bool>
135
allocation_command(Allocator &a, allocation_type command,
136
size_type, size_type preferred_size,
137
size_type &received_size, const pointer &)
139
std::pair<pointer, bool> ret(pointer(), false);
140
if(!(command & allocate_new)){
141
if(!(command & nothrow_allocation)){
142
throw_logic_error("version 1 allocator without allocate_new flag");
146
received_size = preferred_size;
148
ret.first = a.allocate(received_size);
151
if(!(command & nothrow_allocation)){
161
} //namespace container_detail {
162
} //namespace container {
163
} //namespace boost {
165
#include <boost/container/detail/config_end.hpp>
167
#endif // ! defined(BOOST_CONTAINER_DETAIL_ALLOCATOR_VERSION_TRAITS_HPP)