Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
source_budget.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <cstddef>
6#include <cstdint>
7#include <expected>
8#include <memory>
9#include <memory_resource>
10#include <mutex>
11
12namespace portals {
39
42 std::size_t bytes{};
43 std::size_t pages{};
44 std::size_t work{};
45 std::size_t consumers{};
46};
47
50 std::size_t maxBytes{8 * 1024 * 1024};
51 std::size_t maxPages{32};
52 std::size_t maxWork{4};
53 std::size_t maxConsumers{8};
54};
55
56class SourceBudget;
57template <class T> class Publisher;
58
59namespace detail {
60struct SourceCapacityState;
61}
62
65public:
66 SourceCredit() = default;
69 SourceCredit(SourceCredit &&other) noexcept;
74 SourceCredit(const SourceCredit &) = delete;
78 std::expected<SourceCredit, SourceError> split(SourceBudgetUsage amount) noexcept;
80 SourceBudgetUsage usage() const noexcept;
81
82private:
83 friend class SourceBudget;
84 SourceCredit(std::shared_ptr<SourceBudget> root, SourceBudgetUsage amount) noexcept;
85 std::shared_ptr<SourceBudget> root_;
86 SourceBudgetUsage amount_;
87};
88
91public:
102 std::expected<SourceCredit, SourceError> reserve(SourceBudgetUsage amount, SourceBudgetUsage peak);
106 std::expected<void, SourceError> arm(SourceBudgetUsage peak);
108 bool takeReady() noexcept;
110 void disarm() noexcept;
111
112private:
113 friend class SourceBudget;
114 explicit SourceCapacityObserver(std::shared_ptr<detail::SourceCapacityState> state) noexcept;
115 std::shared_ptr<detail::SourceCapacityState> state_;
116};
117
119class SourceBudget : public std::enable_shared_from_this<SourceBudget> {
120public:
121 static constexpr std::size_t ROOT_METADATA_BYTES =
122 4096;
123 static constexpr std::size_t CAPACITY_OBSERVER_BYTES = 4096;
127 static std::expected<std::shared_ptr<SourceBudget>, SourceError> create(SourceBudgetOptions options = {});
131 std::expected<SourceCredit, SourceError> reserve(SourceBudgetUsage amount);
135 std::expected<SourceCapacityObserver, SourceError> observeCapacity(Publisher<int> wake);
137 std::uint64_t capacityEpoch() const noexcept;
141 SourceBudgetOptions options() const noexcept;
142
143private:
144 friend class SourceCredit;
145 friend class SourceCapacityObserver;
146 friend struct detail::SourceCapacityState;
147 std::shared_ptr<detail::SourceCapacityState> eligibleObserverLocked() noexcept;
148 void removeObserver(detail::SourceCapacityState &state) noexcept;
149 void disarmObserver(detail::SourceCapacityState &state) noexcept;
150 std::expected<void, SourceError> armObserver(detail::SourceCapacityState &state, SourceBudgetUsage peak);
151 std::expected<SourceCredit, SourceError> reserveObserved(detail::SourceCapacityState &state, SourceBudgetUsage amount,
152 SourceBudgetUsage peak);
153 explicit SourceBudget(SourceBudgetOptions options);
154 void release(SourceBudgetUsage amount) noexcept;
155 SourceBudgetOptions options_;
156 mutable std::mutex mutex_;
157 SourceBudgetUsage used_;
158 detail::SourceCapacityState *observers_{};
159 detail::SourceCapacityState *granted_{};
160 std::size_t observerCount_{};
161 std::uint64_t capacityEpoch_{};
162};
163
168class SourceArena final : public std::pmr::memory_resource {
169public:
173 static std::expected<std::shared_ptr<SourceArena>, SourceError> create(SourceCredit credit);
174 ~SourceArena() override;
176 std::size_t used() const noexcept;
178 std::size_t capacity() const noexcept;
180 static constexpr std::size_t METADATA_BYTES = 4096;
181
182private:
183 explicit SourceArena(SourceCredit credit);
184 void *do_allocate(std::size_t bytes, std::size_t alignment) override;
185 void do_deallocate(void *, std::size_t, std::size_t) override;
186 bool do_is_equal(const std::pmr::memory_resource &other) const noexcept override;
187 SourceCredit credit_;
188 std::unique_ptr<std::byte[]> storage_;
189 std::size_t capacity_{};
190 std::size_t used_{};
191};
192} // namespace portals
Copyable, worker-safe endpoint. It owns the closed channel state, never UI captures....
Definition scheduler.hpp:189
std::size_t used() const noexcept
static std::expected< std::shared_ptr< SourceArena >, SourceError > create(SourceCredit credit)
~SourceArena() override
Release backing before returning its credit.
static constexpr std::size_t METADATA_BYTES
Conservative per-allocation envelope for arena/control-block metadata.
Definition source_budget.hpp:180
std::size_t capacity() const noexcept
static constexpr std::size_t ROOT_METADATA_BYTES
Bootstrap ledger allowance, retained for the root lifetime.
Definition source_budget.hpp:121
static constexpr std::size_t CAPACITY_OBSERVER_BYTES
Precharged allowance for one capacity observer.
Definition source_budget.hpp:123
SourceBudgetUsage usage() const
std::expected< SourceCapacityObserver, SourceError > observeCapacity(Publisher< int > wake)
SourceBudgetOptions options() const noexcept
static std::expected< std::shared_ptr< SourceBudget >, SourceError > create(SourceBudgetOptions options={})
std::uint64_t capacityEpoch() const noexcept
std::expected< SourceCredit, SourceError > reserve(SourceBudgetUsage amount)
~SourceCapacityObserver()
Unregister without allocation or callbacks into UI.
SourceCapacityObserver & operator=(SourceCapacityObserver &&other) noexcept
std::expected< SourceCredit, SourceError > reserve(SourceBudgetUsage amount, SourceBudgetUsage peak)
void disarm() noexcept
Drop pending admission priority; no UI callback or worker join occurs.
std::expected< void, SourceError > arm(SourceBudgetUsage peak)
SourceCapacityObserver()=default
Empty observer; admission returns CLOSED.
SourceCapacityObserver(SourceCapacityObserver &&other) noexcept
Move-only credit; backing storage must be destroyed before this final credit owner.
Definition source_budget.hpp:64
SourceCredit(SourceCredit &&other) noexcept
std::expected< SourceCredit, SourceError > split(SourceBudgetUsage amount) noexcept
SourceCredit()=default
Empty credit.
SourceCredit & operator=(SourceCredit other) noexcept
SourceBudgetUsage usage() const noexcept
~SourceCredit()
Return the reservation to its root.
Public Portals API.
Definition action.hpp:8
@ CALLBACK_FAILED
A non-allocation exception escaped into a runner callback boundary.
Definition application.hpp:34
@ NO_MEMORY
Allocation failed at a runner exception boundary.
Definition application.hpp:33
@ CLOSED
Owner, node scope, scheduler or completion endpoint is closed.
Definition async.hpp:28
@ WRONG_THREAD
The operation or coroutine frame belongs to another thread.
Definition async.hpp:27
@ CANCELLED
Explicit cancellation or replacement stopped the invocation.
Definition async.hpp:29
@ WOULD_BLOCK
Active resume, invocation, registration or queue capacity rejects the operation.
Definition async.hpp:30
@ IDENTITY_EXHAUSTED
Invocation identity cannot advance without reuse.
Definition async.hpp:33
@ INVALID_ARGUMENT
Empty task, callback or immutable data owner, or invalid policy.
Definition async.hpp:26
@ UNSUPPORTED
The application explicitly excludes this annotated phase.
Definition key_binding.hpp:44
@ STALE_SOURCE
Result does not have the exact requested source revision.
Definition model_query.hpp:16
SourceError
Source protocol and admission failures, independent of a provider's storage format.
Definition source_budget.hpp:14
@ NO_PROGRESS
A MORE reply repeated its input gap.
Definition source_budget.hpp:32
@ IO_ERROR
Native source I/O failed; a typed adapter retains its per-operation cause.
Definition source_budget.hpp:37
@ INVALID_CURSOR
The provider cannot validate this opaque token.
Definition source_budget.hpp:25
@ CURSOR_CYCLE
A MORE reply repeated an earlier gap of this bounded fill.
Definition source_budget.hpp:33
@ CONTINUATION_LIMIT
Demand remains after four automatic page loads; partial pin is valid.
Definition source_budget.hpp:34
@ ROW_TOO_LARGE
One materialized row exceeds its inline/transitive byte limit.
Definition source_budget.hpp:29
@ INVALID_REPLY
Result shape, identities, revision or boundaries violate the protocol.
Definition source_budget.hpp:31
@ CONTEXT_ERROR
The tree rejected a stale/foreign context or invalidation.
Definition source_budget.hpp:22
@ REVISION_CHANGED
Requested provider revision is unavailable or a notification is stale.
Definition source_budget.hpp:26
@ POSITION_EXHAUSTED
Provider position representation cannot advance without wrapping.
Definition source_budget.hpp:27
@ PAGE_TOO_LARGE
The fixed page/workspace cannot hold the requested allocation.
Definition source_budget.hpp:30
@ ROW_NOT_FOUND
A supported stable-identity lookup did not find its row.
Definition source_budget.hpp:28
@ ALREADY_COMPLETED
This request generation already ran its single load.
Definition source_budget.hpp:36
Configurable root shared by typed pages, future text pages and replacement sessions.
Definition source_budget.hpp:49
std::size_t maxPages
Aggregate page allocations.
Definition source_budget.hpp:51
std::size_t maxWork
Aggregate work reservations.
Definition source_budget.hpp:52
std::size_t maxConsumers
Aggregate consumer reservations.
Definition source_budget.hpp:53
std::size_t maxBytes
Aggregate reserved capacity.
Definition source_budget.hpp:50
Simultaneous source reservations; bytes are requested capacities, not process RSS.
Definition source_budget.hpp:41
std::size_t work
Work owners, including cancelled but still live work.
Definition source_budget.hpp:44
std::size_t pages
Building, queued, cached and retained page allocations.
Definition source_budget.hpp:43
std::size_t bytes
Reserved bytes, including accounting allowances.
Definition source_budget.hpp:42
std::size_t consumers
Live consumer storage reservations.
Definition source_budget.hpp:45