| 1 |
// Copyright 2019-2025 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
//go:build windows |
| 15 |
|
| 16 |
package sysmem |
| 17 |
|
| 18 |
import ( |
| 19 |
"unsafe" |
| 20 |
|
| 21 |
"golang.org/x/sys/windows" |
| 22 |
) |
| 23 |
|
| 24 |
var winKernel32 = windows.NewLazySystemDLL("kernel32.dll") |
| 25 |
var winGlobalMemoryStatusEx = winKernel32.NewProc("GlobalMemoryStatusEx") |
| 26 |
|
| 27 |
func init() { |
| 28 |
if err := winKernel32.Load(); err != nil { |
| 29 |
panic(err) |
| 30 |
} |
| 31 |
if err := winGlobalMemoryStatusEx.Find(); err != nil { |
| 32 |
panic(err) |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
// https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-memorystatusex |
| 37 |
type _memoryStatusEx struct { |
| 38 |
dwLength uint32 |
| 39 |
dwMemoryLoad uint32 |
| 40 |
ullTotalPhys uint64 |
| 41 |
unused [6]uint64 // ignore rest of struct |
| 42 |
} |
| 43 |
|
| 44 |
func Memory() int64 { |
| 45 |
msx := &_memoryStatusEx{dwLength: 64} |
| 46 |
res, _, _ := winGlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(msx))) |
| 47 |
if res == 0 { |
| 48 |
return 0 |
| 49 |
} |
| 50 |
return int64(msx.ullTotalPhys) |
| 51 |
} |