Files
Library/tests/standalone/Memory/BudgetResource.cpp

147 lines
4.7 KiB
C++

#include <Void/Memory/BudgetResource.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <atomic>
#include <cstddef>
#include <limits>
#include <memory_resource>
#include <new>
#include <thread>
#include <vector>
namespace
{
class FailingUpstream final : public std::pmr::memory_resource
{
public:
bool fail_next{false};
std::size_t live_allocs{0};
private:
void* do_allocate(std::size_t bytes, std::size_t alignment) override
{
if (fail_next) {
throw std::bad_alloc{};
}
++live_allocs;
return std::pmr::get_default_resource()->allocate(bytes, alignment);
}
void do_deallocate(void* p, std::size_t bytes,
std::size_t alignment) override
{
--live_allocs;
std::pmr::get_default_resource()->deallocate(p, bytes, alignment);
}
bool do_is_equal(
const std::pmr::memory_resource& other) const noexcept override
{
return this == &other;
}
};
} // namespace
Void::Testing::Test budget_enforces_zero_exact_and_overflow_limits()
{
Void::Memory::BudgetResource zero_cap{0};
const auto r0 = zero_cap.try_allocate(1, 1);
Void::Testing::expect(!r0.has_value());
Void::Testing::expect(r0.error() ==
Void::Memory::AllocationError::exhausted);
Void::Memory::BudgetResource cap{128};
const auto bad_align = cap.try_allocate(16, 3);
Void::Testing::expect(!bad_align.has_value());
Void::Testing::expect(bad_align.error() ==
Void::Memory::AllocationError::invalid_alignment);
auto p1 = cap.try_allocate(64, 16);
Void::Testing::expect(p1.has_value());
auto p2 = cap.try_allocate(64, 16);
Void::Testing::expect(p2.has_value());
auto p3 = cap.try_allocate(1, 1);
Void::Testing::expect(!p3.has_value());
Void::Testing::expect(p3.error() ==
Void::Memory::AllocationError::exhausted);
Void::Memory::BudgetResource unlim{Void::Memory::unlimited};
auto ok_u = unlim.try_allocate(32, 8);
Void::Testing::expect(ok_u.has_value());
const auto ovf =
unlim.try_allocate(std::numeric_limits<std::size_t>::max(), 8);
Void::Testing::expect(!ovf.has_value());
Void::Testing::expect(ovf.error() ==
Void::Memory::AllocationError::size_overflow);
unlim.deallocate_checked(*ok_u, 32, 8);
cap.deallocate_checked(*p1, 64, 16);
cap.deallocate_checked(*p2, 64, 16);
const auto st = cap.stats();
Void::Testing::expect_eq(st.committed_live, std::size_t{0});
Void::Testing::expect_eq(st.peak_committed, std::size_t{128});
Void::Testing::expect_eq(st.allocations, std::uint64_t{2});
Void::Testing::expect_eq(st.deallocations, std::uint64_t{2});
Void::Testing::expect_eq(st.failures, std::uint64_t{2});
}
Void::Testing::Test budget_rolls_back_reservation_on_upstream_failure()
{
FailingUpstream upstream;
Void::Memory::BudgetResource budget{256, upstream};
auto ok = budget.try_allocate(64, 8);
Void::Testing::expect(ok.has_value());
upstream.fail_next = true;
const auto fail = budget.try_allocate(128, 8);
Void::Testing::expect(!fail.has_value());
Void::Testing::expect(fail.error() ==
Void::Memory::AllocationError::upstream_failure);
Void::Testing::expect_eq(budget.stats().committed_live, std::size_t{64});
// After upstream failure rollback, full remaining budget is still usable.
upstream.fail_next = false;
auto ok2 = budget.try_allocate(192, 8);
Void::Testing::expect(ok2.has_value());
budget.deallocate_checked(*ok, 64, 8);
budget.deallocate_checked(*ok2, 192, 8);
Void::Testing::expect_eq(upstream.live_allocs, std::size_t{0});
}
Void::Testing::Test budget_pmr_throws_bad_alloc_and_supports_threads()
{
Void::Memory::BudgetResource tiny{16};
bool threw = false;
try {
std::pmr::vector<std::byte> v{&tiny};
v.resize(64);
} catch (const std::bad_alloc&) {
threw = true;
}
Void::Testing::expect(threw);
Void::Testing::expect_eq(tiny.stats().committed_live, std::size_t{0});
Void::Memory::BudgetResource shared{4096};
std::atomic<int> successes{0};
std::vector<std::thread> workers;
for (int t = 0; t < 4; ++t) {
workers.emplace_back([&]() {
for (int i = 0; i < 100; ++i) {
if (auto p = shared.try_allocate(64, 8)) {
successes.fetch_add(1, std::memory_order_relaxed);
shared.deallocate_checked(*p, 64, 8);
}
}
});
}
for (auto& w : workers) {
w.join();
}
Void::Testing::expect(successes.load() > 0);
Void::Testing::expect_eq(shared.stats().committed_live, std::size_t{0});
}