Should be able to create std::shared_ptr with pool_allocator
Concurrent Data Structure library
Brought to you by:
khizmax
The following code doesn't compile :
static cds::memory::vyukov_queue_pool<Toto> pool;
struct PoolAccessor
{
typedef cds::memory::vyukov_queue_pool<Toto>::value_type value_type;
cds::memory::vyukov_queue_pool<Toto>& operator()() const
{
return pool;
}
};
typedef cds::memory::pool_allocator< Toto, PoolAccessor > PoolAllocator;
template<class... TArgs>
static std::shared_ptr<Toto> createNew(TArgs... args)
{
return std::allocate_shared<Toto, PoolAllocator, TArgs...>(PoolAllocator(), std::forward<TArgs>(args)...);
}
Due to the following static_assert :
static_assert( sizeof(value_type) <= sizeof(typename accessor_type::value_type), "Incompatible type" );
Granted we still can use the overload of the shared_ptr constructor, but in this case, we loose the opimization of make_shared / allocate_shared.
And i'm not sure how to avoid this limitation. Any ideas ?
vyukov_queue_pool and its variations are not suitable for std::allocate_shared. According to http://en.cppreference.com/w/cpp/memory/shared_ptr/allocate_shared, "this function typically allocates memory for the T object and for the shared_ptr's control block with a single memory allocation".
vyukov_queue_pool is not a general-purpose allocator. It is a free-list - a pool of free fixed-sized block for objects of some type T.