Source file src/runtime/mem_bsd.go

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

View as plain text