A11 (C++ runtime)
Native C++ implementation of the A11 streaming action runtime
Loading...
Searching...
No Matches
limits.h
Go to the documentation of this file.
1// Copyright 2026 The A11 Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef A11_FLOW_LIMITS_H_
16#define A11_FLOW_LIMITS_H_
17
18#include <algorithm>
19#include <chrono>
20#include <cstddef>
21
22namespace a11::flow {
25 size_t max_modules = 256;
26 size_t max_source_bytes = 8 * 1024 * 1024;
27 size_t max_depth = 128;
28 size_t max_nesting = 128;
29 size_t max_tokens = 250000;
30 size_t max_work = 2000000;
31 size_t max_linked_name_bytes = 64 * 1024 * 1024;
32 size_t max_duration_ms = 5000;
33};
34
37 public:
39 : remaining_(limits.max_work),
40 remaining_tokens_(limits.max_tokens),
41 deadline_(std::chrono::steady_clock::now() +
42 std::chrono::milliseconds(std::min<size_t>(
43 limits.max_duration_ms, 24 * 60 * 60 * 1000))) {}
44
45 bool Check(size_t work = 1) {
46 if (exhausted_) {
47 return false;
48 }
49 if (work > remaining_ || std::chrono::steady_clock::now() >= deadline_) {
50 exhausted_ = true;
51 return false;
52 }
53 remaining_ -= work;
54 return true;
55 }
56
58 bool TryOptional(size_t work = 1) {
59 if (exhausted_ || work > remaining_ ||
60 std::chrono::steady_clock::now() >= deadline_) {
61 return false;
62 }
63 remaining_ -= work;
64 return true;
65 }
66
67 bool ConsumeTokens(size_t count) {
68 if (count > remaining_tokens_) {
69 exhausted_ = true;
70 } else {
71 remaining_tokens_ -= count;
72 }
73 return Check(0);
74 }
75
76 bool exhausted() const { return exhausted_; }
77
78 private:
79 size_t remaining_;
80 size_t remaining_tokens_;
81 std::chrono::steady_clock::time_point deadline_;
82 bool exhausted_ = false;
83};
84} // namespace a11::flow
85#endif // A11_FLOW_LIMITS_H_
One cooperative deadline and work counter for a compilation request.
Definition limits.h:36
CompilationBudget(const CompilationLimits &limits)
Definition limits.h:38
bool exhausted() const
Definition limits.h:76
bool TryOptional(size_t work=1)
Charge optional work without invalidating an already valid baseline.
Definition limits.h:58
bool Check(size_t work=1)
Definition limits.h:45
bool ConsumeTokens(size_t count)
Definition limits.h:67
CompilationLimits limits
Definition main.cc:148
Definition catalogue.cc:29
Host-controlled bounds shared by parsing, linking and semantic analysis.
Definition limits.h:24
size_t max_depth
Definition limits.h:27
size_t max_source_bytes
Definition limits.h:26
size_t max_nesting
Definition limits.h:28
size_t max_tokens
Definition limits.h:29
size_t max_modules
Definition limits.h:25
size_t max_linked_name_bytes
Definition limits.h:31
size_t max_duration_ms
Definition limits.h:32
size_t max_work
Definition limits.h:30