From a67f205cfe2153ad65828e28de0006fa8e7b3bac Mon Sep 17 00:00:00 2001
From: Morph <39850852+Morph1984@users.noreply.github.com>
Date: Sun, 17 May 2020 14:45:12 -0400
Subject: [PATCH 1/5] main: Log host system memory parameters

Logs both physical memory and swapfile sizes, this is useful for support.
---
 src/citra_qt/main.cpp        |  4 +++
 src/common/CMakeLists.txt    |  2 ++
 src/common/memory_detect.cpp | 57 ++++++++++++++++++++++++++++++++++++
 src/common/memory_detect.h   | 22 ++++++++++++++
 4 files changed, 85 insertions(+)
 create mode 100644 src/common/memory_detect.cpp
 create mode 100644 src/common/memory_detect.h

diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp
index b11692f59..b677ddba0 100644
--- a/src/citra_qt/main.cpp
+++ b/src/citra_qt/main.cpp
@@ -213,6 +213,10 @@ GMainWindow::GMainWindow()
     LOG_INFO(Frontend, "Host CPU: {}", cpu_string);
 #endif
     LOG_INFO(Frontend, "Host OS: {}", QSysInfo::prettyProductName().toStdString());
+    LOG_INFO(Frontend, "Host RAM: {:.2f} GB",
+             Common::GetMemInfo().TotalPhysicalMemory / 1024.0f / 1024 / 1024);
+    LOG_INFO(Frontend, "Host Swap: {:.2f} GB",
+             Common::GetMemInfo().TotalSwapMemory / 1024.0f / 1024 / 1024);
     UpdateWindowTitle();
 
     show();
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index a68983b05..65fd3ad1a 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -83,6 +83,8 @@ add_library(common STATIC
     logging/text_formatter.cpp
     logging/text_formatter.h
     math_util.h
+    memory_detect.cpp
+    memory_detect.h
     memory_ref.h
     memory_ref.cpp
     microprofile.cpp
diff --git a/src/common/memory_detect.cpp b/src/common/memory_detect.cpp
new file mode 100644
index 000000000..b59a45d55
--- /dev/null
+++ b/src/common/memory_detect.cpp
@@ -0,0 +1,57 @@
+// Copyright 2020 yuzu Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#ifdef _WIN32
+// clang-format off
+#include <windows.h>
+#include <sysinfoapi.h>
+// clang-format on
+#else
+#include <sys/types.h>
+#ifdef __APPLE__
+#include <sys/sysctl.h>
+#else
+#include <sys/sysinfo.h>
+#endif
+#endif
+
+#include "common/memory_detect.h"
+
+namespace Common {
+
+// Detects the RAM and Swapfile sizes
+static MemoryInfo Detect() {
+    MemoryInfo mem_info{};
+
+#ifdef _WIN32
+    MEMORYSTATUSEX memorystatus;
+    memorystatus.dwLength = sizeof(memorystatus);
+    GlobalMemoryStatusEx(&memorystatus);
+    mem_info.TotalPhysicalMemory = memorystatus.ullTotalPhys;
+    mem_info.TotalSwapMemory = memorystatus.ullTotalPageFile - mem_info.TotalPhysicalMemory;
+#elif defined(__APPLE__)
+    u64 ramsize;
+    struct xsw_usage vmusage;
+    // hw and vm are defined in sysctl.h
+    // https://github.com/apple/darwin-xnu/blob/master/bsd/sys/sysctl.h#L471
+    sysctlbyname(hw.memsize, &ramsize, sizeof(ramsize), NULL, 0);
+    sysctlbyname(vm.swapusage, &vmusage, sizeof(vmusage), NULL, 0);
+    mem_info.TotalPhysicalMemory = ramsize;
+    mem_info.TotalSwapMemory = vmusage.xsu_total;
+#else
+    struct sysinfo meminfo;
+    sysinfo(&meminfo);
+    mem_info.TotalPhysicalMemory = meminfo.totalram;
+    mem_info.TotalSwapMemory = meminfo.totalswap;
+#endif
+
+    return mem_info;
+}
+
+const MemoryInfo& GetMemInfo() {
+    static MemoryInfo mem_info = Detect();
+    return mem_info;
+}
+
+} // namespace Common
\ No newline at end of file
diff --git a/src/common/memory_detect.h b/src/common/memory_detect.h
new file mode 100644
index 000000000..a73c0f3f4
--- /dev/null
+++ b/src/common/memory_detect.h
@@ -0,0 +1,22 @@
+// Copyright 2020 yuzu Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include "common/common_types.h"
+
+namespace Common {
+
+struct MemoryInfo {
+    u64 TotalPhysicalMemory{};
+    u64 TotalSwapMemory{};
+};
+
+/**
+ * Gets the memory info of the host system
+ * @return Reference to a MemoryInfo struct with the physical and swap memory sizes in bytes
+ */
+const MemoryInfo& GetMemInfo();
+
+} // namespace Common
\ No newline at end of file

From cb8b72069fd398f8ed118ce5ffb0e205ae19fc32 Mon Sep 17 00:00:00 2001
From: Morph <39850852+Morph1984@users.noreply.github.com>
Date: Wed, 27 May 2020 11:21:59 -0400
Subject: [PATCH 2/5] Fix macOS code and change "Swapfile" to "Swap"

---
 src/citra_qt/main.cpp        | 1 +
 src/common/memory_detect.cpp | 7 +++++--
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp
index b677ddba0..00196d102 100644
--- a/src/citra_qt/main.cpp
+++ b/src/citra_qt/main.cpp
@@ -65,6 +65,7 @@
 #include "common/logging/filter.h"
 #include "common/logging/log.h"
 #include "common/logging/text_formatter.h"
+#include "common/memory_detect.h"
 #include "common/microprofile.h"
 #include "common/scm_rev.h"
 #include "common/scope_exit.h"
diff --git a/src/common/memory_detect.cpp b/src/common/memory_detect.cpp
index b59a45d55..3fdc309a2 100644
--- a/src/common/memory_detect.cpp
+++ b/src/common/memory_detect.cpp
@@ -33,10 +33,13 @@ static MemoryInfo Detect() {
 #elif defined(__APPLE__)
     u64 ramsize;
     struct xsw_usage vmusage;
+    std::size_t sizeof_ramsize = sizeof(ramsize);
+    std::size_t sizeof_vmusage = sizeof(vmusage);
     // hw and vm are defined in sysctl.h
     // https://github.com/apple/darwin-xnu/blob/master/bsd/sys/sysctl.h#L471
-    sysctlbyname(hw.memsize, &ramsize, sizeof(ramsize), NULL, 0);
-    sysctlbyname(vm.swapusage, &vmusage, sizeof(vmusage), NULL, 0);
+    // sysctlbyname(const char *, void *, size_t *, void *, size_t);
+    sysctlbyname("hw.memsize", &ramsize, &sizeof_ramsize, NULL, 0);
+    sysctlbyname("vm.swapusage", &vmusage, &sizeof_vmusage, NULL, 0);
     mem_info.TotalPhysicalMemory = ramsize;
     mem_info.TotalSwapMemory = vmusage.xsu_total;
 #else

From 6dabf100096ac92f8ac169e1b7abb7f5c6049b30 Mon Sep 17 00:00:00 2001
From: Jan Beich <jbeich@FreeBSD.org>
Date: Mon, 29 Jun 2020 22:39:31 +0000
Subject: [PATCH 3/5] common: add sysconf() fallback

src/common/memory_detect.cpp:15:10: fatal error: 'sys/sysinfo.h' file not found
 #include <sys/sysinfo.h>
          ^~~~~~~~~~~~~~~
---
 src/common/memory_detect.cpp | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/src/common/memory_detect.cpp b/src/common/memory_detect.cpp
index 3fdc309a2..e981b9a2a 100644
--- a/src/common/memory_detect.cpp
+++ b/src/common/memory_detect.cpp
@@ -9,10 +9,12 @@
 // clang-format on
 #else
 #include <sys/types.h>
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(__FreeBSD__)
 #include <sys/sysctl.h>
-#else
+#elif defined(__linux__)
 #include <sys/sysinfo.h>
+#else
+#include <unistd.h>
 #endif
 #endif
 
@@ -42,11 +44,22 @@ static MemoryInfo Detect() {
     sysctlbyname("vm.swapusage", &vmusage, &sizeof_vmusage, NULL, 0);
     mem_info.TotalPhysicalMemory = ramsize;
     mem_info.TotalSwapMemory = vmusage.xsu_total;
-#else
+#elif defined(__FreeBSD__)
+    u_long physmem, swap_total;
+    std::size_t sizeof_u_long = sizeof(u_long);
+    // sysctlbyname(const char *, void *, size_t *, const void *, size_t);
+    sysctlbyname("hw.physmem", &physmem, &sizeof_u_long, NULL, 0);
+    sysctlbyname("vm.swap_total", &swap_total, &sizeof_u_long, NULL, 0);
+    mem_info.TotalPhysicalMemory = physmem;
+    mem_info.TotalSwapMemory = swap_total;
+#elif defined(__linux__)
     struct sysinfo meminfo;
     sysinfo(&meminfo);
     mem_info.TotalPhysicalMemory = meminfo.totalram;
     mem_info.TotalSwapMemory = meminfo.totalswap;
+#else
+    mem_info.TotalPhysicalMemory = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGE_SIZE);
+    mem_info.TotalSwapMemory = 0;
 #endif
 
     return mem_info;

From 35f7f5e3e3ad2ac4c50d92f663ec91de98942fc3 Mon Sep 17 00:00:00 2001
From: Jan Beich <jbeich@FreeBSD.org>
Date: Tue, 30 Jun 2020 22:55:47 +0000
Subject: [PATCH 4/5] common: switch to nullptr for sysctl's empty new value

---
 src/common/memory_detect.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/common/memory_detect.cpp b/src/common/memory_detect.cpp
index e981b9a2a..8cff6ec37 100644
--- a/src/common/memory_detect.cpp
+++ b/src/common/memory_detect.cpp
@@ -40,16 +40,16 @@ static MemoryInfo Detect() {
     // hw and vm are defined in sysctl.h
     // https://github.com/apple/darwin-xnu/blob/master/bsd/sys/sysctl.h#L471
     // sysctlbyname(const char *, void *, size_t *, void *, size_t);
-    sysctlbyname("hw.memsize", &ramsize, &sizeof_ramsize, NULL, 0);
-    sysctlbyname("vm.swapusage", &vmusage, &sizeof_vmusage, NULL, 0);
+    sysctlbyname("hw.memsize", &ramsize, &sizeof_ramsize, nullptr, 0);
+    sysctlbyname("vm.swapusage", &vmusage, &sizeof_vmusage, nullptr, 0);
     mem_info.TotalPhysicalMemory = ramsize;
     mem_info.TotalSwapMemory = vmusage.xsu_total;
 #elif defined(__FreeBSD__)
     u_long physmem, swap_total;
     std::size_t sizeof_u_long = sizeof(u_long);
     // sysctlbyname(const char *, void *, size_t *, const void *, size_t);
-    sysctlbyname("hw.physmem", &physmem, &sizeof_u_long, NULL, 0);
-    sysctlbyname("vm.swap_total", &swap_total, &sizeof_u_long, NULL, 0);
+    sysctlbyname("hw.physmem", &physmem, &sizeof_u_long, nullptr, 0);
+    sysctlbyname("vm.swap_total", &swap_total, &sizeof_u_long, nullptr, 0);
     mem_info.TotalPhysicalMemory = physmem;
     mem_info.TotalSwapMemory = swap_total;
 #elif defined(__linux__)

From de1fe7e6e36056d06a5f347cbee421809a9901c6 Mon Sep 17 00:00:00 2001
From: FearlessTobi <thm.frey@gmail.com>
Date: Tue, 15 Nov 2022 11:16:45 +0100
Subject: [PATCH 5/5] Address review comments

---
 src/citra_qt/main.cpp        |  9 +++++----
 src/common/CMakeLists.txt    |  1 +
 src/common/literals.h        | 31 +++++++++++++++++++++++++++++++
 src/common/memory_detect.cpp | 32 +++++++++++++-------------------
 src/common/memory_detect.h   |  8 ++++----
 5 files changed, 54 insertions(+), 27 deletions(-)
 create mode 100644 src/common/literals.h

diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp
index 00196d102..5a9d07fba 100644
--- a/src/citra_qt/main.cpp
+++ b/src/citra_qt/main.cpp
@@ -61,6 +61,7 @@
 #include "common/common_paths.h"
 #include "common/detached_tasks.h"
 #include "common/file_util.h"
+#include "common/literals.h"
 #include "common/logging/backend.h"
 #include "common/logging/filter.h"
 #include "common/logging/log.h"
@@ -214,10 +215,10 @@ GMainWindow::GMainWindow()
     LOG_INFO(Frontend, "Host CPU: {}", cpu_string);
 #endif
     LOG_INFO(Frontend, "Host OS: {}", QSysInfo::prettyProductName().toStdString());
-    LOG_INFO(Frontend, "Host RAM: {:.2f} GB",
-             Common::GetMemInfo().TotalPhysicalMemory / 1024.0f / 1024 / 1024);
-    LOG_INFO(Frontend, "Host Swap: {:.2f} GB",
-             Common::GetMemInfo().TotalSwapMemory / 1024.0f / 1024 / 1024);
+    const auto& mem_info = Common::GetMemInfo();
+    using namespace Common::Literals;
+    LOG_INFO(Frontend, "Host RAM: {:.2f} GiB", mem_info.total_physical_memory / f64{1_GiB});
+    LOG_INFO(Frontend, "Host Swap: {:.2f} GiB", mem_info.total_swap_memory / f64{1_GiB});
     UpdateWindowTitle();
 
     show();
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 65fd3ad1a..48914c960 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -74,6 +74,7 @@ add_library(common STATIC
     file_util.h
     hash.h
     linear_disk_cache.h
+    literals.h
     logging/backend.cpp
     logging/backend.h
     logging/filter.cpp
diff --git a/src/common/literals.h b/src/common/literals.h
new file mode 100644
index 000000000..d55fed40b
--- /dev/null
+++ b/src/common/literals.h
@@ -0,0 +1,31 @@
+// Copyright 2021 yuzu emulator team
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include "common/common_types.h"
+
+namespace Common::Literals {
+
+constexpr u64 operator""_KiB(unsigned long long int x) {
+    return 1024ULL * x;
+}
+
+constexpr u64 operator""_MiB(unsigned long long int x) {
+    return 1024_KiB * x;
+}
+
+constexpr u64 operator""_GiB(unsigned long long int x) {
+    return 1024_MiB * x;
+}
+
+constexpr u64 operator""_TiB(unsigned long long int x) {
+    return 1024_GiB * x;
+}
+
+constexpr u64 operator""_PiB(unsigned long long int x) {
+    return 1024_TiB * x;
+}
+
+} // namespace Common::Literals
diff --git a/src/common/memory_detect.cpp b/src/common/memory_detect.cpp
index 8cff6ec37..e97ff9af2 100644
--- a/src/common/memory_detect.cpp
+++ b/src/common/memory_detect.cpp
@@ -3,10 +3,9 @@
 // Refer to the license.txt file included.
 
 #ifdef _WIN32
-// clang-format off
 #include <windows.h>
+// Depends on <windows.h> coming first
 #include <sysinfoapi.h>
-// clang-format on
 #else
 #include <sys/types.h>
 #if defined(__APPLE__) || defined(__FreeBSD__)
@@ -23,15 +22,15 @@
 namespace Common {
 
 // Detects the RAM and Swapfile sizes
-static MemoryInfo Detect() {
+const MemoryInfo GetMemInfo() {
     MemoryInfo mem_info{};
 
 #ifdef _WIN32
     MEMORYSTATUSEX memorystatus;
     memorystatus.dwLength = sizeof(memorystatus);
     GlobalMemoryStatusEx(&memorystatus);
-    mem_info.TotalPhysicalMemory = memorystatus.ullTotalPhys;
-    mem_info.TotalSwapMemory = memorystatus.ullTotalPageFile - mem_info.TotalPhysicalMemory;
+    mem_info.total_physical_memory = memorystatus.ullTotalPhys;
+    mem_info.total_swap_memory = memorystatus.ullTotalPageFile - mem_info.total_physical_memory;
 #elif defined(__APPLE__)
     u64 ramsize;
     struct xsw_usage vmusage;
@@ -42,32 +41,27 @@ static MemoryInfo Detect() {
     // sysctlbyname(const char *, void *, size_t *, void *, size_t);
     sysctlbyname("hw.memsize", &ramsize, &sizeof_ramsize, nullptr, 0);
     sysctlbyname("vm.swapusage", &vmusage, &sizeof_vmusage, nullptr, 0);
-    mem_info.TotalPhysicalMemory = ramsize;
-    mem_info.TotalSwapMemory = vmusage.xsu_total;
+    mem_info.total_physical_memory = ramsize;
+    mem_info.total_swap_memory = vmusage.xsu_total;
 #elif defined(__FreeBSD__)
     u_long physmem, swap_total;
     std::size_t sizeof_u_long = sizeof(u_long);
     // sysctlbyname(const char *, void *, size_t *, const void *, size_t);
     sysctlbyname("hw.physmem", &physmem, &sizeof_u_long, nullptr, 0);
     sysctlbyname("vm.swap_total", &swap_total, &sizeof_u_long, nullptr, 0);
-    mem_info.TotalPhysicalMemory = physmem;
-    mem_info.TotalSwapMemory = swap_total;
+    mem_info.total_physical_memory = physmem;
+    mem_info.total_swap_memory = swap_total;
 #elif defined(__linux__)
     struct sysinfo meminfo;
     sysinfo(&meminfo);
-    mem_info.TotalPhysicalMemory = meminfo.totalram;
-    mem_info.TotalSwapMemory = meminfo.totalswap;
+    mem_info.total_physical_memory = meminfo.totalram;
+    mem_info.total_swap_memory = meminfo.totalswap;
 #else
-    mem_info.TotalPhysicalMemory = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGE_SIZE);
-    mem_info.TotalSwapMemory = 0;
+    mem_info.total_physical_memory = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGE_SIZE);
+    mem_info.total_swap_memory = 0;
 #endif
 
     return mem_info;
 }
 
-const MemoryInfo& GetMemInfo() {
-    static MemoryInfo mem_info = Detect();
-    return mem_info;
-}
-
-} // namespace Common
\ No newline at end of file
+} // namespace Common
diff --git a/src/common/memory_detect.h b/src/common/memory_detect.h
index a73c0f3f4..eb07d8ab0 100644
--- a/src/common/memory_detect.h
+++ b/src/common/memory_detect.h
@@ -9,14 +9,14 @@
 namespace Common {
 
 struct MemoryInfo {
-    u64 TotalPhysicalMemory{};
-    u64 TotalSwapMemory{};
+    u64 total_physical_memory{};
+    u64 total_swap_memory{};
 };
 
 /**
  * Gets the memory info of the host system
  * @return Reference to a MemoryInfo struct with the physical and swap memory sizes in bytes
  */
-const MemoryInfo& GetMemInfo();
+[[nodiscard]] const MemoryInfo GetMemInfo();
 
-} // namespace Common
\ No newline at end of file
+} // namespace Common