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 Apr 22, 2024
1 parent bedf6ab commit 139aca4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
4 changes: 1 addition & 3 deletions cpu/cpu_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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"
)

// 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
}

0 comments on commit 139aca4

Please sign in to comment.