memsize_windows.go 791 B

12345678910111213141516171819202122232425262728293031
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package ur
  7. import (
  8. "encoding/binary"
  9. "syscall"
  10. "unsafe"
  11. )
  12. var (
  13. kernel32, _ = syscall.LoadLibrary("kernel32.dll")
  14. globalMemoryStatusEx, _ = syscall.GetProcAddress(kernel32, "GlobalMemoryStatusEx")
  15. )
  16. func memorySize() int64 {
  17. var memoryStatusEx [64]byte
  18. binary.LittleEndian.PutUint32(memoryStatusEx[:], 64)
  19. ret, _, _ := syscall.Syscall(uintptr(globalMemoryStatusEx), 1, uintptr(unsafe.Pointer(&memoryStatusEx[0])), 0, 0)
  20. if ret == 0 {
  21. return 0
  22. }
  23. return int64(binary.LittleEndian.Uint64(memoryStatusEx[8:]))
  24. }