Black Lives Matter. Support the Equal Justice Initiative.

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

View as plain text