diff --git a/include/boost/lockfree/queue.hpp b/include/boost/lockfree/queue.hpp index eb9cd27..19c950d 100644 --- a/include/boost/lockfree/queue.hpp +++ b/include/boost/lockfree/queue.hpp @@ -253,6 +253,21 @@ class queue initialize(); } + /** Construct a variable-sized queue with a custom allocator + * + * Allocate n nodes initially for the freelist + * + * \pre Must \b not specify a capacity<> argument + * */ + queue( size_type n, allocator const& alloc ) : + head_( tagged_node_handle( 0, 0 ) ), + tail_( tagged_node_handle( 0, 0 ) ), + pool( alloc, n + 1 ) + { + BOOST_STATIC_ASSERT( !has_capacity ); + initialize(); + } + /** \copydoc boost::lockfree::stack::reserve * */ void reserve( size_type n ) diff --git a/include/boost/lockfree/stack.hpp b/include/boost/lockfree/stack.hpp index c78eea0..f730020 100644 --- a/include/boost/lockfree/stack.hpp +++ b/include/boost/lockfree/stack.hpp @@ -200,6 +200,19 @@ class stack initialize(); } + /** Construct a variable-sized stack with a custom allocator + * + * Allocate n nodes initially for the freelist + * + * \pre Must \b not specify a capacity<> argument + * */ + stack( size_type n, node_allocator const& alloc ) : + pool( alloc, n ) + { + BOOST_STATIC_ASSERT( !has_capacity ); + initialize(); + } + /** Allocate n nodes for freelist * * \pre only valid if no capacity<> argument given diff --git a/test/queue_test.cpp b/test/queue_test.cpp index 56b77d5..22defcd 100644 --- a/test/queue_test.cpp +++ b/test/queue_test.cpp @@ -18,7 +18,6 @@ #include -#include "test_helpers.hpp" using namespace boost; using namespace boost::lockfree; @@ -191,3 +190,31 @@ BOOST_AUTO_TEST_CASE( reserve_test ) ms.reserve( 1 ); ms.reserve_unsafe( 1 ); } + +BOOST_AUTO_TEST_CASE( queue_with_allocator ) +{ + using allocator_type = std::allocator< char >; + + using queue_t = boost::lockfree::queue< char, boost::lockfree::allocator< allocator_type > >; + + auto allocator = queue_t::allocator {}; + + { + boost::lockfree::queue< char, boost::lockfree::allocator< allocator_type > > q_with_allocator { + allocator, + }; + boost::lockfree::queue< char, boost::lockfree::allocator< allocator_type > > q_with_size_and_allocator { + 5, + allocator, + }; + } + { + boost::lockfree::queue< char, boost::lockfree::allocator< allocator_type > > q_with_allocator { + allocator_type {}, + }; + boost::lockfree::queue< char, boost::lockfree::allocator< allocator_type > > q_with_size_and_allocator { + 5, + allocator_type {}, + }; + } +} diff --git a/test/stack_test.cpp b/test/stack_test.cpp index 4b25ec6..f87300b 100644 --- a/test/stack_test.cpp +++ b/test/stack_test.cpp @@ -228,3 +228,31 @@ BOOST_AUTO_TEST_CASE( reserve_test ) ms.reserve( 1 ); ms.reserve_unsafe( 1 ); } + +BOOST_AUTO_TEST_CASE( stack_with_allocator ) +{ + using allocator_type = std::allocator< char >; + + using stack_t = boost::lockfree::stack< char, boost::lockfree::allocator< allocator_type > >; + + auto allocator = stack_t::allocator {}; + + { + boost::lockfree::stack< char, boost::lockfree::allocator< allocator_type > > stack_with_allocator { + allocator, + }; + boost::lockfree::stack< char, boost::lockfree::allocator< allocator_type > > stack_with_size_and_allocator { + 5, + allocator, + }; + } + { + boost::lockfree::stack< char, boost::lockfree::allocator< allocator_type > > stack_with_allocator { + allocator_type {}, + }; + boost::lockfree::stack< char, boost::lockfree::allocator< allocator_type > > stack_with_size_and_allocator { + 5, + allocator_type {}, + }; + } +}