master ohmu/fastfetch-openbsd / src / detection / swap / swap_windows.c
ohmu  ·  2026-10-15
 1#include "swap.h"
 2#include "common/debug.h"
 3#include "common/windows/unicode.h"
 4
 5#include <winternl.h>
 6#include <ntstatus.h>
 7#include <windows.h>
 8#include <stdalign.h>
 9
10const char* ffDetectSwap(FFlist* result) {
11    alignas(SYSTEM_PAGEFILE_INFORMATION) uint8_t buffer[4096];
12    ULONG size = sizeof(buffer);
13    SYSTEM_PAGEFILE_INFORMATION* pstart = (SYSTEM_PAGEFILE_INFORMATION*) buffer;
14    NTSTATUS status = NtQuerySystemInformation(SystemPagefileInformation, pstart, size, &size);
15    if (!NT_SUCCESS(status)) {
16        FF_DEBUG("NtQuerySystemInformation(SystemPagefileInformation, size) failed: %s", ffDebugNtStatus(status));
17        return "NtQuerySystemInformation(SystemPagefileInformation, size) failed";
18    }
19
20    if (size == 0) {
21        return nullptr;
22    }
23
24    const uint32_t pageSizeShift = instance.state.platform.sysinfo.pageSizeShift;
25    for (SYSTEM_PAGEFILE_INFORMATION* current = pstart;; current = (SYSTEM_PAGEFILE_INFORMATION*) ((uint8_t*) current + current->NextEntryOffset)) {
26        FFSwapResult* swap = FF_LIST_ADD(FFSwapResult, *result);
27        ffStrbufInitNWS(&swap->name, current->FileName.Length / sizeof(wchar_t), current->FileName.Buffer);
28        if (ffStrbufStartsWithS(&swap->name, "\\??\\")) {
29            ffStrbufSubstrAfter(&swap->name, strlen("\\??\\") - 1);
30        }
31        swap->bytesUsed = (uint64_t) current->TotalUsed << pageSizeShift;
32        swap->bytesTotal = (uint64_t) current->CurrentSize << pageSizeShift;
33        if (current->NextEntryOffset == 0) {
34            break;
35        }
36    }
37    return nullptr;
38}