diff --git a/base/allocator/partition_allocator/partition_alloc_unittest.cc b/base/allocator/partition_allocator/partition_alloc_unittest.cc index a4b02e0274689b..41bcd6e2002415 100644 --- a/base/allocator/partition_allocator/partition_alloc_unittest.cc +++ b/base/allocator/partition_allocator/partition_alloc_unittest.cc @@ -97,7 +97,7 @@ bool IsLargeMemoryDevice() { // // Set to 4GiB, since we have 2GiB Android devices where tests flakily fail // (e.g. Nexus 5X, crbug.com/1191195). - return base::SysInfo::AmountOfPhysicalMemory() >= 4000ULL * 1024 * 1024; + return base::SysInfo::AmountOfPhysicalMemory() >= 4000LL * 1024 * 1024; } bool SetAddressSpaceLimit() { diff --git a/base/process/process_metrics_unittest.cc b/base/process/process_metrics_unittest.cc index 4a589f120619f3..8ab3e9b13e0ecf 100644 --- a/base/process/process_metrics_unittest.cc +++ b/base/process/process_metrics_unittest.cc @@ -207,11 +207,11 @@ TEST_F(SystemMetricsTest, ParseMeminfo) { EXPECT_EQ(meminfo.shmem, 140204); EXPECT_EQ(meminfo.slab, 54212); #endif - EXPECT_EQ(355725u, + EXPECT_EQ(355725, base::SysInfo::AmountOfAvailablePhysicalMemory(meminfo) / 1024); // Simulate as if there is no MemAvailable. meminfo.available = 0; - EXPECT_EQ(374448u, + EXPECT_EQ(374448, base::SysInfo::AmountOfAvailablePhysicalMemory(meminfo) / 1024); meminfo = {}; EXPECT_TRUE(ParseProcMeminfo(valid_input2, &meminfo)); @@ -223,7 +223,7 @@ TEST_F(SystemMetricsTest, ParseMeminfo) { EXPECT_EQ(meminfo.swap_total, 524280); EXPECT_EQ(meminfo.swap_free, 524200); EXPECT_EQ(meminfo.dirty, 4); - EXPECT_EQ(69936u, + EXPECT_EQ(69936, base::SysInfo::AmountOfAvailablePhysicalMemory(meminfo) / 1024); } diff --git a/base/profiler/stack_copier_signal.cc b/base/profiler/stack_copier_signal.cc index bc2ecb0e0c72c0..1cf08fc6e6deeb 100644 --- a/base/profiler/stack_copier_signal.cc +++ b/base/profiler/stack_copier_signal.cc @@ -37,7 +37,7 @@ class AsyncSafeWaitableEvent { // futex() can wake up spuriously if this memory address was previously used // for a pthread mutex. So, also check the condition. while (true) { - long res = + int res = syscall(SYS_futex, futex_int_ptr(), FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, nullptr, nullptr, 0); if (futex_.load(std::memory_order_acquire) != 0) diff --git a/base/sync_socket_posix.cc b/base/sync_socket_posix.cc index 1b75de7b23f8f9..92d47fc0897fea 100644 --- a/base/sync_socket_posix.cc +++ b/base/sync_socket_posix.cc @@ -23,7 +23,6 @@ #include "base/check_op.h" #include "base/containers/span.h" #include "base/files/file_util.h" -#include "base/numerics/safe_conversions.h" #include "base/threading/scoped_blocking_call.h" #include "build/build_config.h" @@ -175,7 +174,8 @@ size_t SyncSocket::Peek() { // If there is an error in ioctl, signal that the channel would block. return 0; } - return checked_cast(number_chars); + DCHECK_GE(number_chars, 0); + return number_chars; } bool SyncSocket::IsValid() const { diff --git a/base/system/sys_info.cc b/base/system/sys_info.cc index 7b068fb13de845..a4c99975439051 100644 --- a/base/system/sys_info.cc +++ b/base/system/sys_info.cc @@ -23,21 +23,21 @@ namespace base { namespace { #if BUILDFLAG(IS_IOS) // For M99, 45% of devices have 2GB of RAM, and 55% have more. -constexpr uint64_t kLowMemoryDeviceThresholdMB = 1024; +constexpr int64_t kLowMemoryDeviceThresholdMB = 1024; #else // Updated Desktop default threshold to match the Android 2021 definition. -constexpr uint64_t kLowMemoryDeviceThresholdMB = 2048; +constexpr int64_t kLowMemoryDeviceThresholdMB = 2048; #endif } // namespace // static -uint64_t SysInfo::AmountOfPhysicalMemory() { +int64_t SysInfo::AmountOfPhysicalMemory() { if (base::CommandLine::ForCurrentProcess()->HasSwitch( switches::kEnableLowEndDeviceMode)) { // Keep using 512MB as the simulated RAM amount for when users or tests have // manually enabled low-end device mode. Note this value is different from // the threshold used for low end devices. - constexpr uint64_t kSimulatedMemoryForEnableLowEndDeviceMode = + constexpr int64_t kSimulatedMemoryForEnableLowEndDeviceMode = 512 * 1024 * 1024; return std::min(kSimulatedMemoryForEnableLowEndDeviceMode, AmountOfPhysicalMemoryImpl()); @@ -47,14 +47,14 @@ uint64_t SysInfo::AmountOfPhysicalMemory() { } // static -uint64_t SysInfo::AmountOfAvailablePhysicalMemory() { +int64_t SysInfo::AmountOfAvailablePhysicalMemory() { if (base::CommandLine::ForCurrentProcess()->HasSwitch( switches::kEnableLowEndDeviceMode)) { // Estimate the available memory by subtracting our memory used estimate // from the fake |kLowMemoryDeviceThresholdMB| limit. - uint64_t memory_used = + int64_t memory_used = AmountOfPhysicalMemoryImpl() - AmountOfAvailablePhysicalMemoryImpl(); - uint64_t memory_limit = kLowMemoryDeviceThresholdMB * 1024 * 1024; + int64_t memory_limit = kLowMemoryDeviceThresholdMB * 1024 * 1024; // std::min ensures no underflow, as |memory_used| can be > |memory_limit|. return memory_limit - std::min(memory_used, memory_limit); } @@ -82,8 +82,7 @@ bool DetectLowEndDevice() { return false; int ram_size_mb = SysInfo::AmountOfPhysicalMemoryMB(); - return ram_size_mb > 0 && - static_cast(ram_size_mb) <= kLowMemoryDeviceThresholdMB; + return (ram_size_mb > 0 && ram_size_mb <= kLowMemoryDeviceThresholdMB); } // static diff --git a/base/system/sys_info.h b/base/system/sys_info.h index 963eb48ffb902b..1cd1231305a372 100644 --- a/base/system/sys_info.h +++ b/base/system/sys_info.h @@ -34,19 +34,19 @@ class BASE_EXPORT SysInfo { // Return the number of bytes of physical memory on the current machine. // If low-end device mode is manually enabled via command line flag, this // will return the lesser of the actual physical memory, or 512MB. - static uint64_t AmountOfPhysicalMemory(); + static int64_t AmountOfPhysicalMemory(); // Return the number of bytes of current available physical memory on the // machine. // (The amount of memory that can be allocated without any significant // impact on the system. It can lead to freeing inactive file-backed // and/or speculative file-backed memory). - static uint64_t AmountOfAvailablePhysicalMemory(); + static int64_t AmountOfAvailablePhysicalMemory(); // Return the number of bytes of virtual memory of this process. A return // value of zero means that there is no limit on the available virtual // memory. - static uint64_t AmountOfVirtualMemory(); + static int64_t AmountOfVirtualMemory(); // Return the number of megabytes of physical memory on the current machine. static int AmountOfPhysicalMemoryMB() { @@ -215,14 +215,14 @@ class BASE_EXPORT SysInfo { FRIEND_TEST_ALL_PREFIXES(SysInfoTest, AmountOfAvailablePhysicalMemory); FRIEND_TEST_ALL_PREFIXES(debug::SystemMetricsTest, ParseMeminfo); - static uint64_t AmountOfPhysicalMemoryImpl(); - static uint64_t AmountOfAvailablePhysicalMemoryImpl(); + static int64_t AmountOfPhysicalMemoryImpl(); + static int64_t AmountOfAvailablePhysicalMemoryImpl(); static bool IsLowEndDeviceImpl(); static HardwareInfo GetHardwareInfoSync(); #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || \ BUILDFLAG(IS_AIX) - static uint64_t AmountOfAvailablePhysicalMemory( + static int64_t AmountOfAvailablePhysicalMemory( const SystemMemoryInfoKB& meminfo); #endif }; diff --git a/base/system/sys_info_fuchsia.cc b/base/system/sys_info_fuchsia.cc index c55594671a342f..25396a83bd4757 100644 --- a/base/system/sys_info_fuchsia.cc +++ b/base/system/sys_info_fuchsia.cc @@ -91,12 +91,12 @@ int64_t GetAmountOfTotalDiskSpaceAndVolumePath(const FilePath& path, } // namespace // static -uint64_t SysInfo::AmountOfPhysicalMemoryImpl() { +int64_t SysInfo::AmountOfPhysicalMemoryImpl() { return zx_system_get_physmem(); } // static -uint64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() { +int64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() { // TODO(https://crbug.com/986608): Implement this. NOTIMPLEMENTED_LOG_ONCE(); return 0; @@ -108,7 +108,7 @@ int SysInfo::NumberOfProcessors() { } // static -uint64_t SysInfo::AmountOfVirtualMemory() { +int64_t SysInfo::AmountOfVirtualMemory() { return 0; } diff --git a/base/system/sys_info_ios.mm b/base/system/sys_info_ios.mm index 2b523e5bb54670..e278a18cdd5e0c 100644 --- a/base/system/sys_info_ios.mm +++ b/base/system/sys_info_ios.mm @@ -14,7 +14,6 @@ #include "base/check_op.h" #include "base/mac/scoped_mach_port.h" #include "base/notreached.h" -#include "base/numerics/safe_conversions.h" #include "base/process/process_metrics.h" #include "base/strings/string_util.h" #include "base/strings/stringprintf.h" @@ -116,7 +115,7 @@ } // static -uint64_t SysInfo::AmountOfPhysicalMemoryImpl() { +int64_t SysInfo::AmountOfPhysicalMemoryImpl() { struct host_basic_info hostinfo; mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; base::mac::ScopedMachSendRight host(mach_host_self()); @@ -127,17 +126,17 @@ return 0; } DCHECK_EQ(HOST_BASIC_INFO_COUNT, count); - return hostinfo.max_mem; + return static_cast(hostinfo.max_mem); } // static -uint64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() { +int64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() { SystemMemoryInfoKB info; if (!GetSystemMemoryInfo(&info)) return 0; // We should add inactive file-backed memory also but there is no such // information from iOS unfortunately. - return checked_cast(info.free + info.speculative) * 1024; + return static_cast(info.free + info.speculative) * 1024; } // static diff --git a/base/system/sys_info_linux.cc b/base/system/sys_info_linux.cc index 1b304276838584..881db6f5aa19d0 100644 --- a/base/system/sys_info_linux.cc +++ b/base/system/sys_info_linux.cc @@ -24,20 +24,22 @@ namespace { -uint64_t AmountOfMemory(int pages_name) { +int64_t AmountOfMemory(int pages_name) { long pages = sysconf(pages_name); long page_size = sysconf(_SC_PAGESIZE); - if (pages < 0 || page_size < 0) + if (pages == -1 || page_size == -1) { + NOTREACHED(); return 0; - return static_cast(pages) * static_cast(page_size); + } + return static_cast(pages) * page_size; } -uint64_t AmountOfPhysicalMemory() { +int64_t AmountOfPhysicalMemory() { return AmountOfMemory(_SC_PHYS_PAGES); } base::LazyInstance< - base::internal::LazySysInfoValue>::Leaky + base::internal::LazySysInfoValue>::Leaky g_lazy_physical_memory = LAZY_INSTANCE_INITIALIZER; } // namespace @@ -45,12 +47,12 @@ base::LazyInstance< namespace base { // static -uint64_t SysInfo::AmountOfPhysicalMemoryImpl() { +int64_t SysInfo::AmountOfPhysicalMemoryImpl() { return g_lazy_physical_memory.Get().value(); } // static -uint64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() { +int64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() { SystemMemoryInfoKB info; if (!GetSystemMemoryInfo(&info)) return 0; @@ -58,16 +60,16 @@ uint64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() { } // static -uint64_t SysInfo::AmountOfAvailablePhysicalMemory( +int64_t SysInfo::AmountOfAvailablePhysicalMemory( const SystemMemoryInfoKB& info) { // See details here: // https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773 // The fallback logic (when there is no MemAvailable) would be more precise // if we had info about zones watermarks (/proc/zoneinfo). - int res_kb = info.available != 0 - ? info.available - info.active_file - : info.free + info.reclaimable + info.inactive_file; - return checked_cast(res_kb) * 1024; + int64_t res_kb = info.available != 0 + ? info.available - info.active_file + : info.free + info.reclaimable + info.inactive_file; + return res_kb * 1024; } // static diff --git a/base/system/sys_info_mac.mm b/base/system/sys_info_mac.mm index 5cc76362d8a267..dc52dea129e85c 100644 --- a/base/system/sys_info_mac.mm +++ b/base/system/sys_info_mac.mm @@ -16,7 +16,6 @@ #include "base/mac/mac_util.h" #include "base/mac/scoped_mach_port.h" #include "base/notreached.h" -#include "base/numerics/safe_conversions.h" #include "base/process/process_metrics.h" #include "base/strings/string_util.h" #include "base/strings/stringprintf.h" @@ -75,7 +74,7 @@ } // static -uint64_t SysInfo::AmountOfPhysicalMemoryImpl() { +int64_t SysInfo::AmountOfPhysicalMemoryImpl() { struct host_basic_info hostinfo; mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; base::mac::ScopedMachSendRight host(mach_host_self()); @@ -86,17 +85,17 @@ return 0; } DCHECK_EQ(HOST_BASIC_INFO_COUNT, count); - return hostinfo.max_mem; + return static_cast(hostinfo.max_mem); } // static -uint64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() { +int64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() { SystemMemoryInfoKB info; if (!GetSystemMemoryInfo(&info)) return 0; // We should add inactive file-backed memory also but there is no such // information from Mac OS unfortunately. - return checked_cast(info.free + info.speculative) * 1024; + return static_cast(info.free + info.speculative) * 1024; } // static diff --git a/base/system/sys_info_openbsd.cc b/base/system/sys_info_openbsd.cc index a32f06ee3d204d..8849960622414a 100644 --- a/base/system/sys_info_openbsd.cc +++ b/base/system/sys_info_openbsd.cc @@ -14,12 +14,14 @@ namespace { -uint64_t AmountOfMemory(int pages_name) { +int64_t AmountOfMemory(int pages_name) { long pages = sysconf(pages_name); long page_size = sysconf(_SC_PAGESIZE); - if (pages < 0 || page_size < 0) + if (pages == -1 || page_size == -1) { + NOTREACHED(); return 0; - return static_cast(pages) * static_cast(page_size); + } + return static_cast(pages) * page_size; } } // namespace @@ -39,12 +41,12 @@ int SysInfo::NumberOfProcessors() { } // static -uint64_t SysInfo::AmountOfPhysicalMemoryImpl() { +int64_t SysInfo::AmountOfPhysicalMemoryImpl() { return AmountOfMemory(_SC_PHYS_PAGES); } // static -uint64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() { +int64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() { // We should add inactive file-backed memory also but there is no such // information from OpenBSD unfortunately. return AmountOfMemory(_SC_AVPHYS_PAGES); diff --git a/base/system/sys_info_posix.cc b/base/system/sys_info_posix.cc index 7ae2d021432069..7495c4c5858032 100644 --- a/base/system/sys_info_posix.cc +++ b/base/system/sys_info_posix.cc @@ -17,7 +17,6 @@ #include "base/files/file_util.h" #include "base/lazy_instance.h" #include "base/notreached.h" -#include "base/numerics/safe_conversions.h" #include "base/strings/utf_string_conversions.h" #include "base/system/sys_info_internal.h" #include "base/threading/scoped_blocking_call.h" @@ -79,7 +78,7 @@ base::LazyInstance>:: Leaky g_lazy_number_of_processors = LAZY_INSTANCE_INITIALIZER; #endif // !BUILDFLAG(IS_OPENBSD) -uint64_t AmountOfVirtualMemory() { +int64_t AmountOfVirtualMemory() { struct rlimit limit; int result = getrlimit(RLIMIT_DATA, &limit); if (result != 0) { @@ -90,7 +89,7 @@ uint64_t AmountOfVirtualMemory() { } base::LazyInstance< - base::internal::LazySysInfoValue>::Leaky + base::internal::LazySysInfoValue>::Leaky g_lazy_virtual_memory = LAZY_INSTANCE_INITIALIZER; #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) @@ -128,14 +127,13 @@ bool GetDiskSpaceInfo(const base::FilePath& path, *available_bytes = zero_size_means_unlimited ? std::numeric_limits::max() - : base::checked_cast(stats.f_bavail * stats.f_frsize); + : static_cast(stats.f_bavail) * stats.f_frsize; } if (total_bytes) { - *total_bytes = - zero_size_means_unlimited - ? std::numeric_limits::max() - : base::checked_cast(stats.f_blocks * stats.f_frsize); + *total_bytes = zero_size_means_unlimited + ? std::numeric_limits::max() + : static_cast(stats.f_blocks) * stats.f_frsize; } return true; } @@ -151,7 +149,7 @@ int SysInfo::NumberOfProcessors() { #endif // !BUILDFLAG(IS_OPENBSD) // static -uint64_t SysInfo::AmountOfVirtualMemory() { +int64_t SysInfo::AmountOfVirtualMemory() { return g_lazy_virtual_memory.Get().value(); } @@ -247,7 +245,7 @@ std::string SysInfo::OperatingSystemArchitecture() { // static size_t SysInfo::VMAllocationGranularity() { - return checked_cast(getpagesize()); + return getpagesize(); } } // namespace base diff --git a/base/system/sys_info_unittest.cc b/base/system/sys_info_unittest.cc index 6f10cf041c0e29..226ef8c230a020 100644 --- a/base/system/sys_info_unittest.cc +++ b/base/system/sys_info_unittest.cc @@ -10,7 +10,6 @@ #include "base/bind.h" #include "base/environment.h" #include "base/files/file_util.h" -#include "base/numerics/safe_conversions.h" #include "base/process/process_metrics.h" #include "base/run_loop.h" #include "base/strings/pattern.h" @@ -58,10 +57,10 @@ TEST_F(SysInfoTest, NumProcs) { TEST_F(SysInfoTest, AmountOfMem) { // We aren't actually testing that it's correct, just that it's sane. - EXPECT_GT(SysInfo::AmountOfPhysicalMemory(), 0u); + EXPECT_GT(SysInfo::AmountOfPhysicalMemory(), 0); EXPECT_GT(SysInfo::AmountOfPhysicalMemoryMB(), 0); // The maxmimal amount of virtual memory can be zero which means unlimited. - EXPECT_GE(SysInfo::AmountOfVirtualMemory(), 0u); + EXPECT_GE(SysInfo::AmountOfVirtualMemory(), 0); } #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) @@ -79,7 +78,7 @@ TEST_F(SysInfoTest, MAYBE_AmountOfAvailablePhysicalMemory) { if (info.available != 0) { // If there is MemAvailable from kernel. EXPECT_LT(info.available, info.total); - const uint64_t amount = SysInfo::AmountOfAvailablePhysicalMemory(info); + const int64_t amount = SysInfo::AmountOfAvailablePhysicalMemory(info); // We aren't actually testing that it's correct, just that it's sane. // Available memory is |free - reserved + reclaimable (inactive, non-free)|. // On some android platforms, reserved is a substantial portion. @@ -89,17 +88,17 @@ TEST_F(SysInfoTest, MAYBE_AmountOfAvailablePhysicalMemory) { #else info.free; #endif // BUILDFLAG(IS_ANDROID) - EXPECT_GT(amount, checked_cast(available) * 1024); - EXPECT_LT(amount / 1024, checked_cast(info.available)); + EXPECT_GT(amount, static_cast(available) * 1024); + EXPECT_LT(amount / 1024, info.available); // Simulate as if there is no MemAvailable. info.available = 0; } // There is no MemAvailable. Check the fallback logic. - const uint64_t amount = SysInfo::AmountOfAvailablePhysicalMemory(info); + const int64_t amount = SysInfo::AmountOfAvailablePhysicalMemory(info); // We aren't actually testing that it's correct, just that it's sane. - EXPECT_GT(amount, checked_cast(info.free) * 1024); - EXPECT_LT(amount / 1024, checked_cast(info.total)); + EXPECT_GT(amount, static_cast(info.free) * 1024); + EXPECT_LT(amount / 1024, info.total); } #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || // BUILDFLAG(IS_ANDROID) diff --git a/base/system/sys_info_win.cc b/base/system/sys_info_win.cc index c881ca559a77af..dd0cc51976e154 100644 --- a/base/system/sys_info_win.cc +++ b/base/system/sys_info_win.cc @@ -13,7 +13,6 @@ #include "base/check.h" #include "base/files/file_path.h" #include "base/notreached.h" -#include "base/numerics/safe_conversions.h" #include "base/process/process_metrics.h" #include "base/strings/string_util.h" #include "base/strings/stringprintf.h" @@ -25,7 +24,7 @@ namespace { -uint64_t AmountOfMemory(DWORDLONG MEMORYSTATUSEX::*memory_field) { +int64_t AmountOfMemory(DWORDLONG MEMORYSTATUSEX::*memory_field) { MEMORYSTATUSEX memory_info; memory_info.dwLength = sizeof(memory_info); if (!GlobalMemoryStatusEx(&memory_info)) { @@ -33,7 +32,8 @@ uint64_t AmountOfMemory(DWORDLONG MEMORYSTATUSEX::*memory_field) { return 0; } - return memory_info.*memory_field; + int64_t rv = static_cast(memory_info.*memory_field); + return rv < 0 ? std::numeric_limits::max() : rv; } bool GetDiskSpaceInfo(const base::FilePath& path, @@ -68,20 +68,20 @@ int SysInfo::NumberOfProcessors() { } // static -uint64_t SysInfo::AmountOfPhysicalMemoryImpl() { +int64_t SysInfo::AmountOfPhysicalMemoryImpl() { return AmountOfMemory(&MEMORYSTATUSEX::ullTotalPhys); } // static -uint64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() { +int64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() { SystemMemoryInfoKB info; if (!GetSystemMemoryInfo(&info)) return 0; - return checked_cast(info.avail_phys) * 1024; + return static_cast(info.avail_phys) * 1024; } // static -uint64_t SysInfo::AmountOfVirtualMemory() { +int64_t SysInfo::AmountOfVirtualMemory() { return AmountOfMemory(&MEMORYSTATUSEX::ullTotalVirtual); } diff --git a/base/third_party/nspr/prtime.cc b/base/third_party/nspr/prtime.cc index 5d36b5b000e206..88fe1eba58fedf 100644 --- a/base/third_party/nspr/prtime.cc +++ b/base/third_party/nspr/prtime.cc @@ -1165,9 +1165,9 @@ PR_ParseTimeString( #endif if (secs != (time_t) -1) { - *result_imploded = secs * (PRTime)PR_USEC_PER_SEC; - *result_imploded += result->tm_usec; - return PR_SUCCESS; + *result_imploded = (PRInt64)secs * PR_USEC_PER_SEC; + *result_imploded += result->tm_usec; + return PR_SUCCESS; } } diff --git a/base/threading/hang_watcher.cc b/base/threading/hang_watcher.cc index 4d65101be9aaff..8f38b8a8a34c69 100644 --- a/base/threading/hang_watcher.cc +++ b/base/threading/hang_watcher.cc @@ -740,7 +740,7 @@ void HangWatcher::WatchStateSnapShot::Init( // Emit trace events for monitored threads. if (ThreadTypeLoggingLevelGreaterOrEqual(watch_state.get()->thread_type(), LoggingLevel::kUmaOnly)) { - const PlatformThreadId thread_id = watch_state.get()->GetThreadID(); + const uint64_t thread_id = watch_state.get()->GetThreadID(); const auto track = perfetto::Track::FromPointer( this, perfetto::ThreadTrack::ForThread(thread_id)); TRACE_EVENT_BEGIN("base", "HangWatcher::ThreadHung", track, deadline); @@ -758,8 +758,9 @@ void HangWatcher::WatchStateSnapShot::Init( // the next capture then they'll already be marked and will be included // in the capture at that time. if (thread_marked && all_threads_marked) { - hung_watch_state_copies_.push_back( - WatchStateCopy{deadline, watch_state.get()->GetThreadID()}); + hung_watch_state_copies_.push_back(WatchStateCopy{ + deadline, + static_cast(watch_state.get()->GetThreadID())}); } else { all_threads_marked = false; } @@ -1171,9 +1172,7 @@ HangWatchState::HangWatchState(HangWatcher::ThreadType thread_type) // provided by PlatformThread. Make sure to use the same for correct // attribution. #if BUILDFLAG(IS_MAC) - uint64_t thread_id; - pthread_threadid_np(pthread_self(), &thread_id); - thread_id_ = thread_id; + pthread_threadid_np(pthread_self(), &thread_id_); #else thread_id_ = PlatformThread::CurrentId(); #endif @@ -1276,7 +1275,7 @@ HangWatchState::GetHangWatchStateForCurrentThread() { return hang_watch_state.get(); } -PlatformThreadId HangWatchState::GetThreadID() const { +uint64_t HangWatchState::GetThreadID() const { return thread_id_; } diff --git a/base/threading/hang_watcher.h b/base/threading/hang_watcher.h index 4f3bf2b246d195..a2a557c41512ba 100644 --- a/base/threading/hang_watcher.h +++ b/base/threading/hang_watcher.h @@ -625,7 +625,7 @@ class BASE_EXPORT HangWatchState { WatchHangsInScope* GetCurrentWatchHangsInScope(); #endif - PlatformThreadId GetThreadID() const; + uint64_t GetThreadID() const; // Retrieve the current hang watch deadline directly. For testing only. HangWatchDeadline* GetHangWatchDeadlineForTesting(); @@ -652,8 +652,9 @@ class BASE_EXPORT HangWatchState { HangWatchDeadline deadline_; // A unique ID of the thread under watch. Used for logging in crash reports - // only. - PlatformThreadId thread_id_; + // only. Unsigned type is used as it provides a correct behavior for all + // platforms for positive thread ids. Any valid thread id should be positive. + uint64_t thread_id_; // Number of active HangWatchScopeEnables on this thread. int nesting_level_ = 0; diff --git a/base/threading/platform_thread_linux.cc b/base/threading/platform_thread_linux.cc index 27c0fd01dbf9aa..48f2670bb6b45c 100644 --- a/base/threading/platform_thread_linux.cc +++ b/base/threading/platform_thread_linux.cc @@ -164,10 +164,8 @@ void SetThreadCgroup(PlatformThreadId thread_id, const FilePath& cgroup_directory) { FilePath tasks_filepath = cgroup_directory.Append(FILE_PATH_LITERAL("tasks")); std::string tid = NumberToString(thread_id); - // TODO(crbug.com/1333521): Remove cast. - const int size = static_cast(tid.size()); - int bytes_written = WriteFile(tasks_filepath, tid.data(), size); - if (bytes_written != size) { + int bytes_written = WriteFile(tasks_filepath, tid.c_str(), tid.size()); + if (bytes_written != static_cast(tid.size())) { DVLOG(1) << "Failed to add " << tid << " to " << tasks_filepath.value(); } } @@ -414,7 +412,7 @@ void PlatformThread::SetThreadType(ProcessId process_id, #endif const int nice_setting = internal::ThreadTypeToNiceValue(thread_type); - if (setpriority(PRIO_PROCESS, static_cast(thread_id), nice_setting)) { + if (setpriority(PRIO_PROCESS, thread_id, nice_setting)) { DVPLOG(1) << "Failed to set nice value of thread (" << thread_id << ") to " << nice_setting; } diff --git a/base/threading/platform_thread_posix.cc b/base/threading/platform_thread_posix.cc index 418efcb3c08c67..23e18d12a72157 100644 --- a/base/threading/platform_thread_posix.cc +++ b/base/threading/platform_thread_posix.cc @@ -220,7 +220,7 @@ PlatformThreadId PlatformThread::CurrentId() { (g_is_main_thread && !g_main_thread_tid_cache_valid.load(std::memory_order_relaxed))) { // Update the cached tid. - g_thread_id = static_cast(syscall(__NR_gettid)); + g_thread_id = syscall(__NR_gettid); // If this is the main thread, we can mark the tid_cache as valid. // Otherwise, stop the current thread from always entering this slow path. if (g_thread_id == getpid()) { diff --git a/base/time/time_exploded_posix.cc b/base/time/time_exploded_posix.cc index a2562a9d25ffd2..45327a89b77c55 100644 --- a/base/time/time_exploded_posix.cc +++ b/base/time/time_exploded_posix.cc @@ -158,7 +158,7 @@ void Time::Explode(bool is_local, Exploded* exploded) const { exploded->hour = timestruct.tm_hour; exploded->minute = timestruct.tm_min; exploded->second = timestruct.tm_sec; - exploded->millisecond = static_cast(millisecond); + exploded->millisecond = millisecond; } // static diff --git a/base/trace_event/malloc_dump_provider.cc b/base/trace_event/malloc_dump_provider.cc index 69251d6de19fa6..7c30c562a3206f 100644 --- a/base/trace_event/malloc_dump_provider.cc +++ b/base/trace_event/malloc_dump_provider.cc @@ -16,7 +16,6 @@ #include "base/format_macros.h" #include "base/memory/nonscannable_memory.h" #include "base/metrics/histogram_functions.h" -#include "base/numerics/safe_conversions.h" #include "base/strings/stringprintf.h" #include "base/trace_event/process_memory_dump.h" #include "base/trace_event/traced_value.h" @@ -185,7 +184,7 @@ void ReportAppleAllocStats(size_t* total_virtual_size, #if (BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) && BUILDFLAG(IS_ANDROID)) || \ (!BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) && !BUILDFLAG(IS_WIN) && \ - !BUILDFLAG(IS_APPLE) && !BUILDFLAG(IS_FUCHSIA)) + !BUILDFLAG(IS_APPLE) && !BUILDFLAG(IS_WIN) && !BUILDFLAG(IS_FUCHSIA)) void ReportMallinfoStats(ProcessMemoryDump* pmd, size_t* total_virtual_size, size_t* resident_size, @@ -204,19 +203,17 @@ void ReportMallinfoStats(ProcessMemoryDump* pmd, // In case of Android's jemalloc |arena| is 0 and the outer pages size is // reported by |hblkhd|. In case of dlmalloc the total is given by // |arena| + |hblkhd|. For more details see link: http://goo.gl/fMR8lF. - *total_virtual_size += checked_cast(info.arena + info.hblkhd); - size_t total_allocated_size = checked_cast(info.uordblks); - *resident_size += total_allocated_size; + *total_virtual_size += info.arena + info.hblkhd; + *resident_size += info.uordblks; // Total allocated space is given by |uordblks|. - *allocated_objects_size += total_allocated_size; + *allocated_objects_size += info.uordblks; if (pmd) { MemoryAllocatorDump* sys_alloc_dump = pmd->CreateAllocatorDump("malloc/sys_malloc"); sys_alloc_dump->AddScalar(MemoryAllocatorDump::kNameSize, - MemoryAllocatorDump::kUnitsBytes, - total_allocated_size); + MemoryAllocatorDump::kUnitsBytes, info.uordblks); } } #endif diff --git a/base/trace_event/trace_log.cc b/base/trace_event/trace_log.cc index fb951928c46538..f9f27c1abef459 100644 --- a/base/trace_event/trace_log.cc +++ b/base/trace_event/trace_log.cc @@ -2208,7 +2208,7 @@ void TraceLog::SetProcessID(ProcessId process_id) { // See http://isthe.com/chongo/tech/comp/fnv/ for algorithm details. const uint64_t kOffsetBasis = 14695981039346656037ull; const uint64_t kFnvPrime = 1099511628211ull; - const uint64_t pid = static_cast(process_id_); + const uint64_t pid = process_id_; process_id_hash_ = (kOffsetBasis ^ pid) * kFnvPrime; } diff --git a/chrome/browser/ash/borealis/borealis_features.cc b/chrome/browser/ash/borealis/borealis_features.cc index 3b2a62ea8e165d..bef4048a38a2f6 100644 --- a/chrome/browser/ash/borealis/borealis_features.cc +++ b/chrome/browser/ash/borealis/borealis_features.cc @@ -35,7 +35,7 @@ namespace borealis { namespace { -constexpr uint64_t kGibi = 1024ull * 1024 * 1024; +constexpr int64_t kGibi = 1024 * 1024 * 1024; // Used to make it difficult to tell what someone's token is based on their // prefs. diff --git a/chrome/browser/ash/borealis/borealis_features_unittest.cc b/chrome/browser/ash/borealis/borealis_features_unittest.cc index 98c05e76eac359..28b2c75fe1a2de 100644 --- a/chrome/browser/ash/borealis/borealis_features_unittest.cc +++ b/chrome/browser/ash/borealis/borealis_features_unittest.cc @@ -135,11 +135,11 @@ TEST(BorealisFeaturesUtilTest, TokenHardwareCheckerWorks) { EXPECT_TRUE(checker.CpuRegexMatches("cp")); EXPECT_TRUE(checker.CpuRegexMatches("^[a-z]*$")); - EXPECT_TRUE(checker.HasMemory(0)); + EXPECT_TRUE(checker.HasMemory(-1)); EXPECT_TRUE(checker.HasMemory(41)); EXPECT_TRUE(checker.HasMemory(42)); EXPECT_FALSE(checker.HasMemory(43)); - EXPECT_FALSE(checker.HasMemory(std::numeric_limits::max())); + EXPECT_FALSE(checker.HasMemory(std::numeric_limits::max())); } TEST(BorealisFeaturesUtilTest, DataCanBeBuilt) { @@ -160,7 +160,7 @@ TEST(BorealisFeaturesUtilTest, DataCanBeBuilt) { // Faking CPU and RAM are not supported, so just assert they have some // trivial values. EXPECT_NE(data.cpu, ""); - EXPECT_GT(data.memory, 0u); + EXPECT_GT(data.memory, 0); loop.Quit(); })); loop.Run(); diff --git a/chrome/browser/ash/borealis/borealis_features_util.cc b/chrome/browser/ash/borealis/borealis_features_util.cc index 2e9b9cc9ffc00d..6ae51eec51e022 100644 --- a/chrome/browser/ash/borealis/borealis_features_util.cc +++ b/chrome/browser/ash/borealis/borealis_features_util.cc @@ -91,7 +91,7 @@ TokenHardwareChecker::Data::Data(std::string token_hash, std::string board, std::string model, std::string cpu, - uint64_t memory) + int64_t memory) : token_hash(std::move(token_hash)), board(std::move(board)), model(std::move(model)), @@ -176,7 +176,7 @@ bool TokenHardwareChecker::CpuRegexMatches(const std::string& cpu_regex) const { return RE2::PartialMatch(token_hardware_.cpu, cpu_regex); } -bool TokenHardwareChecker::HasMemory(uint64_t mem_bytes) const { +bool TokenHardwareChecker::HasMemory(int64_t mem_bytes) const { return token_hardware_.memory >= mem_bytes; } diff --git a/chrome/browser/ash/borealis/borealis_features_util.h b/chrome/browser/ash/borealis/borealis_features_util.h index d0311b8f7d6f06..1bd73fbe01cf8b 100644 --- a/chrome/browser/ash/borealis/borealis_features_util.h +++ b/chrome/browser/ash/borealis/borealis_features_util.h @@ -23,7 +23,7 @@ class TokenHardwareChecker { std::string board, std::string model, std::string cpu, - uint64_t memory); + int64_t memory); Data(const Data& other); ~Data(); @@ -31,7 +31,7 @@ class TokenHardwareChecker { std::string board; std::string model; std::string cpu; - uint64_t memory; + int64_t memory; }; // A hashing function used for creating tokens. @@ -53,7 +53,7 @@ class TokenHardwareChecker { bool IsModel(const std::string& model) const; bool ModelIn(base::flat_set models) const; bool CpuRegexMatches(const std::string& cpu_regex) const; - bool HasMemory(uint64_t mem_bytes) const; + bool HasMemory(int64_t mem_bytes) const; private: const Data token_hardware_; diff --git a/chrome/browser/ash/policy/status_collector/device_status_collector.h b/chrome/browser/ash/policy/status_collector/device_status_collector.h index 02487e33264ee5..fcadffdf752ef2 100644 --- a/chrome/browser/ash/policy/status_collector/device_status_collector.h +++ b/chrome/browser/ash/policy/status_collector/device_status_collector.h @@ -373,7 +373,7 @@ class DeviceStatusCollector : public StatusCollector, struct MemoryUsage { // Amount of free RAM (measures raw memory used by processes, not internal // memory waiting to be reclaimed by GC). - uint64_t bytes_of_ram_free; + int64_t bytes_of_ram_free; // Sampling timestamp. base::Time timestamp; diff --git a/chrome/browser/ash/policy/status_collector/legacy_device_status_collector.h b/chrome/browser/ash/policy/status_collector/legacy_device_status_collector.h index d5d21d16b98403..47e09b8f003e5f 100644 --- a/chrome/browser/ash/policy/status_collector/legacy_device_status_collector.h +++ b/chrome/browser/ash/policy/status_collector/legacy_device_status_collector.h @@ -342,7 +342,7 @@ class LegacyDeviceStatusCollector // Amount of free RAM (measures raw memory used by processes, not internal // memory waiting to be reclaimed by GC). - uint64_t bytes_of_ram_free; + int64_t bytes_of_ram_free; // Sampling timestamp. base::Time timestamp; diff --git a/chrome/browser/performance_manager/policies/background_tab_loading_policy.cc b/chrome/browser/performance_manager/policies/background_tab_loading_policy.cc index 0ffb5cb9acfc64..eb76d9b81668da 100644 --- a/chrome/browser/performance_manager/policies/background_tab_loading_policy.cc +++ b/chrome/browser/performance_manager/policies/background_tab_loading_policy.cc @@ -486,8 +486,11 @@ void BackgroundTabLoadingPolicy::LoadNextTab() { size_t BackgroundTabLoadingPolicy::GetFreePhysicalMemoryMib() const { if (free_memory_mb_for_testing_ != 0) return free_memory_mb_for_testing_; - constexpr uint64_t kMibibytesInBytes = 1 << 20; - return base::SysInfo::AmountOfAvailablePhysicalMemory() / kMibibytesInBytes; + constexpr int64_t kMibibytesInBytes = 1 << 20; + int64_t free_mem = + base::SysInfo::AmountOfAvailablePhysicalMemory() / kMibibytesInBytes; + DCHECK_GE(free_mem, 0); + return free_mem; } void BackgroundTabLoadingPolicy::ErasePageNodeToLoadData( diff --git a/chrome/browser/resource_coordinator/session_restore_policy.cc b/chrome/browser/resource_coordinator/session_restore_policy.cc index 6787763f54d5ad..15ca094017351a 100644 --- a/chrome/browser/resource_coordinator/session_restore_policy.cc +++ b/chrome/browser/resource_coordinator/session_restore_policy.cc @@ -68,8 +68,11 @@ class SysInfoDelegate : public SessionRestorePolicy::Delegate { } size_t GetFreeMemoryMiB() const override { - constexpr uint64_t kMibibytesInBytes = 1 << 20; - return base::SysInfo::AmountOfAvailablePhysicalMemory() / kMibibytesInBytes; + constexpr int64_t kMibibytesInBytes = 1 << 20; + int64_t free_mem = + base::SysInfo::AmountOfAvailablePhysicalMemory() / kMibibytesInBytes; + DCHECK(free_mem >= 0); + return free_mem; } base::TimeTicks NowTicks() const override { return base::TimeTicks::Now(); } diff --git a/components/discardable_memory/service/discardable_shared_memory_manager.cc b/components/discardable_memory/service/discardable_shared_memory_manager.cc index 61c8a45a48415f..ad058b888a8b8b 100644 --- a/components/discardable_memory/service/discardable_shared_memory_manager.cc +++ b/components/discardable_memory/service/discardable_shared_memory_manager.cc @@ -158,20 +158,20 @@ class DiscardableMemoryImpl : public base::DiscardableMemory { // Returns the default memory limit to use for discardable memory, taking // the amount physical memory available and other platform specific constraints // into account. -uint64_t GetDefaultMemoryLimit() { - const uint64_t kMegabyte = 1024ull * 1024; +int64_t GetDefaultMemoryLimit() { + const int kMegabyte = 1024 * 1024; #if BUILDFLAG(IS_CASTOS) || BUILDFLAG(IS_CAST_ANDROID) // Bypass IsLowEndDevice() check and fix max_default_memory_limit to 64MB on // Chromecast devices. Set value here as IsLowEndDevice() is used on some, but // not all Chromecast devices. - uint64_t max_default_memory_limit = 64 * kMegabyte; + int64_t max_default_memory_limit = 64 * kMegabyte; #else #if BUILDFLAG(IS_ANDROID) // Limits the number of FDs used to 32, assuming a 4MB allocation size. - uint64_t max_default_memory_limit = 128 * kMegabyte; + int64_t max_default_memory_limit = 128 * kMegabyte; #else - uint64_t max_default_memory_limit = 512 * kMegabyte; + int64_t max_default_memory_limit = 512 * kMegabyte; #endif // Use 1/8th of discardable memory on low-end devices. @@ -201,8 +201,7 @@ uint64_t GetDefaultMemoryLimit() { // Allow 1/2 of available shmem dir space to be used for discardable memory. max_default_memory_limit = - std::min(max_default_memory_limit, - static_cast(shmem_dir_amount_of_free_space / 2)); + std::min(max_default_memory_limit, shmem_dir_amount_of_free_space / 2); } #endif diff --git a/components/update_client/protocol_serializer.cc b/components/update_client/protocol_serializer.cc index 408e99b19cf3cd..16bd70bb02e842 100644 --- a/components/update_client/protocol_serializer.cc +++ b/components/update_client/protocol_serializer.cc @@ -37,7 +37,9 @@ namespace { // Returns the amount of physical memory in GB, rounded to the nearest GB. int GetPhysicalMemoryGB() { - return base::ClampRound(base::SysInfo::AmountOfPhysicalMemoryMB() / 1024.0f); + const double kOneGB = 1024 * 1024 * 1024; + const int64_t phys_mem = base::SysInfo::AmountOfPhysicalMemory(); + return static_cast(std::floor(0.5 + phys_mem / kOneGB)); } std::string GetOSVersion() { diff --git a/content/browser/renderer_host/render_frame_host_impl.cc b/content/browser/renderer_host/render_frame_host_impl.cc index 2771f3f0d499ce..41f51aef601517 100644 --- a/content/browser/renderer_host/render_frame_host_impl.cc +++ b/content/browser/renderer_host/render_frame_host_impl.cc @@ -1018,10 +1018,10 @@ base::TimeDelta GetSubframeProcessShutdownDelay( case features::SubframeShutdownDelayType::kMemoryBased: { // See subframe-reuse design doc for more detail on these values. // docs.google.com/document/d/1x_h4Gg4ForILEj8A4rMBX6d84uHWyQ9RSXmGVqMlBTk - static constexpr uint64_t kHighMemoryThreshold = 8'000'000'000; - static constexpr uint64_t kMaxMemoryThreshold = 16'000'000'000; + static constexpr int64_t kHighMemoryThreshold = 8000000000; + static constexpr int64_t kMaxMemoryThreshold = 16000000000; - const uint64_t available_memory = + const int64_t available_memory = base::SysInfo::AmountOfAvailablePhysicalMemory(); if (available_memory <= kHighMemoryThreshold) return kShortDelay; @@ -1030,7 +1030,7 @@ base::TimeDelta GetSubframeProcessShutdownDelay( // Scale delay linearly based on where |available_memory| lies between // |kHighMemoryThreshold| and |kMaxMemoryThreshold|. - const uint64_t available_memory_factor = + const int64_t available_memory_factor = (available_memory - kHighMemoryThreshold) / (kMaxMemoryThreshold - kHighMemoryThreshold); return kShortDelay + (kLongDelay - kShortDelay) * available_memory_factor; diff --git a/content/common/partition_alloc_support.cc b/content/common/partition_alloc_support.cc index a525eb9d9ac6cd..cf990027e7ec79 100644 --- a/content/common/partition_alloc_support.cc +++ b/content/common/partition_alloc_support.cc @@ -420,7 +420,7 @@ void PartitionAllocSupport::ReconfigureAfterTaskRunnerInit( #if BUILDFLAG(IS_ANDROID) && defined(ARCH_CPU_32_BITS) // Devices almost always report less physical memory than what they actually // have, so anything above 3GiB will catch 4GiB and above. - if (base::SysInfo::AmountOfPhysicalMemoryMB() <= 3500) + if (base::SysInfo::AmountOfPhysicalMemory() <= int64_t{3500} * 1024 * 1024) largest_cached_size_ = ::partition_alloc::ThreadCacheLimits::kDefaultSizeThreshold; #endif // BUILDFLAG(IS_ANDROID) && !defined(ARCH_CPU_64_BITS) diff --git a/gpu/command_buffer/client/shared_memory_limits.h b/gpu/command_buffer/client/shared_memory_limits.h index d3e13c084caecb..238785db3e8cbb 100644 --- a/gpu/command_buffer/client/shared_memory_limits.h +++ b/gpu/command_buffer/client/shared_memory_limits.h @@ -21,7 +21,7 @@ struct SharedMemoryLimits { // Do not use more than 5% of extra shared memory, and do not use any extra // for memory contrained devices (<=1GB). max_mapped_memory_for_texture_upload = - base::SysInfo::AmountOfPhysicalMemory() > 1024ULL * 1024 * 1024 + base::SysInfo::AmountOfPhysicalMemory() > 1024 * 1024 * 1024 ? base::saturated_cast( base::SysInfo::AmountOfPhysicalMemory() / 20) : 0; diff --git a/gpu/config/skia_limits.cc b/gpu/config/skia_limits.cc index 390f820d8b348a..ee23325f5987ff 100644 --- a/gpu/config/skia_limits.cc +++ b/gpu/config/skia_limits.cc @@ -30,7 +30,7 @@ void DetermineGrCacheLimitsFromAvailableMemory( // Limits for glyph cache textures. constexpr size_t kMaxLowEndGlyphCacheTextureBytes = 1024 * 512 * 4; // High-end / low-end memory cutoffs. - constexpr uint64_t kHighEndMemoryThreshold = 4096ULL * 1024 * 1024; + constexpr int64_t kHighEndMemoryThreshold = 4096LL * 1024 * 1024; if (base::SysInfo::IsLowEndDevice()) { *max_resource_cache_bytes = kMaxLowEndGaneshResourceCacheBytes; diff --git a/net/disk_cache/blockfile/backend_impl.cc b/net/disk_cache/blockfile/backend_impl.cc index 9cdd801a79d06e..a0f9d6d750a40f 100644 --- a/net/disk_cache/blockfile/backend_impl.cc +++ b/net/disk_cache/blockfile/backend_impl.cc @@ -2106,17 +2106,18 @@ bool BackendImpl::CheckEntry(EntryImpl* cache_entry) { } int BackendImpl::MaxBuffersSize() { - static uint64_t total_memory = base::SysInfo::AmountOfPhysicalMemory(); + static int64_t total_memory = base::SysInfo::AmountOfPhysicalMemory(); static bool done = false; if (!done) { - done = true; + const int kMaxBuffersSize = 30 * 1024 * 1024; - // We want to use up to 2% of the computer's memory, limit 30 MB. + // We want to use up to 2% of the computer's memory. total_memory = total_memory * 2 / 100; - constexpr uint64_t kMaxBuffersSize = 30 * 1024 * 1024; - if (total_memory > kMaxBuffersSize || total_memory == 0) + if (total_memory > kMaxBuffersSize || total_memory <= 0) total_memory = kMaxBuffersSize; + + done = true; } return static_cast(total_memory); diff --git a/net/disk_cache/memory/mem_backend_impl.cc b/net/disk_cache/memory/mem_backend_impl.cc index ab9f0416f96790..951427dc76b9d6 100644 --- a/net/disk_cache/memory/mem_backend_impl.cc +++ b/net/disk_cache/memory/mem_backend_impl.cc @@ -76,9 +76,9 @@ bool MemBackendImpl::Init() { if (max_size_) return true; - uint64_t total_memory = base::SysInfo::AmountOfPhysicalMemory(); + int64_t total_memory = base::SysInfo::AmountOfPhysicalMemory(); - if (total_memory == 0) { + if (total_memory <= 0) { max_size_ = kDefaultInMemoryCacheSize; return true; } @@ -86,7 +86,7 @@ bool MemBackendImpl::Init() { // We want to use up to 2% of the computer's memory, with a limit of 50 MB, // reached on system with more than 2.5 GB of RAM. total_memory = total_memory * 2 / 100; - if (total_memory > static_cast(kDefaultInMemoryCacheSize) * 5) + if (total_memory > kDefaultInMemoryCacheSize * 5) max_size_ = kDefaultInMemoryCacheSize * 5; else max_size_ = static_cast(total_memory); diff --git a/sandbox/policy/win/sandbox_win.cc b/sandbox/policy/win/sandbox_win.cc index f83aa27a17d6e8..f7b0de8ca8cab0 100644 --- a/sandbox/policy/win/sandbox_win.cc +++ b/sandbox/policy/win/sandbox_win.cc @@ -543,13 +543,13 @@ ResultCode SetJobMemoryLimit(Sandbox sandbox_type, TargetPolicy* policy) { size_t memory_limit = static_cast(kDataSizeLimit); if (sandbox_type == Sandbox::kGpu || sandbox_type == Sandbox::kRenderer) { - constexpr uint64_t GB = 1024 * 1024 * 1024; + int64_t GB = 1024 * 1024 * 1024; // Allow the GPU/RENDERER process's sandbox to access more physical memory // if it's available on the system. // // Renderer processes are allowed to access 16 GB; the GPU process, up // to 64 GB. - uint64_t physical_memory = base::SysInfo::AmountOfPhysicalMemory(); + int64_t physical_memory = base::SysInfo::AmountOfPhysicalMemory(); if (sandbox_type == Sandbox::kGpu && physical_memory > 64 * GB) { memory_limit = 64 * GB; } else if (sandbox_type == Sandbox::kGpu && physical_memory > 32 * GB) { diff --git a/storage/browser/blob/blob_memory_controller.cc b/storage/browser/blob/blob_memory_controller.cc index 87c87f83d837b0..f5d1e16a3b501c 100644 --- a/storage/browser/blob/blob_memory_controller.cc +++ b/storage/browser/blob/blob_memory_controller.cc @@ -82,11 +82,11 @@ File::Error CreateBlobDirectory(const FilePath& blob_storage_dir) { BlobStorageLimits CalculateBlobStorageLimitsImpl( const FilePath& storage_dir, bool disk_enabled, - absl::optional optional_memory_size_for_testing) { + absl::optional optional_memory_size_for_testing) { int64_t disk_size = 0ull; - uint64_t memory_size = optional_memory_size_for_testing - ? optional_memory_size_for_testing.value() - : base::SysInfo::AmountOfPhysicalMemory(); + int64_t memory_size = optional_memory_size_for_testing + ? optional_memory_size_for_testing.value() + : base::SysInfo::AmountOfPhysicalMemory(); if (disk_enabled && CreateBlobDirectory(storage_dir) == base::File::FILE_OK) disk_size = base::SysInfo::AmountOfTotalDiskSpace(storage_dir); @@ -99,9 +99,9 @@ BlobStorageLimits CalculateBlobStorageLimitsImpl( constexpr size_t kTwoGigabytes = 2ull * 1024 * 1024 * 1024; limits.max_blob_in_memory_space = kTwoGigabytes; #elif BUILDFLAG(IS_ANDROID) - limits.max_blob_in_memory_space = static_cast(memory_size / 100); + limits.max_blob_in_memory_space = static_cast(memory_size / 100ll); #else - limits.max_blob_in_memory_space = static_cast(memory_size / 5); + limits.max_blob_in_memory_space = static_cast(memory_size / 5ll); #endif } // Devices just on the edge (RAM == 256MB) should not fail because diff --git a/storage/browser/blob/blob_memory_controller.h b/storage/browser/blob/blob_memory_controller.h index 168c100e2ef036..ecb449b88b7915 100644 --- a/storage/browser/blob/blob_memory_controller.h +++ b/storage/browser/blob/blob_memory_controller.h @@ -204,7 +204,7 @@ class COMPONENT_EXPORT(STORAGE_BROWSER) BlobMemoryController { // synchronously. void CallWhenStorageLimitsAreKnown(base::OnceClosure callback); - void set_amount_of_physical_memory_for_testing(uint64_t amount_of_memory) { + void set_amount_of_physical_memory_for_testing(int64_t amount_of_memory) { amount_of_memory_for_testing_ = amount_of_memory; } @@ -283,7 +283,7 @@ class COMPONENT_EXPORT(STORAGE_BROWSER) BlobMemoryController { bool did_calculate_storage_limits_ = false; std::vector on_calculate_limits_callbacks_; - absl::optional amount_of_memory_for_testing_; + absl::optional amount_of_memory_for_testing_; // Memory bookkeeping. These numbers are all disjoint. // This is the amount of memory we're using for blobs in RAM, including the diff --git a/storage/browser/file_system/obfuscated_file_util_memory_delegate.cc b/storage/browser/file_system/obfuscated_file_util_memory_delegate.cc index e9db5bb939b3fe..326e9d85df92af 100644 --- a/storage/browser/file_system/obfuscated_file_util_memory_delegate.cc +++ b/storage/browser/file_system/obfuscated_file_util_memory_delegate.cc @@ -10,7 +10,6 @@ #include "base/allocator/partition_allocator/partition_alloc_constants.h" #include "base/files/file_util.h" #include "base/numerics/checked_math.h" -#include "base/numerics/safe_conversions.h" #include "base/system/sys_info.h" #include "build/build_config.h" #include "net/base/io_buffer.h" @@ -27,14 +26,14 @@ namespace { // crash possibility. // Note that quota assignment is the same for on-disk filesystem and the // assigned quota is not guaranteed to be allocatable later. -bool IsMemoryAvailable(size_t required_memory) { +bool IsMemoryAvailable(int64_t required_memory) { #if BUILDFLAG(IS_FUCHSIA) // This function is not implemented on FUCHSIA, yet. (crbug.com/986608) return true; #else - uint64_t max_allocatable = + int64_t max_allocatable = std::min(base::SysInfo::AmountOfAvailablePhysicalMemory(), - static_cast(partition_alloc::MaxDirectMapped())); + static_cast(partition_alloc::MaxDirectMapped())); return max_allocatable >= required_memory; #endif @@ -336,13 +335,12 @@ base::File::Error ObfuscatedFileUtilMemoryDelegate::Truncate( return base::File::FILE_ERROR_NOT_FOUND; // Fail if enough memory is not available. - if (!base::IsValueInRangeForNumericType(length) || - (static_cast(length) > dp->entry->file_content.capacity() && - !IsMemoryAvailable(static_cast(length)))) { + if (static_cast(length) > dp->entry->file_content.capacity() && + !IsMemoryAvailable(length)) { return base::File::FILE_ERROR_NO_SPACE; } - dp->entry->file_content.resize(static_cast(length)); + dp->entry->file_content.resize(length); return base::File::FILE_OK; } @@ -622,7 +620,7 @@ base::File::Error ObfuscatedFileUtilMemoryDelegate::CopyInForeignFile( } // Fail if enough memory is not available. - if (!IsMemoryAvailable(static_cast(source_info.size))) + if (!IsMemoryAvailable(source_info.size)) return base::File::FILE_ERROR_NO_SPACE; // Create file. diff --git a/storage/browser/quota/quota_device_info_helper.cc b/storage/browser/quota/quota_device_info_helper.cc index cf12c20ac81629..8f415442983cd8 100644 --- a/storage/browser/quota/quota_device_info_helper.cc +++ b/storage/browser/quota/quota_device_info_helper.cc @@ -17,7 +17,7 @@ int64_t QuotaDeviceInfoHelper::AmountOfTotalDiskSpace( return disk_space; } -uint64_t QuotaDeviceInfoHelper::AmountOfPhysicalMemory() const { +int64_t QuotaDeviceInfoHelper::AmountOfPhysicalMemory() const { return base::SysInfo::AmountOfPhysicalMemory(); } diff --git a/storage/browser/quota/quota_device_info_helper.h b/storage/browser/quota/quota_device_info_helper.h index 1f865d87d4f8c6..2ce8b16f8ee4af 100644 --- a/storage/browser/quota/quota_device_info_helper.h +++ b/storage/browser/quota/quota_device_info_helper.h @@ -25,7 +25,7 @@ class COMPONENT_EXPORT(STORAGE_BROWSER) QuotaDeviceInfoHelper { virtual int64_t AmountOfTotalDiskSpace(const base::FilePath& path) const; - virtual uint64_t AmountOfPhysicalMemory() const; + virtual int64_t AmountOfPhysicalMemory() const; }; // class QuotaDeviceInfoHelper } // namespace storage diff --git a/storage/browser/quota/quota_settings.cc b/storage/browser/quota/quota_settings.cc index 718e6cf09e4bb0..c2a5a209635aff 100644 --- a/storage/browser/quota/quota_settings.cc +++ b/storage/browser/quota/quota_settings.cc @@ -37,7 +37,7 @@ int64_t RandomizeByPercent(int64_t value, int percent) { } QuotaSettings CalculateIncognitoDynamicSettings( - uint64_t physical_memory_amount) { + int64_t physical_memory_amount) { // The incognito pool size is a fraction of the amount of system memory. double incognito_pool_size_ratio = kIncognitoQuotaRatioLowerBound + diff --git a/storage/browser/quota/quota_settings_unittest.cc b/storage/browser/quota/quota_settings_unittest.cc index 84e03cb5cd64f4..ef2bf9a1c6478d 100644 --- a/storage/browser/quota/quota_settings_unittest.cc +++ b/storage/browser/quota/quota_settings_unittest.cc @@ -8,7 +8,6 @@ #include "base/bind.h" #include "base/callback.h" #include "base/files/scoped_temp_dir.h" -#include "base/numerics/safe_conversions.h" #include "base/run_loop.h" #include "base/test/bind.h" #include "base/test/scoped_feature_list.h" @@ -23,8 +22,8 @@ using ::testing::_; namespace { -constexpr uint64_t kLowPhysicalMemory = 1024 * 1024; -constexpr uint64_t kHighPhysicalMemory = 65536 * kLowPhysicalMemory; +constexpr int64_t kLowPhysicalMemory = 1024 * 1024; +constexpr int64_t kHighPhysicalMemory = 65536 * kLowPhysicalMemory; } // namespace @@ -34,7 +33,7 @@ class MockQuotaDeviceInfoHelper : public QuotaDeviceInfoHelper { public: MockQuotaDeviceInfoHelper() = default; MOCK_CONST_METHOD1(AmountOfTotalDiskSpace, int64_t(const base::FilePath&)); - MOCK_CONST_METHOD0(AmountOfPhysicalMemory, uint64_t()); + MOCK_CONST_METHOD0(AmountOfPhysicalMemory, int64_t()); }; class QuotaSettingsTest : public testing::Test { @@ -72,25 +71,23 @@ class QuotaSettingsIncognitoTest : public QuotaSettingsTest { protected: void SetUpDeviceInfoHelper(const int expected_calls, - const uint64_t physical_memory_amount) { + const int64_t physical_memory_amount) { ON_CALL(device_info_helper_, AmountOfPhysicalMemory()) .WillByDefault(::testing::Return(physical_memory_amount)); EXPECT_CALL(device_info_helper_, AmountOfPhysicalMemory()) .Times(expected_calls); } - void GetAndTestSettings(const uint64_t physical_memory_amount) { + void GetAndTestSettings(const int64_t physical_memory_amount) { absl::optional settings = GetSettings(true, &device_info_helper_); ASSERT_TRUE(settings.has_value()); - const uint64_t pool_size = - base::checked_cast(settings->pool_size); EXPECT_LE( physical_memory_amount * GetIncognitoQuotaRatioLowerBound_ForTesting(), - pool_size); + settings->pool_size); EXPECT_GE( physical_memory_amount * GetIncognitoQuotaRatioUpperBound_ForTesting(), - pool_size); + settings->pool_size); } private: diff --git a/third_party/blink/renderer/bindings/core/v8/v8_initializer.cc b/third_party/blink/renderer/bindings/core/v8/v8_initializer.cc index ab7f0b5a8fe06a..c1f153ac03c941 100644 --- a/third_party/blink/renderer/bindings/core/v8/v8_initializer.cc +++ b/third_party/blink/renderer/bindings/core/v8/v8_initializer.cc @@ -729,8 +729,10 @@ class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { ArrayBufferAllocator() : total_allocation_(0) { // size_t may be equivalent to uint32_t or uint64_t, cast all values to // uint64_t to compare. - uint64_t virtual_size = base::SysInfo::AmountOfVirtualMemory(); - uint64_t size_t_max = std::numeric_limits::max(); + uint64_t virtual_size = + static_cast(base::SysInfo::AmountOfVirtualMemory()); + uint64_t size_t_max = + static_cast(std::numeric_limits::max()); DCHECK(virtual_size < size_t_max); // If AmountOfVirtualMemory() returns 0, there is no limit on virtual // memory, do not limit the total allocation. Otherwise, Limit the total diff --git a/third_party/blink/renderer/controller/user_level_memory_pressure_signal_generator.cc b/third_party/blink/renderer/controller/user_level_memory_pressure_signal_generator.cc index dec4bafe247c2a..8e2ba7bbd8c211 100644 --- a/third_party/blink/renderer/controller/user_level_memory_pressure_signal_generator.cc +++ b/third_party/blink/renderer/controller/user_level_memory_pressure_signal_generator.cc @@ -69,15 +69,21 @@ base::TimeDelta MinimumIntervalSeconds() { } double MemoryThresholdParam() { - int physical_memory_mb = base::SysInfo::AmountOfPhysicalMemoryMB(); - if (physical_memory_mb > 3.1 * 1024) - return MemoryThresholdParamOf4GbDevices(); - if (physical_memory_mb > 2.1 * 1024) - return MemoryThresholdParamOf3GbDevices(); - if (physical_memory_mb > 1.1 * 1024) - return MemoryThresholdParamOf2GbDevices(); - return (physical_memory_mb > 600) ? MemoryThresholdParamOf1GbDevices() - : MemoryThresholdParamOf512MbDevices(); + int64_t physical_memory = base::SysInfo::AmountOfPhysicalMemory(); + double memory_threshold_mb = kDefaultMemoryThresholdMB; + + if (physical_memory > 3.1 * 1024 * 1024 * 1024) + memory_threshold_mb = MemoryThresholdParamOf4GbDevices(); + else if (physical_memory > 2.1 * 1024 * 1024 * 1024) + memory_threshold_mb = MemoryThresholdParamOf3GbDevices(); + else if (physical_memory > 1.1 * 1024 * 1024 * 1024) + memory_threshold_mb = MemoryThresholdParamOf2GbDevices(); + else if (physical_memory > 600 * 1024 * 1024) + memory_threshold_mb = MemoryThresholdParamOf1GbDevices(); + else + memory_threshold_mb = MemoryThresholdParamOf512MbDevices(); + + return memory_threshold_mb; } } // namespace