Implement M5.4 bounded compact chunk ownership
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
#pragma once
|
||||
#include "evo/world/generation.hpp"
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <memory>
|
||||
#include <span>
|
||||
|
||||
namespace evo::world {
|
||||
enum class ChunkError : std::uint8_t {
|
||||
invalid_config,
|
||||
invalid_descriptor,
|
||||
invalid_address,
|
||||
invalid_cells,
|
||||
capacity,
|
||||
memory_budget,
|
||||
missing,
|
||||
stale_view,
|
||||
invalid_local,
|
||||
dirty,
|
||||
revision_exhausted
|
||||
};
|
||||
enum class CellLayout : std::uint8_t { uniform, palette, dense };
|
||||
class ChunkStore;
|
||||
// Immutable owned geometry. Construction does not authorize a terrain edit.
|
||||
class CompactCells {
|
||||
public:
|
||||
// Moves reset the source to a readable uniform void chunk.
|
||||
CompactCells(CompactCells&& other) noexcept;
|
||||
CompactCells& operator=(CompactCells&& other) noexcept;
|
||||
CompactCells(const CompactCells&) = delete;
|
||||
CompactCells& operator=(const CompactCells&) = delete;
|
||||
[[nodiscard]] static std::expected<CompactCells, ChunkError>
|
||||
create(std::span<const CellSample> cells);
|
||||
[[nodiscard]] std::expected<CellSample, ChunkError> read(std::uint32_t index) const noexcept;
|
||||
[[nodiscard]] std::size_t payload_bytes() const noexcept;
|
||||
[[nodiscard]] CellLayout layout() const noexcept {
|
||||
return layout_;
|
||||
}
|
||||
|
||||
private:
|
||||
friend class ChunkStore;
|
||||
CompactCells() = default;
|
||||
static std::size_t required_bytes(std::span<const std::uint32_t> words) noexcept;
|
||||
static CompactCells encode(std::span<const std::uint32_t> words);
|
||||
CellLayout layout_{CellLayout::uniform};
|
||||
std::uint32_t uniform_{};
|
||||
std::uint32_t keys_{};
|
||||
std::unique_ptr<std::uint32_t[]> data_;
|
||||
};
|
||||
// Non-owning token; must not outlive the issuing store. Never exposes storage pointers.
|
||||
class ChunkView {
|
||||
public:
|
||||
[[nodiscard]] std::uint64_t revision() const noexcept {
|
||||
return revision_;
|
||||
}
|
||||
|
||||
private:
|
||||
friend class ChunkStore;
|
||||
ChunkView(const ChunkStore* owner, std::size_t slot, std::uint64_t revision)
|
||||
: owner_{owner}, slot_{slot}, revision_{revision} {}
|
||||
const ChunkStore* owner_;
|
||||
std::size_t slot_;
|
||||
std::uint64_t revision_;
|
||||
};
|
||||
class ChunkStore {
|
||||
public:
|
||||
static constexpr std::size_t max_slots = 4096;
|
||||
static constexpr std::size_t scratch_bytes = (4096 + 256) * sizeof(std::uint32_t);
|
||||
[[nodiscard]] static std::expected<std::unique_ptr<ChunkStore>, ChunkError>
|
||||
create(BaseDescriptor descriptor, std::size_t slots, std::size_t byte_budget);
|
||||
ChunkStore(const ChunkStore&) = delete;
|
||||
ChunkStore& operator=(const ChunkStore&) = delete;
|
||||
ChunkStore(ChunkStore&&) = delete;
|
||||
ChunkStore& operator=(ChunkStore&&) = delete;
|
||||
~ChunkStore();
|
||||
[[nodiscard]] static std::expected<std::size_t, ChunkError>
|
||||
minimum_bytes(std::size_t slots) noexcept;
|
||||
[[nodiscard]] std::expected<ChunkView, ChunkError> load(ChunkAddress address);
|
||||
[[nodiscard]] std::expected<ChunkView, ChunkError> view(ChunkAddress address) const noexcept;
|
||||
[[nodiscard]] std::expected<CellSample, ChunkError> read(ChunkView view,
|
||||
LocalCell local) const noexcept;
|
||||
// Conservative pin only: no geometry edit or durability acknowledgement.
|
||||
[[nodiscard]] std::expected<ChunkView, ChunkError> mark_dirty(ChunkView view) noexcept;
|
||||
[[nodiscard]] std::expected<void, ChunkError> evict(ChunkView view) noexcept;
|
||||
[[nodiscard]] std::size_t accounted_bytes() const noexcept {
|
||||
return bytes_;
|
||||
}
|
||||
[[nodiscard]] std::size_t size() const noexcept {
|
||||
return size_;
|
||||
}
|
||||
|
||||
private:
|
||||
struct Entry;
|
||||
ChunkStore(BaseDescriptor descriptor, std::size_t slots, std::size_t budget, std::size_t bytes);
|
||||
[[nodiscard]] bool current(ChunkView view) const noexcept;
|
||||
BaseDescriptor descriptor_;
|
||||
std::size_t slots_, budget_, bytes_, size_{};
|
||||
std::uint64_t revision_{};
|
||||
std::unique_ptr<Entry[]> entries_;
|
||||
};
|
||||
} // namespace evo::world
|
||||
Reference in New Issue
Block a user