125 lines
4.0 KiB
C++
125 lines
4.0 KiB
C++
#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());
|
|
}
|