浏览代码

libobs: Add utility function to get total RAM

derrod 3 年之前
父节点
当前提交
1b6e1ce655

+ 6 - 0
docs/sphinx/reference-libobs-util-platform.rst

@@ -447,6 +447,12 @@ Other Functions
 
 ---------------------
 
+.. function:: uint64_t os_get_sys_total_size(void)
+
+   Returns the amount of memory installed.
+
+---------------------
+
 .. struct:: os_proc_memory_usage
 
    Memory usage structure.

+ 22 - 0
libobs/util/platform-cocoa.m

@@ -386,6 +386,28 @@ uint64_t os_get_sys_free_size(void)
 	return vmstat.free_count * vm_page_size;
 }
 
+static uint64_t total_memory = 0;
+static bool total_memory_initialized = false;
+
+static void os_get_sys_total_size_internal()
+{
+	total_memory_initialized = true;
+
+	size_t size;
+	int ret;
+
+	size = sizeof(total_memory);
+	ret = sysctlbyname("hw.memsize", &total_memory, &size, NULL, 0);
+}
+
+uint64_t os_get_sys_total_size(void)
+{
+	if (!total_memory_initialized)
+		os_get_sys_total_size_internal();
+
+	return total_memory;
+}
+
 #ifndef MACH_TASK_BASIC_INFO
 typedef task_basic_info_data_t mach_task_basic_info_data_t;
 #endif

+ 27 - 0
libobs/util/platform-nix.c

@@ -53,6 +53,9 @@
 #else
 #include <sys/resource.h>
 #endif
+#if !defined(__OpenBSD__)
+#include <sys/sysinfo.h>
+#endif
 #include <spawn.h>
 #endif
 
@@ -1100,6 +1103,30 @@ uint64_t os_get_proc_virtual_size(void)
 	return (uint64_t)statm.virtual_size;
 }
 #endif
+
+static uint64_t total_memory = 0;
+static bool total_memory_initialized = false;
+
+static void os_get_sys_total_size_internal()
+{
+	total_memory_initialized = true;
+
+#ifndef __OpenBSD__
+	struct sysinfo info;
+	if (sysinfo(&info) < 0)
+		return;
+
+	total_memory = (uint64_t)info.totalram * info.mem_unit;
+#endif
+}
+
+uint64_t os_get_sys_total_size(void)
+{
+	if (!total_memory_initialized)
+		os_get_sys_total_size_internal();
+
+	return total_memory;
+}
 #endif
 
 uint64_t os_get_free_disk_space(const char *dir)

+ 22 - 0
libobs/util/platform-windows.c

@@ -1390,6 +1390,28 @@ uint64_t os_get_sys_free_size(void)
 	return msex.ullAvailPhys;
 }
 
+static uint64_t total_memory = 0;
+static bool total_memory_initialized = false;
+
+static void os_get_sys_total_size_internal()
+{
+	total_memory_initialized = true;
+
+	MEMORYSTATUSEX msex = {sizeof(MEMORYSTATUSEX)};
+	if (!os_get_sys_memory_usage_internal(&msex))
+		return;
+
+	total_memory = msex.ullTotalPhys;
+}
+
+uint64_t os_get_sys_total_size(void)
+{
+	if (!total_memory_initialized)
+		os_get_sys_total_size_internal();
+
+	return total_memory;
+}
+
 static inline bool
 os_get_proc_memory_usage_internal(PROCESS_MEMORY_COUNTERS *pmc)
 {

+ 1 - 0
libobs/util/platform.h

@@ -189,6 +189,7 @@ EXPORT int os_get_physical_cores(void);
 EXPORT int os_get_logical_cores(void);
 
 EXPORT uint64_t os_get_sys_free_size(void);
+EXPORT uint64_t os_get_sys_total_size(void);
 
 struct os_proc_memory_usage {
 	uint64_t resident_size;