github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/runtime/mem_windows.c (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 #include "runtime.h" 6 #include "arch_GOARCH.h" 7 #include "os_GOOS.h" 8 #include "defs_GOOS_GOARCH.h" 9 #include "malloc.h" 10 11 enum { 12 MEM_COMMIT = 0x1000, 13 MEM_RESERVE = 0x2000, 14 MEM_DECOMMIT = 0x4000, 15 MEM_RELEASE = 0x8000, 16 17 PAGE_READWRITE = 0x0004, 18 }; 19 20 #pragma dynimport runtime·VirtualAlloc VirtualAlloc "kernel32.dll" 21 #pragma dynimport runtime·VirtualFree VirtualFree "kernel32.dll" 22 extern void *runtime·VirtualAlloc; 23 extern void *runtime·VirtualFree; 24 25 void* 26 runtime·SysAlloc(uintptr n) 27 { 28 mstats.sys += n; 29 return runtime·stdcall(runtime·VirtualAlloc, 4, nil, n, (uintptr)(MEM_COMMIT|MEM_RESERVE), (uintptr)PAGE_READWRITE); 30 } 31 32 void 33 runtime·SysUnused(void *v, uintptr n) 34 { 35 void *r; 36 37 r = runtime·stdcall(runtime·VirtualFree, 3, v, n, (uintptr)MEM_DECOMMIT); 38 if(r == nil) 39 runtime·throw("runtime: failed to decommit pages"); 40 } 41 42 void 43 runtime·SysUsed(void *v, uintptr n) 44 { 45 void *r; 46 47 r = runtime·stdcall(runtime·VirtualAlloc, 4, v, n, (uintptr)MEM_COMMIT, (uintptr)PAGE_READWRITE); 48 if(r != v) 49 runtime·throw("runtime: failed to commit pages"); 50 } 51 52 void 53 runtime·SysFree(void *v, uintptr n) 54 { 55 uintptr r; 56 57 mstats.sys -= n; 58 r = (uintptr)runtime·stdcall(runtime·VirtualFree, 3, v, (uintptr)0, (uintptr)MEM_RELEASE); 59 if(r == 0) 60 runtime·throw("runtime: failed to release pages"); 61 } 62 63 void* 64 runtime·SysReserve(void *v, uintptr n) 65 { 66 // v is just a hint. 67 // First try at v. 68 v = runtime·stdcall(runtime·VirtualAlloc, 4, v, n, (uintptr)MEM_RESERVE, (uintptr)PAGE_READWRITE); 69 if(v != nil) 70 return v; 71 72 // Next let the kernel choose the address. 73 return runtime·stdcall(runtime·VirtualAlloc, 4, nil, n, (uintptr)MEM_RESERVE, (uintptr)PAGE_READWRITE); 74 } 75 76 void 77 runtime·SysMap(void *v, uintptr n) 78 { 79 void *p; 80 81 mstats.sys += n; 82 p = runtime·stdcall(runtime·VirtualAlloc, 4, v, n, (uintptr)MEM_COMMIT, (uintptr)PAGE_READWRITE); 83 if(p != v) 84 runtime·throw("runtime: cannot map pages in arena address space"); 85 }