Files
rpi5-health/internal/memory/win_ram.go
T
2026-10-05 18:19:36 +02:00

56 lines
1.3 KiB
Go

//go:build windows
package memory
import (
"log"
"unsafe"
"rpi5-health/internal/windows_system"
"golang.org/x/sys/windows"
)
type DWORD uint32
type DWORDLONG uint64
type memoryStatusEx struct {
/*
* Struct for storing memory status information.
*
* It gets populated via syscall.
*/
dwLength DWORD
dwMemoryLoad DWORD
ullTotalPhys DWORDLONG
ullAvailPhys DWORDLONG
ullTotalPageFile DWORDLONG
ullAvailPageFile DWORDLONG
ullTotalVirtual DWORDLONG
ullAvailVirtual DWORDLONG
ullAvailExtendedVirtual DWORDLONG
}
func getWinRamInfo(procGlobalMemoryStatusEx *windows.LazyProc) MemoryInformation {
var mem memoryStatusEx
// * Windows requires the dwLength field to be set to the struct's size
// * before execution so it knows which OS struct version is being used.
mem.dwLength = DWORD(unsafe.Sizeof(mem))
ret, _, err := procGlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(&mem))) // exec syscall
if ret == 0 { // winapi syscalls return 0 on failure
log.Fatalf("GlobalMemoryStatusEx failed: %v", err)
}
return MemoryInformation{
SystemMemory: uint64(mem.ullTotalPhys),
AvailableMemory: uint64(mem.ullAvailPhys),
}
}
func GetRamInformation() (MemoryInformation, error) {
return getWinRamInfo(windows_system.GetWindowsTools().ProcGlobalMemoryStatusEx), nil
}