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 39c8ff1
Show file tree
Hide file tree
Showing 4 changed files with 88 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
31 changes: 30 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,33 @@ 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 > >;
using queue_with_capacity_t
= boost::lockfree::queue< char, boost::lockfree::allocator< allocator_type >, boost::lockfree::capacity< 16 > >;

auto allocator = queue_t::allocator {};

{
queue_with_capacity_t q_with_allocator {
allocator,
};
queue_t q_with_size_and_allocator {
5,
allocator,
};
}
{
queue_with_capacity_t q_with_allocator {
allocator_type {},
};
queue_t q_with_size_and_allocator {
5,
allocator_type {},
};
}
}
30 changes: 30 additions & 0 deletions test/stack_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,33 @@ 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 > >;
using stack_with_capacity_t
= boost::lockfree::stack< char, boost::lockfree::allocator< allocator_type >, boost::lockfree::capacity< 16 > >;

auto allocator = stack_t::allocator {};

{
stack_with_capacity_t stack_with_allocator {
allocator,
};
stack_t stack_with_size_and_allocator {
5,
allocator,
};
}
{
stack_with_capacity_t stack_with_allocator {
allocator_type {},
};
stack_t stack_with_size_and_allocator {
5,
allocator_type {},
};
}
}

0 comments on commit 39c8ff1

Please sign in to comment.