...

Source file src/runtime/mem_bsd.go

Documentation: runtime

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

View as plain text