Memory / Swap (Windows): simplify implementation

This commit is contained in:
李通洲 2023-08-05 00:29:04 +08:00
parent 819c78e74c
commit 0c2edd4064
2 changed files with 15 additions and 24 deletions

View File

@ -1,16 +1,19 @@
#include "memory.h"
#include <winternl.h>
#include <ntstatus.h>
#include <windows.h>
const char* ffDetectMemory(FFMemoryResult* ram)
{
MEMORYSTATUSEX statex = {
.dwLength = sizeof(statex),
};
if (!GlobalMemoryStatusEx(&statex))
return "GlobalMemoryStatusEx() failed";
SYSTEM_INFO sysInfo;
GetNativeSystemInfo(&sysInfo);
SYSTEM_PERFORMANCE_INFORMATION info;
if (!NT_SUCCESS(NtQuerySystemInformation(SystemPerformanceInformation, &info, sizeof(info), NULL)))
return "NtQuerySystemInformation(SystemPerformanceInformation) failed";
ram->bytesUsed = (uint64_t)(info.TotalCommitLimit - info.AvailablePages) * sysInfo.dwPageSize;
ram->bytesTotal = (uint64_t)info.TotalCommitLimit * sysInfo.dwPageSize;
ram->bytesTotal = statex.ullTotalPhys;
ram->bytesUsed = statex.ullTotalPhys - statex.ullAvailPhys;
return NULL;
}

View File

@ -1,5 +1,4 @@
#include "swap.h"
#include "util/mallocHelper.h"
#include <winternl.h>
#include <ntstatus.h>
@ -10,22 +9,11 @@ const char* ffDetectSwap(FFSwapResult* swap)
SYSTEM_INFO sysInfo;
GetNativeSystemInfo(&sysInfo);
ULONG size = sizeof(SYSTEM_PAGEFILE_INFORMATION);
SYSTEM_PAGEFILE_INFORMATION* FF_AUTO_FREE pstart = (SYSTEM_PAGEFILE_INFORMATION*)malloc(size);
while(true)
{
NTSTATUS status = NtQuerySystemInformation(SystemPagefileInformation, pstart, size, &size);
if(status == STATUS_INFO_LENGTH_MISMATCH)
{
if(!(pstart = (SYSTEM_PAGEFILE_INFORMATION*)realloc(pstart, size)))
return "realloc(pstart, size) failed";
}
else if(!NT_SUCCESS(status))
return "NtQuerySystemInformation(SystemPagefileInformation, size) failed";
break;
}
swap->bytesUsed = (uint64_t)pstart->TotalUsed * sysInfo.dwPageSize;
swap->bytesTotal = (uint64_t)pstart->CurrentSize * sysInfo.dwPageSize;
SYSTEM_PAGEFILE_INFORMATION info;
if (!NT_SUCCESS(NtQuerySystemInformation(SystemPagefileInformation, &info, sizeof(info), NULL)))
return "NtQuerySystemInformation(SystemPagefileInformation) failed";
swap->bytesUsed = (uint64_t)info.TotalUsed * sysInfo.dwPageSize;
swap->bytesTotal = (uint64_t)info.CurrentSize * sysInfo.dwPageSize;
return NULL;
}