Skip to content

Commit

Permalink
stack/queue - add missing allocator ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
timblechmann committed Nov 3, 2023
1 parent bafa33b commit 1c875d6
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
15 changes: 15 additions & 0 deletions include/boost/lockfree/queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
13 changes: 13 additions & 0 deletions include/boost/lockfree/stack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 28 additions & 1 deletion test/queue_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include <memory>

#include "test_helpers.hpp"

using namespace boost;
using namespace boost::lockfree;
Expand Down Expand Up @@ -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 {},
};
}
}
28 changes: 28 additions & 0 deletions test/stack_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {},
};
}
}

0 comments on commit 1c875d6

Please sign in to comment.