feat(Containers): fix pool lease lifetimes/budgets and non-spinning MPSC ring (M-CONTAINERS-02, M-CONTAINERS-03)
This commit is contained in:
1 parent
883bbdcf38
commit
9ac76bbcf9
7 files changed
+1252
-285
No files matched your search
@@ -1,8 +1,13 @@
|
||||
#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 ────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -101,3 +106,134 @@ Void::Testing::Test fixed_pool_reuse_after_release()
|
||||
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});
|
||||
}
|
||||
Reference in new issue
Block a user