Restructure into umbrella modules (~55 -> 16 targets)
Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
da2e93c405
commit
28b94883fd
156 files changed
+278
-220
No files matched your search
@@ -0,0 +1,166 @@
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <new>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Pool
|
||||
{
|
||||
|
||||
// ── ObjectPool<T, ChunkSize> ──────────────────────────────────────────────────
|
||||
// A slab-based object pool. acquire() returns a unique_ptr<T> with a custom
|
||||
// deleter that returns the object to the pool instead of freeing memory.
|
||||
// Objects are constructed in-place; the pool owns the backing storage.
|
||||
//
|
||||
// Thread-safety: NOT thread-safe. Add external synchronisation if needed.
|
||||
|
||||
template <typename T, std::size_t ChunkSize = 32>
|
||||
class ObjectPool
|
||||
{
|
||||
public:
|
||||
ObjectPool() = default;
|
||||
|
||||
ObjectPool(const ObjectPool&) = delete;
|
||||
ObjectPool& operator=(const ObjectPool&) = delete;
|
||||
ObjectPool(ObjectPool&&) = default;
|
||||
ObjectPool& operator=(ObjectPool&&) = default;
|
||||
|
||||
~ObjectPool() = default;
|
||||
|
||||
struct Deleter
|
||||
{
|
||||
ObjectPool* pool;
|
||||
void operator()(T* p) const noexcept { pool->release(p); }
|
||||
};
|
||||
|
||||
using Ptr = std::unique_ptr<T, Deleter>;
|
||||
|
||||
template <typename... Args>
|
||||
[[nodiscard]] Ptr acquire(Args&&... args)
|
||||
{
|
||||
T* slot = pop_free();
|
||||
if (!slot) {
|
||||
slot = allocate_slot();
|
||||
}
|
||||
std::construct_at(slot, std::forward<Args>(args)...);
|
||||
return Ptr{slot, Deleter{this}};
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t capacity() const noexcept
|
||||
{
|
||||
return slabs_.size() * ChunkSize;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t available() const noexcept
|
||||
{
|
||||
return free_.size();
|
||||
}
|
||||
|
||||
private:
|
||||
void release(T* p) noexcept
|
||||
{
|
||||
std::destroy_at(p);
|
||||
free_.push_back(p);
|
||||
}
|
||||
|
||||
T* pop_free() noexcept
|
||||
{
|
||||
if (free_.empty()) return nullptr;
|
||||
T* p = free_.back();
|
||||
free_.pop_back();
|
||||
return p;
|
||||
}
|
||||
|
||||
T* allocate_slot()
|
||||
{
|
||||
// Grow by one slab; keep all subsequent objects from the same slab
|
||||
// available in free_.
|
||||
auto& slab = slabs_.emplace_back();
|
||||
slab = std::make_unique<Slab>();
|
||||
// Push all but the first slot into free_.
|
||||
for (std::size_t i = 1; i < ChunkSize; ++i) {
|
||||
free_.push_back(reinterpret_cast<T*>(&slab->storage[i]));
|
||||
}
|
||||
return reinterpret_cast<T*>(&slab->storage[0]);
|
||||
}
|
||||
|
||||
struct alignas(alignof(T)) Slot
|
||||
{
|
||||
std::byte storage[sizeof(T)];
|
||||
};
|
||||
|
||||
struct Slab
|
||||
{
|
||||
Slot storage[ChunkSize];
|
||||
};
|
||||
|
||||
std::vector<std::unique_ptr<Slab>> slabs_;
|
||||
std::vector<T*> free_;
|
||||
};
|
||||
|
||||
// ── FixedPool<T, N> ───────────────────────────────────────────────────────────
|
||||
// A fixed-capacity pool allocated in-place (no heap after construction).
|
||||
// Capacity is known at compile time; acquire() returns nullptr when full.
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
class FixedPool
|
||||
{
|
||||
public:
|
||||
FixedPool() noexcept
|
||||
{
|
||||
for (std::size_t i = 0; i < N; ++i) {
|
||||
free_[i] = slot_ptr(i);
|
||||
}
|
||||
free_count_ = N;
|
||||
}
|
||||
|
||||
FixedPool(const FixedPool&) = delete;
|
||||
FixedPool& operator=(const FixedPool&) = delete;
|
||||
|
||||
struct Deleter
|
||||
{
|
||||
FixedPool* pool;
|
||||
void operator()(T* p) const noexcept { pool->release(p); }
|
||||
};
|
||||
|
||||
using Ptr = std::unique_ptr<T, Deleter>;
|
||||
|
||||
template <typename... Args>
|
||||
[[nodiscard]] Ptr acquire(Args&&... args) noexcept(
|
||||
std::is_nothrow_constructible_v<T, Args...>)
|
||||
{
|
||||
if (free_count_ == 0) return nullptr;
|
||||
T* slot = free_[--free_count_];
|
||||
std::construct_at(slot, std::forward<Args>(args)...);
|
||||
return Ptr{slot, Deleter{this}};
|
||||
}
|
||||
|
||||
[[nodiscard]] static constexpr std::size_t capacity() noexcept { return N; }
|
||||
[[nodiscard]] std::size_t available() const noexcept { return free_count_; }
|
||||
|
||||
private:
|
||||
void release(T* p) noexcept
|
||||
{
|
||||
std::destroy_at(p);
|
||||
free_[free_count_++] = p;
|
||||
}
|
||||
|
||||
T* slot_ptr(std::size_t i) noexcept
|
||||
{
|
||||
return reinterpret_cast<T*>(&storage_[i]);
|
||||
}
|
||||
|
||||
struct alignas(alignof(T)) Slot
|
||||
{
|
||||
std::byte storage[sizeof(T)];
|
||||
};
|
||||
|
||||
Slot storage_[N];
|
||||
T* free_[N];
|
||||
std::size_t free_count_{0};
|
||||
};
|
||||
|
||||
} // namespace Void::Pool
|
||||
Reference in new issue
Block a user