CDVD: Adjust ram requirements when precaching.

This commit is contained in:
lightningterror 2025-04-15 09:57:15 +02:00
parent c6d1c8063b
commit cc30f529d5
3 changed files with 29 additions and 0 deletions

View File

@ -181,6 +181,10 @@ private:
extern u64 GetTickFrequency();
extern u64 GetCPUTicks();
extern u64 GetPhysicalMemory();
#ifdef _WIN32
// TODO: Someone do linux/mac.
extern u64 GetAvailablePhysicalMemory();
#endif
/// Spin for a short period of time (call while spinning waiting for a lock)
/// Returns the approximate number of ns that passed
extern u32 ShortSpin();

View File

@ -57,6 +57,14 @@ u64 GetPhysicalMemory()
return status.ullTotalPhys;
}
u64 GetAvailablePhysicalMemory()
{
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
GlobalMemoryStatusEx(&status);
return status.ullAvailPhys;
}
// Calculates the Windows OS Version and processor architecture, and returns it as a
// human-readable string. :)
std::string GetOSVersionString()

View File

@ -266,10 +266,26 @@ bool ThreadedFileReader::Precache2(ProgressCallback* progress, Error* error)
bool ThreadedFileReader::CheckAvailableMemoryForPrecaching(u64 required_size, Error* error)
{
#ifdef _WIN32
// We want to check available physical memory instead of total.
const u64 memory_available = GetAvailablePhysicalMemory();
// Reserve 2GB of available memory for headroom.
constexpr u64 memory_reserve = 2147483648;
const u64 max_precache_size = std::max(0LL, static_cast<s64>(memory_available - memory_reserve));
if (required_size > max_precache_size)
{
Error::SetStringFmt(error,
TRANSLATE_FS("CDVD", "Not enough free memory available for precaching, ({}GB) required."),
(required_size + memory_reserve) / _1gb);
return false;
}
#else
// Don't allow precaching to use more than 50% of system memory.
// Hopefully nobody's running 2-4GB potatoes anymore....
const u64 memory_size = GetPhysicalMemory();
const u64 max_precache_size = memory_size / 2;
if (required_size > max_precache_size)
{
Error::SetStringFmt(error,
@ -277,6 +293,7 @@ bool ThreadedFileReader::CheckAvailableMemoryForPrecaching(u64 required_size, Er
required_size / _1gb, max_precache_size / _1gb);
return false;
}
#endif
return true;
}