github.com/s1s1ty/go@v0.0.0-20180207192209-104445e3140f/src/runtime/cgo_mmap.go (about)

     1  // Copyright 2015 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  // Support for memory sanitizer. See runtime/cgo/mmap.go.
     6  
     7  // +build linux,amd64
     8  
     9  package runtime
    10  
    11  import "unsafe"
    12  
    13  // _cgo_mmap is filled in by runtime/cgo when it is linked into the
    14  // program, so it is only non-nil when using cgo.
    15  //go:linkname _cgo_mmap _cgo_mmap
    16  var _cgo_mmap unsafe.Pointer
    17  
    18  // _cgo_munmap is filled in by runtime/cgo when it is linked into the
    19  // program, so it is only non-nil when using cgo.
    20  //go:linkname _cgo_munmap _cgo_munmap
    21  var _cgo_munmap unsafe.Pointer
    22  
    23  func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) (unsafe.Pointer, int) {
    24  	if _cgo_mmap != nil {
    25  		// Make ret a uintptr so that writing to it in the
    26  		// function literal does not trigger a write barrier.
    27  		// A write barrier here could break because of the way
    28  		// that mmap uses the same value both as a pointer and
    29  		// an errno value.
    30  		var ret uintptr
    31  		systemstack(func() {
    32  			ret = callCgoMmap(addr, n, prot, flags, fd, off)
    33  		})
    34  		if ret < 4096 {
    35  			return nil, int(ret)
    36  		}
    37  		return unsafe.Pointer(ret), 0
    38  	}
    39  	return sysMmap(addr, n, prot, flags, fd, off)
    40  }
    41  
    42  func munmap(addr unsafe.Pointer, n uintptr) {
    43  	if _cgo_munmap != nil {
    44  		systemstack(func() { callCgoMunmap(addr, n) })
    45  		return
    46  	}
    47  	sysMunmap(addr, n)
    48  }
    49  
    50  // sysMmap calls the mmap system call. It is implemented in assembly.
    51  func sysMmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) (p unsafe.Pointer, err int)
    52  
    53  // callCgoMmap calls the mmap function in the runtime/cgo package
    54  // using the GCC calling convention. It is implemented in assembly.
    55  func callCgoMmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) uintptr
    56  
    57  // sysMunmap calls the munmap system call. It is implemented in assembly.
    58  func sysMunmap(addr unsafe.Pointer, n uintptr)
    59  
    60  // callCgoMunmap calls the munmap function in the runtime/cgo package
    61  // using the GCC calling convention. It is implemented in assembly.
    62  func callCgoMunmap(addr unsafe.Pointer, n uintptr)