diff --git a/common/Darwin/DarwinMisc.cpp b/common/Darwin/DarwinMisc.cpp index 400c26c9b5..5f12d186f7 100644 --- a/common/Darwin/DarwinMisc.cpp +++ b/common/Darwin/DarwinMisc.cpp @@ -50,6 +50,27 @@ u64 GetPhysicalMemory() return getmem; } +u64 GetAvailablePhysicalMemory() +{ + const mach_port_t host_port = mach_host_self(); + vm_size_t page_size; + + if (host_page_size(host_port, &page_size) != KERN_SUCCESS) + return 0; + + vm_statistics64_data_t vm_stat; + mach_msg_type_number_t host_size = sizeof(vm_statistics64_data_t) / sizeof(integer_t); + + if (host_statistics64(host_port, HOST_VM_INFO, reinterpret_cast(&vm_stat), &host_size) != KERN_SUCCESS) + return 0; + + const u64 free_pages = static_cast(vm_stat.free_count); + const u64 inactive_pages = static_cast(vm_stat.inactive_count); + const u64 get_available_mem = (free_pages + inactive_pages) * page_size; + + return get_available_mem; +} + static mach_timebase_info_data_t s_timebase_info; static const u64 tickfreq = []() { if (mach_timebase_info(&s_timebase_info) != KERN_SUCCESS) diff --git a/common/HostSys.h b/common/HostSys.h index f79c36fb70..08eb31232c 100644 --- a/common/HostSys.h +++ b/common/HostSys.h @@ -181,10 +181,7 @@ 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(); diff --git a/common/Linux/LnxMisc.cpp b/common/Linux/LnxMisc.cpp index acefebe701..2ef073932b 100644 --- a/common/Linux/LnxMisc.cpp +++ b/common/Linux/LnxMisc.cpp @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -40,6 +41,15 @@ u64 GetPhysicalMemory() return pages * getpagesize(); } +u64 GetAvailablePhysicalMemory() +{ + struct sysinfo info; + if (sysinfo(&info) != 0) + return 0; + + return static_cast(info.freeram) * info.mem_unit; +} + u64 GetTickFrequency() { return 1000000000; // unix measures in nanoseconds diff --git a/pcsx2/CDVD/ThreadedFileReader.cpp b/pcsx2/CDVD/ThreadedFileReader.cpp index 550b2794a7..1e9d9273aa 100644 --- a/pcsx2/CDVD/ThreadedFileReader.cpp +++ b/pcsx2/CDVD/ThreadedFileReader.cpp @@ -266,12 +266,11 @@ 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(memory_available - memory_reserve)); + const u64 max_precache_size = std::max(s64{0}, static_cast(memory_available - memory_reserve)); if (required_size > max_precache_size) { @@ -280,20 +279,6 @@ bool ThreadedFileReader::CheckAvailableMemoryForPrecaching(u64 required_size, Er (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, - TRANSLATE_FS("CDVD", "Required memory ({}GB) is the above the maximum allowed ({}GB)."), - required_size / _1gb, max_precache_size / _1gb); - return false; - } -#endif return true; }