From 3a586b35159dc664b5a5f527eaf6e7e0dd63d322 Mon Sep 17 00:00:00 2001 From: shirou Date: Sat, 24 Feb 2024 04:24:34 +0000 Subject: [PATCH] [mem][windows]: add ExWindows and implement VirualTotal/Avail This commit fixes #1588. Thank you! --- cpu/cpu_windows.go | 4 +--- mem/ex_windows.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 mem/ex_windows.go diff --git a/cpu/cpu_windows.go b/cpu/cpu_windows.go index c9136b75d..4476b91cb 100644 --- a/cpu/cpu_windows.go +++ b/cpu/cpu_windows.go @@ -13,9 +13,7 @@ import ( "golang.org/x/sys/windows" ) -var ( - procGetNativeSystemInfo = common.Modkernel32.NewProc("GetNativeSystemInfo") -) +var procGetNativeSystemInfo = common.Modkernel32.NewProc("GetNativeSystemInfo") type win32_Processor struct { Family uint16 diff --git a/mem/ex_windows.go b/mem/ex_windows.go new file mode 100644 index 000000000..4f1573b3c --- /dev/null +++ b/mem/ex_windows.go @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: BSD-3-Clause +//go:build windows + +package mem + +import ( + "unsafe" + + "golang.org/x/sys/windows" +) + +// ExVirtualMemory represents Windows specific information +// https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-memorystatusex +type ExVirtualMemory struct { + VirtualTotal uint64 `json:"virtualTotal"` + VirtualAvail uint64 `json:"virtualAvail"` +} + +type ExWindows struct{} + +func NewExWindows() *ExWindows { + return &ExWindows{} +} + +func (e *ExWindows) VirtualMemory() (*ExVirtualMemory, error) { + var memInfo memoryStatusEx + memInfo.cbSize = uint32(unsafe.Sizeof(memInfo)) + mem, _, _ := procGlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(&memInfo))) + if mem == 0 { + return nil, windows.GetLastError() + } + + ret := &ExVirtualMemory{ + VirtualTotal: memInfo.ullTotalVirtual, + VirtualAvail: memInfo.ullAvailVirtual, + } + + return ret, nil +}