Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions include/foonathan/memory/memory_pool_collection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ namespace foonathan
/// \throws Anything thrown by the \concept{concept_blockallocator,BlockAllocator} if a growth is needed or a \ref bad_node_size exception if the node size is too big.
void* allocate_node(std::size_t node_size)
{
detail::check_allocation_size<
bad_node_size>(node_size, [&] { return max_node_size(); }, info());
detail::check_allocation_size<bad_node_size>(
node_size, [&] { return max_node_size(); }, info());
auto& pool = pools_.get(node_size);
if (pool.empty())
{
Expand Down Expand Up @@ -169,8 +169,8 @@ namespace foonathan
/// \c node_size must be valid \concept{concept_node,node size}.
void* allocate_array(std::size_t count, std::size_t node_size)
{
detail::check_allocation_size<
bad_node_size>(node_size, [&] { return max_node_size(); }, info());
detail::check_allocation_size<bad_node_size>(
node_size, [&] { return max_node_size(); }, info());

auto& pool = pools_.get(node_size);

Expand Down Expand Up @@ -425,8 +425,10 @@ namespace foonathan
std::size_t alignment)
{
// node already checked
auto const aligned_size =
detail::round_up_to_multiple_of_alignment(size, alignment);
detail::check_allocation_size<bad_alignment>(
alignment, [&] { return detail::alignment_for(size); }, state.info());
aligned_size, [&] { return state.max_node_size(); }, state.info());
auto mem = state.allocate_node(size);
state.on_allocate(size);
return mem;
Expand All @@ -439,8 +441,10 @@ namespace foonathan
std::size_t alignment)
{
// node and array already checked
auto const aligned_size =
detail::round_up_to_multiple_of_alignment(size, alignment);
detail::check_allocation_size<bad_alignment>(
alignment, [&] { return detail::alignment_for(size); }, state.info());
aligned_size, [&] { return state.max_node_size(); }, state.info());
auto mem = state.allocate_array(count, size);
state.on_allocate(count * size);
return mem;
Expand Down
35 changes: 35 additions & 0 deletions test/memory_pool_collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ TEST_CASE("memory_pool_collection")
for (auto ptr : b)
pool.deallocate_node(ptr, 5);
}
SUBCASE("normal alloc/dealloc with traits")
{
std::vector<void*> a, b;
for (auto i = 0u; i != 5u; ++i)
{
a.push_back(allocator_traits<pools>::allocate_node(pool, 1, 8u));
b.push_back(allocator_traits<pools>::allocate_node(pool, 5, 8u));
}
REQUIRE(alloc.no_allocated() == 1u);
REQUIRE(pool.capacity_left() <= 4000u);

std::shuffle(a.begin(), a.end(), std::mt19937{});
std::shuffle(b.begin(), b.end(), std::mt19937{});

for (auto ptr : a)
allocator_traits<pools>::deallocate_node(pool, ptr, 1, 8);
for (auto ptr : b)
allocator_traits<pools>::deallocate_node(pool, ptr, 5, 8u);
}
SUBCASE("single array alloc")
{
auto memory = pool.allocate_array(4, 4);
Expand All @@ -75,6 +94,22 @@ TEST_CASE("memory_pool_collection")
for (auto ptr : b)
pool.deallocate_array(ptr, 5, 5);
}
SUBCASE("array alloc/dealloc with traits")
{
std::vector<void*> a;
for (auto i = 0u; i != 5u; ++i)
{
a.push_back(allocator_traits<pools>::allocate_array(pool, 5, 5, 8u));
REQUIRE(a.back());
}
REQUIRE(alloc.no_allocated() == 1u);
REQUIRE(pool.capacity_left() <= 4000u);

std::shuffle(a.begin(), a.end(), std::mt19937{});

for (auto ptr : a)
allocator_traits<pools>::deallocate_array(pool, ptr, 5, 5, 8u);
}
SUBCASE("multiple block alloc/dealloc")
{
std::vector<void*> a, b;
Expand Down