mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-04 10:12:28 +02:00
util: Return uint64_t from _MiB and _GiB operators
This commit is contained in:
@@ -30,7 +30,7 @@ static constexpr size_t MAX_32BIT_DBCACHE{1_GiB};
|
|||||||
//! Larger default dbcache on 64-bit systems with enough RAM.
|
//! Larger default dbcache on 64-bit systems with enough RAM.
|
||||||
static constexpr size_t HIGH_DEFAULT_DBCACHE{1_GiB};
|
static constexpr size_t HIGH_DEFAULT_DBCACHE{1_GiB};
|
||||||
//! Minimum detected RAM required for HIGH_DEFAULT_DBCACHE.
|
//! Minimum detected RAM required for HIGH_DEFAULT_DBCACHE.
|
||||||
static constexpr uint64_t HIGH_DEFAULT_DBCACHE_MIN_TOTAL_RAM{uint64_t{4} * 1_GiB};
|
static constexpr uint64_t HIGH_DEFAULT_DBCACHE_MIN_TOTAL_RAM{4_GiB};
|
||||||
|
|
||||||
namespace node {
|
namespace node {
|
||||||
size_t GetDefaultDBCache()
|
size_t GetDefaultDBCache()
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
#include <test/util/setup_common.h>
|
#include <test/util/setup_common.h>
|
||||||
#include <test/util/time.h>
|
#include <test/util/time.h>
|
||||||
#include <uint256.h>
|
#include <uint256.h>
|
||||||
|
#include <univalue.h>
|
||||||
#include <util/bitdeque.h>
|
#include <util/bitdeque.h>
|
||||||
#include <util/byte_units.h>
|
#include <util/byte_units.h>
|
||||||
#include <util/fs.h>
|
#include <util/fs.h>
|
||||||
@@ -36,7 +37,7 @@
|
|||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <univalue.h>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -1827,6 +1828,9 @@ BOOST_AUTO_TEST_CASE(saturating_left_shift_test)
|
|||||||
TestSaturatingLeftShift<int64_t>();
|
TestSaturatingLeftShift<int64_t>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <class Int, auto bytes>
|
||||||
|
concept BraceInitializesTo = requires { Int{bytes}; };
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(mib_string_literal_test)
|
BOOST_AUTO_TEST_CASE(mib_string_literal_test)
|
||||||
{
|
{
|
||||||
// Basic equivalences and simple arithmetic operations
|
// Basic equivalences and simple arithmetic operations
|
||||||
@@ -1850,16 +1854,12 @@ BOOST_AUTO_TEST_CASE(mib_string_literal_test)
|
|||||||
BOOST_CHECK_EQUAL(128_MiB, 0x8000000U);
|
BOOST_CHECK_EQUAL(128_MiB, 0x8000000U);
|
||||||
BOOST_CHECK_EQUAL(550_MiB, 550ULL * 1024 * 1024);
|
BOOST_CHECK_EQUAL(550_MiB, 550ULL * 1024 * 1024);
|
||||||
|
|
||||||
// Overflow handling
|
// 4095 MiB fits in uint32_t bytes. 4096 MiB requires the uint64_t return type.
|
||||||
constexpr auto max_mib{std::numeric_limits<size_t>::max() >> 20};
|
static_assert(BraceInitializesTo<uint32_t, 4095_MiB>);
|
||||||
if constexpr (SIZE_MAX == UINT32_MAX) {
|
static_assert(!BraceInitializesTo<uint32_t, 4096_MiB>);
|
||||||
BOOST_CHECK_EQUAL(max_mib, 4095U);
|
static_assert(BraceInitializesTo<uint64_t, 4096_MiB>);
|
||||||
BOOST_CHECK_EQUAL(4095_MiB, size_t{4095} << 20);
|
BOOST_CHECK_EQUAL(4095_MiB, uint32_t{4095} << 20);
|
||||||
BOOST_CHECK_EXCEPTION(4096_MiB, std::overflow_error, HasReason("MiB value too large for size_t byte conversion"));
|
BOOST_CHECK_EQUAL(4096_MiB, uint64_t{4096} << 20);
|
||||||
} else {
|
|
||||||
BOOST_CHECK_EQUAL(4096_MiB, size_t{4096} << 20);
|
|
||||||
}
|
|
||||||
BOOST_CHECK_EXCEPTION(operator""_MiB(max_mib + 1), std::overflow_error, HasReason("MiB value too large for size_t byte conversion"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(ceil_div_test)
|
BOOST_AUTO_TEST_CASE(ceil_div_test)
|
||||||
@@ -1916,20 +1916,18 @@ BOOST_AUTO_TEST_CASE(gib_string_literal_test)
|
|||||||
BOOST_CHECK_EQUAL(3_GiB / 1_GiB, 3U);
|
BOOST_CHECK_EQUAL(3_GiB / 1_GiB, 3U);
|
||||||
BOOST_CHECK_EQUAL(3_GiB, 3U << 30);
|
BOOST_CHECK_EQUAL(3_GiB, 3U << 30);
|
||||||
|
|
||||||
// Overflow handling and specific codebase values
|
// 3 GiB fits in uint32_t bytes. 4 GiB requires the uint64_t return type.
|
||||||
constexpr auto max_gib{std::numeric_limits<size_t>::max() >> 30};
|
static_assert(BraceInitializesTo<uint32_t, 3_GiB>);
|
||||||
if constexpr (SIZE_MAX == UINT32_MAX) {
|
static_assert(!BraceInitializesTo<uint32_t, 4_GiB>);
|
||||||
BOOST_CHECK_EQUAL(max_gib, 3U);
|
static_assert(BraceInitializesTo<uint64_t, 4_GiB>);
|
||||||
BOOST_CHECK_EXCEPTION(4_GiB, std::overflow_error, HasReason("GiB value too large for size_t byte conversion"));
|
BOOST_CHECK_EQUAL(3_GiB, uint32_t{3} << 30);
|
||||||
} else {
|
BOOST_CHECK_EQUAL(4_GiB, uint64_t{4} << 30);
|
||||||
BOOST_CHECK_GT(max_gib, 3U);
|
|
||||||
BOOST_CHECK_EQUAL(4_GiB, size_t{4} << 30);
|
// Specific codebase values
|
||||||
BOOST_CHECK_EQUAL(4_GiB, 4096_MiB);
|
BOOST_CHECK_EQUAL(4_GiB, 4096_MiB);
|
||||||
BOOST_CHECK_EQUAL(8_GiB, 8192_MiB);
|
BOOST_CHECK_EQUAL(8_GiB, 8192_MiB);
|
||||||
BOOST_CHECK_EQUAL(16_GiB, 16384_MiB);
|
BOOST_CHECK_EQUAL(16_GiB, 16384_MiB);
|
||||||
BOOST_CHECK_EQUAL(32_GiB, 32768_MiB);
|
BOOST_CHECK_EQUAL(32_GiB, 32768_MiB);
|
||||||
}
|
|
||||||
BOOST_CHECK_EXCEPTION(operator""_GiB(max_gib + 1), std::overflow_error, HasReason("GiB value too large for size_t byte conversion"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|||||||
@@ -12,26 +12,26 @@
|
|||||||
|
|
||||||
namespace util::detail {
|
namespace util::detail {
|
||||||
template <unsigned SHIFT>
|
template <unsigned SHIFT>
|
||||||
constexpr size_t ByteUnitsToBytes(unsigned long long units, const char* exception_msg)
|
consteval uint64_t ByteUnitsToBytes(unsigned long long units)
|
||||||
{
|
{
|
||||||
const auto bytes{CheckedLeftShift(units, SHIFT)};
|
const auto bytes{CheckedLeftShift(units, SHIFT)};
|
||||||
if (!bytes || *bytes > std::numeric_limits<size_t>::max()) {
|
if (!bytes || *bytes > std::numeric_limits<uint64_t>::max()) {
|
||||||
throw std::overflow_error(exception_msg);
|
throw std::overflow_error("Too large");
|
||||||
}
|
}
|
||||||
return *bytes;
|
return *bytes;
|
||||||
}
|
}
|
||||||
} // namespace util::detail
|
} // namespace util::detail
|
||||||
|
|
||||||
//! Overflow-safe conversion of MiB to bytes.
|
/// Conversion of MiB to bytes.
|
||||||
constexpr size_t operator""_MiB(unsigned long long mebibytes)
|
consteval uint64_t operator""_MiB(unsigned long long mebibytes)
|
||||||
{
|
{
|
||||||
return util::detail::ByteUnitsToBytes<20>(mebibytes, "MiB value too large for size_t byte conversion");
|
return util::detail::ByteUnitsToBytes<20>(mebibytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Overflow-safe conversion of GiB to bytes.
|
/// Conversion of GiB to bytes.
|
||||||
constexpr size_t operator""_GiB(unsigned long long gibibytes)
|
consteval uint64_t operator""_GiB(unsigned long long gibibytes)
|
||||||
{
|
{
|
||||||
return util::detail::ByteUnitsToBytes<30>(gibibytes, "GiB value too large for size_t byte conversion");
|
return util::detail::ByteUnitsToBytes<30>(gibibytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // BITCOIN_UTIL_BYTE_UNITS_H
|
#endif // BITCOIN_UTIL_BYTE_UNITS_H
|
||||||
|
|||||||
Reference in New Issue
Block a user