memtotal_darwin.go 594 B

1234567891011121314151617181920212223242526272829
  1. package sysinfo
  2. import (
  3. "syscall"
  4. "unsafe"
  5. )
  6. // PhysicalMemoryBytes returns the total amount of host memory.
  7. func PhysicalMemoryBytes() (uint64, error) {
  8. mib := []int32{6 /* CTL_HW */, 24 /* HW_MEMSIZE */}
  9. buf := make([]byte, 8)
  10. bufLen := uintptr(8)
  11. _, _, e1 := syscall.Syscall6(syscall.SYS___SYSCTL,
  12. uintptr(unsafe.Pointer(&mib[0])), uintptr(len(mib)),
  13. uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&bufLen)),
  14. uintptr(0), uintptr(0))
  15. if e1 != 0 {
  16. return 0, e1
  17. }
  18. if bufLen != 8 {
  19. return 0, syscall.EIO
  20. }
  21. return *(*uint64)(unsafe.Pointer(&buf[0])), nil
  22. }