feat(Memory): add Memory subsystem with BudgetResource, Arena, and BlockPool (S-MEMORY-02..04)
This commit is contained in:
1 parent
4227348051
commit
4d48f2a8f2
11 files changed
+1306
No files matched your search
@@ -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});
|
||||
}
|
||||
Reference in new issue
Block a user