* Improved vector's insertion performance.

*  Changed again experimental multiallocation interface for better performance (still experimental).
*  Added no exception support for those willing to disable exceptions in their compilers.
*  Fixed GCC -Wshadow warnings.
*  Replaced deprecated BOOST_NO_XXXX with newer BOOST_NO_CXX11_XXX macros.


[SVN r81518]
This commit is contained in:
Ion Gaztañaga committed 2012-11-24 21:08:18 +00:00
1 parent a75d72d03c
commit bc5c91bb79
28 files changed
+2449 -1892

No files matched your search

@@ -21,16 +21,28 @@
#include <boost/container/detail/utilities.hpp>
#include <boost/intrusive/pointer_traits.hpp>
#include <boost/intrusive/set.hpp>
#include <boost/intrusive/list.hpp>
#include <boost/intrusive/slist.hpp>
#include <boost/container/detail/type_traits.hpp>
#include <boost/container/detail/math_functions.hpp>
#include <boost/container/detail/mpl.hpp>
#include <boost/container/detail/pool_common.hpp>
#include <boost/assert.hpp>
#include <boost/detail/no_exceptions_support.hpp>
#include <cstddef>
namespace boost {
namespace container {
namespace adaptive_pool_flag {
static const unsigned int none = 0u;
static const unsigned int align_only = 1u << 0u;
static const unsigned int size_ordered = 1u << 1u;
static const unsigned int address_ordered = 1u << 2u;
} //namespace adaptive_pool_flag{
namespace container_detail {
template<class size_type>
@@ -42,42 +54,160 @@ struct hdr_offset_holder_t
size_type hdr_offset;
};
template<class SizeType, unsigned int Flags>
struct less_func;
template<class SizeType>
struct less_func<SizeType, adaptive_pool_flag::none>
{
static bool less(SizeType, SizeType, const void *, const void *)
{ return true; }
};
template<class SizeType>
struct less_func<SizeType, adaptive_pool_flag::size_ordered>
{
static bool less(SizeType ls, SizeType rs, const void *, const void *)
{ return ls < rs; }
};
template<class SizeType>
struct less_func<SizeType, adaptive_pool_flag::address_ordered>
{
static bool less(SizeType, SizeType, const void *la, const void *ra)
{ return &la < &ra; }
};
template<class SizeType>
struct less_func<SizeType, adaptive_pool_flag::size_ordered | adaptive_pool_flag::address_ordered>
{
static bool less(SizeType ls, SizeType rs, const void *la, const void *ra)
{ return (ls < rs) || ((ls == rs) && (la < ra)); }
};
template<class VoidPointer, class SizeType, bool ordered>
struct block_container_traits
{
typedef typename bi::make_set_base_hook
< bi::void_pointer<VoidPointer>
, bi::optimize_size<true>
, bi::link_mode<bi::normal_link> >::type hook_t;
template<class T>
struct container
{
typedef typename bi::make_multiset
<T, bi::base_hook<hook_t>, bi::size_type<SizeType> >::type type;
};
template<class Container>
static void reinsert_was_used(Container &container, typename Container::reference v, bool)
{
typedef typename Container::const_iterator const_block_iterator;
const const_block_iterator this_block
(Container::s_iterator_to(const_cast<typename Container::const_reference>(v)));
const_block_iterator next_block(this_block);
if(++next_block != container.cend()){
if(this_block->free_nodes.size() > next_block->free_nodes.size()){
container.erase(this_block);
container.insert(v);
}
}
}
template<class Container>
static void insert_was_empty(Container &container, typename Container::value_type &v, bool)
{
container.insert(v);
}
template<class Container>
static void erase_first(Container &container)
{
container.erase(container.cbegin());
}
template<class Container>
static void erase_last(Container &container)
{
container.erase(--container.cend());
}
};
template<class VoidPointer, class SizeType>
struct block_container_traits<VoidPointer, SizeType, false>
{
typedef typename bi::make_list_base_hook
< bi::void_pointer<VoidPointer>
, bi::link_mode<bi::normal_link> >::type hook_t;
template<class T>
struct container
{
typedef typename bi::make_list
<T, bi::base_hook<hook_t>, bi::size_type<SizeType>, bi::constant_time_size<false> >::type type;
};
template<class Container>
static void reinsert_was_used(Container &container, typename Container::value_type &v, bool is_full)
{
if(is_full){
container.erase(Container::s_iterator_to(v));
container.push_back(v);
}
}
template<class Container>
static void insert_was_empty(Container &container, typename Container::value_type &v, bool is_full)
{
if(is_full){
container.push_back(v);
}
else{
container.push_front(v);
}
}
template<class Container>
static void erase_first(Container &container)
{
container.pop_front();
}
template<class Container>
static void erase_last(Container &container)
{
container.pop_back();
}
};
template<class MultiallocationChain, class VoidPointer, class SizeType, unsigned int Flags>
struct adaptive_pool_types
{
typedef VoidPointer void_pointer;
typedef typename bi::make_set_base_hook
< bi::void_pointer<void_pointer>
, bi::optimize_size<true>
, bi::constant_time_size<false>
, bi::link_mode<bi::normal_link> >::type multiset_hook_t;
static const bool ordered = (Flags & (adaptive_pool_flag::size_ordered | adaptive_pool_flag::address_ordered)) != 0;
typedef block_container_traits<VoidPointer, SizeType, ordered> block_container_traits_t;
typedef typename block_container_traits_t::hook_t hook_t;
typedef hdr_offset_holder_t<SizeType> hdr_offset_holder;
static const unsigned int order_flags = Flags & (adaptive_pool_flag::size_ordered | adaptive_pool_flag::address_ordered);
typedef MultiallocationChain free_nodes_t;
struct block_info_t
:
public hdr_offset_holder,
public multiset_hook_t
: public hdr_offset_holder,
public hook_t
{
typedef typename node_slist<void_pointer>::node_slist_t free_nodes_t;
//An intrusive list of free node from this block
free_nodes_t free_nodes;
friend bool operator <(const block_info_t &l, const block_info_t &r)
{
// { return l.free_nodes.size() < r.free_nodes.size(); }
//Let's order blocks first by free nodes and then by address
//so that highest address fully free blocks are deallocated.
//This improves returning memory to the OS (trimming).
const bool is_less = l.free_nodes.size() < r.free_nodes.size();
const bool is_equal = l.free_nodes.size() == r.free_nodes.size();
return is_less || (is_equal && (&l < &r));
return less_func<SizeType, order_flags>::
less(l.free_nodes.size(), r.free_nodes.size(), &l , &r);
}
friend bool operator ==(const block_info_t &l, const block_info_t &r)
{ return &l == &r; }
};
typedef typename bi::make_multiset
<block_info_t, bi::base_hook<multiset_hook_t> >::type block_multiset_t;
typedef typename block_container_traits_t:: template container<block_info_t>::type block_container_t;
};
template<class size_type>
@@ -113,9 +243,9 @@ inline void calculate_num_subblocks
, size_type &num_subblocks, size_type &real_num_node, size_type overhead_percent
, size_type hdr_size, size_type hdr_offset_size, size_type payload_per_allocation)
{
const size_type hdr_subblock_elements = (alignment - hdr_size - payload_per_allocation)/real_node_size;
size_type elements_per_subblock = (alignment - hdr_offset_size)/real_node_size;
size_type possible_num_subblock = (elements_per_block - 1)/elements_per_subblock + 1;
size_type hdr_subblock_elements = (alignment - hdr_size - payload_per_allocation)/real_node_size;
while(((possible_num_subblock-1)*elements_per_subblock + hdr_subblock_elements) < elements_per_block){
++possible_num_subblock;
}
@@ -135,7 +265,7 @@ inline void calculate_num_subblocks
real_num_node = (possible_num_subblock-1)*elements_per_subblock + hdr_subblock_elements;
}
template<class SegmentManagerBase, bool AlignOnly = false>
template<class SegmentManagerBase, unsigned int Flags>
class private_adaptive_node_pool_impl
{
//Non-copyable
@@ -147,27 +277,42 @@ class private_adaptive_node_pool_impl
typedef typename SegmentManagerBase::void_pointer void_pointer;
static const typename SegmentManagerBase::
size_type PayloadPerAllocation = SegmentManagerBase::PayloadPerAllocation;
//Flags
//align_only
static const bool AlignOnly = (Flags & adaptive_pool_flag::align_only) != 0;
typedef bool_<AlignOnly> IsAlignOnly;
typedef true_ AlignOnlyTrue;
typedef false_ AlignOnlyFalse;
//size_ordered
static const bool SizeOrdered = (Flags & adaptive_pool_flag::size_ordered) != 0;
typedef bool_<SizeOrdered> IsSizeOrdered;
typedef true_ SizeOrderedTrue;
typedef false_ SizeOrderedFalse;
//address_ordered
static const bool AddressOrdered = (Flags & adaptive_pool_flag::address_ordered) != 0;
typedef bool_<AddressOrdered> IsAddressOrdered;
typedef true_ AddressOrderedTrue;
typedef false_ AddressOrderedFalse;
public:
typedef typename node_slist<void_pointer>::node_t node_t;
typedef typename node_slist<void_pointer>::node_slist_t free_nodes_t;
typedef typename SegmentManagerBase::multiallocation_chain multiallocation_chain;
typedef typename SegmentManagerBase::size_type size_type;
typedef typename SegmentManagerBase::multiallocation_chain multiallocation_chain;
typedef typename SegmentManagerBase::size_type size_type;
private:
typedef typename adaptive_pool_types<void_pointer, size_type>::block_info_t block_info_t;
typedef typename adaptive_pool_types<void_pointer, size_type>::block_multiset_t block_multiset_t;
typedef typename block_multiset_t::iterator block_iterator;
typedef typename adaptive_pool_types<void_pointer, size_type>::hdr_offset_holder hdr_offset_holder;
typedef adaptive_pool_types
<multiallocation_chain, void_pointer, size_type, Flags> adaptive_pool_types_t;
typedef typename adaptive_pool_types_t::free_nodes_t free_nodes_t;
typedef typename adaptive_pool_types_t::block_info_t block_info_t;
typedef typename adaptive_pool_types_t::block_container_t block_container_t;
typedef typename adaptive_pool_types_t::block_container_traits_t block_container_traits_t;
typedef typename block_container_t::iterator block_iterator;
typedef typename block_container_t::const_iterator const_block_iterator;
typedef typename adaptive_pool_types_t::hdr_offset_holder hdr_offset_holder;
static const size_type MaxAlign = alignment_of<node_t>::value;
static const size_type MaxAlign = alignment_of<void_pointer>::value;
static const size_type HdrSize = ((sizeof(block_info_t)-1)/MaxAlign+1)*MaxAlign;
static const size_type HdrOffsetSize = ((sizeof(hdr_offset_holder)-1)/MaxAlign+1)*MaxAlign;
public:
//!Segment manager typedef
typedef SegmentManagerBase segment_manager_base_type;
@@ -181,7 +326,7 @@ class private_adaptive_node_pool_impl
, unsigned char overhead_percent
)
: m_max_free_blocks(max_free_blocks)
, m_real_node_size(lcm(node_size, size_type(alignment_of<node_t>::value)))
, m_real_node_size(lcm(node_size, size_type(alignment_of<void_pointer>::value)))
//Round the size to a power of two value.
//This is the total memory size (including payload) that we want to
//allocate from the general-purpose allocator
@@ -195,7 +340,7 @@ class private_adaptive_node_pool_impl
, m_real_num_node(AlignOnly ? (m_real_block_alignment - PayloadPerAllocation - HdrSize)/m_real_node_size : 0)
//General purpose allocator
, mp_segment_mngr_base(segment_mngr_base)
, m_block_multiset()
, m_block_container()
, m_totally_free_blocks(0)
{
if(!AlignOnly){
@@ -214,7 +359,7 @@ class private_adaptive_node_pool_impl
//!Destructor. Deallocates all allocated blocks. Never throws
~private_adaptive_node_pool_impl()
{ priv_clear(); }
{ this->priv_clear(); }
size_type get_real_num_node() const
{ return m_real_num_node; }
@@ -226,84 +371,157 @@ class private_adaptive_node_pool_impl
//!Allocates array of count elements. Can throw
void *allocate_node()
{
priv_invariants();
this->priv_invariants();
//If there are no free nodes we allocate a new block
if (m_block_multiset.empty()){
priv_alloc_block(1);
if(!m_block_container.empty()){
//We take the first free node the multiset can't be empty
free_nodes_t &free_nodes = m_block_container.begin()->free_nodes;
BOOST_ASSERT(!free_nodes.empty());
const size_type free_nodes_count = free_nodes.size();
void *first_node = container_detail::to_raw_pointer(free_nodes.pop_front());
if(free_nodes.empty()){
block_container_traits_t::erase_first(m_block_container);
}
m_totally_free_blocks -= static_cast<size_type>(free_nodes_count == m_real_num_node);
this->priv_invariants();
return first_node;
}
else{
multiallocation_chain chain;
this->priv_append_from_new_blocks(1, chain, IsAlignOnly());
return container_detail::to_raw_pointer(chain.pop_front());
}
//We take the first free node the multiset can't be empty
return priv_take_first_node();
}
//!Deallocates an array pointed by ptr. Never throws
void deallocate_node(void *pElem)
{
multiallocation_chain chain;
chain.push_front(void_pointer(pElem));
this->priv_reinsert_nodes_in_block(chain, 1);
//Update free block count<
if(m_totally_free_blocks > m_max_free_blocks){
this->priv_deallocate_free_blocks(m_max_free_blocks);
}
priv_invariants();
this->priv_invariants();
block_info_t &block_info = *this->priv_block_from_node(pElem);
BOOST_ASSERT(block_info.free_nodes.size() < m_real_num_node);
//We put the node at the beginning of the free node list
block_info.free_nodes.push_back(void_pointer(pElem));
//The loop reinserts all blocks except the last one
this->priv_reinsert_block(block_info, block_info.free_nodes.size() == 1);
this->priv_deallocate_free_blocks(m_max_free_blocks);
this->priv_invariants();
}
//!Allocates n nodes.
//!Can throw
multiallocation_chain allocate_nodes(const size_type n)
void allocate_nodes(const size_type n, multiallocation_chain &chain)
{
multiallocation_chain chain;
size_type i = 0;
try{
priv_invariants();
BOOST_TRY{
this->priv_invariants();
while(i != n){
//If there are no free nodes we allocate all needed blocks
if (m_block_multiset.empty()){
priv_alloc_block(((n - i) - 1)/m_real_num_node + 1);
if (m_block_container.empty()){
this->priv_append_from_new_blocks(n - i, chain, IsAlignOnly());
BOOST_ASSERT(m_block_container.empty() || (++m_block_container.cbegin() == m_block_container.cend()));
BOOST_ASSERT(chain.size() == n);
break;
}
free_nodes_t &free_nodes = m_block_multiset.begin()->free_nodes;
free_nodes_t &free_nodes = m_block_container.begin()->free_nodes;
const size_type free_nodes_count_before = free_nodes.size();
if(free_nodes_count_before == m_real_num_node){
--m_totally_free_blocks;
}
const size_type num_elems = ((n-i) < free_nodes_count_before) ? (n-i) : free_nodes_count_before;
for(size_type j = 0; j != num_elems; ++j){
void *new_node = &free_nodes.front();
free_nodes.pop_front();
chain.push_back(new_node);
}
m_totally_free_blocks -= static_cast<size_type>(free_nodes_count_before == m_real_num_node);
const size_type num_left = n-i;
const size_type num_elems = (num_left < free_nodes_count_before) ? num_left : free_nodes_count_before;
typedef typename free_nodes_t::iterator free_nodes_iterator;
if(free_nodes.empty()){
m_block_multiset.erase(m_block_multiset.begin());
if(num_left < free_nodes_count_before){
const free_nodes_iterator it_bbeg(free_nodes.before_begin());
free_nodes_iterator it_bend(it_bbeg);
for(size_type j = 0; j != num_elems; ++j){
++it_bend;
}
free_nodes_iterator it_end = it_bend; ++it_end;
free_nodes_iterator it_beg = it_bbeg; ++it_beg;
free_nodes.erase_after(it_bbeg, it_end, num_elems);
chain.incorporate_after(chain.last(), &*it_beg, &*it_bend, num_elems);
//chain.splice_after(chain.last(), free_nodes, it_bbeg, it_bend, num_elems);
BOOST_ASSERT(!free_nodes.empty());
}
else{
const free_nodes_iterator it_beg(free_nodes.begin()), it_bend(free_nodes.last());
free_nodes.clear();
chain.incorporate_after(chain.last(), &*it_beg, &*it_bend, num_elems);
block_container_traits_t::erase_first(m_block_container);
}
i += num_elems;
}
}
catch(...){
this->deallocate_nodes(boost::move(chain));
throw;
BOOST_CATCH(...){
this->deallocate_nodes(chain);
BOOST_RETHROW
}
priv_invariants();
return boost::move(chain);
BOOST_CATCH_END
this->priv_invariants();
}
//!Deallocates a linked list of nodes. Never throws
void deallocate_nodes(multiallocation_chain nodes)
void deallocate_nodes(multiallocation_chain &nodes)
{
this->priv_reinsert_nodes_in_block(nodes, nodes.size());
if(m_totally_free_blocks > m_max_free_blocks){
this->priv_invariants();
//To take advantage of node locality, wait until two
//nodes belong to different blocks. Only then reinsert
//the block of the first node in the block tree.
//Cache of the previous block
block_info_t *prev_block_info = 0;
//If block was empty before this call, it's not already
//inserted in the block tree.
bool prev_block_was_empty = false;
typedef typename free_nodes_t::iterator free_nodes_iterator;
{
const free_nodes_iterator itbb(nodes.before_begin()), ite(nodes.end());
free_nodes_iterator itf(nodes.begin()), itbf(itbb);
size_type splice_node_count = size_type(-1);
while(itf != ite){
void *pElem = container_detail::to_raw_pointer(&*itf);
block_info_t &block_info = *this->priv_block_from_node(pElem);
BOOST_ASSERT(block_info.free_nodes.size() < m_real_num_node);
++splice_node_count;
//If block change is detected calculate the cached block position in the tree
if(&block_info != prev_block_info){
if(prev_block_info){ //Make sure we skip the initial "dummy" cache
free_nodes_iterator it(itbb); ++it;
nodes.erase_after(itbb, itf, splice_node_count);
prev_block_info->free_nodes.incorporate_after(prev_block_info->free_nodes.last(), &*it, &*itbf, splice_node_count);
this->priv_reinsert_block(*prev_block_info, prev_block_was_empty);
splice_node_count = 0;
}
//Update cache with new data
prev_block_was_empty = block_info.free_nodes.empty();
prev_block_info = &block_info;
}
itbf = itf;
++itf;
}
}
if(prev_block_info){
//The loop reinserts all blocks except the last one
const free_nodes_iterator itfirst(nodes.begin()), itlast(nodes.last());
const size_type splice_node_count = nodes.size();
nodes.clear();
prev_block_info->free_nodes.incorporate_after(prev_block_info->free_nodes.last(), &*itfirst, &*itlast, splice_node_count);
this->priv_reinsert_block(*prev_block_info, prev_block_was_empty);
this->priv_invariants();
this->priv_deallocate_free_blocks(m_max_free_blocks);
}
}
void deallocate_free_blocks()
{ this->priv_deallocate_free_blocks(0); }
{ this->priv_deallocate_free_blocks(0); }
size_type num_free_nodes()
{
typedef typename block_multiset_t::const_iterator citerator;
typedef typename block_container_t::const_iterator citerator;
size_type count = 0;
citerator it (m_block_multiset.begin()), itend(m_block_multiset.end());
citerator it (m_block_container.begin()), itend(m_block_container.end());
for(; it != itend; ++it){
count += it->free_nodes.size();
}
@@ -318,7 +536,7 @@ class private_adaptive_node_pool_impl
BOOST_ASSERT(m_real_num_node == other.m_real_num_node);
std::swap(mp_segment_mngr_base, other.mp_segment_mngr_base);
std::swap(m_totally_free_blocks, other.m_totally_free_blocks);
m_block_multiset.swap(other.m_block_multiset);
m_block_container.swap(other.m_block_container);
}
//Deprecated, use deallocate_free_blocks
@@ -326,83 +544,54 @@ class private_adaptive_node_pool_impl
{ this->priv_deallocate_free_blocks(0); }
private:
void priv_deallocate_free_blocks(size_type max_free_blocks)
{ //Trampoline function to ease inlining
if(m_totally_free_blocks > max_free_blocks){
this->priv_deallocate_free_blocks_impl(max_free_blocks);
}
}
void priv_deallocate_free_blocks_impl(size_type max_free_blocks)
{
priv_invariants();
this->priv_invariants();
//Now check if we've reached the free nodes limit
//and check if we have free blocks. If so, deallocate as much
//as we can to stay below the limit
for( block_iterator itend = m_block_multiset.end()
; m_totally_free_blocks > max_free_blocks
; --m_totally_free_blocks
){
BOOST_ASSERT(!m_block_multiset.empty());
block_iterator it = itend;
multiallocation_chain chain;
{
const const_block_iterator itend = m_block_container.cend();
const_block_iterator it = itend;
--it;
BOOST_ASSERT(it->free_nodes.size() == m_real_num_node);
m_block_multiset.erase_and_dispose(it, block_destroyer(this));
size_type totally_free_blocks = m_totally_free_blocks;
for( ; totally_free_blocks > max_free_blocks; --totally_free_blocks){
BOOST_ASSERT(it->free_nodes.size() == m_real_num_node);
void *addr = priv_first_subblock_from_block(const_cast<block_info_t*>(&*it));
--it;
block_container_traits_t::erase_last(m_block_container);
chain.push_front(void_pointer(addr));
}
BOOST_ASSERT((m_totally_free_blocks - max_free_blocks) == chain.size());
m_totally_free_blocks = max_free_blocks;
}
this->mp_segment_mngr_base->deallocate_many(chain);
}
void priv_reinsert_nodes_in_block(multiallocation_chain &chain, size_type n)
void priv_reinsert_block(block_info_t &prev_block_info, const bool prev_block_was_empty)
{
block_iterator block_it(m_block_multiset.end());
while(n--){
void *pElem = container_detail::to_raw_pointer(chain.pop_front());
priv_invariants();
block_info_t *block_info = this->priv_block_from_node(pElem);
BOOST_ASSERT(block_info->free_nodes.size() < m_real_num_node);
//We put the node at the beginning of the free node list
node_t * to_deallocate = static_cast<node_t*>(pElem);
block_info->free_nodes.push_front(*to_deallocate);
//Cache the free nodes from the block
const size_type this_block_free_nodes = prev_block_info.free_nodes.size();
const bool is_full = this_block_free_nodes == m_real_num_node;
block_iterator this_block(block_multiset_t::s_iterator_to(*block_info));
block_iterator next_block(this_block);
++next_block;
//Cache the free nodes from the block
size_type this_block_free_nodes = this_block->free_nodes.size();
if(this_block_free_nodes == 1){
m_block_multiset.insert(m_block_multiset.begin(), *block_info);
}
else{
block_iterator next_block(this_block);
++next_block;
if(next_block != block_it){
size_type next_free_nodes = next_block->free_nodes.size();
if(this_block_free_nodes > next_free_nodes){
//Now move the block to the new position
m_block_multiset.erase(this_block);
m_block_multiset.insert(*block_info);
}
}
}
//Update free block count
if(this_block_free_nodes == m_real_num_node){
++m_totally_free_blocks;
}
priv_invariants();
//Update free block count
m_totally_free_blocks += static_cast<size_type>(is_full);
if(prev_block_was_empty){
block_container_traits_t::insert_was_empty(m_block_container, prev_block_info, is_full);
}
}
node_t *priv_take_first_node()
{
BOOST_ASSERT(m_block_multiset.begin() != m_block_multiset.end());
//We take the first free node the multiset can't be empty
free_nodes_t &free_nodes = m_block_multiset.begin()->free_nodes;
node_t *first_node = &free_nodes.front();
const size_type free_nodes_count = free_nodes.size();
BOOST_ASSERT(0 != free_nodes_count);
free_nodes.pop_front();
if(free_nodes_count == 1){
m_block_multiset.erase(m_block_multiset.begin());
else{
block_container_traits_t::reinsert_was_used(m_block_container, prev_block_info, is_full);
}
else if(free_nodes_count == m_real_num_node){
--m_totally_free_blocks;
}
priv_invariants();
return first_node;
}
class block_destroyer;
@@ -411,33 +600,31 @@ class private_adaptive_node_pool_impl
class block_destroyer
{
public:
block_destroyer(const this_type *impl)
: mp_impl(impl)
block_destroyer(const this_type *impl, multiallocation_chain &chain)
: mp_impl(impl), m_chain(chain)
{}
void operator()(typename block_multiset_t::pointer to_deallocate)
void operator()(typename block_container_t::pointer to_deallocate)
{ return this->do_destroy(to_deallocate, IsAlignOnly()); }
private:
void do_destroy(typename block_multiset_t::pointer to_deallocate, AlignOnlyTrue)
void do_destroy(typename block_container_t::pointer to_deallocate, AlignOnlyTrue)
{
size_type free_nodes = to_deallocate->free_nodes.size();
(void)free_nodes;
BOOST_ASSERT(free_nodes == mp_impl->m_real_num_node);
mp_impl->mp_segment_mngr_base->deallocate(to_deallocate);
BOOST_ASSERT(to_deallocate->free_nodes.size() == mp_impl->m_real_num_node);
m_chain.push_back(to_deallocate);
}
void do_destroy(typename block_multiset_t::pointer to_deallocate, AlignOnlyFalse)
void do_destroy(typename block_container_t::pointer to_deallocate, AlignOnlyFalse)
{
size_type free_nodes = to_deallocate->free_nodes.size();
(void)free_nodes;
BOOST_ASSERT(free_nodes == mp_impl->m_real_num_node);
BOOST_ASSERT(to_deallocate->free_nodes.size() == mp_impl->m_real_num_node);
BOOST_ASSERT(0 == to_deallocate->hdr_offset);
hdr_offset_holder *hdr_off_holder = mp_impl->priv_first_subblock_from_block(container_detail::to_raw_pointer(to_deallocate));
mp_impl->mp_segment_mngr_base->deallocate(hdr_off_holder);
hdr_offset_holder *hdr_off_holder =
mp_impl->priv_first_subblock_from_block(container_detail::to_raw_pointer(to_deallocate));
m_chain.push_back(hdr_off_holder);
}
const this_type *mp_impl;
multiallocation_chain &m_chain;
};
//This macro will activate invariant checking. Slow, but helpful for debugging the code.
@@ -446,44 +633,45 @@ class private_adaptive_node_pool_impl
#ifdef BOOST_CONTAINER_ADAPTIVE_NODE_POOL_CHECK_INVARIANTS
#undef BOOST_CONTAINER_ADAPTIVE_NODE_POOL_CHECK_INVARIANTS
{
//We iterate through the block tree to free the memory
block_iterator it(m_block_multiset.begin()),
itend(m_block_multiset.end()), to_deallocate;
if(it != itend){
for(++it; it != itend; ++it){
block_iterator prev(it);
--prev;
size_type sp = prev->free_nodes.size(),
si = it->free_nodes.size();
BOOST_ASSERT(sp <= si);
(void)sp; (void)si;
const const_block_iterator itend(m_block_container.end());
{ //We iterate through the block tree to free the memory
const_block_iterator it(m_block_container.begin());
if(it != itend){
for(++it; it != itend; ++it){
const_block_iterator prev(it);
--prev;
BOOST_ASSERT(*prev < *it);
(void)prev; (void)it;
}
}
}
//Check that the total free nodes are correct
it = m_block_multiset.begin();
itend = m_block_multiset.end();
size_type total_free_nodes = 0;
for(; it != itend; ++it){
total_free_nodes += it->free_nodes.size();
{ //Check that the total free nodes are correct
const_block_iterator it(m_block_container.cbegin());
size_type total_free_nodes = 0;
for(; it != itend; ++it){
total_free_nodes += it->free_nodes.size();
}
BOOST_ASSERT(total_free_nodes >= m_totally_free_blocks*m_real_num_node);
}
BOOST_ASSERT(total_free_nodes >= m_totally_free_blocks*m_real_num_node);
//Check that the total totally free blocks are correct
it = m_block_multiset.begin();
itend = m_block_multiset.end();
total_free = 0;
for(; it != itend; ++it){
total_free += it->free_nodes.size() == m_real_num_node;
{ //Check that the total totally free blocks are correct
BOOST_ASSERT(m_block_container.size() >= m_totally_free_blocks);
const_block_iterator it = m_block_container.cend();
size_type total_free_blocks = m_totally_free_blocks;
while(total_free_blocks--){
BOOST_ASSERT((--it)->free_nodes.size() == m_real_num_node);
}
}
BOOST_ASSERT(total_free >= m_totally_free_blocks);
if(!AlignOnly){
//Check that header offsets are correct
it = m_block_multiset.begin();
const_block_iterator it = m_block_container.begin();
for(; it != itend; ++it){
hdr_offset_holder *hdr_off_holder = priv_first_subblock_from_block(&*it);
hdr_offset_holder *hdr_off_holder = this->priv_first_subblock_from_block(const_cast<block_info_t *>(&*it));
for(size_type i = 0, max = m_num_subblocks; i < max; ++i){
BOOST_ASSERT(hdr_off_holder->hdr_offset == size_type(reinterpret_cast<char*>(&*it)- reinterpret_cast<char*>(hdr_off_holder)));
const size_type offset = reinterpret_cast<char*>(const_cast<block_info_t *>(&*it)) - reinterpret_cast<char*>(hdr_off_holder);
BOOST_ASSERT(hdr_off_holder->hdr_offset == offset);
BOOST_ASSERT(0 == ((size_type)hdr_off_holder & (m_real_block_alignment - 1)));
BOOST_ASSERT(0 == (hdr_off_holder->hdr_offset & (m_real_block_alignment - 1)));
hdr_off_holder = reinterpret_cast<hdr_offset_holder *>(reinterpret_cast<char*>(hdr_off_holder) + m_real_block_alignment);
@@ -499,19 +687,21 @@ class private_adaptive_node_pool_impl
void priv_clear()
{
#ifndef NDEBUG
block_iterator it = m_block_multiset.begin();
block_iterator itend = m_block_multiset.end();
size_type num_free_nodes = 0;
block_iterator it = m_block_container.begin();
block_iterator itend = m_block_container.end();
size_type n_free_nodes = 0;
for(; it != itend; ++it){
//Check for memory leak
BOOST_ASSERT(it->free_nodes.size() == m_real_num_node);
++num_free_nodes;
++n_free_nodes;
}
BOOST_ASSERT(num_free_nodes == m_totally_free_blocks);
BOOST_ASSERT(n_free_nodes == m_totally_free_blocks);
#endif
//Check for memory leaks
priv_invariants();
m_block_multiset.clear_and_dispose(block_destroyer(this));
this->priv_invariants();
multiallocation_chain chain;
m_block_container.clear_and_dispose(block_destroyer(this, chain));
this->mp_segment_mngr_base->deallocate_many(chain);
m_totally_free_blocks = 0;
}
@@ -533,93 +723,129 @@ class private_adaptive_node_pool_impl
}
block_info_t *priv_block_from_node(void *node) const
{ return priv_block_from_node(node, IsAlignOnly()); }
{ return this->priv_block_from_node(node, IsAlignOnly()); }
hdr_offset_holder *priv_first_subblock_from_block(block_info_t *block) const
{ return this->priv_first_subblock_from_block(block, IsAlignOnly()); }
hdr_offset_holder *priv_first_subblock_from_block(block_info_t *block, AlignOnlyFalse) const
{
hdr_offset_holder *hdr_off_holder = reinterpret_cast<hdr_offset_holder*>
hdr_offset_holder *const hdr_off_holder = reinterpret_cast<hdr_offset_holder*>
(reinterpret_cast<char*>(block) - (m_num_subblocks-1)*m_real_block_alignment);
BOOST_ASSERT(hdr_off_holder->hdr_offset == size_type(reinterpret_cast<char*>(block) - reinterpret_cast<char*>(hdr_off_holder)));
BOOST_ASSERT(0 == ((std::size_t)hdr_off_holder & (m_real_block_alignment - 1)));
BOOST_ASSERT(0 == ((std::size_t)hdr_off_holder & (m_real_block_alignment - 1)));
BOOST_ASSERT(0 == (hdr_off_holder->hdr_offset & (m_real_block_alignment - 1)));
return hdr_off_holder;
}
//!Allocates a several blocks of nodes. Can throw
void priv_alloc_block(size_type n, AlignOnlyTrue)
hdr_offset_holder *priv_first_subblock_from_block(block_info_t *block, AlignOnlyTrue) const
{
size_type real_block_size = m_real_block_alignment - PayloadPerAllocation;
for(size_type i = 0; i != n; ++i){
//We allocate a new NodeBlock and put it the last
//element of the tree
char *mem_address = static_cast<char*>
(mp_segment_mngr_base->allocate_aligned(real_block_size, m_real_block_alignment));
if(!mem_address) throw std::bad_alloc();
++m_totally_free_blocks;
block_info_t *c_info = new(mem_address)block_info_t();
m_block_multiset.insert(m_block_multiset.end(), *c_info);
mem_address += HdrSize;
//We initialize all Nodes in Node Block to insert
//them in the free Node list
typename free_nodes_t::iterator prev_insert_pos = c_info->free_nodes.before_begin();
for(size_type i = 0; i < m_real_num_node; ++i){
prev_insert_pos = c_info->free_nodes.insert_after(prev_insert_pos, *(node_t*)mem_address);
mem_address += m_real_node_size;
return reinterpret_cast<hdr_offset_holder*>(block);
}
void priv_dispatch_block_chain_or_free
( multiallocation_chain &chain, block_info_t &c_info, size_type num_node
, char *mem_address, size_type total_elements, bool insert_block_if_free)
{
BOOST_ASSERT(chain.size() <= total_elements);
//First add all possible nodes to the chain
const size_type left = total_elements - chain.size();
const size_type max_chain = (num_node < left) ? num_node : left;
mem_address = static_cast<char *>(container_detail::to_raw_pointer
(chain.incorporate_after(chain.last(), void_pointer(mem_address), m_real_node_size, max_chain)));
//Now store remaining nodes in the free list
if(const size_type max_free = num_node - max_chain){
free_nodes_t & free_nodes = c_info.free_nodes;
free_nodes.incorporate_after(free_nodes.last(), void_pointer(mem_address), m_real_node_size, max_free);
if(insert_block_if_free){
m_block_container.push_front(c_info);
}
}
}
void priv_alloc_block(size_type n, AlignOnlyFalse)
//!Allocates a several blocks of nodes. Can throw
void priv_append_from_new_blocks(size_type min_elements, multiallocation_chain &chain, AlignOnlyTrue)
{
size_type real_block_size = m_real_block_alignment*m_num_subblocks - PayloadPerAllocation;
size_type elements_per_subblock = (m_real_block_alignment - HdrOffsetSize)/m_real_node_size;
size_type hdr_subblock_elements = (m_real_block_alignment - HdrSize - PayloadPerAllocation)/m_real_node_size;
BOOST_ASSERT(m_block_container.empty());
BOOST_ASSERT(min_elements > 0);
const size_type n = (min_elements - 1)/m_real_num_node + 1;
const size_type real_block_size = m_real_block_alignment - PayloadPerAllocation;
const size_type total_elements = chain.size() + min_elements;
for(size_type i = 0; i != n; ++i){
//We allocate a new NodeBlock and put it the last
//element of the tree
char *mem_address = static_cast<char*>
(mp_segment_mngr_base->allocate_aligned(real_block_size, m_real_block_alignment));
if(!mem_address){
//In case of error, free memory deallocating all nodes (the new ones allocated
//in this function plus previously stored nodes in chain).
this->deallocate_nodes(chain);
throw std::bad_alloc();
}
block_info_t &c_info = *new(mem_address)block_info_t();
mem_address += HdrSize;
if(i != (n-1)){
chain.incorporate_after(chain.last(), void_pointer(mem_address), m_real_node_size, m_real_num_node);
}
else{
this->priv_dispatch_block_chain_or_free(chain, c_info, m_real_num_node, mem_address, total_elements, true);
}
}
}
void priv_append_from_new_blocks(size_type min_elements, multiallocation_chain &chain, AlignOnlyFalse)
{
BOOST_ASSERT(m_block_container.empty());
BOOST_ASSERT(min_elements > 0);
const size_type n = (min_elements - 1)/m_real_num_node + 1;
const size_type real_block_size = m_real_block_alignment*m_num_subblocks - PayloadPerAllocation;
const size_type elements_per_subblock = (m_real_block_alignment - HdrOffsetSize)/m_real_node_size;
const size_type hdr_subblock_elements = (m_real_block_alignment - HdrSize - PayloadPerAllocation)/m_real_node_size;
const size_type total_elements = chain.size() + min_elements;
for(size_type i = 0; i != n; ++i){
//We allocate a new NodeBlock and put it the last
//element of the tree
char *mem_address = static_cast<char*>
(mp_segment_mngr_base->allocate_aligned(real_block_size, m_real_block_alignment));
if(!mem_address) throw std::bad_alloc();
++m_totally_free_blocks;
if(!mem_address){
//In case of error, free memory deallocating all nodes (the new ones allocated
//in this function plus previously stored nodes in chain).
this->deallocate_nodes(chain);
throw std::bad_alloc();
}
//First initialize header information on the last subblock
char *hdr_addr = mem_address + m_real_block_alignment*(m_num_subblocks-1);
block_info_t *c_info = new(hdr_addr)block_info_t();
block_info_t &c_info = *new(hdr_addr)block_info_t();
//Some structural checks
BOOST_ASSERT(static_cast<void*>(&static_cast<hdr_offset_holder*>(c_info)->hdr_offset) ==
static_cast<void*>(c_info));
typename free_nodes_t::iterator prev_insert_pos = c_info->free_nodes.before_begin();
for( size_type subblock = 0, maxsubblock = m_num_subblocks - 1
; subblock < maxsubblock
; ++subblock, mem_address += m_real_block_alignment){
//Initialize header offset mark
new(mem_address) hdr_offset_holder(size_type(hdr_addr - mem_address));
char *pNode = mem_address + HdrOffsetSize;
for(size_type i = 0; i < elements_per_subblock; ++i){
prev_insert_pos = c_info->free_nodes.insert_after(prev_insert_pos, *new (pNode) node_t);
pNode += m_real_node_size;
BOOST_ASSERT(static_cast<void*>(&static_cast<hdr_offset_holder&>(c_info).hdr_offset) ==
static_cast<void*>(&c_info)); (void)c_info;
if(i != (n-1)){
for( size_type subblock = 0, maxsubblock = m_num_subblocks - 1
; subblock < maxsubblock
; ++subblock, mem_address += m_real_block_alignment){
//Initialize header offset mark
new(mem_address) hdr_offset_holder(size_type(hdr_addr - mem_address));
chain.incorporate_after
(chain.last(), void_pointer(mem_address + HdrOffsetSize), m_real_node_size, elements_per_subblock);
}
chain.incorporate_after(chain.last(), void_pointer(hdr_addr + HdrSize), m_real_node_size, hdr_subblock_elements);
}
{
char *pNode = hdr_addr + HdrSize;
//We initialize all Nodes in Node Block to insert
//them in the free Node list
for(size_type i = 0; i < hdr_subblock_elements; ++i){
prev_insert_pos = c_info->free_nodes.insert_after(prev_insert_pos, *new (pNode) node_t);
pNode += m_real_node_size;
else{
for( size_type subblock = 0, maxsubblock = m_num_subblocks - 1
; subblock < maxsubblock
; ++subblock, mem_address += m_real_block_alignment){
//Initialize header offset mark
new(mem_address) hdr_offset_holder(size_type(hdr_addr - mem_address));
this->priv_dispatch_block_chain_or_free
(chain, c_info, elements_per_subblock, mem_address + HdrOffsetSize, total_elements, false);
}
this->priv_dispatch_block_chain_or_free
(chain, c_info, hdr_subblock_elements, hdr_addr + HdrSize, total_elements, true);
}
//Insert the block after the free node list is full
m_block_multiset.insert(m_block_multiset.end(), *c_info);
}
}
//!Allocates a block of nodes. Can throw std::bad_alloc
void priv_alloc_block(size_type n)
{ return priv_alloc_block(n, IsAlignOnly()); }
private:
typedef typename boost::intrusive::pointer_traits
<void_pointer>::template rebind_pointer<segment_manager_base_type>::type segment_mngr_base_ptr_t;
@@ -633,9 +859,9 @@ class private_adaptive_node_pool_impl
//This is the real number of nodes per block
//const
size_type m_real_num_node;
segment_mngr_base_ptr_t mp_segment_mngr_base; //Segment manager
block_multiset_t m_block_multiset; //Intrusive block list
size_type m_totally_free_blocks; //Free blocks
segment_mngr_base_ptr_t mp_segment_mngr_base; //Segment manager
block_container_t m_block_container; //Intrusive block list
size_type m_totally_free_blocks; //Free blocks
};
} //namespace container_detail {
@@ -20,160 +20,177 @@
#include <boost/container/allocator_traits.hpp>
#include <boost/container/detail/destroyers.hpp>
#include <boost/aligned_storage.hpp>
#include <boost/move/move.hpp>
#include <boost/move/utility.hpp>
#include <iterator> //std::iterator_traits
#include <boost/assert.hpp>
#include <boost/detail/no_exceptions_support.hpp>
namespace boost { namespace container { namespace container_detail {
//This class will be interface for operations dependent on FwdIt types used advanced_insert_aux_impl
template<class Iterator>
struct advanced_insert_aux_int
{
typedef typename std::iterator_traits<Iterator>::difference_type difference_type;
virtual void copy_remaining_to(Iterator p) = 0;
virtual void uninitialized_copy_remaining_to(Iterator p) = 0;
virtual void uninitialized_copy_some_and_update(Iterator pos, difference_type division_count, bool first) = 0;
virtual void copy_some_and_update(Iterator pos, difference_type division_count, bool first) = 0;
virtual ~advanced_insert_aux_int() {}
};
//This class template will adapt each FwIt types to advanced_insert_aux_int
template<class A, class FwdIt, class Iterator>
struct advanced_insert_aux_proxy
: public advanced_insert_aux_int<Iterator>
struct insert_range_proxy
{
typedef typename allocator_traits<A>::size_type size_type;
typedef typename allocator_traits<A>::value_type value_type;
typedef typename advanced_insert_aux_int<Iterator>::difference_type difference_type;
advanced_insert_aux_proxy(A& a, FwdIt first, FwdIt last)
: a_(a), first_(first), last_(last)
insert_range_proxy(A& a, FwdIt first)
: a_(a), first_(first)
{}
virtual ~advanced_insert_aux_proxy()
{}
virtual void copy_remaining_to(Iterator p)
{ ::boost::copy_or_move(this->first_, this->last_, p); }
virtual void uninitialized_copy_remaining_to(Iterator p)
{ ::boost::container::uninitialized_copy_or_move_alloc(this->a_, this->first_, this->last_, p); }
virtual void uninitialized_copy_some_and_update(Iterator pos, difference_type division_count, bool first_n)
void uninitialized_copy_n_and_update(Iterator pos, size_type n)
{
FwdIt mid = this->first_;
std::advance(mid, division_count);
if(first_n){
::boost::container::uninitialized_copy_or_move_alloc(this->a_, this->first_, mid, pos);
this->first_ = mid;
}
else{
::boost::container::uninitialized_copy_or_move_alloc(this->a_, mid, this->last_, pos);
this->last_ = mid;
}
this->first_ = ::boost::container::uninitialized_copy_or_move_alloc_n_source
(this->a_, this->first_, n, pos);
}
virtual void copy_some_and_update(Iterator pos, difference_type division_count, bool first_n)
void copy_n_and_update(Iterator pos, size_type n)
{
FwdIt mid = this->first_;
std::advance(mid, division_count);
if(first_n){
::boost::copy_or_move(this->first_, mid, pos);
this->first_ = mid;
}
else{
::boost::copy_or_move(mid, this->last_, pos);
this->last_ = mid;
}
this->first_ = ::boost::container::copy_or_move_n_source(this->first_, n, pos);
}
A &a_;
FwdIt first_, last_;
FwdIt first_;
};
//This class template will adapt default construction insertions to advanced_insert_aux_int
template<class A, class Iterator>
struct default_construct_aux_proxy
: public advanced_insert_aux_int<Iterator>
struct insert_n_copies_proxy
{
typedef typename allocator_traits<A>::size_type size_type;
typedef typename allocator_traits<A>::value_type value_type;
insert_n_copies_proxy(A& a, const value_type &v)
: a_(a), v_(v)
{}
void uninitialized_copy_n_and_update(Iterator p, size_type n)
{ std::uninitialized_fill_n(p, n, v_); }
void copy_n_and_update(Iterator p, size_type n)
{ std::fill_n(p, n, v_); }
A &a_;
const value_type &v_;
};
template<class A, class Iterator>
struct insert_default_constructed_n_proxy
{
typedef ::boost::container::allocator_traits<A> alloc_traits;
typedef typename allocator_traits<A>::size_type size_type;
typedef typename allocator_traits<A>::value_type value_type;
typedef typename advanced_insert_aux_int<Iterator>::difference_type difference_type;
default_construct_aux_proxy(A &a, size_type count)
: a_(a), count_(count)
explicit insert_default_constructed_n_proxy(A &a)
: a_(a)
{}
virtual ~default_construct_aux_proxy()
{}
virtual void copy_remaining_to(Iterator)
{ //This should never be called with any count
BOOST_ASSERT(this->count_ == 0);
}
virtual void uninitialized_copy_remaining_to(Iterator p)
{ this->priv_uninitialized_copy(p, this->count_); }
virtual void uninitialized_copy_some_and_update(Iterator pos, difference_type division_count, bool first_n)
void uninitialized_copy_n_and_update(Iterator p, size_type n)
{
size_type new_count;
if(first_n){
new_count = division_count;
}
else{
BOOST_ASSERT(difference_type(this->count_)>= division_count);
new_count = this->count_ - division_count;
}
this->priv_uninitialized_copy(pos, new_count);
}
virtual void copy_some_and_update(Iterator , difference_type division_count, bool first_n)
{
BOOST_ASSERT(this->count_ == 0);
size_type new_count;
if(first_n){
new_count = division_count;
}
else{
BOOST_ASSERT(difference_type(this->count_)>= division_count);
new_count = this->count_ - division_count;
}
//This function should never called with a count different to zero
BOOST_ASSERT(new_count == 0);
(void)new_count;
}
private:
void priv_uninitialized_copy(Iterator p, const size_type n)
{
BOOST_ASSERT(n <= this->count_);
Iterator orig_p = p;
size_type i = 0;
try{
for(; i < n; ++i, ++p){
size_type n_left = n;
BOOST_TRY{
for(; n_left--; ++p){
alloc_traits::construct(this->a_, container_detail::to_raw_pointer(&*p));
}
}
catch(...){
while(i--){
BOOST_CATCH(...){
for(; orig_p != p; ++orig_p){
alloc_traits::destroy(this->a_, container_detail::to_raw_pointer(&*orig_p++));
}
throw;
BOOST_RETHROW
}
this->count_ -= n;
BOOST_CATCH_END
}
void copy_n_and_update(Iterator, size_type)
{
BOOST_ASSERT(false);
}
private:
A &a_;
size_type count_;
};
template<class A, class Iterator>
struct insert_copy_proxy
{
typedef boost::container::allocator_traits<A> alloc_traits;
typedef typename alloc_traits::size_type size_type;
typedef typename alloc_traits::value_type value_type;
insert_copy_proxy(A& a, const value_type &v)
: a_(a), v_(v)
{}
void uninitialized_copy_n_and_update(Iterator p, size_type n)
{
BOOST_ASSERT(n == 1); (void)n;
alloc_traits::construct( this->a_
, container_detail::to_raw_pointer(&*p)
, v_
);
}
void copy_n_and_update(Iterator p, size_type n)
{
BOOST_ASSERT(n == 1); (void)n;
*p =v_;
}
A &a_;
const value_type &v_;
};
template<class A, class Iterator>
struct insert_move_proxy
{
typedef boost::container::allocator_traits<A> alloc_traits;
typedef typename alloc_traits::size_type size_type;
typedef typename alloc_traits::value_type value_type;
insert_move_proxy(A& a, value_type &v)
: a_(a), v_(v)
{}
void uninitialized_copy_n_and_update(Iterator p, size_type n)
{
BOOST_ASSERT(n == 1); (void)n;
alloc_traits::construct( this->a_
, container_detail::to_raw_pointer(&*p)
, ::boost::move(v_)
);
}
void copy_n_and_update(Iterator p, size_type n)
{
BOOST_ASSERT(n == 1); (void)n;
*p = ::boost::move(v_);
}
A &a_;
value_type &v_;
};
template<class It, class A>
insert_move_proxy<A, It> get_insert_value_proxy(A& a, BOOST_RV_REF(typename std::iterator_traits<It>::value_type) v)
{
return insert_move_proxy<A, It>(a, v);
}
template<class It, class A>
insert_copy_proxy<A, It> get_insert_value_proxy(A& a, const typename std::iterator_traits<It>::value_type &v)
{
return insert_copy_proxy<A, It>(a, v);
}
}}} //namespace boost { namespace container { namespace container_detail {
#ifdef BOOST_CONTAINER_PERFECT_FORWARDING
#include <boost/container/detail/variadic_templates_tools.hpp>
#include <boost/move/move.hpp>
#include <boost/move/utility.hpp>
#include <typeinfo>
//#include <iostream> //For debugging purposes
@@ -181,138 +198,74 @@ namespace boost {
namespace container {
namespace container_detail {
//This class template will adapt emplace construction insertions of movable types
//to advanced_insert_aux_int
template<class A, class Iterator, class ...Args>
struct advanced_insert_aux_non_movable_emplace
: public advanced_insert_aux_int<Iterator>
struct insert_non_movable_emplace_proxy
{
typedef boost::container::allocator_traits<A> alloc_traits;
typedef typename allocator_traits<A>::size_type size_type;
typedef typename allocator_traits<A>::value_type value_type;
typedef typename advanced_insert_aux_int<Iterator>::difference_type difference_type;
typedef typename build_number_seq<sizeof...(Args)>::type index_tuple_t;
typedef boost::container::allocator_traits<A> alloc_traits;
typedef typename alloc_traits::size_type size_type;
typedef typename alloc_traits::value_type value_type;
explicit advanced_insert_aux_non_movable_emplace(A &a, Args&&... args)
: a_(a)
, args_(args...)
, used_(false)
typedef typename build_number_seq<sizeof...(Args)>::type index_tuple_t;
explicit insert_non_movable_emplace_proxy(A &a, Args&&... args)
: a_(a), args_(args...)
{}
~advanced_insert_aux_non_movable_emplace()
{}
virtual void copy_remaining_to(Iterator)
//This code can't be called since value_type is not movable or copyable
{ BOOST_ASSERT(false); }
virtual void uninitialized_copy_remaining_to(Iterator p)
{ this->priv_uninitialized_copy_remaining_to(index_tuple_t(), p); }
virtual void uninitialized_copy_some_and_update(Iterator p, difference_type division_count, bool first_n)
{ this->priv_uninitialized_copy_some_and_update(index_tuple_t(), p, division_count, first_n); }
virtual void copy_some_and_update(Iterator, difference_type, bool )
//This code can't be called since value_type is not movable or copyable
{ BOOST_ASSERT(false); }
void uninitialized_copy_n_and_update(Iterator p, size_type n)
{ this->priv_uninitialized_copy_some_and_update(index_tuple_t(), p, n); }
private:
template<int ...IdxPack>
void priv_uninitialized_copy_some_and_update(const index_tuple<IdxPack...>&, Iterator p, difference_type division_count, bool first_n)
void priv_uninitialized_copy_some_and_update(const index_tuple<IdxPack...>&, Iterator p, size_type n)
{
BOOST_ASSERT(division_count <=1);
if((first_n && division_count == 1) || (!first_n && division_count == 0)){
if(!this->used_){
alloc_traits::construct( this->a_
, container_detail::to_raw_pointer(&*p)
, ::boost::forward<Args>(get<IdxPack>(this->args_))...
);
this->used_ = true;
}
}
}
template<int ...IdxPack>
void priv_uninitialized_copy_remaining_to(const index_tuple<IdxPack...>&, Iterator p)
{
if(!this->used_){
alloc_traits::construct( this->a_
, container_detail::to_raw_pointer(&*p)
, ::boost::forward<Args>(get<IdxPack>(this->args_))...
);
this->used_ = true;
}
BOOST_ASSERT(n == 1); (void)n;
alloc_traits::construct( this->a_
, container_detail::to_raw_pointer(&*p)
, ::boost::forward<Args>(get<IdxPack>(this->args_))...
);
}
protected:
A &a_;
tuple<Args&...> args_;
bool used_;
};
//This class template will adapt emplace construction insertions of movable types
//to advanced_insert_aux_int
template<class A, class Iterator, class ...Args>
struct advanced_insert_aux_emplace
: public advanced_insert_aux_non_movable_emplace<A, Iterator, Args...>
struct insert_emplace_proxy
: public insert_non_movable_emplace_proxy<A, Iterator, Args...>
{
typedef advanced_insert_aux_non_movable_emplace<A, Iterator, Args...> base_t;
typedef boost::container::allocator_traits<A> alloc_traits;
typedef typename base_t::value_type value_type;
typedef typename base_t::difference_type difference_type;
typedef typename base_t::index_tuple_t index_tuple_t;
typedef insert_non_movable_emplace_proxy<A, Iterator, Args...> base_t;
typedef boost::container::allocator_traits<A> alloc_traits;
typedef typename base_t::value_type value_type;
typedef typename base_t::size_type size_type;
typedef typename base_t::index_tuple_t index_tuple_t;
explicit advanced_insert_aux_emplace(A &a, Args&&... args)
explicit insert_emplace_proxy(A &a, Args&&... args)
: base_t(a, ::boost::forward<Args>(args)...)
{}
~advanced_insert_aux_emplace()
{}
//Override only needed functions
virtual void copy_remaining_to(Iterator p)
{ this->priv_copy_remaining_to(index_tuple_t(), p); }
virtual void copy_some_and_update(Iterator p, difference_type division_count, bool first_n)
{ this->priv_copy_some_and_update(index_tuple_t(), p, division_count, first_n); }
void copy_n_and_update(Iterator p, size_type n)
{ this->priv_copy_some_and_update(index_tuple_t(), p, n); }
private:
template<int ...IdxPack>
void priv_copy_remaining_to(const index_tuple<IdxPack...>&, Iterator p)
{
if(!this->used_){
aligned_storage<sizeof(value_type), alignment_of<value_type>::value> v;
value_type *vp = static_cast<value_type *>(static_cast<void *>(&v));
alloc_traits::construct(this->a_, vp,
::boost::forward<Args>(get<IdxPack>(this->args_))...);
scoped_destructor<A> d(this->a_, vp);
*p = ::boost::move(*vp);
d.release();
this->used_ = true;
}
}
template<int ...IdxPack>
void priv_copy_some_and_update(const index_tuple<IdxPack...>&, Iterator p, difference_type division_count, bool first_n)
void priv_copy_some_and_update(const index_tuple<IdxPack...>&, Iterator p, size_type n)
{
BOOST_ASSERT(division_count <=1);
if((first_n && division_count == 1) || (!first_n && division_count == 0)){
if(!this->used_){
aligned_storage<sizeof(value_type), alignment_of<value_type>::value> v;
value_type *vp = static_cast<value_type *>(static_cast<void *>(&v));
alloc_traits::construct(this->a_, vp,
::boost::forward<Args>(get<IdxPack>(this->args_))...);
try {
*p = ::boost::move(*vp);
} catch (...) {
alloc_traits::destroy(this->a_, vp);
throw;
}
alloc_traits::destroy(this->a_, vp);
this->used_ = true;
}
BOOST_ASSERT(n ==1); (void)n;
aligned_storage<sizeof(value_type), alignment_of<value_type>::value> v;
value_type *vp = static_cast<value_type *>(static_cast<void *>(&v));
alloc_traits::construct(this->a_, vp,
::boost::forward<Args>(get<IdxPack>(this->args_))...);
BOOST_TRY{
*p = ::boost::move(*vp);
}
BOOST_CATCH(...){
alloc_traits::destroy(this->a_, vp);
BOOST_RETHROW
}
BOOST_CATCH_END
alloc_traits::destroy(this->a_, vp);
}
};
@@ -327,115 +280,73 @@ namespace boost {
namespace container {
namespace container_detail {
#define BOOST_PP_LOCAL_MACRO(n) \
template<class A, class Iterator BOOST_PP_ENUM_TRAILING_PARAMS(n, class P) > \
struct BOOST_PP_CAT(BOOST_PP_CAT(advanced_insert_aux_non_movable_emplace, n), arg) \
: public advanced_insert_aux_int<Iterator> \
#define BOOST_PP_LOCAL_MACRO(N) \
template<class A, class Iterator BOOST_PP_ENUM_TRAILING_PARAMS(N, class P) > \
struct BOOST_PP_CAT(insert_non_movable_emplace_proxy_arg, N) \
{ \
typedef boost::container::allocator_traits<A> alloc_traits; \
typedef typename allocator_traits<A>::size_type size_type; \
typedef typename allocator_traits<A>::value_type value_type; \
typedef typename advanced_insert_aux_int<Iterator>::difference_type \
difference_type; \
typedef typename alloc_traits::size_type size_type; \
typedef typename alloc_traits::value_type value_type; \
\
BOOST_PP_CAT(BOOST_PP_CAT(advanced_insert_aux_non_movable_emplace, n), arg) \
( A &a BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _) ) \
BOOST_PP_CAT(insert_non_movable_emplace_proxy_arg, N) \
( A &a BOOST_PP_ENUM_TRAILING(N, BOOST_CONTAINER_PP_PARAM_LIST, _) ) \
: a_(a) \
, used_(false) \
BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_INIT, _) \
{} \
BOOST_PP_ENUM_TRAILING(N, BOOST_CONTAINER_PP_PARAM_INIT, _) \
{} \
\
virtual void copy_remaining_to(Iterator) \
{ BOOST_ASSERT(false); } \
\
virtual void uninitialized_copy_remaining_to(Iterator p) \
void uninitialized_copy_n_and_update(Iterator p, size_type n) \
{ \
if(!this->used_){ \
alloc_traits::construct \
( this->a_ \
, container_detail::to_raw_pointer(&*p) \
BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_MEMBER_FORWARD, _) \
); \
this->used_ = true; \
} \
BOOST_ASSERT(n == 1); (void)n; \
alloc_traits::construct \
( this->a_ \
, container_detail::to_raw_pointer(&*p) \
BOOST_PP_ENUM_TRAILING(N, BOOST_CONTAINER_PP_MEMBER_FORWARD, _) \
); \
} \
\
virtual void uninitialized_copy_some_and_update \
(Iterator p, difference_type division_count, bool first_n) \
{ \
BOOST_ASSERT(division_count <=1); \
if((first_n && division_count == 1) || (!first_n && division_count == 0)){ \
if(!this->used_){ \
alloc_traits::construct \
( this->a_ \
, container_detail::to_raw_pointer(&*p) \
BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_MEMBER_FORWARD, _) \
); \
this->used_ = true; \
} \
} \
} \
\
virtual void copy_some_and_update(Iterator, difference_type, bool) \
void copy_n_and_update(Iterator, size_type) \
{ BOOST_ASSERT(false); } \
\
protected: \
A &a_; \
bool used_; \
BOOST_PP_REPEAT(n, BOOST_CONTAINER_PP_PARAM_DEFINE, _) \
BOOST_PP_REPEAT(N, BOOST_CONTAINER_PP_PARAM_DEFINE, _) \
}; \
\
template<class A, class Iterator BOOST_PP_ENUM_TRAILING_PARAMS(n, class P) > \
struct BOOST_PP_CAT(BOOST_PP_CAT(advanced_insert_aux_emplace, n), arg) \
: BOOST_PP_CAT(BOOST_PP_CAT( \
advanced_insert_aux_non_movable_emplace, n), arg) \
< A, Iterator BOOST_PP_ENUM_TRAILING_PARAMS(n, P) > \
template<class A, class Iterator BOOST_PP_ENUM_TRAILING_PARAMS(N, class P) > \
struct BOOST_PP_CAT(insert_emplace_proxy_arg, N) \
: BOOST_PP_CAT(insert_non_movable_emplace_proxy_arg, N) \
< A, Iterator BOOST_PP_ENUM_TRAILING_PARAMS(N, P) > \
{ \
typedef BOOST_PP_CAT(BOOST_PP_CAT( \
advanced_insert_aux_non_movable_emplace, n), arg) \
<A, Iterator BOOST_PP_ENUM_TRAILING_PARAMS(n, P) > base_t; \
typedef BOOST_PP_CAT(insert_non_movable_emplace_proxy_arg, N) \
<A, Iterator BOOST_PP_ENUM_TRAILING_PARAMS(N, P) > base_t; \
typedef typename base_t::value_type value_type; \
typedef typename base_t::difference_type difference_type; \
typedef typename base_t::size_type size_type; \
typedef boost::container::allocator_traits<A> alloc_traits; \
\
BOOST_PP_CAT(BOOST_PP_CAT(advanced_insert_aux_emplace, n), arg) \
( A &a BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _) ) \
: base_t(a BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ) \
{} \
BOOST_PP_CAT(insert_emplace_proxy_arg, N) \
( A &a BOOST_PP_ENUM_TRAILING(N, BOOST_CONTAINER_PP_PARAM_LIST, _) ) \
: base_t(a BOOST_PP_ENUM_TRAILING(N, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ) \
{} \
\
virtual void copy_remaining_to(Iterator p) \
void copy_n_and_update(Iterator p, size_type n) \
{ \
if(!this->used_){ \
aligned_storage<sizeof(value_type), alignment_of<value_type>::value> v; \
value_type *vp = static_cast<value_type *>(static_cast<void *>(&v)); \
alloc_traits::construct(this->a_, vp \
BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_MEMBER_FORWARD, _)); \
scoped_destructor<A> d(this->a_, vp); \
BOOST_ASSERT(n == 1); (void)n; \
aligned_storage<sizeof(value_type), alignment_of<value_type>::value> v; \
value_type *vp = static_cast<value_type *>(static_cast<void *>(&v)); \
alloc_traits::construct(this->a_, vp \
BOOST_PP_ENUM_TRAILING(N, BOOST_CONTAINER_PP_MEMBER_FORWARD, _)); \
BOOST_TRY{ \
*p = ::boost::move(*vp); \
d.release(); \
this->used_ = true; \
} \
} \
\
virtual void copy_some_and_update \
(Iterator p, difference_type division_count, bool first_n) \
{ \
BOOST_ASSERT(division_count <=1); \
if((first_n && division_count == 1) || (!first_n && division_count == 0)){ \
if(!this->used_){ \
aligned_storage<sizeof(value_type), alignment_of<value_type>::value> v; \
value_type *vp = static_cast<value_type *>(static_cast<void *>(&v)); \
alloc_traits::construct(this->a_, vp \
BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_MEMBER_FORWARD, _)); \
scoped_destructor<A> d(this->a_, vp); \
*p = ::boost::move(*vp); \
d.release(); \
this->used_ = true; \
} \
BOOST_CATCH(...){ \
alloc_traits::destroy(this->a_, vp); \
BOOST_RETHROW \
} \
BOOST_CATCH_END \
alloc_traits::destroy(this->a_, vp); \
} \
}; \
//!
#define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS)
#include BOOST_PP_LOCAL_ITERATE()
@@ -0,0 +1,163 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2012-2012. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/container for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#ifndef BOOST_CONTAINER_DETAIL_ALLOCATOR_VERSION_TRAITS_HPP
#define BOOST_CONTAINER_DETAIL_ALLOCATOR_VERSION_TRAITS_HPP
#if (defined _MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif
#include <boost/container/detail/config_begin.hpp>
#include <boost/container/detail/workaround.hpp>
#include <boost/container/allocator_traits.hpp> //allocator_traits
#include <boost/container/detail/multiallocation_chain.hpp> //multiallocation_chain
#include <boost/container/detail/version_type.hpp> //version_type
#include <boost/container/detail/allocation_type.hpp> //allocation_type
#include <boost/container/detail/mpl.hpp> //integral_constant
#include <boost/intrusive/pointer_traits.hpp> //pointer_traits
#include <utility> //pair
#include <stdexcept> //runtime_error
#include <boost/detail/no_exceptions_support.hpp> //BOOST_TRY
namespace boost {
namespace container {
namespace container_detail {
template<class Allocator, unsigned Version = boost::container::container_detail::version<Allocator>::value>
struct allocator_version_traits
{
typedef ::boost::container::container_detail::integral_constant
<unsigned, Version> alloc_version;
typedef typename Allocator::multiallocation_chain multiallocation_chain;
typedef typename boost::container::allocator_traits<Allocator>::pointer pointer;
typedef typename boost::container::allocator_traits<Allocator>::size_type size_type;
//Node allocation interface
static pointer allocate_one(Allocator &a)
{ return a.allocate_one(); }
static void deallocate_one(Allocator &a, const pointer &p)
{ a.deallocate_one(p); }
static void allocate_individual(Allocator &a, size_type n, multiallocation_chain &m)
{ return a.allocate_individual(n, m); }
static void deallocate_individual(Allocator &a, multiallocation_chain &holder)
{ a.deallocate_individual(holder); }
static std::pair<pointer, bool>
allocation_command(Allocator &a, allocation_type command,
size_type limit_size, size_type preferred_size,
size_type &received_size, const pointer &reuse)
{
return a.allocation_command
(command, limit_size, preferred_size, received_size, reuse);
}
};
template<class Allocator>
struct allocator_version_traits<Allocator, 1>
{
typedef ::boost::container::container_detail::integral_constant
<unsigned, 1> alloc_version;
typedef typename boost::container::allocator_traits<Allocator>::pointer pointer;
typedef typename boost::container::allocator_traits<Allocator>::size_type size_type;
typedef typename boost::container::allocator_traits<Allocator>::value_type value_type;
typedef typename boost::intrusive::pointer_traits<pointer>::
template rebind_pointer<void>::type void_ptr;
typedef container_detail::basic_multiallocation_chain
<void_ptr> multialloc_cached_counted;
typedef boost::container::container_detail::
transform_multiallocation_chain
< multialloc_cached_counted, value_type> multiallocation_chain;
//Node allocation interface
static pointer allocate_one(Allocator &a)
{ return a.allocate(1); }
static void deallocate_one(Allocator &a, const pointer &p)
{ a.deallocate(p, 1); }
static void deallocate_individual(Allocator &a, multiallocation_chain &holder)
{
while(!holder.empty()){
a.deallocate(holder.pop_front(), 1);
}
}
struct allocate_individual_rollback
{
allocate_individual_rollback(Allocator &a, multiallocation_chain &chain)
: mr_a(a), mp_chain(&chain)
{}
~allocate_individual_rollback()
{
if(mp_chain)
allocator_version_traits::deallocate_individual(mr_a, *mp_chain);
}
void release()
{
mp_chain = 0;
}
Allocator &mr_a;
multiallocation_chain * mp_chain;
};
static void allocate_individual(Allocator &a, size_type n, multiallocation_chain &m)
{
allocate_individual_rollback rollback(a, m);
while(n--){
m.push_front(a.allocate(1));
}
rollback.release();
}
static std::pair<pointer, bool>
allocation_command(Allocator &a, allocation_type command,
size_type, size_type preferred_size,
size_type &received_size, const pointer &)
{
std::pair<pointer, bool> ret(pointer(), false);
if(!(command & allocate_new)){
if(!(command & nothrow_allocation)){
throw std::runtime_error("version 1 allocator without allocate_new flag");
}
}
else{
received_size = preferred_size;
BOOST_TRY{
ret.first = a.allocate(received_size);
}
BOOST_CATCH(...){
if(!(command & nothrow_allocation)){
BOOST_RETHROW
}
}
BOOST_CATCH_END
}
return ret;
}
};
} //namespace container_detail {
} //namespace container {
} //namespace boost {
#include <boost/container/detail/config_end.hpp>
#endif // ! defined(BOOST_CONTAINER_DETAIL_ALLOCATOR_VERSION_TRAITS_HPP)
@@ -188,13 +188,17 @@ struct scoped_destructor_n
void increment_size_backwards(size_type inc)
{ m_n += inc; m_p -= inc; }
void shrink_forward(size_type inc)
{ m_n -= inc; m_p += inc; }
~scoped_destructor_n()
{
if(!m_p) return;
value_type *raw_ptr = container_detail::to_raw_pointer(m_p);
for(size_type i = 0; i < m_n; ++i, ++raw_ptr)
while(m_n--){
AllocTraits::destroy(m_a, raw_ptr);
}
}
private:
@@ -323,7 +327,7 @@ class allocator_destroyer_and_chain_builder
void operator()(const typename A::pointer &p)
{
allocator_traits<A>::destroy(a_, container_detail::to_raw_pointer(p));
c_.push_front(p);
c_.push_back(p);
}
};
@@ -348,8 +352,7 @@ class allocator_multialloc_chain_node_deallocator
~allocator_multialloc_chain_node_deallocator()
{
if(!c_.empty())
a_.deallocate_individual(boost::move(c_));
a_.deallocate_individual(c_);
}
};
+36 -36
View File
@@ -25,7 +25,7 @@
#include <utility>
#include <boost/type_traits/has_trivial_destructor.hpp>
#include <boost/move/move.hpp>
#include <boost/move/utility.hpp>
#include <boost/container/detail/utilities.hpp>
#include <boost/container/detail/pair.hpp>
@@ -48,7 +48,7 @@ class flat_tree_value_compare
typedef Value first_argument_type;
typedef Value second_argument_type;
typedef bool return_type;
public:
public:
flat_tree_value_compare()
: Compare()
{}
@@ -65,7 +65,7 @@ class flat_tree_value_compare
const Compare &get_comp() const
{ return *this; }
Compare &get_comp()
{ return *this; }
};
@@ -238,7 +238,7 @@ class flat_tree
flat_tree& operator=(BOOST_RV_REF(flat_tree) mx)
{ m_data = boost::move(mx.m_data); return *this; }
public:
public:
// accessors:
Compare key_comp() const
{ return this->m_data.get_comp(); }
@@ -441,17 +441,17 @@ class flat_tree
//Prereserve all memory so that iterators are not invalidated
this->reserve(this->size()+len);
const const_iterator beg(this->cbegin());
const_iterator pos(beg);
const const_iterator b(this->cbegin());
const_iterator pos(b);
//Loop in burst sizes
while(len){
const size_type burst = len < BurstSize ? len : BurstSize;
const const_iterator cend_(this->cend());
const const_iterator ce(this->cend());
len -= burst;
for(size_type i = 0; i != burst; ++i){
//Get the insertion position for each key
pos = const_cast<const flat_tree&>(*this).priv_upper_bound(pos, cend_, KeyOfValue()(*first));
positions[i] = static_cast<size_type>(pos - beg);
pos = const_cast<const flat_tree&>(*this).priv_upper_bound(pos, ce, KeyOfValue()(*first));
positions[i] = static_cast<size_type>(pos - b);
++first;
}
//Insert all in a single step in the precalculated positions
@@ -489,22 +489,22 @@ class flat_tree
//Prereserve all memory so that iterators are not invalidated
this->reserve(this->size()+len);
const const_iterator beg(this->cbegin());
const_iterator pos(beg);
const const_iterator b(this->cbegin());
const_iterator pos(b);
const value_compare &value_comp = this->m_data;
skips[0u] = 0u;
//Loop in burst sizes
while(len){
const size_type burst = len < BurstSize ? len : BurstSize;
size_type unique_burst = 0u;
const const_iterator cend_(this->cend());
const const_iterator ce(this->cend());
while(unique_burst < burst && len > 0){
//Get the insertion position for each key
const value_type & val = *first++;
--len;
pos = const_cast<const flat_tree&>(*this).priv_lower_bound(pos, cend_, KeyOfValue()(val));
pos = const_cast<const flat_tree&>(*this).priv_lower_bound(pos, ce, KeyOfValue()(val));
//Check if already present
if(pos != cend_ && !value_comp(val, *pos)){
if(pos != ce && !value_comp(val, *pos)){
if(unique_burst > 0){
++skips[unique_burst-1];
}
@@ -512,7 +512,7 @@ class flat_tree
}
//If not present, calculate position
positions[unique_burst] = static_cast<size_type>(pos - beg);
positions[unique_burst] = static_cast<size_type>(pos - b);
skips[unique_burst++] = 0u;
}
if(unique_burst){
@@ -692,22 +692,22 @@ class flat_tree
// set operations:
iterator find(const key_type& k)
{
const Compare &key_comp_ = this->m_data.get_comp();
const Compare &key_cmp = this->m_data.get_comp();
iterator i = this->lower_bound(k);
if (i != this->end() && key_comp_(k, KeyOfValue()(*i))){
i = this->end();
if (i != this->end() && key_cmp(k, KeyOfValue()(*i))){
i = this->end();
}
return i;
}
const_iterator find(const key_type& k) const
{
const Compare &key_comp_ = this->m_data.get_comp();
const Compare &key_cmp = this->m_data.get_comp();
const_iterator i = this->lower_bound(k);
if (i != this->end() && key_comp_(k, KeyOfValue()(*i))){
i = this->end();
if (i != this->end() && key_cmp(k, KeyOfValue()(*i))){
i = this->end();
}
return i;
}
@@ -737,11 +737,11 @@ class flat_tree
std::pair<const_iterator, const_iterator> equal_range(const key_type& k) const
{ return this->priv_equal_range(this->begin(), this->end(), k); }
size_type capacity() const
size_type capacity() const
{ return this->m_data.m_vect.capacity(); }
void reserve(size_type count_)
{ this->m_data.m_vect.reserve(count_); }
void reserve(size_type cnt)
{ this->m_data.m_vect.reserve(cnt); }
private:
struct insert_commit_data
@@ -780,13 +780,13 @@ class flat_tree
}
std::pair<iterator,bool> priv_insert_unique_prepare
(const_iterator beg, const_iterator end_, const value_type& val, insert_commit_data &commit_data)
(const_iterator b, const_iterator e, const value_type& val, insert_commit_data &commit_data)
{
const value_compare &value_comp = this->m_data;
commit_data.position = this->priv_lower_bound(beg, end_, KeyOfValue()(val));
commit_data.position = this->priv_lower_bound(b, e, KeyOfValue()(val));
return std::pair<iterator,bool>
( *reinterpret_cast<iterator*>(&commit_data.position)
, commit_data.position == end_ || value_comp(val, *commit_data.position));
, commit_data.position == e || value_comp(val, *commit_data.position));
}
std::pair<iterator,bool> priv_insert_unique_prepare
@@ -854,7 +854,7 @@ class flat_tree
RanIt priv_lower_bound(RanIt first, RanIt last,
const key_type & key) const
{
const Compare &key_comp_ = this->m_data.get_comp();
const Compare &key_cmp = this->m_data.get_comp();
KeyOfValue key_extract;
difference_type len = last - first, half;
RanIt middle;
@@ -864,7 +864,7 @@ class flat_tree
middle = first;
middle += half;
if (key_comp_(key_extract(*middle), key)) {
if (key_cmp(key_extract(*middle), key)) {
++middle;
first = middle;
len = len - half - 1;
@@ -879,7 +879,7 @@ class flat_tree
RanIt priv_upper_bound(RanIt first, RanIt last,
const key_type & key) const
{
const Compare &key_comp_ = this->m_data.get_comp();
const Compare &key_cmp = this->m_data.get_comp();
KeyOfValue key_extract;
difference_type len = last - first, half;
RanIt middle;
@@ -889,12 +889,12 @@ class flat_tree
middle = first;
middle += half;
if (key_comp_(key, key_extract(*middle))) {
if (key_cmp(key, key_extract(*middle))) {
len = half;
}
else{
first = ++middle;
len = len - half - 1;
len = len - half - 1;
}
}
return first;
@@ -904,7 +904,7 @@ class flat_tree
std::pair<RanIt, RanIt>
priv_equal_range(RanIt first, RanIt last, const key_type& key) const
{
const Compare &key_comp_ = this->m_data.get_comp();
const Compare &key_cmp = this->m_data.get_comp();
KeyOfValue key_extract;
difference_type len = last - first, half;
RanIt middle, left, right;
@@ -914,12 +914,12 @@ class flat_tree
middle = first;
middle += half;
if (key_comp_(key_extract(*middle), key)){
if (key_cmp(key_extract(*middle), key)){
first = middle;
++first;
len = len - half - 1;
}
else if (key_comp_(key, key_extract(*middle))){
else if (key_cmp(key, key_extract(*middle))){
len = half;
}
else {
@@ -1033,7 +1033,7 @@ template <class K, class V, class KOV,
class C, class A>
struct has_trivial_destructor_after_move<boost::container::container_detail::flat_tree<K, V, KOV, C, A> >
{
static const bool value = has_trivial_destructor<A>::value && has_trivial_destructor<C>::value;
static const bool value = has_trivial_destructor_after_move<A>::value && has_trivial_destructor_after_move<C>::value;
};
*/
} //namespace boost {
+1 -1
View File
@@ -20,7 +20,7 @@
#include "config_begin.hpp"
#include <boost/container/detail/workaround.hpp>
#include <boost/move/move.hpp>
#include <boost/move/utility.hpp>
#include <boost/container/allocator_traits.hpp>
#include <boost/container/detail/type_traits.hpp>
@@ -19,7 +19,7 @@
#include <boost/intrusive/slist.hpp>
#include <boost/intrusive/pointer_traits.hpp>
#include <boost/type_traits/make_unsigned.hpp>
#include <boost/move/move.hpp>
#include <boost/move/utility.hpp>
namespace boost {
namespace container {
@@ -50,41 +50,38 @@ class basic_multiallocation_chain
typedef typename boost::intrusive::
pointer_traits<node_ptr> node_ptr_traits;
static node & build_node(const VoidPointer &p)
{
return *::new (static_cast<node*>(static_cast<void*>(container_detail::to_raw_pointer(p)))) node;
}
static node & to_node(const VoidPointer &p)
{ return *static_cast<node*>(static_cast<void*>(container_detail::to_raw_pointer(p))); }
static VoidPointer destroy_node(node &n)
{
VoidPointer retptr = node_ptr_traits::pointer_to(n);
n.~node();
return retptr;
}
static VoidPointer from_node(node &n)
{ return node_ptr_traits::pointer_to(n); }
static node_ptr to_node_ptr(VoidPointer p)
static node_ptr to_node_ptr(const VoidPointer &p)
{ return node_ptr_traits::static_cast_from(p); }
BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_multiallocation_chain)
public:
typedef VoidPointer void_pointer;
typedef typename slist_impl_t::iterator iterator;
typedef typename slist_impl_t::size_type size_type;
typedef VoidPointer void_pointer;
typedef typename slist_impl_t::iterator iterator;
typedef typename slist_impl_t::size_type size_type;
basic_multiallocation_chain()
: slist_impl_()
{}
basic_multiallocation_chain(const void_pointer &b, const void_pointer &before_e, size_type n)
: slist_impl_(to_node_ptr(b), to_node_ptr(before_e), n)
{}
basic_multiallocation_chain(BOOST_RV_REF(basic_multiallocation_chain) other)
: slist_impl_()
{ slist_impl_.swap(other.slist_impl_); }
: slist_impl_(::boost::move(other.slist_impl_))
{}
basic_multiallocation_chain& operator=(BOOST_RV_REF(basic_multiallocation_chain) other)
{
basic_multiallocation_chain tmp(boost::move(other));
this->swap(tmp);
slist_impl_ = ::boost::move(other.slist_impl_);
return *this;
}
@@ -110,43 +107,54 @@ class basic_multiallocation_chain
{ slist_impl_.clear(); }
iterator insert_after(iterator it, void_pointer m)
{ return slist_impl_.insert_after(it, build_node(m)); }
{ return slist_impl_.insert_after(it, to_node(m)); }
void push_front(void_pointer m)
{ return slist_impl_.push_front(build_node(m)); }
void push_front(const void_pointer &m)
{ return slist_impl_.push_front(to_node(m)); }
void push_back(void_pointer m)
{ return slist_impl_.push_back(build_node(m)); }
void push_back(const void_pointer &m)
{ return slist_impl_.push_back(to_node(m)); }
void_pointer pop_front()
{
node & n = slist_impl_.front();
void_pointer ret = destroy_node(n);
void_pointer ret = from_node(n);
slist_impl_.pop_front();
return ret;
}
void splice_after(iterator after_this, basic_multiallocation_chain &x, iterator before_begin_, iterator before_end)
{ slist_impl_.splice_after(after_this, x.slist_impl_, before_begin_, before_end); }
void splice_after(iterator after_this, basic_multiallocation_chain &x, iterator before_begin_, iterator before_end, size_type n)
{ slist_impl_.splice_after(after_this, x.slist_impl_, before_begin_, before_end, n); }
void splice_after(iterator after_this, basic_multiallocation_chain &x, iterator before_b, iterator before_e, size_type n)
{ slist_impl_.splice_after(after_this, x.slist_impl_, before_b, before_e, n); }
void splice_after(iterator after_this, basic_multiallocation_chain &x)
{ slist_impl_.splice_after(after_this, x.slist_impl_); }
void incorporate_after(iterator after_this, void_pointer begin_ , iterator before_end)
void erase_after(iterator before_b, iterator e, size_type n)
{ slist_impl_.erase_after(before_b, e, n); }
void_pointer incorporate_after(iterator after_this, const void_pointer &b, size_type unit_bytes, size_type num_units)
{
slist_impl_.incorporate_after(after_this, to_node_ptr(begin_), to_node_ptr(before_end));
typedef typename boost::intrusive::pointer_traits<char_ptr> char_pointer_traits;
char_ptr elem = char_pointer_traits::static_cast_from(b);
if(num_units){
char_ptr prev_elem = elem;
elem += unit_bytes;
for(size_type i = 0; i != num_units-1; ++i, elem += unit_bytes){
::new (container_detail::to_raw_pointer(prev_elem)) void_pointer(elem);
prev_elem = elem;
}
slist_impl_.incorporate_after(after_this, to_node_ptr(b), to_node_ptr(prev_elem), num_units);
}
return elem;
}
void incorporate_after(iterator after_this, void_pointer begin_, void_pointer before_end, size_type n)
{ slist_impl_.incorporate_after(after_this, to_node_ptr(begin_), to_node_ptr(before_end), n); }
void incorporate_after(iterator after_this, void_pointer b, void_pointer before_e, size_type n)
{ slist_impl_.incorporate_after(after_this, to_node_ptr(b), to_node_ptr(before_e), n); }
void swap(basic_multiallocation_chain &x)
{ slist_impl_.swap(x.slist_impl_); }
static iterator iterator_to(void_pointer p)
static iterator iterator_to(const void_pointer &p)
{ return slist_impl_t::s_iterator_to(to_node(p)); }
std::pair<void_pointer, void_pointer> extract_data()
@@ -170,11 +178,13 @@ struct cast_functor
template<class MultiallocationChain, class T>
class transform_multiallocation_chain
: public MultiallocationChain
{
private:
BOOST_MOVABLE_BUT_NOT_COPYABLE(transform_multiallocation_chain)
//transform_multiallocation_chain(const transform_multiallocation_chain &);
//transform_multiallocation_chain & operator=(const transform_multiallocation_chain &);
MultiallocationChain holder_;
typedef typename MultiallocationChain::void_pointer void_pointer;
typedef typename boost::intrusive::pointer_traits
<void_pointer> void_pointer_traits;
@@ -193,39 +203,41 @@ class transform_multiallocation_chain
typedef typename MultiallocationChain::size_type size_type;
transform_multiallocation_chain()
: holder_()
: MultiallocationChain()
{}
transform_multiallocation_chain(BOOST_RV_REF(transform_multiallocation_chain) other)
: holder_()
{ this->swap(other); }
: MultiallocationChain(::boost::move(static_cast<MultiallocationChain&>(other)))
{}
transform_multiallocation_chain(BOOST_RV_REF(MultiallocationChain) other)
: holder_(boost::move(other))
: MultiallocationChain(::boost::move(static_cast<MultiallocationChain&>(other)))
{}
transform_multiallocation_chain& operator=(BOOST_RV_REF(transform_multiallocation_chain) other)
{
transform_multiallocation_chain tmp(boost::move(other));
this->swap(tmp);
return *this;
return static_cast<MultiallocationChain&>
(this->MultiallocationChain::operator=(::boost::move(static_cast<MultiallocationChain&>(other))));
}
void push_front(pointer mem)
/*
void push_front(const pointer &mem)
{ holder_.push_front(mem); }
void push_back(const pointer &mem)
{ return holder_.push_back(mem); }
void swap(transform_multiallocation_chain &other_chain)
{ holder_.swap(other_chain.holder_); }
void splice_after(iterator after_this, transform_multiallocation_chain &x, iterator before_begin_, iterator before_end, size_type n)
{ holder_.splice_after(after_this.base(), x.holder_, before_begin_.base(), before_end.base(), n); }
void incorporate_after(iterator after_this, pointer begin_, pointer before_end, size_type n)
{ holder_.incorporate_after(after_this.base(), begin_, before_end, n); }
void splice_after(iterator after_this, transform_multiallocation_chain &x, iterator before_b, iterator before_e, size_type n)
{ holder_.splice_after(after_this.base(), x.holder_, before_b.base(), before_e.base(), n); }
void incorporate_after(iterator after_this, pointer b, pointer before_e, size_type n)
{ holder_.incorporate_after(after_this.base(), b, before_e, n); }
*/
pointer pop_front()
{ return cast(holder_.pop_front()); }
{ return cast(this->MultiallocationChain::pop_front()); }
/*
bool empty() const
{ return holder_.empty(); }
@@ -246,23 +258,21 @@ class transform_multiallocation_chain
void clear()
{ holder_.clear(); }
*/
iterator insert_after(iterator it, pointer m)
{ return iterator(holder_.insert_after(it.base(), m)); }
{ return iterator(this->MultiallocationChain::insert_after(it.base(), m)); }
static iterator iterator_to(pointer p)
static iterator iterator_to(const pointer &p)
{ return iterator(MultiallocationChain::iterator_to(p)); }
std::pair<pointer, pointer> extract_data()
{
std::pair<void_pointer, void_pointer> data(holder_.extract_data());
std::pair<void_pointer, void_pointer> data(this->MultiallocationChain::extract_data());
return std::pair<pointer, pointer>(cast(data.first), cast(data.second));
}
MultiallocationChain extract_multiallocation_chain()
{
return MultiallocationChain(boost::move(holder_));
}
/*
MultiallocationChain &extract_multiallocation_chain()
{ return holder_; }*/
};
}}}
@@ -21,7 +21,7 @@
#include <utility>
#include <functional>
#include <boost/move/move.hpp>
#include <boost/move/utility.hpp>
#include <boost/intrusive/options.hpp>
#include <boost/container/detail/version_type.hpp>
@@ -30,6 +30,8 @@
#include <boost/container/allocator_traits.hpp>
#include <boost/container/detail/mpl.hpp>
#include <boost/container/detail/destroyers.hpp>
#include <boost/container/detail/allocator_version_traits.hpp>
#include <boost/detail/no_exceptions_support.hpp>
#ifndef BOOST_CONTAINER_PERFECT_FORWARDING
#include <boost/container/detail/preprocessor.hpp>
@@ -89,6 +91,7 @@ struct node_alloc_holder
typedef typename ICont::const_iterator icont_citerator;
typedef allocator_destroyer<NodeAlloc> Destroyer;
typedef allocator_traits<NodeAlloc> NodeAllocTraits;
typedef allocator_version_traits<NodeAlloc> AllocVersionTraits;
private:
BOOST_COPYABLE_AND_MOVABLE(node_alloc_holder)
@@ -151,22 +154,10 @@ struct node_alloc_holder
{ return allocator_traits_type::max_size(this->node_alloc()); }
NodePtr allocate_one()
{ return this->allocate_one(alloc_version()); }
NodePtr allocate_one(allocator_v1)
{ return this->node_alloc().allocate(1); }
NodePtr allocate_one(allocator_v2)
{ return this->node_alloc().allocate_one(); }
{ return AllocVersionTraits::allocate_one(this->node_alloc()); }
void deallocate_one(const NodePtr &p)
{ return this->deallocate_one(p, alloc_version()); }
void deallocate_one(const NodePtr &p, allocator_v1)
{ this->node_alloc().deallocate(p, 1); }
void deallocate_one(const NodePtr &p, allocator_v2)
{ this->node_alloc().deallocate_one(p); }
{ AllocVersionTraits::deallocate_one(this->node_alloc(), p); }
#ifdef BOOST_CONTAINER_PERFECT_FORWARDING
@@ -235,40 +226,51 @@ struct node_alloc_holder
}
template<class FwdIterator, class Inserter>
FwdIterator allocate_many_and_construct
void allocate_many_and_construct
(FwdIterator beg, difference_type n, Inserter inserter)
{
if(n){
typedef typename NodeAlloc::multiallocation_chain multiallocation_chain;
/*
NodePtr p = this->allocate_one();
Deallocator node_deallocator(p, this->node_alloc());
::boost::container::construct_in_place(this->node_alloc(), container_detail::addressof(p->m_data), it);
node_deallocator.release();
//This does not throw
typedef typename Node::hook_type hook_type;
::new(static_cast<hook_type*>(container_detail::to_raw_pointer(p))) hook_type;
return (p);
*/
typedef typename NodeAlloc::multiallocation_chain multiallocation_chain;
//Try to allocate memory in a single block
multiallocation_chain mem(this->node_alloc().allocate_individual(n));
int constructed = 0;
Node *p = 0;
BOOST_TRY{
for(difference_type i = 0; i < n; ++i, ++beg, --constructed){
p = container_detail::to_raw_pointer(mem.pop_front());
//This can throw
constructed = 0;
boost::container::construct_in_place(this->node_alloc(), container_detail::addressof(p->m_data), beg);
++constructed;
//This does not throw
typedef typename Node::hook_type hook_type;
::new(static_cast<hook_type*>(container_detail::to_raw_pointer(p))) hook_type;
//This can throw in some containers (predicate might throw)
inserter(*p);
}
//Try to allocate memory in a single block
typedef typename multiallocation_chain::iterator multialloc_iterator;
multiallocation_chain mem;
this->node_alloc().allocate_individual(n, mem);
multialloc_iterator itbeg(mem.begin()), itlast(mem.last());
mem.clear();
Node *p = 0;
NodeAlloc &nalloc = this->node_alloc();
BOOST_TRY{
while(n--){
p = container_detail::to_raw_pointer(&*itbeg);
++itbeg;
//This can throw
Deallocator node_deallocator(p, nalloc);
boost::container::construct_in_place(nalloc, container_detail::addressof(p->m_data), beg);
++beg;
node_deallocator.release();
//This does not throw
typedef typename Node::hook_type hook_type;
::new(static_cast<hook_type*>(p)) hook_type;
//This can throw in some containers (predicate might throw)
inserter(*p);
}
BOOST_CATCH(...){
if(constructed){
allocator_traits<NodeAlloc>::destroy(this->node_alloc(), container_detail::to_raw_pointer(p));
}
this->node_alloc().deallocate_individual(boost::move(mem));
BOOST_RETHROW
}
BOOST_CATCH_END
}
return beg;
BOOST_CATCH(...){
mem.incorporate_after(mem.last(), &*itbeg, &*itlast, n);
this->node_alloc().deallocate_individual(mem);
BOOST_RETHROW
}
BOOST_CATCH_END
}
void clear(allocator_v1)
@@ -281,7 +283,7 @@ struct node_alloc_holder
this->icont().clear_and_dispose(builder);
//BOOST_STATIC_ASSERT((::boost::has_move_emulation_enabled<typename NodeAlloc::multiallocation_chain>::value == true));
if(!chain.empty())
this->node_alloc().deallocate_individual(boost::move(chain));
this->node_alloc().deallocate_individual(chain);
}
icont_iterator erase_range(const icont_iterator &first, const icont_iterator &last, allocator_v1)
@@ -289,8 +291,13 @@ struct node_alloc_holder
icont_iterator erase_range(const icont_iterator &first, const icont_iterator &last, allocator_v2)
{
allocator_multialloc_chain_node_deallocator<NodeAlloc> chain_holder(this->node_alloc());
return this->icont().erase_and_dispose(first, last, chain_holder.get_chain_builder());
typedef typename NodeAlloc::multiallocation_chain multiallocation_chain;
NodeAlloc & nalloc = this->node_alloc();
multiallocation_chain chain;
allocator_destroyer_and_chain_builder<NodeAlloc> chain_builder(nalloc, chain);
icont_iterator ret_it = this->icont().erase_and_dispose(first, last, chain_builder);
nalloc.deallocate_individual(chain);
return ret_it;
}
template<class Key, class Comparator>
@@ -26,10 +26,12 @@
#include <boost/container/detail/math_functions.hpp>
#include <boost/container/detail/mpl.hpp>
#include <boost/container/detail/pool_common.hpp>
#include <boost/detail/no_exceptions_support.hpp>
#include <boost/assert.hpp>
#include <cstddef>
#include <functional> //std::unary_function
namespace boost {
namespace container {
namespace container_detail {
@@ -85,19 +87,19 @@ class private_node_pool_impl
{ return container_detail::to_raw_pointer(mp_segment_mngr_base); }
void *allocate_node()
{ return priv_alloc_node(); }
{ return this->priv_alloc_node(); }
//!Deallocates an array pointed by ptr. Never throws
void deallocate_node(void *ptr)
{ priv_dealloc_node(ptr); }
{ this->priv_dealloc_node(ptr); }
//!Allocates a singly linked list of n nodes ending in null pointer.
multiallocation_chain allocate_nodes(const size_type n)
void allocate_nodes(const size_type n, multiallocation_chain &chain)
{
//Preallocate all needed blocks to fulfill the request
size_type cur_nodes = m_freelist.size();
if(cur_nodes < n){
priv_alloc_block(((n - cur_nodes) - 1)/m_nodes_per_block + 1);
this->priv_alloc_block(((n - cur_nodes) - 1)/m_nodes_per_block + 1);
}
//We just iterate the needed nodes to get the last we'll erase
@@ -118,20 +120,18 @@ class private_node_pool_impl
//Now take the last erased node and just splice it in the end
//of the intrusive list that will be traversed by the multialloc iterator.
multiallocation_chain chain;
chain.incorporate_after(chain.before_begin(), &*first_node, &*last_node, n);
m_allocated += n;
return boost::move(chain);
}
void deallocate_nodes(multiallocation_chain chain)
void deallocate_nodes(multiallocation_chain &chain)
{
typedef typename multiallocation_chain::iterator iterator;
iterator it(chain.begin()), itend(chain.end());
while(it != itend){
void *pElem = &*it;
++it;
priv_dealloc_node(pElem);
this->priv_dealloc_node(pElem);
}
}
@@ -275,7 +275,7 @@ class private_node_pool_impl
{
//If there are no free nodes we allocate a new block
if (m_freelist.empty())
priv_alloc_block();
this->priv_alloc_block(1);
//We take the first free node
node_t *n = (node_t*)&m_freelist.front();
m_freelist.pop_front();
@@ -295,14 +295,13 @@ class private_node_pool_impl
}
//!Allocates several blocks of nodes. Can throw
void priv_alloc_block(size_type num_blocks = 1)
void priv_alloc_block(size_type num_blocks)
{
if(!num_blocks)
return;
BOOST_ASSERT(num_blocks > 0);
size_type blocksize =
get_rounded_size(m_real_node_size*m_nodes_per_block, (size_type)alignment_of<node_t>::value);
try{
BOOST_TRY{
for(size_type i = 0; i != num_blocks; ++i){
//We allocate a new NodeBlock and put it as first
//element in the free Node list
@@ -313,15 +312,16 @@ class private_node_pool_impl
//We initialize all Nodes in Node Block to insert
//them in the free Node list
for(size_type i = 0; i < m_nodes_per_block; ++i, pNode += m_real_node_size){
for(size_type j = 0; j < m_nodes_per_block; ++j, pNode += m_real_node_size){
m_freelist.push_front(*new (pNode) node_t);
}
}
}
catch(...){
BOOST_CATCH(...){
//to-do: if possible, an efficient way to deallocate allocated blocks
throw;
BOOST_RETHROW
}
BOOST_CATCH_END
}
//!Deprecated, use deallocate_free_blocks
+1 -1
View File
@@ -27,7 +27,7 @@
#include <utility> //std::pair
#include <boost/move/move.hpp>
#include <boost/move/utility.hpp>
#include <boost/type_traits/is_class.hpp>
#ifndef BOOST_CONTAINER_PERFECT_FORWARDING
@@ -34,7 +34,7 @@ struct node_slist
typedef slist_hook_t node_t;
typedef typename bi::make_slist
<node_t, bi::linear<true>, bi::base_hook<slist_hook_t> >::type node_slist_t;
<node_t, bi::linear<true>, bi::cache_last<true>, bi::base_hook<slist_hook_t> >::type node_slist_t;
};
template<class T>
@@ -17,6 +17,7 @@
#include <boost/container/detail/config_begin.hpp>
#include <boost/container/detail/workaround.hpp>
#include <boost/move/utility.hpp>
#ifdef BOOST_CONTAINER_PERFECT_FORWARDING
//#error "This file is not needed when perfect forwarding is available"
@@ -38,6 +39,7 @@
#include <boost/preprocessor/arithmetic/sub.hpp>
#include <boost/preprocessor/arithmetic/add.hpp>
#include <boost/preprocessor/iteration/iterate.hpp>
#include <boost/move/utility.hpp>
#define BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS 10
@@ -97,7 +99,7 @@ const BOOST_PP_CAT(Q, n) & BOOST_PP_CAT(q, n) \
template<class T>
struct ref_holder<T &>
{
ref_holder(T &t)
explicit ref_holder(T &t)
: t_(t)
{}
T &t_;
@@ -107,7 +109,7 @@ const BOOST_PP_CAT(Q, n) & BOOST_PP_CAT(q, n) \
template<class T>
struct ref_holder<const T>
{
ref_holder(const T &t)
explicit ref_holder(const T &t)
: t_(t)
{}
const T &t_;
@@ -117,7 +119,7 @@ const BOOST_PP_CAT(Q, n) & BOOST_PP_CAT(q, n) \
template<class T>
struct ref_holder<const T &&>
{
ref_holder(const T &t)
explicit ref_holder(const T &t)
: t_(t)
{}
const T &t_;
@@ -127,7 +129,7 @@ const BOOST_PP_CAT(Q, n) & BOOST_PP_CAT(q, n) \
template<class T>
struct ref_holder
{
ref_holder(T &&t)
explicit ref_holder(T &&t)
: t_(t)
{}
T &t_;
@@ -137,10 +139,10 @@ const BOOST_PP_CAT(Q, n) & BOOST_PP_CAT(q, n) \
template<class T>
struct ref_holder<T &&>
{
ref_holder(T &&t)
: t(t)
explicit ref_holder(T &&t)
: t_(t)
{}
T &t;
T &t_;
T && get() { return ::boost::move(t_); }
};
+11 -8
View File
@@ -15,7 +15,7 @@
#include <boost/container/detail/workaround.hpp>
#include <boost/container/container_fwd.hpp>
#include <boost/move/move.hpp>
#include <boost/move/utility.hpp>
#include <boost/intrusive/pointer_traits.hpp>
#include <boost/type_traits/has_trivial_destructor.hpp>
#include <boost/detail/no_exceptions_support.hpp>
@@ -28,6 +28,7 @@
#include <boost/container/detail/pair.hpp>
#include <boost/container/detail/type_traits.hpp>
#include <boost/container/allocator_traits.hpp>
#include <boost/detail/no_exceptions_support.hpp>
#ifndef BOOST_CONTAINER_PERFECT_FORWARDING
#include <boost/container/detail/preprocessor.hpp>
#endif
@@ -247,19 +248,20 @@ class rbtree
{
if(NodePtr p = m_icont.unlink_leftmost_without_rebalance()){
//First recycle a node (this can't throw)
try{
BOOST_TRY{
//This can throw
p->do_assign(other.m_data);
return p;
}
catch(...){
BOOST_CATCH(...){
//If there is an exception destroy the whole source
m_holder.destroy_node(p);
while((p = m_icont.unlink_leftmost_without_rebalance())){
m_holder.destroy_node(p);
}
throw;
BOOST_RETHROW
}
BOOST_CATCH_END
}
else{
return m_holder.create_node(other.m_data);
@@ -284,19 +286,20 @@ class rbtree
{
if(NodePtr p = m_icont.unlink_leftmost_without_rebalance()){
//First recycle a node (this can't throw)
try{
BOOST_TRY{
//This can throw
p->do_move_assign(const_cast<Node &>(other).m_data);
return p;
}
catch(...){
BOOST_CATCH(...){
//If there is an exception destroy the whole source
m_holder.destroy_node(p);
while((p = m_icont.unlink_leftmost_without_rebalance())){
m_holder.destroy_node(p);
}
throw;
BOOST_RETHROW
}
BOOST_CATCH_END
}
else{
return m_holder.create_node(other.m_data);
@@ -1121,7 +1124,7 @@ class C, class A>
struct has_trivial_destructor_after_move
<boost::container::container_detail::rbtree<K, V, KOV, C, A> >
{
static const bool value = has_trivial_destructor<A>::value && has_trivial_destructor<C>::value;
static const bool value = has_trivial_destructor_after_move<A>::value && has_trivial_destructor_after_move<C>::value;
};
*/
} //namespace boost {
@@ -21,7 +21,7 @@
#include "config_begin.hpp"
#include <boost/move/move.hpp>
#include <boost/move/utility.hpp>
namespace boost {
namespace container {
+349 -20
View File
@@ -18,11 +18,14 @@
#include <boost/type_traits/is_enum.hpp>
#include <boost/type_traits/is_member_pointer.hpp>
#include <boost/type_traits/is_class.hpp>
#include <boost/move/move.hpp>
#include <boost/move/utility.hpp>
#include <boost/move/iterator.hpp>
#include <boost/container/detail/mpl.hpp>
#include <boost/container/detail/type_traits.hpp>
#include <boost/container/allocator_traits.hpp>
#include <boost/detail/no_exceptions_support.hpp>
#include <algorithm>
#include <iterator>
namespace boost {
namespace container {
@@ -123,16 +126,6 @@ struct ct_rounded_size
enum { value = ((OrigSize-1)/RoundTo+1)*RoundTo };
};
template<class T>
struct move_const_ref_type
: if_c
// < ::boost::is_fundamental<T>::value || ::boost::is_pointer<T>::value || ::boost::is_member_pointer<T>::value || ::boost::is_enum<T>::value
< !::boost::is_class<T>::value
,const T &
,BOOST_CATCH_CONST_RLVALUE(T)
>
{};
} //namespace container_detail {
//////////////////////////////////////////////////////////////////////////////
@@ -154,13 +147,95 @@ template
typename F> // F models ForwardIterator
F uninitialized_move_alloc(A &a, I f, I l, F r)
{
while (f != l) {
allocator_traits<A>::construct(a, container_detail::to_raw_pointer(&*r), boost::move(*f));
++f; ++r;
F back = r;
BOOST_TRY{
while (f != l) {
allocator_traits<A>::construct(a, container_detail::to_raw_pointer(&*r), boost::move(*f));
++f; ++r;
}
}
BOOST_CATCH(...){
for (; back != r; ++back){
allocator_traits<A>::destroy(a, container_detail::to_raw_pointer(&*back));
}
BOOST_RETHROW;
}
BOOST_CATCH_END
return r;
}
//////////////////////////////////////////////////////////////////////////////
//
// uninitialized_move_alloc_n
//
//////////////////////////////////////////////////////////////////////////////
//! <b>Effects</b>:
//! \code
//! for (; n--; ++result, ++first)
//! allocator_traits::construct(a, &*result, boost::move(*first));
//! \endcode
//!
//! <b>Returns</b>: result
template
<typename A,
typename I, // I models InputIterator
typename F> // F models ForwardIterator
F uninitialized_move_alloc_n(A &a, I f, typename std::iterator_traits<I>::difference_type n, F r)
{
F back = r;
BOOST_TRY{
while (n--) {
allocator_traits<A>::construct(a, container_detail::to_raw_pointer(&*r), boost::move(*f));
++f; ++r;
}
}
BOOST_CATCH(...){
for (; back != r; ++back){
allocator_traits<A>::destroy(a, container_detail::to_raw_pointer(&*back));
}
BOOST_RETHROW;
}
BOOST_CATCH_END
return r;
}
//////////////////////////////////////////////////////////////////////////////
//
// uninitialized_move_alloc_n_source
//
//////////////////////////////////////////////////////////////////////////////
//! <b>Effects</b>:
//! \code
//! for (; n--; ++result, ++first)
//! allocator_traits::construct(a, &*result, boost::move(*first));
//! \endcode
//!
//! <b>Returns</b>: first (after incremented)
template
<typename A,
typename I, // I models InputIterator
typename F> // F models ForwardIterator
I uninitialized_move_alloc_n_source(A &a, I f, typename std::iterator_traits<I>::difference_type n, F r)
{
F back = r;
BOOST_TRY{
while (n--) {
allocator_traits<A>::construct(a, container_detail::to_raw_pointer(&*r), boost::move(*f));
++f; ++r;
}
}
BOOST_CATCH(...){
for (; back != r; ++back){
allocator_traits<A>::destroy(a, container_detail::to_raw_pointer(&*back));
}
BOOST_RETHROW;
}
BOOST_CATCH_END
return f;
}
//////////////////////////////////////////////////////////////////////////////
//
// uninitialized_copy_alloc
@@ -180,13 +255,95 @@ template
typename F> // F models ForwardIterator
F uninitialized_copy_alloc(A &a, I f, I l, F r)
{
while (f != l) {
allocator_traits<A>::construct(a, container_detail::to_raw_pointer(&*r), *f);
++f; ++r;
F back = r;
BOOST_TRY{
while (f != l) {
allocator_traits<A>::construct(a, container_detail::to_raw_pointer(&*r), *f);
++f; ++r;
}
}
BOOST_CATCH(...){
for (; back != r; ++back){
allocator_traits<A>::destroy(a, container_detail::to_raw_pointer(&*back));
}
BOOST_RETHROW;
}
BOOST_CATCH_END
return r;
}
//////////////////////////////////////////////////////////////////////////////
//
// uninitialized_copy_alloc_n
//
//////////////////////////////////////////////////////////////////////////////
//! <b>Effects</b>:
//! \code
//! for (; n--; ++result, ++first)
//! allocator_traits::construct(a, &*result, *first);
//! \endcode
//!
//! <b>Returns</b>: result
template
<typename A,
typename I, // I models InputIterator
typename F> // F models ForwardIterator
F uninitialized_copy_alloc_n(A &a, I f, typename std::iterator_traits<I>::difference_type n, F r)
{
F back = r;
BOOST_TRY{
while (n--) {
allocator_traits<A>::construct(a, container_detail::to_raw_pointer(&*r), *f);
++f; ++r;
}
}
BOOST_CATCH(...){
for (; back != r; ++back){
allocator_traits<A>::destroy(a, container_detail::to_raw_pointer(&*back));
}
BOOST_RETHROW;
}
BOOST_CATCH_END
return r;
}
//////////////////////////////////////////////////////////////////////////////
//
// uninitialized_copy_alloc_n_source
//
//////////////////////////////////////////////////////////////////////////////
//! <b>Effects</b>:
//! \code
//! for (; n--; ++result, ++first)
//! allocator_traits::construct(a, &*result, *first);
//! \endcode
//!
//! <b>Returns</b>: first (after incremented)
template
<typename A,
typename I, // I models InputIterator
typename F> // F models ForwardIterator
I uninitialized_copy_alloc_n_source(A &a, I f, typename std::iterator_traits<I>::difference_type n, F r)
{
F back = r;
BOOST_TRY{
while (n--) {
allocator_traits<A>::construct(a, container_detail::to_raw_pointer(&*r), *f);
++f; ++r;
}
}
BOOST_CATCH(...){
for (; back != r; ++back){
allocator_traits<A>::destroy(a, container_detail::to_raw_pointer(&*back));
}
BOOST_RETHROW;
}
BOOST_CATCH_END
return f;
}
//////////////////////////////////////////////////////////////////////////////
//
// uninitialized_copy_alloc
@@ -206,10 +363,20 @@ template
typename T>
void uninitialized_fill_alloc(A &a, F f, F l, const T &t)
{
while (f != l) {
allocator_traits<A>::construct(a, container_detail::to_raw_pointer(&*f), t);
++f;
F back = f;
BOOST_TRY{
while (f != l) {
allocator_traits<A>::construct(a, container_detail::to_raw_pointer(&*f), t);
++f;
}
}
BOOST_CATCH(...){
for (; back != l; ++back){
allocator_traits<A>::destroy(a, container_detail::to_raw_pointer(&*back));
}
BOOST_RETHROW;
}
BOOST_CATCH_END
}
//////////////////////////////////////////////////////////////////////////////
@@ -242,6 +409,168 @@ F uninitialized_copy_or_move_alloc
return ::boost::container::uninitialized_copy_alloc(a, f, l, r);
}
//////////////////////////////////////////////////////////////////////////////
//
// uninitialized_copy_or_move_alloc_n
//
//////////////////////////////////////////////////////////////////////////////
template
<typename A
,typename I // I models InputIterator
,typename F> // F models ForwardIterator
F uninitialized_copy_or_move_alloc_n
(A &a, I f, typename std::iterator_traits<I>::difference_type n, F r
,typename boost::container::container_detail::enable_if
< boost::move_detail::is_move_iterator<I> >::type* = 0)
{
return ::boost::container::uninitialized_move_alloc_n(a, f, n, r);
}
template
<typename A
,typename I // I models InputIterator
,typename F> // F models ForwardIterator
F uninitialized_copy_or_move_alloc_n
(A &a, I f, typename std::iterator_traits<I>::difference_type n, F r
,typename boost::container::container_detail::disable_if
< boost::move_detail::is_move_iterator<I> >::type* = 0)
{
return ::boost::container::uninitialized_copy_alloc_n(a, f, n, r);
}
//////////////////////////////////////////////////////////////////////////////
//
// uninitialized_copy_or_move_alloc_n_source
//
//////////////////////////////////////////////////////////////////////////////
template
<typename A
,typename I // I models InputIterator
,typename F> // F models ForwardIterator
I uninitialized_copy_or_move_alloc_n_source
(A &a, I f, typename std::iterator_traits<I>::difference_type n, F r
,typename boost::container::container_detail::enable_if
< boost::move_detail::is_move_iterator<I> >::type* = 0)
{
return ::boost::container::uninitialized_move_alloc_n_source(a, f, n, r);
}
template
<typename A
,typename I // I models InputIterator
,typename F> // F models ForwardIterator
I uninitialized_copy_or_move_alloc_n_source
(A &a, I f, typename std::iterator_traits<I>::difference_type n, F r
,typename boost::container::container_detail::disable_if
< boost::move_detail::is_move_iterator<I> >::type* = 0)
{
return ::boost::container::uninitialized_copy_alloc_n_source(a, f, n, r);
}
//////////////////////////////////////////////////////////////////////////////
//
// copy_or_move
//
//////////////////////////////////////////////////////////////////////////////
template
<typename I, // I models InputIterator
typename F> // F models ForwardIterator
inline F copy_or_move(I f, I l, F r
,typename boost::container::container_detail::enable_if
< boost::move_detail::is_move_iterator<I> >::type* = 0)
{
while (f != l) {
*r = ::boost::move(*f);
++f; ++r;
}
return r;
}
template
<typename I, // I models InputIterator
typename F> // F models ForwardIterator
inline F copy_or_move(I f, I l, F r
,typename boost::container::container_detail::disable_if
< boost::move_detail::is_move_iterator<I> >::type* = 0)
{
while (f != l) {
*r = *f;
++f; ++r;
}
return r;
}
//////////////////////////////////////////////////////////////////////////////
//
// copy_or_move_n
//
//////////////////////////////////////////////////////////////////////////////
template
<typename I, // I models InputIterator
typename F> // F models ForwardIterator
inline F copy_or_move_n(I f, typename std::iterator_traits<I>::difference_type n, F r
,typename boost::container::container_detail::enable_if
< boost::move_detail::is_move_iterator<I> >::type* = 0)
{
while (n--) {
*r = ::boost::move(*f);
++f; ++r;
}
return r;
}
template
<typename I, // I models InputIterator
typename F> // F models ForwardIterator
inline F copy_or_move_n(I f, typename std::iterator_traits<I>::difference_type n, F r
,typename boost::container::container_detail::disable_if
< boost::move_detail::is_move_iterator<I> >::type* = 0)
{
while (n--) {
*r = *f;
++f; ++r;
}
return r;
}
//////////////////////////////////////////////////////////////////////////////
//
// copy_or_move_n_source
//
//////////////////////////////////////////////////////////////////////////////
template
<typename I, // I models InputIterator
typename F> // F models ForwardIterator
inline I copy_or_move_n_source(I f, typename std::iterator_traits<I>::difference_type n, F r
,typename boost::container::container_detail::enable_if
< boost::move_detail::is_move_iterator<I> >::type* = 0)
{
while (n--) {
*r = ::boost::move(*f);
++f; ++r;
}
return f;
}
template
<typename I, // I models InputIterator
typename F> // F models ForwardIterator
inline I copy_or_move_n_source(I f, typename std::iterator_traits<I>::difference_type n, F r
,typename boost::container::container_detail::disable_if
< boost::move_detail::is_move_iterator<I> >::type* = 0)
{
while (n--) {
*r = *f;
++f; ++r;
}
return f;
}
} //namespace container {
} //namespace boost {
@@ -18,8 +18,12 @@
#define BOOST_CONTAINER_PERFECT_FORWARDING
#endif
#if defined(BOOST_NO_CXX11_NOEXCEPT)
#define BOOST_CONTAINER_NOEXCEPT
#if defined(BOOST_NO_NOEXCEPT)
#if defined(BOOST_MSVC)
#define BOOST_CONTAINER_NOEXCEPT throw()
#else
#define BOOST_CONTAINER_NOEXCEPT
#endif
#define BOOST_CONTAINER_NOEXCEPT_IF(x)
#else
#define BOOST_CONTAINER_NOEXCEPT noexcept