From 6ff67e87eb79d70824fa501714f49a93ca0fe509 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 28 Dec 2022 15:55:31 +0800 Subject: [PATCH] Memory: refactor, don't cache results --- CMakeLists.txt | 2 -- src/detection/memory/memory.c | 13 ------------- src/detection/memory/memory.h | 2 +- src/detection/memory/memory_apple.c | 2 +- src/detection/memory/memory_bsd.c | 2 +- src/detection/memory/memory_linux.c | 12 ++++++------ src/detection/memory/memory_windows.c | 2 +- src/detection/storage.h | 1 - src/detection/swap/swap.c | 13 ------------- src/detection/swap/swap.h | 2 +- src/detection/swap/swap_apple.c | 2 +- src/detection/swap/swap_bsd.c | 2 +- src/detection/swap/swap_linux.c | 2 +- src/detection/swap/swap_windows.cpp | 2 +- src/fastfetch.h | 1 + src/modules/memory.c | 12 ++++++++++-- 16 files changed, 26 insertions(+), 46 deletions(-) delete mode 100644 src/detection/memory/memory.c delete mode 100644 src/detection/swap/swap.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 8ce915f1c..866e5d0aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -240,10 +240,8 @@ set(LIBFASTFETCH_SRC src/detection/host/host.c src/detection/locale/locale.c src/detection/media/media.c - src/detection/memory/memory.c src/detection/os/os.c src/detection/packages/packages.c - src/detection/swap/swap.c src/detection/terminalfont/terminalfont.c src/detection/terminalshell/terminalshell.c src/detection/title.c diff --git a/src/detection/memory/memory.c b/src/detection/memory/memory.c deleted file mode 100644 index 107e7d716..000000000 --- a/src/detection/memory/memory.c +++ /dev/null @@ -1,13 +0,0 @@ -#include "memory.h" -#include "detection/internal.h" - -void ffDetectMemoryImpl(FFMemoryStorage* memory); - -const FFMemoryStorage* ffDetectMemory() -{ - FF_DETECTION_INTERNAL_GUARD(FFMemoryStorage, - ffStrbufInit(&result.error); - - ffDetectMemoryImpl(&result); - ); -} diff --git a/src/detection/memory/memory.h b/src/detection/memory/memory.h index 1114ff4cb..ee34c8038 100644 --- a/src/detection/memory/memory.h +++ b/src/detection/memory/memory.h @@ -5,6 +5,6 @@ #include "detection/storage.h" -const FFMemoryStorage* ffDetectMemory(); +void ffDetectMemory(FFMemoryStorage* result); #endif diff --git a/src/detection/memory/memory_apple.c b/src/detection/memory/memory_apple.c index b3616ec59..f86f52cd4 100644 --- a/src/detection/memory/memory_apple.c +++ b/src/detection/memory/memory_apple.c @@ -4,7 +4,7 @@ #include #include -void ffDetectMemoryImpl(FFMemoryStorage* ram) +void ffDetectMemory(FFMemoryStorage* ram) { ram->bytesTotal = (uint64_t) ffSysctlGetInt64("hw.memsize", 0); if(ram->bytesTotal == 0) diff --git a/src/detection/memory/memory_bsd.c b/src/detection/memory/memory_bsd.c index 881b3dfb3..52d19a0c9 100644 --- a/src/detection/memory/memory_bsd.c +++ b/src/detection/memory/memory_bsd.c @@ -1,7 +1,7 @@ #include "memory.h" #include "common/sysctl.h" -void ffDetectMemoryImpl(FFMemoryStorage* ram) +void ffDetectMemory(FFMemoryStorage* ram) { uint32_t pageSize = (uint32_t) ffSysctlGetInt("hw.pagesize", 0); if(pageSize == 0) diff --git a/src/detection/memory/memory_linux.c b/src/detection/memory/memory_linux.c index 3a5d55ee0..339dda4fe 100644 --- a/src/detection/memory/memory_linux.c +++ b/src/detection/memory/memory_linux.c @@ -3,12 +3,12 @@ #include #include -void ffDetectMemoryImpl(FFMemoryStorage* swap) +void ffDetectMemory(FFMemoryStorage* ram) { FILE* meminfo = fopen("/proc/meminfo", "r"); if(meminfo == NULL) { - ffStrbufAppendS(&swap->error, "Failed to open /proc/meminfo"); + ffStrbufAppendS(&ram->error, "Failed to open /proc/meminfo"); return; } @@ -37,9 +37,9 @@ void ffDetectMemoryImpl(FFMemoryStorage* swap) fclose(meminfo); - swap->bytesTotal = memTotal * (uint64_t) 1024; - if(swap->bytesTotal == 0) - ffStrbufAppendS(&swap->error, "Failed to read MemTotal"); + ram->bytesTotal = memTotal * (uint64_t) 1024; + if(ram->bytesTotal == 0) + ffStrbufAppendS(&ram->error, "Failed to read MemTotal"); else - swap->bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * (uint64_t) 1024; + ram->bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * (uint64_t) 1024; } diff --git a/src/detection/memory/memory_windows.c b/src/detection/memory/memory_windows.c index 2072d7232..ef3153843 100644 --- a/src/detection/memory/memory_windows.c +++ b/src/detection/memory/memory_windows.c @@ -1,6 +1,6 @@ #include "memory.h" -void ffDetectMemoryImpl(FFMemoryStorage* ram) +void ffDetectMemory(FFMemoryStorage* ram) { MEMORYSTATUSEX statex = { .dwLength = sizeof(statex), diff --git a/src/detection/storage.h b/src/detection/storage.h index c4cce7ede..c8cf7ed0d 100644 --- a/src/detection/storage.h +++ b/src/detection/storage.h @@ -10,7 +10,6 @@ typedef struct FFMemoryStorage FFstrbuf error; uint64_t bytesUsed; uint64_t bytesTotal; - uint8_t percentage; } FFMemoryStorage; #endif diff --git a/src/detection/swap/swap.c b/src/detection/swap/swap.c deleted file mode 100644 index 0a790c9f7..000000000 --- a/src/detection/swap/swap.c +++ /dev/null @@ -1,13 +0,0 @@ -#include "swap.h" -#include "detection/internal.h" - -void ffDetectSwapImpl(FFMemoryStorage* swap); - -const FFMemoryStorage* ffDetectSwap() -{ - FF_DETECTION_INTERNAL_GUARD(FFMemoryStorage, - ffStrbufInit(&result.error); - - ffDetectSwapImpl(&result); - ); -} diff --git a/src/detection/swap/swap.h b/src/detection/swap/swap.h index a0374cf2b..246b2bfa3 100644 --- a/src/detection/swap/swap.h +++ b/src/detection/swap/swap.h @@ -5,6 +5,6 @@ #include "detection/storage.h" -const FFMemoryStorage* ffDetectSwap(); +void ffDetectSwap(FFMemoryStorage* result); #endif diff --git a/src/detection/swap/swap_apple.c b/src/detection/swap/swap_apple.c index 19ea32c00..34e72802a 100644 --- a/src/detection/swap/swap_apple.c +++ b/src/detection/swap/swap_apple.c @@ -3,7 +3,7 @@ #include -void ffDetectSwapImpl(FFMemoryStorage* swap) +void ffDetectSwap(FFMemoryStorage* swap) { struct xsw_usage xsw; size_t size = sizeof(xsw); diff --git a/src/detection/swap/swap_bsd.c b/src/detection/swap/swap_bsd.c index 8e8089600..b4c5fa7c7 100644 --- a/src/detection/swap/swap_bsd.c +++ b/src/detection/swap/swap_bsd.c @@ -1,7 +1,7 @@ #include "swap.h" #include "common/sysctl.h" -void ffDetectSwapImpl(FFMemoryStorage* swap) +void ffDetectSwap(FFMemoryStorage* swap) { swap->bytesTotal = (uint64_t)ffSysctlGetInt64("vm.swap_total", 0); swap->bytesUsed = (uint64_t)ffSysctlGetInt64("vm.swap_reserved", 0); diff --git a/src/detection/swap/swap_linux.c b/src/detection/swap/swap_linux.c index e176bb158..487b68bcf 100644 --- a/src/detection/swap/swap_linux.c +++ b/src/detection/swap/swap_linux.c @@ -3,7 +3,7 @@ #include #include -void ffDetectSwapImpl(FFMemoryStorage* swap) +void ffDetectSwap(FFMemoryStorage* swap) { FILE* meminfo = fopen("/proc/meminfo", "r"); if(meminfo == NULL) diff --git a/src/detection/swap/swap_windows.cpp b/src/detection/swap/swap_windows.cpp index 5e7597602..224a3ce35 100644 --- a/src/detection/swap/swap_windows.cpp +++ b/src/detection/swap/swap_windows.cpp @@ -9,7 +9,7 @@ extern "C" { #include extern "C" -void ffDetectSwapImpl(FFMemoryStorage* swap) +void ffDetectSwap(FFMemoryStorage* swap) { SYSTEM_INFO sysInfo; GetNativeSystemInfo(&sysInfo); diff --git a/src/fastfetch.h b/src/fastfetch.h index 3f592156d..a33dce50a 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -25,6 +25,7 @@ static inline void ffUnused(int dummy, ...) { (void) dummy; } #define FF_UNUSED(...) ffUnused(0, __VA_ARGS__); +#define FF_UNUSED_PARAM __attribute__ ((__unused__)) #define FASTFETCH_LOGO_MAX_COLORS 9 //two digits would make parsing much more complicated (index 1 - 9) diff --git a/src/modules/memory.c b/src/modules/memory.c index 5fab1b383..149eef45b 100644 --- a/src/modules/memory.c +++ b/src/modules/memory.c @@ -76,10 +76,18 @@ static void printMemory(FFinstance* instance, const char* name, const FFModuleAr void ffPrintMemory(FFinstance* instance) { - printMemory(instance, FF_MEMORY_MODULE_NAME, &instance->config.memory, ffDetectMemory()); + FFMemoryStorage result; + ffStrbufInit(&result.error); + ffDetectMemory(&result); + printMemory(instance, FF_MEMORY_MODULE_NAME, &instance->config.memory, &result); + ffStrbufDestroy(&result.error); } void ffPrintSwap(FFinstance* instance) { - printMemory(instance, FF_SWAP_MODULE_NAME, &instance->config.swap, ffDetectSwap()); + FFMemoryStorage result; + ffStrbufInit(&result.error); + ffDetectSwap(&result); + printMemory(instance, FF_SWAP_MODULE_NAME, &instance->config.swap, &result); + ffStrbufDestroy(&result.error); }