/
githubmirror
/
bitcoin
Обзор
Документация
Войти
/
githubmirror
/
bitcoin
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/common/system.cpp
125 строк
3 KB
kevkevinpal
windows: remove deprecated codecvt via UTF-8 narrow APIs
07 авг 2026, 02:20
Не верифицирован
07 авг 2026, 02:20
6b6d77c
Код
Авторство
О чём код?
// Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-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. #include <bitcoin-build-config.h> // IWYU pragma: keep #include <common/system.h> #include <util/log.h> #include <util/string.h> #include <util/time.h> #ifdef WIN32 #include <compat/compat.h> #include <util/check.h> #include <windows.h> #else #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #endif #ifdef HAVE_MALLOPT_ARENA_MAX #include <malloc.h> #endif #include <cstdint> #include <cstdlib> #include <locale> #include <optional> #include <stdexcept> #include <string> #include <thread> using util::ReplaceAll; #ifndef WIN32 std::string ShellEscape(const std::string& arg) { std::string escaped = arg; ReplaceAll(escaped, "'", "'\"'\"'"); return "'" + escaped + "'"; } #endif #if HAVE_SYSTEM void runCommand(const std::string& strCommand) { if (strCommand.empty()) return; int nErr = ::system(strCommand.c_str()); if (nErr) { LogWarning("runCommand error: system(%s) returned %d", strCommand, nErr); } } #endif void SetupEnvironment() { #ifdef HAVE_MALLOPT_ARENA_MAX // glibc-specific: On 32-bit systems set the number of arenas to 1. // By default, since glibc 2.10, the C library will create up to two heap // arenas per core. This is known to cause excessive virtual address space // usage in our usage. Work around it by setting the maximum number of // arenas to 1. if (sizeof(void*) == 4) { mallopt(M_ARENA_MAX, 1); } #endif // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale // may be invalid, in which case the "C.UTF-8" locale is used as fallback. #if !defined(WIN32) && !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) try { std::locale(""); // Raises a runtime error if current locale is invalid } catch (const std::runtime_error&) { setenv("LC_ALL", "C.UTF-8", 1); } #elif defined(WIN32) assert(GetACP() == CP_UTF8); // Set the default input/output charset is utf-8 SetConsoleCP(CP_UTF8); SetConsoleOutputCP(CP_UTF8); #endif #ifndef WIN32 constexpr mode_t private_umask = 0077; umask(private_umask); #endif } bool SetupNetworking() { #ifdef WIN32 // Initialize Windows Sockets WSADATA wsadata; int ret = WSAStartup(MAKEWORD(2,2), &wsadata); if (ret != NO_ERROR || LOBYTE(wsadata.wVersion ) != 2 || HIBYTE(wsadata.wVersion) != 2) return false; #endif return true; } int GetNumCores() { return std::thread::hardware_concurrency(); } std::optional<uint64_t> TryGetTotalRam() { static const auto total_ram{[]() -> std::optional<uint64_t> { #ifdef WIN32 if (MEMORYSTATUSEX m{}; (m.dwLength = sizeof(m), GlobalMemoryStatusEx(&m))) return m.ullTotalPhys; #elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__illumos__) || defined(__linux__) if (long p{sysconf(_SC_PHYS_PAGES)}, s{sysconf(_SC_PAGESIZE)}; p > 0 && s > 0) return 1ULL * p * s; #endif return std::nullopt; }()}; return total_ram; } namespace { const auto g_startup_time{SteadyClock::now()}; } // namespace SteadyClock::duration GetUptime() { return SteadyClock::now() - g_startup_time; }