Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
backing_memory.cpp
Go to the documentation of this file.
3#include <atomic>
4#include <cctype>
5#include <cstdlib>
6#include <limits>
7#include <string>
8
9// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
11 std::getenv("BB_SLOW_LOW_MEMORY") == nullptr ? false : std::string(std::getenv("BB_SLOW_LOW_MEMORY")) == "1";
12
13// Storage budget is disabled for WASM builds
14#ifndef __wasm__
15
16// Parse storage size string (e.g., "500m", "2g", "1024k")
17size_t parse_size_string(const std::string& size_str)
18{
19 if (size_str.empty()) {
20 return std::numeric_limits<size_t>::max();
21 }
22
23 try {
24 std::string str = size_str;
25
26 // Convert to lowercase for case-insensitive comparison
27 char suffix = static_cast<char>(std::tolower(static_cast<unsigned char>(str.back())));
28 size_t multiplier = 1;
29
30 // Check for unit suffix
31 if (suffix == 'k') {
32 multiplier = 1024ULL;
33 str.pop_back();
34 } else if (suffix == 'm') {
35 multiplier = 1024ULL * 1024ULL;
36 str.pop_back();
37 } else if (suffix == 'g') {
38 multiplier = 1024ULL * 1024ULL * 1024ULL;
39 str.pop_back();
40 } else if (std::isdigit(static_cast<unsigned char>(suffix)) == 0) {
41 // Invalid suffix
42 throw_or_abort("Invalid storage size format: '" + size_str + "'. Use format like '500m', '2g', or '1024k'");
43 }
44
45 // Check if remaining string is a valid number
46 if (str.empty()) {
47 throw_or_abort("Invalid storage size format: '" + size_str + "'. No numeric value provided");
48 }
49
50 size_t value = std::stoull(str);
51 return value * multiplier;
52 } catch (const std::invalid_argument&) {
53 throw_or_abort("Invalid storage size format: '" + size_str + "'. Not a valid number");
54 } catch (const std::out_of_range&) {
55 throw_or_abort("Invalid storage size format: '" + size_str + "'. Value out of range");
56 }
57}
58
59namespace {
60// Parse storage budget from environment variable (supports k/m/g suffixes like Docker)
61size_t parse_storage_budget()
62{
63 const char* env_val = std::getenv("BB_STORAGE_BUDGET");
64 if (env_val == nullptr) {
65 return std::numeric_limits<size_t>::max(); // No limit by default
66 }
67
68 return parse_size_string(std::string(env_val));
69}
70} // namespace
71
72// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
73size_t storage_budget = parse_storage_budget();
74
75// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
76std::atomic<size_t> current_storage_usage{ 0 };
77
78#endif // __wasm__
size_t parse_size_string(const std::string &size_str)
std::atomic< size_t > current_storage_usage
bool slow_low_memory
size_t storage_budget
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
void throw_or_abort(std::string const &err)