github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/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  func sysNoHugePageOS(v unsafe.Pointer, n uintptr) {
    40  }
    41  
    42  // Don't split the stack as this function may be invoked without a valid G,
    43  // which prevents us from allocating more stack.
    44  //
    45  //go:nosplit
    46  func sysFreeOS(v unsafe.Pointer, n uintptr) {
    47  	munmap(v, n)
    48  }
    49  
    50  func sysFaultOS(v unsafe.Pointer, n uintptr) {
    51  	mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE|_MAP_FIXED, -1, 0)
    52  }
    53  
    54  // Indicates not to reserve swap space for the mapping.
    55  const _sunosMAP_NORESERVE = 0x40
    56  
    57  func sysReserveOS(v unsafe.Pointer, n uintptr) unsafe.Pointer {
    58  	flags := int32(_MAP_ANON | _MAP_PRIVATE)
    59  	if GOOS == "solaris" || GOOS == "illumos" {
    60  		// Be explicit that we don't want to reserve swap space
    61  		// for PROT_NONE anonymous mappings. This avoids an issue
    62  		// wherein large mappings can cause fork to fail.
    63  		flags |= _sunosMAP_NORESERVE
    64  	}
    65  	p, err := mmap(v, n, _PROT_NONE, flags, -1, 0)
    66  	if err != 0 {
    67  		return nil
    68  	}
    69  	return p
    70  }
    71  
    72  const _sunosEAGAIN = 11
    73  const _ENOMEM = 12
    74  
    75  func sysMapOS(v unsafe.Pointer, n uintptr) {
    76  	p, err := mmap(v, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_FIXED|_MAP_PRIVATE, -1, 0)
    77  	if err == _ENOMEM || ((GOOS == "solaris" || GOOS == "illumos") && err == _sunosEAGAIN) {
    78  		throw("runtime: out of memory")
    79  	}
    80  	if p != v || err != 0 {
    81  		print("runtime: mmap(", v, ", ", n, ") returned ", p, ", ", err, "\n")
    82  		throw("runtime: cannot map pages in arena address space")
    83  	}
    84  }