Skip to content
Bitcoin Roots

v29.4-roots.4 · Commit 2: [PATCH 02/37] runtime: preserve shared operator controls

src/util/byte_units.h

Before: src/util/byte_units.h · After: src/util/byte_units.h

File metadata
index 3d6a9627d88850a5369ce9d1b10289b625b63e82..08fa8612a584171a1ca9915ffa869bbb9ae9efb6 100644

Download original patch · Release notes

Diff rows 1–37 of 37. Line numbers refer to the original before/after files.

+ Added− RemovedUnmarked lines are context.

Scroll the code area horizontally to read long lines. The two line-number columns refer to before and after.

src/util/byte_units.h CHANGED
@@ -7,16 +7,32 @@
7
7
 
8
8
  #include <util/overflow.h>
9
9
 
10
+ #include <cstdint>
11
+ #include <limits>
10
12
  #include <stdexcept>
11
13
 
12
- //! Overflow-safe conversion of MiB to bytes.
13
- constexpr size_t operator""_MiB(unsigned long long mebibytes)
14
+ namespace util::detail {
15
+ template <unsigned SHIFT>
16
+ consteval uint64_t ByteUnitsToBytes(unsigned long long units)
14
17
  {
15
- auto bytes{CheckedLeftShift(mebibytes, 20)};
16
- if (!bytes || *bytes > std::numeric_limits<size_t>::max()) {
17
- throw std::overflow_error("MiB value too large for size_t byte conversion");
18
+ const auto bytes{CheckedLeftShift(units, SHIFT)};
19
+ if (!bytes || *bytes > std::numeric_limits<uint64_t>::max()) {
20
+ throw std::overflow_error("Too large");
18
21
  }
19
22
  return *bytes;
20
23
  }
24
+ } // namespace util::detail
25
+
26
+ /// Conversion of MiB to bytes.
27
+ consteval uint64_t operator""_MiB(unsigned long long mebibytes)
28
+ {
29
+ return util::detail::ByteUnitsToBytes<20>(mebibytes);
30
+ }
31
+
32
+ /// Conversion of GiB to bytes.
33
+ consteval uint64_t operator""_GiB(unsigned long long gibibytes)
34
+ {
35
+ return util::detail::ByteUnitsToBytes<30>(gibibytes);
36
+ }
21
37
 
22
38
  #endif // BITCOIN_UTIL_BYTE_UNITS_H