github.com/rsc/go@v0.0.0-20150416155037-e040fd465409/src/runtime/mem_windows.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 ( 8 "unsafe" 9 ) 10 11 const ( 12 _MEM_COMMIT = 0x1000 13 _MEM_RESERVE = 0x2000 14 _MEM_DECOMMIT = 0x4000 15 _MEM_RELEASE = 0x8000 16 17 _PAGE_READWRITE = 0x0004 18 _PAGE_NOACCESS = 0x0001 19 ) 20 21 //go:nosplit 22 func sysAlloc(n uintptr, stat *uint64) unsafe.Pointer { 23 xadd64(stat, int64(n)) 24 return unsafe.Pointer(stdcall4(_VirtualAlloc, 0, n, _MEM_COMMIT|_MEM_RESERVE, _PAGE_READWRITE)) 25 } 26 27 func sysUnused(v unsafe.Pointer, n uintptr) { 28 r := stdcall3(_VirtualFree, uintptr(v), n, _MEM_DECOMMIT) 29 if r != 0 { 30 return 31 } 32 33 // Decommit failed. Usual reason is that we've merged memory from two different 34 // VirtualAlloc calls, and Windows will only let each VirtualFree handle pages from 35 // a single VirtualAlloc. It is okay to specify a subset of the pages from a single alloc, 36 // just not pages from multiple allocs. This is a rare case, arising only when we're 37 // trying to give memory back to the operating system, which happens on a time 38 // scale of minutes. It doesn't have to be terribly fast. Instead of extra bookkeeping 39 // on all our VirtualAlloc calls, try freeing successively smaller pieces until 40 // we manage to free something, and then repeat. This ends up being O(n log n) 41 // in the worst case, but that's fast enough. 42 for n > 0 { 43 small := n 44 for small >= 4096 && stdcall3(_VirtualFree, uintptr(v), small, _MEM_DECOMMIT) == 0 { 45 small /= 2 46 small &^= 4096 - 1 47 } 48 if small < 4096 { 49 throw("runtime: failed to decommit pages") 50 } 51 v = add(v, small) 52 n -= small 53 } 54 } 55 56 func sysUsed(v unsafe.Pointer, n uintptr) { 57 r := stdcall4(_VirtualAlloc, uintptr(v), n, _MEM_COMMIT, _PAGE_READWRITE) 58 if r != uintptr(v) { 59 throw("runtime: failed to commit pages") 60 } 61 62 // Commit failed. See SysUnused. 63 for n > 0 { 64 small := n 65 for small >= 4096 && stdcall4(_VirtualAlloc, uintptr(v), small, _MEM_COMMIT, _PAGE_READWRITE) == 0 { 66 small /= 2 67 small &^= 4096 - 1 68 } 69 if small < 4096 { 70 throw("runtime: failed to decommit pages") 71 } 72 v = add(v, small) 73 n -= small 74 } 75 } 76 77 func sysFree(v unsafe.Pointer, n uintptr, stat *uint64) { 78 xadd64(stat, -int64(n)) 79 r := stdcall3(_VirtualFree, uintptr(v), 0, _MEM_RELEASE) 80 if r == 0 { 81 throw("runtime: failed to release pages") 82 } 83 } 84 85 func sysFault(v unsafe.Pointer, n uintptr) { 86 // SysUnused makes the memory inaccessible and prevents its reuse 87 sysUnused(v, n) 88 } 89 90 func sysReserve(v unsafe.Pointer, n uintptr, reserved *bool) unsafe.Pointer { 91 *reserved = true 92 // v is just a hint. 93 // First try at v. 94 v = unsafe.Pointer(stdcall4(_VirtualAlloc, uintptr(v), n, _MEM_RESERVE, _PAGE_READWRITE)) 95 if v != nil { 96 return v 97 } 98 99 // Next let the kernel choose the address. 100 return unsafe.Pointer(stdcall4(_VirtualAlloc, 0, n, _MEM_RESERVE, _PAGE_READWRITE)) 101 } 102 103 func sysMap(v unsafe.Pointer, n uintptr, reserved bool, stat *uint64) { 104 xadd64(stat, int64(n)) 105 p := stdcall4(_VirtualAlloc, uintptr(v), n, _MEM_COMMIT, _PAGE_READWRITE) 106 if p != uintptr(v) { 107 throw("runtime: cannot map pages in arena address space") 108 } 109 }