#pragma once #include #include #include #include #include namespace ReloadableHost { /// Aggregate retained-resource permits shared by candidate and retired /// generations. These are host-owned budgets, not a total-process RSS /// promise: mapped images, stacks and kernel buffers are accounted /// separately by the platform. struct ResourceLimits { std::size_t max_total_bytes{256ULL * 1024ULL * 1024ULL}; std::size_t max_generation_bytes{64ULL * 1024ULL * 1024ULL}; std::size_t max_generations{4}; }; struct ResourceUsage { std::size_t total_bytes{0}; std::size_t generations{0}; }; /// Thread-safe byte/generation ledger. /// /// `reserve` is admission: it rejects an over-budget generation with /// `exhausted` before any publication happens. A generation stays reserved /// until every pin on it is gone and the host releases it, so budget /// saturation is observable and recoverable rather than silent eviction. class ResourceLedger { public: explicit ResourceLedger(ResourceLimits limits = {}); [[nodiscard]] Void::Result::Result reserve(std::uint64_t generation, std::size_t bytes); /// Release one reserved generation. False when it was not held. bool release(std::uint64_t generation) noexcept; [[nodiscard]] bool holds(std::uint64_t generation) const noexcept; [[nodiscard]] ResourceUsage usage() const noexcept; /// High-water mark, for retained-resource and RSS reporting. [[nodiscard]] ResourceUsage peak_usage() const noexcept; [[nodiscard]] const ResourceLimits& limits() const noexcept { return m_limits; } private: mutable std::mutex m_mutex; ResourceLimits m_limits; std::map m_reserved; std::size_t m_total_bytes{0}; ResourceUsage m_peak{}; }; } // namespace ReloadableHost