]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: tidy OpenBSD sysctl code
authorAustin Clements <austin@google.com>
Fri, 29 Jun 2018 20:09:01 +0000 (16:09 -0400)
committerAustin Clements <austin@google.com>
Mon, 2 Jul 2018 14:44:21 +0000 (14:44 +0000)
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>
src/runtime/os_openbsd.go

index 73b01daec437a17c8286837768f8053e0e0d1ef1..1660511616965021dafc94d6ab90c48a633ca10d 100644 (file)
@@ -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
 }