github.com/mtsmfm/go/src@v0.0.0-20221020090648-44bdcb9f8fde/runtime/mem_bsd.go (about)

     1  // Copyright 2010 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 dragonfly || freebsd || netbsd || openbsd || solaris
     6  
     7  package runtime
     8  
     9  import (
    10  	"unsafe"
    11  )
    12  
    13  // Don't split the stack as this function may be invoked without a valid G,
    14  // which prevents us from allocating more stack.
    15  //
    16  //go:nosplit
    17  func sysAllocOS(n uintptr) unsafe.Pointer {
    18  	v, err := mmap(nil, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_PRIVATE, -1, 0)
    19  	if err != 0 {
    20  		return nil
    21  	}
    22  	return v
    23  }
    24  
    25  func sysUnusedOS(v unsafe.Pointer, n uintptr) {
    26  	if debug.madvdontneed != 0 {
    27  		madvise(v, n, _MADV_DONTNEED)
    28  	} else {
    29  		madvise(v, n, _MADV_FREE)
    30  	}
    31  }
    32  
    33  func sysUsedOS(v unsafe.Pointer, n uintptr) {
    34  }
    35  
    36  func sysHugePageOS(v unsafe.Pointer, n uintptr) {
    37  }
    38  
    39  // Don't split the stack as this function may be invoked without a valid G,
    40  // which prevents us from allocating more stack.
    41  //
    42  //go:nosplit
    43  func sysFreeOS(v unsafe.Pointer, n uintptr) {
    44  	munmap(v, n)
    45  }
    46  
    47  func sysFaultOS(v unsafe.Pointer, n uintptr) {
    48  	mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE|_MAP_FIXED, -1, 0)
    49  }
    50  
    51  // Indicates not to reserve swap space for the mapping.
    52  const _sunosMAP_NORESERVE = 0x40
    53  
    54  func sysReserveOS(v unsafe.Pointer, n uintptr) unsafe.Pointer {
    55  	flags := int32(_MAP_ANON | _MAP_PRIVATE)
    56  	if GOOS == "solaris" || GOOS == "illumos" {
    57  		// Be explicit that we don't want to reserve swap space
    58  		// for PROT_NONE anonymous mappings. This avoids an issue
    59  		// wherein large mappings can cause fork to fail.
    60  		flags |= _sunosMAP_NORESERVE
    61  	}
    62  	p, err := mmap(v, n, _PROT_NONE, flags, -1, 0)
    63  	if err != 0 {
    64  		return nil
    65  	}
    66  	return p
    67  }
    68  
    69  const _sunosEAGAIN = 11
    70  const _ENOMEM = 12
    71  
    72  func sysMapOS(v unsafe.Pointer, n uintptr) {
    73  	p, err := mmap(v, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_FIXED|_MAP_PRIVATE, -1, 0)
    74  	if err == _ENOMEM || ((GOOS == "solaris" || GOOS == "illumos") && err == _sunosEAGAIN) {
    75  		throw("runtime: out of memory")
    76  	}
    77  	if p != v || err != 0 {
    78  		print("runtime: mmap(", v, ", ", n, ") returned ", p, ", ", err, "\n")
    79  		throw("runtime: cannot map pages in arena address space")
    80  	}
    81  }