#ifndef BOOST_CONTAINER_NODE_ALLOCATOR_HPP
#define BOOST_CONTAINER_NODE_ALLOCATOR_HPP
#ifndef BOOST_CONFIG_HPP
# include <boost/config.hpp>
#endif
#if defined(BOOST_HAS_PRAGMA_ONCE)
# pragma once
#endif
#include <boost/container/detail/config_begin.hpp>
#include <boost/container/detail/workaround.hpp>
#include <boost/container/container_fwd.hpp>
#include <boost/container/throw_exception.hpp>
#include <boost/container/detail/node_pool.hpp>
#include <boost/container/detail/mpl.hpp>
#include <boost/container/detail/multiallocation_chain.hpp>
#include <boost/move/detail/iterator_to_raw_pointer.hpp>
#include <boost/container/detail/dlmalloc.hpp>
#include <boost/container/detail/singleton.hpp>
#include <boost/assert.hpp>
#include <cstddef>
namespace boost {
namespace container {
#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
template
< class T
, std::size_t NodesPerBlock = NodeAlloc_nodes_per_block>
#else
template
< class T
, std::size_t NodesPerBlock
, std::size_t Version>
#endif
class node_allocator
{
#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
public:
typedef unsigned int allocation_type;
typedef node_allocator<T, NodesPerBlock, Version> self_t;
BOOST_STATIC_CONSTEXPR std::size_t nodes_per_block = NodesPerBlock;
BOOST_CONTAINER_STATIC_ASSERT((Version <=2));
#endif
public:
typedef T value_type;
typedef T * pointer;
typedef const T * const_pointer;
typedef typename ::boost::container::
dtl::unvoid_ref<T>::type reference;
typedef typename ::boost::container::
dtl::unvoid_ref<const T>::type const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef boost::container::dtl::
version_type<self_t, (unsigned int) Version> version;
#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
typedef boost::container::dtl::
basic_multiallocation_chain<void*> multiallocation_chain_void;
typedef boost::container::dtl::
transform_multiallocation_chain
<multiallocation_chain_void, T> multiallocation_chain;
#endif
template<class T2>
struct rebind
{
typedef node_allocator< T2, NodesPerBlock
#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
, Version
#endif
> other;
};
#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
private:
template<class T2, std::size_t N2>
node_allocator& operator=
(const node_allocator<T2, N2>&);
#endif
public:
node_allocator() BOOST_NOEXCEPT_OR_NOTHROW
{}
node_allocator(const node_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
{}
template<class T2>
node_allocator
(const node_allocator<T2, NodesPerBlock
#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
, Version
#endif
> &) BOOST_NOEXCEPT_OR_NOTHROW
{}
~node_allocator() BOOST_NOEXCEPT_OR_NOTHROW
{}
size_type max_size() const
{ return size_type(-1)/sizeof(T); }
pointer allocate(size_type count, const void * = 0)
{
if(BOOST_UNLIKELY(count > this->max_size()))
boost::container::throw_bad_alloc();
if(Version == 1 && count == 1){
typedef dtl::shared_node_pool
<sizeof(T), NodesPerBlock> shared_pool_t;
typedef dtl::singleton_default<shared_pool_t> singleton_t;
return pointer(static_cast<T*>(singleton_t::instance().allocate_node()));
}
else{
void *ret = dlmalloc_malloc(count*sizeof(T));
if(BOOST_UNLIKELY(!ret))
boost::container::throw_bad_alloc();
return static_cast<pointer>(ret);
}
}
void deallocate(const pointer &ptr, size_type count) BOOST_NOEXCEPT_OR_NOTHROW
{
(void)count;
if(Version == 1 && count == 1){
typedef dtl::shared_node_pool
<sizeof(T), NodesPerBlock> shared_pool_t;
typedef dtl::singleton_default<shared_pool_t> singleton_t;
singleton_t::instance().deallocate_node(ptr);
}
else{
dlmalloc_free(ptr);
}
}
static void deallocate_free_blocks() BOOST_NOEXCEPT_OR_NOTHROW
{
typedef dtl::shared_node_pool
<sizeof(T), NodesPerBlock> shared_pool_t;
typedef dtl::singleton_default<shared_pool_t> singleton_t;
singleton_t::instance().deallocate_free_blocks();
}
pointer allocation_command
(allocation_type command, size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse)
{
BOOST_CONTAINER_STATIC_ASSERT(( Version > 1 ));
pointer ret = this->priv_allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse);
if(BOOST_UNLIKELY(!ret && !(command & BOOST_CONTAINER_NOTHROW_ALLOCATION)))
boost::container::throw_bad_alloc();
return ret;
}
size_type size(pointer p) const BOOST_NOEXCEPT_OR_NOTHROW
{
BOOST_CONTAINER_STATIC_ASSERT(( Version > 1 ));
return dlmalloc_size(p);
}
pointer allocate_one()
{
BOOST_CONTAINER_STATIC_ASSERT(( Version > 1 ));
typedef dtl::shared_node_pool
<sizeof(T), NodesPerBlock> shared_pool_t;
typedef dtl::singleton_default<shared_pool_t> singleton_t;
return (pointer)singleton_t::instance().allocate_node();
}
void allocate_individual(std::size_t num_elements, multiallocation_chain &chain)
{
BOOST_CONTAINER_STATIC_ASSERT(( Version > 1 ));
typedef dtl::shared_node_pool
<sizeof(T), NodesPerBlock> shared_pool_t;
typedef dtl::singleton_default<shared_pool_t> singleton_t;
typename shared_pool_t::multiallocation_chain ch;
singleton_t::instance().allocate_nodes(num_elements, ch);
chain.incorporate_after(chain.before_begin()
, (T*)boost::movelib::iterator_to_raw_pointer(ch.begin())
, (T*)boost::movelib::iterator_to_raw_pointer(ch.last())
, ch.size());
}
void deallocate_one(pointer p) BOOST_NOEXCEPT_OR_NOTHROW
{
BOOST_CONTAINER_STATIC_ASSERT(( Version > 1 ));
typedef dtl::shared_node_pool
<sizeof(T), NodesPerBlock> shared_pool_t;
typedef dtl::singleton_default<shared_pool_t> singleton_t;
singleton_t::instance().deallocate_node(p);
}
void deallocate_individual(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW
{
BOOST_CONTAINER_STATIC_ASSERT(( Version > 1 ));
typedef dtl::shared_node_pool
<sizeof(T), NodesPerBlock> shared_pool_t;
typedef dtl::singleton_default<shared_pool_t> singleton_t;
typename shared_pool_t::multiallocation_chain ch
( boost::movelib::iterator_to_raw_pointer(chain.begin())
, boost::movelib::iterator_to_raw_pointer(chain.last())
, chain.size());
singleton_t::instance().deallocate_nodes(ch);
}
void allocate_many(size_type elem_size, std::size_t n_elements, multiallocation_chain &chain)
{
BOOST_CONTAINER_STATIC_ASSERT(( Version > 1 ));
dlmalloc_memchain ch;
BOOST_CONTAINER_MEMCHAIN_INIT(&ch);
if(BOOST_UNLIKELY(!dlmalloc_multialloc_nodes(n_elements, elem_size*sizeof(T), BOOST_CONTAINER_DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch))){
boost::container::throw_bad_alloc();
}
chain.incorporate_after( chain.before_begin()
, (T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch)
, (T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch)
, BOOST_CONTAINER_MEMCHAIN_SIZE(&ch));
}
void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain)
{
BOOST_CONTAINER_STATIC_ASSERT(( Version > 1 ));
dlmalloc_memchain ch;
dlmalloc_multialloc_arrays(n_elements, elem_sizes, sizeof(T), BOOST_CONTAINER_DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch);
if(BOOST_UNLIKELY(BOOST_CONTAINER_MEMCHAIN_EMPTY(&ch))){
boost::container::throw_bad_alloc();
}
chain.incorporate_after( chain.before_begin()
, (T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch)
, (T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch)
, BOOST_CONTAINER_MEMCHAIN_SIZE(&ch));
}
void deallocate_many(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW
{
BOOST_CONTAINER_STATIC_ASSERT(( Version > 1 ));
void *first = boost::movelib::iterator_to_raw_pointer(chain.begin());
void *last = boost::movelib::iterator_to_raw_pointer(chain.last());
size_t num = chain.size();
dlmalloc_memchain ch;
BOOST_CONTAINER_MEMCHAIN_INIT_FROM(&ch, first, last, num);
dlmalloc_multidealloc(&ch);
}
friend void swap(self_t &, self_t &) BOOST_NOEXCEPT_OR_NOTHROW
{}
friend bool operator==(const node_allocator &, const node_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
{ return true; }
friend bool operator!=(const node_allocator &, const node_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
{ return false; }
private:
pointer priv_allocation_command
(allocation_type command, std::size_t limit_size
,size_type &prefer_in_recvd_out_size
,pointer &reuse)
{
std::size_t const preferred_size = prefer_in_recvd_out_size;
dlmalloc_command_ret_t ret = {0 , 0};
if((limit_size > this->max_size()) || (preferred_size > this->max_size())){
return pointer();
}
std::size_t l_size = limit_size*sizeof(T);
std::size_t p_size = preferred_size*sizeof(T);
std::size_t r_size;
{
void* reuse_ptr_void = reuse;
ret = dlmalloc_allocation_command(command, sizeof(T), l_size, p_size, &r_size, reuse_ptr_void);
reuse = static_cast<T*>(reuse_ptr_void);
}
prefer_in_recvd_out_size = r_size/sizeof(T);
return (pointer)ret.first;
}
};
} }
#include <boost/container/detail/config_end.hpp>
#endif