v29.4-roots.3 · Commit 2: [PATCH 02/30] 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. Before is on the left; after is on the right. Unified view fits narrow screens more easily.
src/util/byte_units.h
CHANGED
|
@@ -7,16 +7,32 @@
|
|
| 7 |
|
| 8 |
#include <util/overflow.h>
|
| 9 |
|
|
|
|
|
|
|
| 10 |
#include <stdexcept>
|
| 11 |
|
| 12 |
-
//! Overflow-safe conversion of MiB to bytes.
|
| 13 |
-
constexpr size_t operator""_MiB(unsigned long long mebibytes)
|
|
|
|
| 14 |
{
|
| 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 |
}
|
| 19 |
return *bytes;
|
| 20 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
#endif // BITCOIN_UTIL_BYTE_UNITS_H
|
|
|
|
| 7 |
|
| 8 |
#include <util/overflow.h>
|
| 9 |
|
| 10 |
+
#include <cstdint>
|
| 11 |
+
#include <limits>
|
| 12 |
#include <stdexcept>
|
| 13 |
|
| 14 |
+
namespace util::detail {
|
| 15 |
+
template <unsigned SHIFT>
|
| 16 |
+
consteval uint64_t ByteUnitsToBytes(unsigned long long units)
|
| 17 |
{
|
| 18 |
+
const auto bytes{CheckedLeftShift(units, SHIFT)};
|
| 19 |
+
if (!bytes || *bytes > std::numeric_limits<uint64_t>::max()) {
|
| 20 |
+
throw std::overflow_error("Too large");
|
| 21 |
}
|
| 22 |
return *bytes;
|
| 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 |
+
}
|
| 37 |
|
| 38 |
#endif // BITCOIN_UTIL_BYTE_UNITS_H
|