github.com/go-darwin/sys@v0.0.0-20220510002607-68fd01f054ca/cgo.go (about)

     1  // Copyright 2021 The Go Darwin Authors
     2  // SPDX-License-Identifier: BSD-3-Clause
     3  
     4  //go:build amd64 && gc
     5  // +build amd64,gc
     6  
     7  package sys
     8  
     9  import (
    10  	_ "runtime" // for go:linkname
    11  	"unsafe"
    12  )
    13  
    14  //go:linkname cgocall runtime.cgocall
    15  //go:noescape
    16  //go:nosplit
    17  func cgocall(fn unsafe.Pointer, arg uintptr) int32
    18  
    19  // CgoCall calls cgo fn function.
    20  //
    21  //go:nosplit
    22  func CgoCall(fn unsafe.Pointer, arg uintptr) int32 {
    23  	return cgocall(fn, arg)
    24  }
    25  
    26  // CString emulates C.String function without cgo.
    27  //
    28  //go:nosplit
    29  func CString(s string) *C_char {
    30  	n := len(s)
    31  	ret := make([]byte, n+1)
    32  	copy(ret, s)
    33  	ret[n] = '\x00'
    34  
    35  	return (*C_char)(unsafe.Pointer(&ret[0]))
    36  }
    37  
    38  // CBytes emulates C.Bytes function without cgo.
    39  //
    40  //go:nosplit
    41  func CBytes(b []byte) uintptr {
    42  	p := (*[]byte)(unsafe.Pointer(&make([]byte, unsafe.Sizeof([]byte("")))[0]))
    43  	*p = b
    44  
    45  	return uintptr(unsafe.Pointer(&p))
    46  }