1
0
mirror of https://github.com/golang/go synced 2024-11-23 16:40:03 -07:00

runtime: tidy OpenBSD sysctl code

The OpenBSD sysctl code has been copy-pasted three times now. Abstract
it.

Change-Id: Ia5558927f0bc2b218b5af425dab368b5485d266c
Reviewed-on: https://go-review.googlesource.com/121775
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Austin Clements 2018-06-29 16:09:01 -04:00
parent 869884daea
commit 9daa35edf0

View File

@ -88,37 +88,34 @@ const (
_HW_PAGESIZE = 7 _HW_PAGESIZE = 7
) )
func getncpu() int32 { func sysctlInt(mib []uint32) (int32, bool) {
mib := [2]uint32{_CTL_HW, _HW_NCPU} var out int32
out := uint32(0)
nout := unsafe.Sizeof(out) nout := unsafe.Sizeof(out)
ret := sysctl(&mib[0], uint32(len(mib)), (*byte)(unsafe.Pointer(&out)), &nout, nil, 0)
if ret < 0 {
return 0, false
}
return out, true
}
func getncpu() int32 {
// Fetch hw.ncpu via sysctl. // Fetch hw.ncpu via sysctl.
ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0) if ncpu, ok := sysctlInt([]uint32{_CTL_HW, _HW_NCPU}); ok {
if ret >= 0 { return int32(ncpu)
return int32(out)
} }
return 1 return 1
} }
func getPageSize() uintptr { func getPageSize() uintptr {
mib := [2]uint32{_CTL_HW, _HW_PAGESIZE} if ps, ok := sysctlInt([]uint32{_CTL_HW, _HW_PAGESIZE}); ok {
out := uint32(0) return uintptr(ps)
nout := unsafe.Sizeof(out)
ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0)
if ret >= 0 {
return uintptr(out)
} }
return 0 return 0
} }
func getOSRev() int32 { func getOSRev() int {
mib := [2]uint32{_CTL_KERN, _KERN_OSREV} if osrev, ok := sysctlInt([]uint32{_CTL_KERN, _KERN_OSREV}); ok {
out := uint32(0) return int(osrev)
nout := unsafe.Sizeof(out)
ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0)
if ret >= 0 {
return int32(out)
} }
return 0 return 0
} }