github.com/zxy12/golang_with_comment@v0.0.0-20190701084843-0e6b2aff5ef3/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 // Don't split the stack as this function may be invoked without a valid G, 10 // which prevents us from allocating more stack. 11 //go:nosplit 12 func sysAlloc(n uintptr, sysStat *uint64) unsafe.Pointer { 13 v := mmap(nil, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_PRIVATE, -1, 0) 14 if uintptr(v) < 4096 { 15 return nil 16 } 17 mSysStatInc(sysStat, n) 18 return v 19 } 20 21 func sysUnused(v unsafe.Pointer, n uintptr) { 22 // Linux's MADV_DONTNEED is like BSD's MADV_FREE. 23 madvise(v, n, _MADV_FREE) 24 } 25 26 func sysUsed(v unsafe.Pointer, n uintptr) { 27 } 28 29 // Don't split the stack as this function may be invoked without a valid G, 30 // which prevents us from allocating more stack. 31 //go:nosplit 32 func sysFree(v unsafe.Pointer, n uintptr, sysStat *uint64) { 33 mSysStatDec(sysStat, n) 34 munmap(v, n) 35 } 36 37 func sysFault(v unsafe.Pointer, n uintptr) { 38 mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE|_MAP_FIXED, -1, 0) 39 } 40 41 func sysReserve(v unsafe.Pointer, n uintptr, reserved *bool) unsafe.Pointer { 42 *reserved = true 43 // 那是为了预留虚拟内存但不占用物理内存,后面会随着分配慢慢的调用mprotect改权限,再调用一次mmap,权限会换 44 p := mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE, -1, 0) 45 println("in sysReserve,p=", p, ",v=", v) 46 if uintptr(p) < 4096 { 47 return nil 48 } 49 return p 50 } 51 52 const ( 53 _ENOMEM = 12 54 ) 55 56 func sysMap(v unsafe.Pointer, n uintptr, reserved bool, sysStat *uint64) { 57 mSysStatInc(sysStat, n) 58 p := mmap(v, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_FIXED|_MAP_PRIVATE, -1, 0) 59 if uintptr(p) == _ENOMEM { 60 throw("runtime: out of memory") 61 } 62 if p != v { 63 throw("runtime: cannot map pages in arena address space") 64 } 65 }