Swap: split module code from memory.c

This commit is contained in:
李通洲
2023-05-11 20:39:54 +08:00
parent aea0986928
commit e837e1e1d1
15 changed files with 140 additions and 121 deletions
+8 -2
View File
@@ -3,8 +3,14 @@
#ifndef FF_INCLUDED_detection_memory_memory
#define FF_INCLUDED_detection_memory_memory
#include "detection/storage.h"
#include "fastfetch.h"
void ffDetectMemory(FFMemoryStorage* result);
typedef struct FFMemoryResult
{
uint64_t bytesUsed;
uint64_t bytesTotal;
} FFMemoryResult;
const char* ffDetectMemory(FFMemoryResult* result);
#endif
+7 -13
View File
@@ -3,31 +3,25 @@
#include <string.h>
#include <mach/mach.h>
#include <sys/sysctl.h>
#include <unistd.h>
void ffDetectMemory(FFMemoryStorage* ram)
const char* ffDetectMemory(FFMemoryResult* ram)
{
size_t length = sizeof(ram->bytesTotal);
if (sysctl((int[]){ CTL_HW, HW_MEMSIZE }, 2, &ram->bytesTotal, &length, NULL, 0))
{
ffStrbufAppendS(&ram->error, "Failed to read hw.memsize");
return;
}
return "Failed to read hw.memsize";
uint32_t pagesize;
length = sizeof(pagesize);
if (sysctl((int[]){ CTL_HW, HW_PAGESIZE }, 2, &pagesize, &length, NULL, 0))
{
ffStrbufAppendS(&ram->error, "Failed to read hw.pagesize");
return;
}
return "Failed to read hw.pagesize";
mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;
vm_statistics64_data_t vmstat;
if(host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info64_t) (&vmstat), &count) != KERN_SUCCESS)
{
ffStrbufAppendS(&ram->error, "Failed to read host_statistics64");
return;
}
return "Failed to read host_statistics64";
ram->bytesUsed = ((uint64_t) vmstat.active_count + vmstat.wire_count) * pagesize;
return NULL;
}
+8 -11
View File
@@ -1,24 +1,21 @@
#include "memory.h"
#include "common/sysctl.h"
void ffDetectMemory(FFMemoryStorage* ram)
const char* ffDetectMemory(FFMemoryResult* ram)
{
uint32_t pageSize = (uint32_t) ffSysctlGetInt("hw.pagesize", 0);
if(pageSize == 0)
{
ffStrbufAppendS(&ram->error, "Failed to read hw.pagesize");
return;
}
uint32_t pageSize;
uint64_t length = sizeof(pageSize);
if (sysctl((int[]){ CTL_HW, HW_PAGESIZE }, 2, &pageSize, &length, NULL, 0))
return "Failed to read hw.pagesize";
ram->bytesTotal = (uint64_t) ffSysctlGetInt64("vm.stats.vm.v_page_count", 0) * pageSize;
if(ram->bytesTotal == 0)
{
ffStrbufAppendS(&ram->error, "Failed to read vm.stats.vm.v_page_count");
return;
}
return "Failed to read vm.stats.vm.v_page_count";
ram->bytesUsed = ram->bytesTotal
- (uint64_t) ffSysctlGetInt64("vm.stats.vm.v_free_count", 0) * pageSize
- (uint64_t) ffSysctlGetInt64("vm.stats.vm.v_inactive_count", 0) * pageSize
;
return NULL;
}
+7 -10
View File
@@ -1,18 +1,16 @@
#include "memory.h"
#include "util/mallocHelper.h"
#include <stdlib.h>
#include <string.h>
void ffDetectMemory(FFMemoryStorage* ram)
const char* ffDetectMemory(FFMemoryResult* ram)
{
FILE* meminfo = fopen("/proc/meminfo", "r");
if(meminfo == NULL)
{
ffStrbufAppendS(&ram->error, "Failed to open /proc/meminfo");
return;
}
return "Failed to open /proc/meminfo";
char* line = NULL;
char* FF_AUTO_FREE line = NULL;
size_t len = 0;
uint32_t memTotal = 0,
@@ -32,14 +30,13 @@ void ffDetectMemory(FFMemoryStorage* ram)
sscanf(line, "SReclaimable: %u", &sReclaimable);
}
if(line != NULL)
free(line);
fclose(meminfo);
ram->bytesTotal = memTotal * (uint64_t) 1024;
if(ram->bytesTotal == 0)
ffStrbufAppendS(&ram->error, "Failed to read MemTotal");
return "Failed to read MemTotal";
else
ram->bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * (uint64_t) 1024;
return NULL;
}
+4 -5
View File
@@ -2,16 +2,15 @@
#include <windows.h>
void ffDetectMemory(FFMemoryStorage* ram)
const char* ffDetectMemory(FFMemoryResult* ram)
{
MEMORYSTATUSEX statex = {
.dwLength = sizeof(statex),
};
if (!GlobalMemoryStatusEx(&statex))
{
ffStrbufAppendS(&ram->error, "GlobalMemoryStatusEx() failed");
return;
}
return "GlobalMemoryStatusEx() failed";
ram->bytesTotal = statex.ullTotalPhys;
ram->bytesUsed = statex.ullTotalPhys - statex.ullAvailPhys;
return NULL;
}