-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[mem][windows]: add ExWindows and implement VirualTotal/Avail
This commit fixes #1588. Thank you!
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |