240 lines
8.0 KiB
C++
240 lines
8.0 KiB
C++
#include <Void/Containers/Pool.hpp>
|
|
#include <Void/Memory/BudgetResource.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <atomic>
|
|
#include <cstdint>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
// ── ObjectPool ────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test object_pool_acquire_returns_valid_ptr()
|
|
{
|
|
Void::Pool::ObjectPool<int> pool;
|
|
auto p = pool.acquire(42);
|
|
Void::Testing::expect(p != nullptr);
|
|
Void::Testing::expect_eq(*p, 42);
|
|
}
|
|
|
|
Void::Testing::Test object_pool_release_on_ptr_destruction()
|
|
{
|
|
Void::Pool::ObjectPool<int, 4> pool;
|
|
Void::Testing::expect_eq(pool.available(), std::size_t{0});
|
|
{
|
|
auto p = pool.acquire(1);
|
|
// capacity grew to 4, 3 slots free
|
|
Void::Testing::expect_eq(pool.available(), std::size_t{3});
|
|
}
|
|
// Released back to pool
|
|
Void::Testing::expect_eq(pool.available(), std::size_t{4});
|
|
}
|
|
|
|
Void::Testing::Test object_pool_reuses_memory()
|
|
{
|
|
Void::Pool::ObjectPool<int, 4> pool;
|
|
int* raw1 = nullptr;
|
|
{
|
|
auto p = pool.acquire(1);
|
|
raw1 = p.get();
|
|
}
|
|
auto p2 = pool.acquire(2);
|
|
// After release the same slot should be reused (LIFO from free list)
|
|
Void::Testing::expect(p2.get() == raw1);
|
|
}
|
|
|
|
Void::Testing::Test object_pool_string_construction()
|
|
{
|
|
Void::Pool::ObjectPool<std::string> pool;
|
|
auto p = pool.acquire("hello");
|
|
Void::Testing::expect_eq(*p, std::string{"hello"});
|
|
}
|
|
|
|
Void::Testing::Test object_pool_capacity_grows()
|
|
{
|
|
Void::Pool::ObjectPool<int, 4> pool;
|
|
Void::Testing::expect_eq(pool.capacity(), std::size_t{0});
|
|
auto p = pool.acquire(1);
|
|
Void::Testing::expect_eq(pool.capacity(), std::size_t{4});
|
|
}
|
|
|
|
// ── FixedPool ─────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test fixed_pool_capacity()
|
|
{
|
|
Void::Pool::FixedPool<int, 8> pool;
|
|
static_assert(pool.capacity() == 8);
|
|
Void::Testing::expect_eq(pool.available(), std::size_t{8});
|
|
}
|
|
|
|
Void::Testing::Test fixed_pool_acquire_decrements_available()
|
|
{
|
|
Void::Pool::FixedPool<int, 4> pool;
|
|
auto p = pool.acquire(99);
|
|
Void::Testing::expect_eq(pool.available(), std::size_t{3});
|
|
Void::Testing::expect_eq(*p, 99);
|
|
}
|
|
|
|
Void::Testing::Test fixed_pool_returns_null_when_full()
|
|
{
|
|
Void::Pool::FixedPool<int, 2> pool;
|
|
auto p1 = pool.acquire(1);
|
|
auto p2 = pool.acquire(2);
|
|
auto p3 = pool.acquire(3); // should be nullptr
|
|
Void::Testing::expect(p3 == nullptr);
|
|
}
|
|
|
|
Void::Testing::Test fixed_pool_release_restores_available()
|
|
{
|
|
Void::Pool::FixedPool<int, 4> pool;
|
|
{
|
|
auto p = pool.acquire(1);
|
|
Void::Testing::expect_eq(pool.available(), std::size_t{3});
|
|
}
|
|
Void::Testing::expect_eq(pool.available(), std::size_t{4});
|
|
}
|
|
|
|
Void::Testing::Test fixed_pool_reuse_after_release()
|
|
{
|
|
Void::Pool::FixedPool<int, 4> pool;
|
|
auto p1 = pool.acquire(1);
|
|
auto p2 = pool.acquire(2);
|
|
p1.reset();
|
|
auto p3 = pool.acquire(3);
|
|
Void::Testing::expect(p3 != nullptr);
|
|
Void::Testing::expect_eq(*p3, 3);
|
|
}
|
|
|
|
namespace
|
|
{
|
|
struct TrackedObject
|
|
{
|
|
int value{0};
|
|
std::atomic<int>* dtor_count{nullptr};
|
|
bool should_throw{false};
|
|
|
|
TrackedObject(int v, std::atomic<int>* dc, bool fail = false)
|
|
: value{v}, dtor_count{dc}, should_throw{fail}
|
|
{
|
|
if (should_throw) {
|
|
throw std::runtime_error{"ctor failure"};
|
|
}
|
|
}
|
|
|
|
~TrackedObject()
|
|
{
|
|
if (dtor_count) {
|
|
dtor_count->fetch_add(1, std::memory_order_relaxed);
|
|
}
|
|
}
|
|
};
|
|
} // namespace
|
|
|
|
Void::Testing::Test object_pool_leases_survive_move_and_owner_destruction()
|
|
{
|
|
std::atomic<int> dtors{0};
|
|
Void::Pool::ObjectPool<TrackedObject, 4>::Ptr retained_a;
|
|
Void::Pool::ObjectPool<TrackedObject, 4>::Ptr retained_b;
|
|
|
|
{
|
|
Void::Pool::ObjectPool<TrackedObject, 4> p1;
|
|
auto a = p1.try_acquire(11, &dtors);
|
|
Void::Testing::require(a.has_value());
|
|
retained_a = std::move(*a);
|
|
|
|
auto p2 = std::move(p1);
|
|
Void::Testing::expect_eq(p2.live(), std::size_t{1});
|
|
auto b = p2.try_acquire(22, &dtors);
|
|
Void::Testing::require(b.has_value());
|
|
retained_b = std::move(*b);
|
|
Void::Testing::expect_eq(p2.live(), std::size_t{2});
|
|
} // p2 destroyed while retained_a and retained_b remain live
|
|
|
|
Void::Testing::expect_eq(dtors.load(), 0);
|
|
Void::Testing::expect_eq(retained_a->value, 11);
|
|
Void::Testing::expect_eq(retained_b->value, 22);
|
|
retained_a.reset();
|
|
Void::Testing::expect_eq(dtors.load(), 1);
|
|
retained_b.reset();
|
|
Void::Testing::expect_eq(dtors.load(), 2);
|
|
}
|
|
|
|
Void::Testing::Test object_pool_enforces_limits_budget_zero_alloc_recycle_and_ctor_throw()
|
|
{
|
|
using Void::Pool::PoolError;
|
|
|
|
// Constructor exception rolls back slot cleanly without leaking live count.
|
|
std::atomic<int> dtors{0};
|
|
Void::Pool::ObjectPool<TrackedObject, 4> exc_pool{
|
|
Void::Pool::ObjectPoolLimits{.max_live = 2, .max_total_slots = 4}};
|
|
bool caught = false;
|
|
try {
|
|
(void)exc_pool.try_acquire(1, &dtors, true);
|
|
} catch (const std::runtime_error&) {
|
|
caught = true;
|
|
}
|
|
Void::Testing::expect(caught);
|
|
Void::Testing::expect_eq(exc_pool.live(), std::size_t{0});
|
|
Void::Testing::expect_eq(exc_pool.available(), std::size_t{4});
|
|
Void::Testing::expect_eq(dtors.load(), 0);
|
|
|
|
// BudgetResource tracks allocations: zero upstream allocs on recycle & reuse.
|
|
Void::Memory::BudgetResource budget{1024};
|
|
Void::Pool::ObjectPool<int, 2> budgeted{
|
|
&budget,
|
|
Void::Pool::ObjectPoolLimits{.max_live = 4, .max_total_slots = 16}};
|
|
{
|
|
auto p1 = budgeted.try_acquire(10);
|
|
auto p2 = budgeted.try_acquire(20);
|
|
Void::Testing::require(p1.has_value() && p2.has_value());
|
|
Void::Testing::expect_eq(budgeted.live(), std::size_t{2});
|
|
}
|
|
const auto allocs_after_first_slab = budget.stats().allocations;
|
|
Void::Testing::expect_eq(allocs_after_first_slab, std::uint64_t{1});
|
|
{
|
|
auto r1 = budgeted.try_acquire(30);
|
|
auto r2 = budgeted.try_acquire(40);
|
|
Void::Testing::require(r1.has_value() && r2.has_value());
|
|
Void::Testing::expect_eq(budget.stats().allocations, allocs_after_first_slab);
|
|
}
|
|
|
|
// max_live limit triggers exhausted.
|
|
Void::Pool::ObjectPool<int, 4> capped{
|
|
Void::Pool::ObjectPoolLimits{.max_live = 2, .max_total_slots = 4}};
|
|
auto c1 = capped.try_acquire(1);
|
|
auto c2 = capped.try_acquire(2);
|
|
auto c3 = capped.try_acquire(3);
|
|
Void::Testing::require(c1.has_value() && c2.has_value());
|
|
Void::Testing::expect(!c3.has_value() && c3.error() == PoolError::exhausted);
|
|
|
|
// Exhausted BudgetResource triggers allocation_failed.
|
|
Void::Memory::BudgetResource tiny_budget{0};
|
|
Void::Pool::ObjectPool<int, 4> oom_pool{&tiny_budget};
|
|
const auto oom = oom_pool.try_acquire(5);
|
|
Void::Testing::expect(!oom.has_value() && oom.error() == PoolError::allocation_failed);
|
|
}
|
|
|
|
Void::Testing::Test fixed_pool_try_acquire_and_throwing_ctor()
|
|
{
|
|
std::atomic<int> dtors{0};
|
|
Void::Pool::FixedPool<TrackedObject, 2> pool;
|
|
bool caught = false;
|
|
try {
|
|
(void)pool.try_acquire(7, &dtors, true);
|
|
} catch (const std::runtime_error&) {
|
|
caught = true;
|
|
}
|
|
Void::Testing::expect(caught);
|
|
Void::Testing::expect_eq(pool.available(), std::size_t{2});
|
|
Void::Testing::expect_eq(pool.live(), std::size_t{0});
|
|
|
|
auto a = pool.try_acquire(1, &dtors);
|
|
auto b = pool.try_acquire(2, &dtors);
|
|
auto c = pool.try_acquire(3, &dtors);
|
|
Void::Testing::require(a.has_value() && b.has_value());
|
|
Void::Testing::expect(!c.has_value() && c.error() == Void::Pool::PoolError::exhausted);
|
|
Void::Testing::expect_eq(pool.live(), std::size_t{2});
|
|
}
|