Source file src/internal/cpu/cpu_darwin.go

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build darwin && !ios
     6  
     7  package cpu
     8  
     9  import _ "unsafe" // for linkname
    10  
    11  // Pushed from runtime.
    12  //
    13  //go:noescape
    14  func sysctlbynameInt32(name []byte) (int32, int32)
    15  
    16  // Pushed from runtime.
    17  //
    18  //go:noescape
    19  func sysctlbynameBytes(name, out []byte) int32
    20  
    21  // sysctlEnabled should be an internal detail,
    22  // but widely used packages access it using linkname.
    23  // Notable members of the hall of shame include:
    24  //   - github.com/bytedance/gopkg
    25  //   - github.com/songzhibin97/gkit
    26  //
    27  // Do not remove or change the type signature.
    28  // See go.dev/issue/67401.
    29  //
    30  //go:linkname sysctlEnabled
    31  func sysctlEnabled(name []byte) bool {
    32  	ret, value := sysctlbynameInt32(name)
    33  	if ret < 0 {
    34  		return false
    35  	}
    36  	return value > 0
    37  }
    38  
    39  // darwinKernelVersionCheck reports if Darwin kernel version is at
    40  // least major.minor.patch.
    41  //
    42  // Code borrowed from x/sys/cpu.
    43  func darwinKernelVersionCheck(major, minor, patch int) bool {
    44  	var release [256]byte
    45  	ret := sysctlbynameBytes([]byte("kern.osrelease\x00"), release[:])
    46  	if ret < 0 {
    47  		return false
    48  	}
    49  
    50  	var mmp [3]int
    51  	c := 0
    52  Loop:
    53  	for _, b := range release[:] {
    54  		switch {
    55  		case b >= '0' && b <= '9':
    56  			mmp[c] = 10*mmp[c] + int(b-'0')
    57  		case b == '.':
    58  			c++
    59  			if c > 2 {
    60  				return false
    61  			}
    62  		case b == 0:
    63  			break Loop
    64  		default:
    65  			return false
    66  		}
    67  	}
    68  	if c != 2 {
    69  		return false
    70  	}
    71  	return mmp[0] > major || mmp[0] == major && (mmp[1] > minor || mmp[1] == minor && mmp[2] >= patch)
    72  }
    73  

View as plain text