Source file src/runtime/cgo/cgo_linux_mmap.go

     1  // Copyright 2026 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 goexperiment.runtimecgopurego && !msan && !tsan && ((linux && (amd64 || arm64 || loong64 || riscv64)) || (freebsd && amd64))
     6  
     7  package cgo
     8  
     9  import "unsafe"
    10  
    11  // Despite the file name, this file is also used on freebsd/amd64, where the
    12  // runtime calls mmap and munmap through runtime/cgo as on Linux.
    13  
    14  //go:linkname _cgo_libc_mmap _cgo_libc_mmap
    15  var _cgo_libc_mmap byte
    16  var libc_mmap = unsafe.Pointer(&_cgo_libc_mmap)
    17  
    18  //go:linkname _cgo_libc_munmap _cgo_libc_munmap
    19  var _cgo_libc_munmap byte
    20  var libc_munmap = unsafe.Pointer(&_cgo_libc_munmap)
    21  
    22  //go:linknamestd _cgoMmap runtime.cgoMmap
    23  var _cgoMmap = cgoMmap
    24  
    25  //go:nosplit
    26  //go:norace
    27  //go:nocheckptr
    28  func cgoMmap(addr unsafe.Pointer, length uintptr, prot, flags, fd int32, offset uint32) uintptr {
    29  	tsanAcquire()
    30  	p := asmcgocall(uintptr(libc_mmap), uintptr(addr), length, uintptr(prot), uintptr(flags), uintptr(fd), uintptr(offset))
    31  	if int(p) == -1 {
    32  		errnoPtr := asmcgocall(uintptr(libc_errno_location), 0, 0, 0, 0, 0, 0)
    33  		errno := uintptr(*(*int32)(unsafe.Pointer(errnoPtr)))
    34  		tsanRelease()
    35  		return errno
    36  	}
    37  	tsanRelease()
    38  	return p
    39  }
    40  
    41  //go:linknamestd _cgoMunmap runtime.cgoMunmap
    42  var _cgoMunmap = cgoMunmap
    43  
    44  //go:nosplit
    45  //go:norace
    46  func cgoMunmap(addr unsafe.Pointer, length uintptr) int32 {
    47  	tsanAcquire()
    48  	r := int32(asmcgocall(uintptr(libc_munmap), uintptr(addr), length, 0, 0, 0, 0))
    49  	tsanRelease()
    50  	return r
    51  }
    52  

View as plain text