github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/runtime/mem_darwin.go (about)

     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  //
    14  //go:nosplit
    15  func sysAllocOS(n uintptr) unsafe.Pointer {
    16  	v, err := mmap(nil, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_PRIVATE, -1, 0)
    17  	if err != 0 {
    18  		return nil
    19  	}
    20  	return v
    21  }
    22  
    23  func sysUnusedOS(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 sysUsedOS(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 sysHugePageOS(v unsafe.Pointer, n uintptr) {
    37  }
    38  
    39  func sysNoHugePageOS(v unsafe.Pointer, n uintptr) {
    40  }
    41  
    42  // Don't split the stack as this function may be invoked without a valid G,
    43  // which prevents us from allocating more stack.
    44  //
    45  //go:nosplit
    46  func sysFreeOS(v unsafe.Pointer, n uintptr) {
    47  	munmap(v, n)
    48  }
    49  
    50  func sysFaultOS(v unsafe.Pointer, n uintptr) {
    51  	mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE|_MAP_FIXED, -1, 0)
    52  }
    53  
    54  func sysReserveOS(v unsafe.Pointer, n uintptr) unsafe.Pointer {
    55  	p, err := mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE, -1, 0)
    56  	if err != 0 {
    57  		return nil
    58  	}
    59  	return p
    60  }
    61  
    62  const _ENOMEM = 12
    63  
    64  func sysMapOS(v unsafe.Pointer, n uintptr) {
    65  	p, err := mmap(v, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_FIXED|_MAP_PRIVATE, -1, 0)
    66  	if err == _ENOMEM {
    67  		throw("runtime: out of memory")
    68  	}
    69  	if p != v || err != 0 {
    70  		print("runtime: mmap(", v, ", ", n, ") returned ", p, ", ", err, "\n")
    71  		throw("runtime: cannot map pages in arena address space")
    72  	}
    73  }