master ohmu/fastfetch-openbsd / src / detection / memory / memory_windows.c
ohmu  ·  2026-10-15
 1#include "memory.h"
 2#include "common/debug.h"
 3
 4#include "common/windows/nt.h"
 5
 6const char* ffDetectMemory(FFMemoryResult* ram) {
 7    // Note: GlobalMemoryStatusEx() internally uses SystemMemoryUsageInformation in Win 10
 8
 9    SYSTEM_BASIC_PERFORMANCE_INFORMATION sbpi;
10    NTSTATUS status = NtQuerySystemInformation(SystemBasicPerformanceInformation, &sbpi, sizeof(sbpi), NULL);
11    if (!NT_SUCCESS(status)) {
12        FF_DEBUG("Failed to query memory information: %s", ffDebugNtStatus(status));
13        return "Failed to query memory information";
14    }
15
16    uint64_t phyPages =
17    #if _WIN64
18        SharedUserData->FullNumberOfPhysicalPages;
19    if (__builtin_expect(phyPages == 0, false)) { // Not supported on Windows 8.1
20        phyPages = SharedUserData->NumberOfPhysicalPages; // ULONG, maximum 16 TB
21    }
22    #else
23        SharedUserData->NumberOfPhysicalPages;
24    #endif
25
26    uint64_t pageSizeShift = instance.state.platform.sysinfo.pageSizeShift;
27    ram->bytesTotal = phyPages << pageSizeShift;
28    ram->bytesUsed = (phyPages - sbpi.AvailablePages) << pageSizeShift;
29    return nullptr;
30}