@ -40,9 +40,11 @@ func VirtualMemory() (*VirtualMemoryStat, error) {
func VirtualMemoryWithContext ( _ context . Context ) ( * VirtualMemoryStat , error ) {
func VirtualMemoryWithContext ( _ context . Context ) ( * VirtualMemoryStat , error ) {
var memInfo memoryStatusEx
var memInfo memoryStatusEx
memInfo . cbSize = uint32 ( unsafe . Sizeof ( memInfo ) )
memInfo . cbSize = uint32 ( unsafe . Sizeof ( memInfo ) )
mem , _ , _ := procGlobalMemoryStatusEx . Call ( uintptr ( unsafe . Pointer ( & memInfo ) ) )
// GlobalMemoryStatusEx returns 0 for error, in which case we check err,
// see https://pkg.go.dev/golang.org/x/sys/windows#LazyProc.Call
mem , _ , err := procGlobalMemoryStatusEx . Call ( uintptr ( unsafe . Pointer ( & memInfo ) ) )
if mem == 0 {
if mem == 0 {
return nil , windows . GetLastError ( )
return nil , err
}
}
ret := & VirtualMemoryStat {
ret := & VirtualMemoryStat {
@ -93,9 +95,11 @@ func SwapMemoryWithContext(_ context.Context) (*SwapMemoryStat, error) {
// Get total memory from performance information
// Get total memory from performance information
var perfInfo performanceInformation
var perfInfo performanceInformation
perfInfo . cb = uint32 ( unsafe . Sizeof ( perfInfo ) )
perfInfo . cb = uint32 ( unsafe . Sizeof ( perfInfo ) )
mem , _ , _ := procGetPerformanceInfo . Call ( uintptr ( unsafe . Pointer ( & perfInfo ) ) , uintptr ( perfInfo . cb ) )
// GetPerformanceInfo returns 0 for error, in which case we check err,
// see https://pkg.go.dev/golang.org/x/sys/windows#LazyProc.Call
mem , _ , err := procGetPerformanceInfo . Call ( uintptr ( unsafe . Pointer ( & perfInfo ) ) , uintptr ( perfInfo . cb ) )
if mem == 0 {
if mem == 0 {
return nil , windows . GetLastError ( )
return nil , err
}
}
totalPhys := perfInfo . physicalTotal * perfInfo . pageSize
totalPhys := perfInfo . physicalTotal * perfInfo . pageSize
totalSys := perfInfo . commitLimit * perfInfo . pageSize
totalSys := perfInfo . commitLimit * perfInfo . pageSize
@ -161,9 +165,11 @@ func SwapDevicesWithContext(_ context.Context) ([]*SwapDevice, error) {
// the following system call invokes the supplied callback function once for each page file before returning
// the following system call invokes the supplied callback function once for each page file before returning
// see https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-enumpagefilesw
// see https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-enumpagefilesw
var swapDevices [ ] * SwapDevice
var swapDevices [ ] * SwapDevice
result , _ , _ := procEnumPageFilesW . Call ( windows . NewCallback ( pEnumPageFileCallbackW ) , uintptr ( unsafe . Pointer ( & swapDevices ) ) )
// EnumPageFilesW returns 0 for error, in which case we check err,
// see https://pkg.go.dev/golang.org/x/sys/windows#LazyProc.Call
result , _ , err := procEnumPageFilesW . Call ( windows . NewCallback ( pEnumPageFileCallbackW ) , uintptr ( unsafe . Pointer ( & swapDevices ) ) )
if result == 0 {
if result == 0 {
return nil , windows . GetLastError ( )
return nil , err
}
}
return swapDevices , nil
return swapDevices , nil