From: Austin Clements Date: Fri, 29 Jun 2018 20:09:01 +0000 (-0400) Subject: runtime: tidy OpenBSD sysctl code X-Git-Tag: go1.11beta2~235 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=9daa35edf08bb76948368fecf388572a4a77c14f;p=gostls13.git 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 TryBot-Result: Gobot Gobot Reviewed-by: Matthew Dempsky --- diff --git a/src/runtime/os_openbsd.go b/src/runtime/os_openbsd.go index 73b01daec4..1660511616 100644 --- a/src/runtime/os_openbsd.go +++ b/src/runtime/os_openbsd.go @@ -88,37 +88,34 @@ const ( _HW_PAGESIZE = 7 ) -func getncpu() int32 { - mib := [2]uint32{_CTL_HW, _HW_NCPU} - out := uint32(0) +func sysctlInt(mib []uint32) (int32, bool) { + var out int32 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. - ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0) - if ret >= 0 { - return int32(out) + if ncpu, ok := sysctlInt([]uint32{_CTL_HW, _HW_NCPU}); ok { + return int32(ncpu) } return 1 } func getPageSize() uintptr { - mib := [2]uint32{_CTL_HW, _HW_PAGESIZE} - out := uint32(0) - nout := unsafe.Sizeof(out) - ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0) - if ret >= 0 { - return uintptr(out) + if ps, ok := sysctlInt([]uint32{_CTL_HW, _HW_PAGESIZE}); ok { + return uintptr(ps) } return 0 } -func getOSRev() int32 { - mib := [2]uint32{_CTL_KERN, _KERN_OSREV} - out := uint32(0) - nout := unsafe.Sizeof(out) - ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0) - if ret >= 0 { - return int32(out) +func getOSRev() int { + if osrev, ok := sysctlInt([]uint32{_CTL_KERN, _KERN_OSREV}); ok { + return int(osrev) } return 0 }