Files
bitcoin/src/kernel/caches.h
MarcoFalke fabafd91f1 refactor: Use u64 over size_t for all cache sizes to fix a 32-bit overflow
This is a refactor on 64-bit systems, because size_t is equal to u64.

However, on 32-bit systems, it fixes an integer overflow while calculating the cache sizes:

src/node/caches.cpp:71:49: runtime error: unsigned integer overflow: 471859200 * 10 cannot be represented in type size_t (aka "unsigned int")

This happens while multiplying the default cache size (450MiB) by 10:

index_sizes.tx_index = std::min(total_cache * 10 / 100, ...)
                                ^^^^^^^^^^^^^^^^

The issue was introduced in commit d06dabf26b.

====

Also, add missing includes in touched files, according to IWYU.
2026-07-08 16:39:18 +02:00

41 lines
1.3 KiB
C++

// Copyright (c) 2024-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_KERNEL_CACHES_H
#define BITCOIN_KERNEL_CACHES_H
#include <util/byte_units.h>
#include <algorithm>
#include <cstdint>
//! Suggested default amount of cache reserved for the kernel (bytes)
static constexpr uint64_t DEFAULT_KERNEL_CACHE{450_MiB};
//! Default LevelDB write batch size
static constexpr uint64_t DEFAULT_DB_CACHE_BATCH{32_MiB};
//! Max memory allocated to block tree DB specific cache (bytes)
static constexpr uint64_t MAX_BLOCK_DB_CACHE{2_MiB};
//! Max memory allocated to coin DB specific cache (bytes)
static constexpr uint64_t MAX_COINS_DB_CACHE{8_MiB};
namespace kernel {
struct CacheSizes {
uint64_t block_tree_db;
uint64_t coins_db;
uint64_t coins;
CacheSizes(uint64_t total_cache)
{
block_tree_db = std::min(total_cache / 8, MAX_BLOCK_DB_CACHE);
total_cache -= block_tree_db;
coins_db = std::min(total_cache / 2, MAX_COINS_DB_CACHE);
total_cache -= coins_db;
coins = total_cache; // the rest goes to the coins cache
}
};
} // namespace kernel
#endif // BITCOIN_KERNEL_CACHES_H