1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors
// SPDX-License-Identifier: Zlib
#include <foonathan/memory/memory_pool.hpp>
#include <algorithm>
#include <doctest/doctest.h>
#include <random>
#include <vector>
#include <foonathan/memory/allocator_storage.hpp>
#include "test_allocator.hpp"
using namespace foonathan::memory;
// don't test actual node allocationg, but the connection between arena and the implementation
// so only test for memory_pool<node_pool>
TEST_CASE("memory_pool")
{
using pool_type = memory_pool<node_pool, allocator_reference<test_allocator>>;
test_allocator alloc;
{
pool_type pool(4, pool_type::min_block_size(4, 25), alloc);
REQUIRE(pool.node_size() >= 4u);
REQUIRE(pool.capacity_left() >= 25 * 4u);
REQUIRE(pool.next_capacity() >= 25 * 4u);
REQUIRE(alloc.no_allocated() == 1u);
SUBCASE("normal alloc/dealloc")
{
std::vector<void*> ptrs;
auto capacity = pool.capacity_left();
REQUIRE(capacity / 4 >= 25);
for (std::size_t i = 0u; i != 25; ++i)
ptrs.push_back(pool.allocate_node());
REQUIRE(pool.capacity_left() >= 0u);
REQUIRE(alloc.no_allocated() == 1u);
std::shuffle(ptrs.begin(), ptrs.end(), std::mt19937{});
for (auto ptr : ptrs)
pool.deallocate_node(ptr);
REQUIRE(pool.capacity_left() == capacity);
}
SUBCASE("multiple block alloc/dealloc")
{
std::vector<void*> ptrs;
auto capacity = pool.capacity_left();
for (std::size_t i = 0u; i != capacity / pool.node_size(); ++i)
ptrs.push_back(pool.allocate_node());
REQUIRE(pool.capacity_left() >= 0u);
ptrs.push_back(pool.allocate_node());
REQUIRE(pool.capacity_left() >= capacity - pool.node_size());
REQUIRE(alloc.no_allocated() == 2u);
std::shuffle(ptrs.begin(), ptrs.end(), std::mt19937{});
for (auto ptr : ptrs)
pool.deallocate_node(ptr);
REQUIRE(pool.capacity_left() >= capacity);
REQUIRE(alloc.no_allocated() == 2u);
}
}
{
pool_type pool(16, pool_type::min_block_size(16, 1), alloc);
REQUIRE(pool.node_size() == 16u);
REQUIRE(pool.capacity_left() == 16u);
REQUIRE(pool.next_capacity() >= 16u);
REQUIRE(alloc.no_allocated() == 1u);
auto ptr = pool.allocate_node();
REQUIRE(ptr);
pool.deallocate_node(ptr);
}
REQUIRE(alloc.no_allocated() == 0u);
}
namespace
{
template <typename PoolType>
void use_min_block_size(std::size_t node_size, std::size_t number_of_nodes)
{
auto min_size = memory_pool<PoolType>::min_block_size(node_size, number_of_nodes);
memory_pool<PoolType> pool(node_size, min_size);
CHECK(pool.capacity_left() >= node_size * number_of_nodes);
// First allocations should not require realloc.
for (auto i = 0u; i != number_of_nodes; ++i)
{
auto ptr = pool.try_allocate_node();
CHECK(ptr);
}
// Further allocation might require it, but should still succeed then.
auto ptr = pool.allocate_node();
CHECK(ptr);
}
} // namespace
TEST_CASE("memory_pool::min_block_size()")
{
SUBCASE("node_pool")
{
use_min_block_size<node_pool>(1, 1);
use_min_block_size<node_pool>(16, 1);
use_min_block_size<node_pool>(1, 1000);
use_min_block_size<node_pool>(16, 1000);
}
SUBCASE("array_pool")
{
use_min_block_size<array_pool>(1, 1);
use_min_block_size<array_pool>(16, 1);
use_min_block_size<array_pool>(1, 1000);
use_min_block_size<array_pool>(16, 1000);
}
SUBCASE("small_node_pool")
{
use_min_block_size<small_node_pool>(1, 1);
use_min_block_size<small_node_pool>(16, 1);
use_min_block_size<small_node_pool>(1, 1000);
use_min_block_size<small_node_pool>(16, 1000);
}
}
|