Source file src/runtime/mem_darwin.go

Documentation: runtime

     1  // Copyright 2018 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  package runtime
     6  
     7  import (
     8  	"unsafe"
     9  )
    10  
    11  // Don't split the stack as this function may be invoked without a valid G,
    12  // which prevents us from allocating more stack.
    13  //go:nosplit
    14  func sysAlloc(n uintptr, sysStat *uint64) unsafe.Pointer {
    15  	v, err := mmap(nil, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_PRIVATE, -1, 0)
    16  	if err != 0 {
    17  		return nil
    18  	}
    19  	mSysStatInc(sysStat, n)
    20  	return v
    21  }
    22  
    23  func sysUnused(v unsafe.Pointer, n uintptr) {
    24  	// MADV_FREE_REUSABLE is like MADV_FREE except it also propagates
    25  	// accounting information about the process to task_info.
    26  	madvise(v, n, _MADV_FREE_REUSABLE)
    27  }
    28  
    29  func sysUsed(v unsafe.Pointer, n uintptr) {
    30  	// MADV_FREE_REUSE is necessary to keep the kernel's accounting
    31  	// accurate. If called on any memory region that hasn't been
    32  	// MADV_FREE_REUSABLE'd, it's a no-op.
    33  	madvise(v, n, _MADV_FREE_REUSE)
    34  }
    35  
    36  func sysHugePage(v unsafe.Pointer, n uintptr) {
    37  }
    38  
    39  // Don't split the stack as this function may be invoked without a valid G,
    40  // which prevents us from allocating more stack.
    41  //go:nosplit
    42  func sysFree(v unsafe.Pointer, n uintptr, sysStat *uint64) {
    43  	mSysStatDec(sysStat, n)
    44  	munmap(v, n)
    45  }
    46  
    47  func sysFault(v unsafe.Pointer, n uintptr) {
    48  	mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE|_MAP_FIXED, -1, 0)
    49  }
    50  
    51  func sysReserve(v unsafe.Pointer, n uintptr) unsafe.Pointer {
    52  	flags := int32(_MAP_ANON | _MAP_PRIVATE)
    53  	if raceenabled {
    54  		// Currently the race detector expects memory to live within a certain
    55  		// range, and on Darwin 10.10 mmap is prone to ignoring hints, moreso
    56  		// than later versions and other BSDs (#26475). So, even though it's
    57  		// potentially dangerous to MAP_FIXED, we do it in the race detection
    58  		// case because it'll help maintain the race detector's invariants.
    59  		//
    60  		// TODO(mknyszek): Drop this once support for Darwin 10.10 is dropped,
    61  		// and reconsider this when #24133 is addressed.
    62  		flags |= _MAP_FIXED
    63  	}
    64  	p, err := mmap(v, n, _PROT_NONE, flags, -1, 0)
    65  	if err != 0 {
    66  		return nil
    67  	}
    68  	return p
    69  }
    70  
    71  const _ENOMEM = 12
    72  
    73  func sysMap(v unsafe.Pointer, n uintptr, sysStat *uint64) {
    74  	mSysStatInc(sysStat, n)
    75  
    76  	p, err := mmap(v, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_FIXED|_MAP_PRIVATE, -1, 0)
    77  	if err == _ENOMEM {
    78  		throw("runtime: out of memory")
    79  	}
    80  	if p != v || err != 0 {
    81  		throw("runtime: cannot map pages in arena address space")
    82  	}
    83  }
    84  

View as plain text