...
Run Format

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  // +build darwin dragonfly freebsd nacl 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  //go:nosplit
    16  func sysAlloc(n uintptr, sysStat *uint64) unsafe.Pointer {
    17  	v, err := mmap(nil, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_PRIVATE, -1, 0)
    18  	if err != 0 {
    19  		return nil
    20  	}
    21  	mSysStatInc(sysStat, n)
    22  	return v
    23  }
    24  
    25  func sysUnused(v unsafe.Pointer, n uintptr) {
    26  	madvise(v, n, _MADV_FREE)
    27  }
    28  
    29  func sysUsed(v unsafe.Pointer, n uintptr) {
    30  }
    31  
    32  // Don't split the stack as this function may be invoked without a valid G,
    33  // which prevents us from allocating more stack.
    34  //go:nosplit
    35  func sysFree(v unsafe.Pointer, n uintptr, sysStat *uint64) {
    36  	mSysStatDec(sysStat, n)
    37  	munmap(v, n)
    38  }
    39  
    40  func sysFault(v unsafe.Pointer, n uintptr) {
    41  	mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE|_MAP_FIXED, -1, 0)
    42  }
    43  
    44  func sysReserve(v unsafe.Pointer, n uintptr) unsafe.Pointer {
    45  	flags := int32(_MAP_ANON | _MAP_PRIVATE)
    46  	if raceenabled && GOOS == "darwin" {
    47  		// Currently the race detector expects memory to live within a certain
    48  		// range, and on Darwin 10.10 mmap is prone to ignoring hints, moreso
    49  		// than later versions and other BSDs (#26475). So, even though it's
    50  		// potentially dangerous to MAP_FIXED, we do it in the race detection
    51  		// case because it'll help maintain the race detector's invariants.
    52  		//
    53  		// TODO(mknyszek): Drop this once support for Darwin 10.10 is dropped,
    54  		// and reconsider this when #24133 is addressed.
    55  		flags |= _MAP_FIXED
    56  	}
    57  	p, err := mmap(v, n, _PROT_NONE, flags, -1, 0)
    58  	if err != 0 {
    59  		return nil
    60  	}
    61  	return p
    62  }
    63  
    64  const _sunosEAGAIN = 11
    65  const _ENOMEM = 12
    66  
    67  func sysMap(v unsafe.Pointer, n uintptr, sysStat *uint64) {
    68  	mSysStatInc(sysStat, n)
    69  
    70  	p, err := mmap(v, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_FIXED|_MAP_PRIVATE, -1, 0)
    71  	if err == _ENOMEM || (GOOS == "solaris" && err == _sunosEAGAIN) {
    72  		throw("runtime: out of memory")
    73  	}
    74  	if p != v || err != 0 {
    75  		throw("runtime: cannot map pages in arena address space")
    76  	}
    77  }
    78  

View as plain text