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