Skip to content

Commit

Permalink
[mem][windows]: add ExWindows and implement VirualTotal/Avail
Browse files Browse the repository at this point in the history
This commit fixes #1588. Thank you!
  • Loading branch information
shirou committed Feb 24, 2024
1 parent 80e3b97 commit 00ddec0
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions mem/ex_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: BSD-3-Clause
//go:build windows

package mem

import (
"unsafe"

"golang.org/x/sys/windows"
)

// ExWindows represents Windows specific information
// https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-memorystatusex
type ExWindows struct {

Check failure on line 14 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / test (1.21.7, windows-2022)

other declaration of ExWindows

Check failure on line 14 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / test (1.22.0, windows-2022)

other declaration of ExWindows

Check failure on line 14 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / build_test_v3 (1.21.7)

other declaration of ExWindows

Check failure on line 14 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / build_test_v3 (1.22.0)

other declaration of ExWindows

Check failure on line 14 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / test (1.21.7, windows-2022)

other declaration of ExWindows

Check failure on line 14 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / test (1.21.7, windows-2019)

other declaration of ExWindows

Check failure on line 14 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / test (1.22.0, windows-2022)

other declaration of ExWindows

Check failure on line 14 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / test (1.22.0, windows-2019)

other declaration of ExWindows

Check failure on line 14 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / test (1.22.0, windows-2019)

other declaration of ExWindows

Check failure on line 14 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / test (1.21.7, windows-2019)

other declaration of ExWindows

Check failure on line 14 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / build_test_v3 (1.21.7)

other declaration of ExWindows

Check failure on line 14 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / build_test_v3 (1.22.0)

other declaration of ExWindows
VirtualTotal uint64 `json:"virtualTotal"`
VirtualAvail uint64 `json:"virtualAvail"`
}

type ExWindows struct{}

Check failure on line 19 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / test (1.21.7, windows-2022)

ExWindows redeclared in this block

Check failure on line 19 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / test (1.22.0, windows-2022)

ExWindows redeclared in this block

Check failure on line 19 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / build_test_v3 (1.21.7)

ExWindows redeclared in this block

Check failure on line 19 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / build_test_v3 (1.22.0)

ExWindows redeclared in this block

Check failure on line 19 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / test (1.21.7, windows-2022)

ExWindows redeclared in this block

Check failure on line 19 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / test (1.21.7, windows-2019)

ExWindows redeclared in this block

Check failure on line 19 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / test (1.22.0, windows-2022)

ExWindows redeclared in this block

Check failure on line 19 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / test (1.22.0, windows-2019)

ExWindows redeclared in this block

Check failure on line 19 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / test (1.22.0, windows-2019)

ExWindows redeclared in this block

Check failure on line 19 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / test (1.21.7, windows-2019)

ExWindows redeclared in this block

Check failure on line 19 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / build_test_v3 (1.21.7)

ExWindows redeclared in this block

Check failure on line 19 in mem/ex_windows.go

View workflow job for this annotation

GitHub Actions / build_test_v3 (1.22.0)

ExWindows redeclared in this block

func NewExWindows() *ExWindows {
return &ExWindows{}
}

func (e *ExWindows) VirtualMemory() (*ExWindows, 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 := &ExWindows{
VirtualTotal: memInfo.ullTotalVirtual,
VirtualAvail: memInfo.ullAvailVirtual,
}

return ret, nil
}

0 comments on commit 00ddec0

Please sign in to comment.