Brent Austin | ba3052e | 2015-04-21 16:08:23 -0700 | [diff] [blame^] | 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 | #include "runtime.h" |
| 6 | #include "arch_GOARCH.h" |
| 7 | #include "os_GOOS.h" |
| 8 | #include "defs_GOOS_GOARCH.h" |
| 9 | #include "malloc.h" |
| 10 | #include "textflag.h" |
| 11 | |
| 12 | enum { |
| 13 | MEM_COMMIT = 0x1000, |
| 14 | MEM_RESERVE = 0x2000, |
| 15 | MEM_DECOMMIT = 0x4000, |
| 16 | MEM_RELEASE = 0x8000, |
| 17 | |
| 18 | PAGE_READWRITE = 0x0004, |
| 19 | PAGE_NOACCESS = 0x0001, |
| 20 | }; |
| 21 | |
| 22 | #pragma dynimport runtime·VirtualAlloc VirtualAlloc "kernel32.dll" |
| 23 | #pragma dynimport runtime·VirtualFree VirtualFree "kernel32.dll" |
| 24 | #pragma dynimport runtime·VirtualProtect VirtualProtect "kernel32.dll" |
| 25 | extern void *runtime·VirtualAlloc; |
| 26 | extern void *runtime·VirtualFree; |
| 27 | extern void *runtime·VirtualProtect; |
| 28 | |
| 29 | #pragma textflag NOSPLIT |
| 30 | void* |
| 31 | runtime·sysAlloc(uintptr n, uint64 *stat) |
| 32 | { |
| 33 | runtime·xadd64(stat, n); |
| 34 | return runtime·stdcall4(runtime·VirtualAlloc, 0, n, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE); |
| 35 | } |
| 36 | |
| 37 | void |
| 38 | runtime·SysUnused(void *v, uintptr n) |
| 39 | { |
| 40 | void *r; |
| 41 | uintptr small; |
| 42 | |
| 43 | r = runtime·stdcall3(runtime·VirtualFree, (uintptr)v, n, MEM_DECOMMIT); |
| 44 | if(r != nil) |
| 45 | return; |
| 46 | |
| 47 | // Decommit failed. Usual reason is that we've merged memory from two different |
| 48 | // VirtualAlloc calls, and Windows will only let each VirtualFree handle pages from |
| 49 | // a single VirtualAlloc. It is okay to specify a subset of the pages from a single alloc, |
| 50 | // just not pages from multiple allocs. This is a rare case, arising only when we're |
| 51 | // trying to give memory back to the operating system, which happens on a time |
| 52 | // scale of minutes. It doesn't have to be terribly fast. Instead of extra bookkeeping |
| 53 | // on all our VirtualAlloc calls, try freeing successively smaller pieces until |
| 54 | // we manage to free something, and then repeat. This ends up being O(n log n) |
| 55 | // in the worst case, but that's fast enough. |
| 56 | while(n > 0) { |
| 57 | small = n; |
| 58 | while(small >= 4096 && runtime·stdcall3(runtime·VirtualFree, (uintptr)v, small, MEM_DECOMMIT) == nil) |
| 59 | small = (small / 2) & ~(4096-1); |
| 60 | if(small < 4096) |
| 61 | runtime·throw("runtime: failed to decommit pages"); |
| 62 | v = (byte*)v + small; |
| 63 | n -= small; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | runtime·SysUsed(void *v, uintptr n) |
| 69 | { |
| 70 | void *r; |
| 71 | uintptr small; |
| 72 | |
| 73 | r = runtime·stdcall4(runtime·VirtualAlloc, (uintptr)v, n, MEM_COMMIT, PAGE_READWRITE); |
| 74 | if(r != v) |
| 75 | runtime·throw("runtime: failed to commit pages"); |
| 76 | |
| 77 | // Commit failed. See SysUnused. |
| 78 | while(n > 0) { |
| 79 | small = n; |
| 80 | while(small >= 4096 && runtime·stdcall4(runtime·VirtualAlloc, (uintptr)v, small, MEM_COMMIT, PAGE_READWRITE) == nil) |
| 81 | small = (small / 2) & ~(4096-1); |
| 82 | if(small < 4096) |
| 83 | runtime·throw("runtime: failed to decommit pages"); |
| 84 | v = (byte*)v + small; |
| 85 | n -= small; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | runtime·SysFree(void *v, uintptr n, uint64 *stat) |
| 91 | { |
| 92 | uintptr r; |
| 93 | |
| 94 | runtime·xadd64(stat, -(uint64)n); |
| 95 | r = (uintptr)runtime·stdcall3(runtime·VirtualFree, (uintptr)v, 0, MEM_RELEASE); |
| 96 | if(r == 0) |
| 97 | runtime·throw("runtime: failed to release pages"); |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | runtime·SysFault(void *v, uintptr n) |
| 102 | { |
| 103 | // SysUnused makes the memory inaccessible and prevents its reuse |
| 104 | runtime·SysUnused(v, n); |
| 105 | } |
| 106 | |
| 107 | void* |
| 108 | runtime·SysReserve(void *v, uintptr n, bool *reserved) |
| 109 | { |
| 110 | *reserved = true; |
| 111 | // v is just a hint. |
| 112 | // First try at v. |
| 113 | v = runtime·stdcall4(runtime·VirtualAlloc, (uintptr)v, n, MEM_RESERVE, PAGE_READWRITE); |
| 114 | if(v != nil) |
| 115 | return v; |
| 116 | |
| 117 | // Next let the kernel choose the address. |
| 118 | return runtime·stdcall4(runtime·VirtualAlloc, 0, n, MEM_RESERVE, PAGE_READWRITE); |
| 119 | } |
| 120 | |
| 121 | void |
| 122 | runtime·SysMap(void *v, uintptr n, bool reserved, uint64 *stat) |
| 123 | { |
| 124 | void *p; |
| 125 | |
| 126 | USED(reserved); |
| 127 | |
| 128 | runtime·xadd64(stat, n); |
| 129 | p = runtime·stdcall4(runtime·VirtualAlloc, (uintptr)v, n, MEM_COMMIT, PAGE_READWRITE); |
| 130 | if(p != v) |
| 131 | runtime·throw("runtime: cannot map pages in arena address space"); |
| 132 | } |