rsc.io/go@v0.0.0-20150416155037-e040fd465409/src/runtime/mem_darwin.go (about)

     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  package runtime
     6  
     7  import "unsafe"
     8  
     9  //go:nosplit
    10  func sysAlloc(n uintptr, stat *uint64) unsafe.Pointer {
    11  	v := (unsafe.Pointer)(mmap(nil, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_PRIVATE, -1, 0))
    12  	if uintptr(v) < 4096 {
    13  		return nil
    14  	}
    15  	xadd64(stat, int64(n))
    16  	return v
    17  }
    18  
    19  func sysUnused(v unsafe.Pointer, n uintptr) {
    20  	// Linux's MADV_DONTNEED is like BSD's MADV_FREE.
    21  	madvise(v, n, _MADV_FREE)
    22  }
    23  
    24  func sysUsed(v unsafe.Pointer, n uintptr) {
    25  }
    26  
    27  func sysFree(v unsafe.Pointer, n uintptr, stat *uint64) {
    28  	xadd64(stat, -int64(n))
    29  	munmap(v, n)
    30  }
    31  
    32  func sysFault(v unsafe.Pointer, n uintptr) {
    33  	mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE|_MAP_FIXED, -1, 0)
    34  }
    35  
    36  func sysReserve(v unsafe.Pointer, n uintptr, reserved *bool) unsafe.Pointer {
    37  	*reserved = true
    38  	p := (unsafe.Pointer)(mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE, -1, 0))
    39  	if uintptr(p) < 4096 {
    40  		return nil
    41  	}
    42  	return p
    43  }
    44  
    45  const (
    46  	_ENOMEM = 12
    47  )
    48  
    49  func sysMap(v unsafe.Pointer, n uintptr, reserved bool, stat *uint64) {
    50  	xadd64(stat, int64(n))
    51  	p := (unsafe.Pointer)(mmap(v, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_FIXED|_MAP_PRIVATE, -1, 0))
    52  	if uintptr(p) == _ENOMEM {
    53  		throw("runtime: out of memory")
    54  	}
    55  	if p != v {
    56  		throw("runtime: cannot map pages in arena address space")
    57  	}
    58  }