github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/sysinfo/memtotal_darwin.go (about)

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