Source file
src/runtime/mem_windows.go
Documentation: runtime
1
2
3
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 _ERROR_NOT_ENOUGH_MEMORY = 8
21 _ERROR_COMMITMENT_LIMIT = 1455
22 )
23
24
25
26
27
28 func sysAllocOS(n uintptr) unsafe.Pointer {
29 return unsafe.Pointer(stdcall4(_VirtualAlloc, 0, n, _MEM_COMMIT|_MEM_RESERVE, _PAGE_READWRITE))
30 }
31
32 func sysUnusedOS(v unsafe.Pointer, n uintptr) {
33 r := stdcall3(_VirtualFree, uintptr(v), n, _MEM_DECOMMIT)
34 if r != 0 {
35 return
36 }
37
38
39
40
41
42
43
44
45
46
47 for n > 0 {
48 small := n
49 for small >= 4096 && stdcall3(_VirtualFree, uintptr(v), small, _MEM_DECOMMIT) == 0 {
50 small /= 2
51 small &^= 4096 - 1
52 }
53 if small < 4096 {
54 print("runtime: VirtualFree of ", small, " bytes failed with errno=", getlasterror(), "\n")
55 throw("runtime: failed to decommit pages")
56 }
57 v = add(v, small)
58 n -= small
59 }
60 }
61
62 func sysUsedOS(v unsafe.Pointer, n uintptr) {
63 p := stdcall4(_VirtualAlloc, uintptr(v), n, _MEM_COMMIT, _PAGE_READWRITE)
64 if p == uintptr(v) {
65 return
66 }
67
68
69
70
71 k := n
72 for k > 0 {
73 small := k
74 for small >= 4096 && stdcall4(_VirtualAlloc, uintptr(v), small, _MEM_COMMIT, _PAGE_READWRITE) == 0 {
75 small /= 2
76 small &^= 4096 - 1
77 }
78 if small < 4096 {
79 errno := getlasterror()
80 switch errno {
81 case _ERROR_NOT_ENOUGH_MEMORY, _ERROR_COMMITMENT_LIMIT:
82 print("runtime: VirtualAlloc of ", n, " bytes failed with errno=", errno, "\n")
83 throw("out of memory")
84 default:
85 print("runtime: VirtualAlloc of ", small, " bytes failed with errno=", errno, "\n")
86 throw("runtime: failed to commit pages")
87 }
88 }
89 v = add(v, small)
90 k -= small
91 }
92 }
93
94 func sysHugePageOS(v unsafe.Pointer, n uintptr) {
95 }
96
97 func sysNoHugePageOS(v unsafe.Pointer, n uintptr) {
98 }
99
100 func sysHugePageCollapseOS(v unsafe.Pointer, n uintptr) {
101 }
102
103
104
105
106
107 func sysFreeOS(v unsafe.Pointer, n uintptr) {
108 r := stdcall3(_VirtualFree, uintptr(v), 0, _MEM_RELEASE)
109 if r == 0 {
110 print("runtime: VirtualFree of ", n, " bytes failed with errno=", getlasterror(), "\n")
111 throw("runtime: failed to release pages")
112 }
113 }
114
115 func sysFaultOS(v unsafe.Pointer, n uintptr) {
116
117 sysUnusedOS(v, n)
118 }
119
120 func sysReserveOS(v unsafe.Pointer, n uintptr) unsafe.Pointer {
121
122
123
124 v = unsafe.Pointer(stdcall4(_VirtualAlloc, uintptr(v), n, _MEM_RESERVE, _PAGE_READWRITE))
125 if v != nil {
126 return v
127 }
128
129
130 return unsafe.Pointer(stdcall4(_VirtualAlloc, 0, n, _MEM_RESERVE, _PAGE_READWRITE))
131 }
132
133 func sysMapOS(v unsafe.Pointer, n uintptr) {
134 }
135
View as plain text