feat(Memory): add Memory subsystem with BudgetResource, Arena, and BlockPool (S-MEMORY-02..04)

This commit is contained in:
faumaray committed 2026-11-24 02:49:28 +03:00
1 parent 4227348051
commit 4d48f2a8f2
11 files changed
+1306

No files matched your search

+5
View File
@@ -86,6 +86,11 @@ if(_has_core)
run_consumer(CoreOnly "${install_prefix}" "CoreOnly" ${_no_external_deps})
endif()
has_modules(_has_memory Memory Core)
if(_has_memory)
run_consumer(MemoryOnly "${install_prefix}" "MemoryOnly" ${_no_external_deps})
endif()
has_modules(_has_binary Binary)
if(_has_binary)
run_consumer(Binary "${install_prefix}" "Binary" ${_no_external_deps})
+10
View File
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 4.0)
project(VoidMemoryOnlyConsumer LANGUAGES CXX)
find_package(Void CONFIG REQUIRED COMPONENTS Memory)
add_executable(void_memory_only_consumer main.cpp)
target_link_libraries(void_memory_only_consumer PRIVATE Void::Memory)
enable_testing()
add_test(NAME VoidMemoryOnlyConsumer COMMAND void_memory_only_consumer)
+19
View File
@@ -0,0 +1,19 @@
#include <Void/Memory/Arena.hpp>
#include <Void/Memory/BlockPool.hpp>
#include <Void/Memory/BudgetResource.hpp>
int main()
{
Void::Memory::BudgetResource budget{1024 * 1024};
Void::Memory::Arena arena{65536, 4096, budget};
Void::Memory::BlockPool pool{64, 16, 16, 4096, 8, budget};
auto mem = arena.try_allocate(128, 32);
auto blk = pool.try_allocate();
if (!mem || !blk) {
return 1;
}
pool.deallocate(*blk);
(void)pool.trim_idle();
return arena.reset().has_value() ? 0 : 1;
}
+124
View File
@@ -0,0 +1,124 @@
#include <Void/Memory/Arena.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <array>
#include <cstddef>
#include <cstdint>
#include <stdexcept>
#include <vector>
namespace
{
struct TrackedItem {
int id;
std::vector<int>* log;
TrackedItem(int i, std::vector<int>* l, bool should_throw = false)
: id(i), log(l)
{
if (should_throw) {
throw std::runtime_error{"ctor throw"};
}
}
~TrackedItem()
{
if (log != nullptr) {
log->push_back(id);
}
}
};
} // namespace
Void::Testing::Test arena_fixed_buffer_overalign_and_zero_bytes()
{
// Deliberately shift base pointer by 1 byte inside buffer to test true
// address alignment.
std::array<std::byte, 512> raw{};
Void::Memory::Arena arena{std::span<std::byte>{raw}.subspan(1)};
auto z = arena.try_allocate(0, 1);
Void::Testing::expect(z.has_value());
Void::Testing::expect_eq(arena.stats().requested_live, std::size_t{1});
auto a64 = arena.try_allocate(16, 64);
Void::Testing::expect(a64.has_value());
Void::Testing::expect_eq(
reinterpret_cast<std::uintptr_t>(*a64) % 64, std::uintptr_t{0});
const auto bad_align = arena.try_allocate(8, 3);
Void::Testing::expect(!bad_align.has_value());
Void::Testing::expect(bad_align.error() ==
Void::Memory::AllocationError::invalid_alignment);
const auto too_big = arena.try_allocate(1024, 8);
Void::Testing::expect(!too_big.has_value());
Void::Testing::expect(too_big.error() ==
Void::Memory::AllocationError::exhausted);
}
Void::Testing::Test arena_grows_slabs_and_rewinds_checkpoints()
{
Void::Memory::Arena arena{1024, 128};
auto p1 = arena.try_allocate(96, 8);
Void::Testing::expect(p1.has_value());
const auto cp1 = arena.checkpoint();
auto p2 = arena.try_allocate(128, 16);
Void::Testing::expect(p2.has_value());
const auto cp2 = arena.checkpoint();
Void::Testing::expect(arena.stats().committed_live >= 256);
Void::Testing::expect(arena.rewind(cp1).has_value());
Void::Testing::expect_eq(arena.stats().requested_live, std::size_t{96});
// Forward/beyond-cursor checkpoint must be rejected after rewind.
const auto bad_cp2 = arena.rewind(cp2);
Void::Testing::expect(!bad_cp2.has_value());
Void::Testing::expect(bad_cp2.error() ==
Void::Memory::AllocationError::invalid_checkpoint);
// Reusing space after rewind should not blow past budget unnecessarily.
auto p3 = arena.try_allocate(128, 16);
Void::Testing::expect(p3.has_value());
Void::Testing::expect(arena.reset().has_value());
Void::Testing::expect_eq(arena.stats().requested_live, std::size_t{0});
// Stale generation checkpoint rejected after reset().
const auto stale = arena.rewind(cp1);
Void::Testing::expect(!stale.has_value());
Void::Testing::expect(stale.error() ==
Void::Memory::AllocationError::invalid_checkpoint);
}
Void::Testing::Test arena_typed_scope_destroys_reverse_and_blocks_reset()
{
Void::Memory::Arena arena{4096, 512};
std::vector<int> destroyed;
{
Void::Memory::Arena::TypedScope scope{arena};
Void::Testing::expect(scope.emplace<TrackedItem>(1, &destroyed).has_value());
Void::Testing::expect(scope.emplace<TrackedItem>(2, &destroyed).has_value());
bool threw = false;
try {
(void)scope.emplace<TrackedItem>(99, &destroyed, true);
} catch (const std::runtime_error&) {
threw = true;
}
Void::Testing::expect(threw);
const auto blocked = arena.reset();
Void::Testing::expect(!blocked.has_value());
Void::Testing::expect(blocked.error() ==
Void::Memory::AllocationError::live_allocations);
}
Void::Testing::expect_eq(destroyed.size(), std::size_t{2});
Void::Testing::expect_eq(destroyed[0], 2);
Void::Testing::expect_eq(destroyed[1], 1);
Void::Testing::expect(arena.reset().has_value());
}
+104
View File
@@ -0,0 +1,104 @@
#include <Void/Memory/BlockPool.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <cstddef>
#include <memory_resource>
#include <thread>
#include <vector>
namespace
{
class CountingUpstream final : public std::pmr::memory_resource
{
public:
std::size_t slab_allocs{0};
std::size_t slab_frees{0};
private:
void* do_allocate(std::size_t bytes, std::size_t alignment) override
{
++slab_allocs;
return std::pmr::get_default_resource()->allocate(bytes, alignment);
}
void do_deallocate(void* p, std::size_t bytes,
std::size_t alignment) override
{
++slab_frees;
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 block_pool_recycles_without_upstream_allocs_and_trims_idle()
{
CountingUpstream upstream;
Void::Memory::BlockPool pool{32, 16, /*max_blocks=*/6,
/*max_committed=*/4096,
/*blocks_per_slab=*/2, upstream};
std::vector<void*> ptrs;
for (int i = 0; i < 6; ++i) {
auto r = pool.try_allocate();
Void::Testing::expect(r.has_value());
ptrs.push_back(*r);
}
Void::Testing::expect_eq(upstream.slab_allocs, std::size_t{3});
const auto full = pool.try_allocate();
Void::Testing::expect(!full.has_value());
Void::Testing::expect(full.error() ==
Void::Memory::AllocationError::exhausted);
// Return slab 1 completely (ptrs 2, 3) and leave slab 0 (ptr 0) and slab 2 (ptr 4) live.
pool.deallocate(ptrs[1]);
pool.deallocate(ptrs[2]);
pool.deallocate(ptrs[3]);
pool.deallocate(ptrs[5]);
// Recycle must hit existing free list without calling upstream!
const std::size_t allocs_before = upstream.slab_allocs;
auto recycled = pool.try_allocate();
Void::Testing::expect(recycled.has_value());
Void::Testing::expect_eq(upstream.slab_allocs, allocs_before);
pool.deallocate(*recycled);
// Re-free ptrs[1..3,5] state: ptrs[2] and ptrs[3] were slab 1 -> idle!
const std::size_t reclaimed = pool.trim_idle();
Void::Testing::expect(reclaimed > 0);
Void::Testing::expect_eq(upstream.slab_frees, std::size_t{1});
Void::Testing::expect_eq(pool.live_blocks(), std::size_t{2});
pool.deallocate(ptrs[0]);
pool.deallocate(ptrs[4]);
(void)pool.trim_idle();
Void::Testing::expect_eq(pool.live_blocks(), std::size_t{0});
Void::Testing::expect_eq(pool.capacity_blocks(), std::size_t{0});
}
Void::Testing::Test synchronized_block_pool_concurrent_alloc_dealloc()
{
Void::Memory::SynchronizedBlockPool pool{64, 8, 256, 65536, 16};
std::vector<std::thread> threads;
for (int t = 0; t < 4; ++t) {
threads.emplace_back([&]() {
for (int i = 0; i < 100; ++i) {
if (auto p = pool.try_allocate()) {
*static_cast<int*>(*p) = i;
pool.deallocate(*p);
}
}
});
}
for (auto& th : threads) {
th.join();
}
Void::Testing::expect_eq(pool.stats().requested_live, std::size_t{0});
}
+146
View File
@@ -0,0 +1,146 @@
#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});
}