mirror of
https://github.com/obsproject/obs-studio.git
synced 2026-08-30 17:58:37 +08:00
libobs: Add utility function to get total RAM
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user