Source file
src/runtime/mem_windows.go
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 func sysAlloc(n uintptr, sysStat *sysMemStat) unsafe.Pointer {
28 sysStat.add(int64(n))
29 return unsafe.Pointer(stdcall4(_VirtualAlloc, 0, n, _MEM_COMMIT|_MEM_RESERVE, _PAGE_READWRITE))
30 }
31
32 func sysUnused(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 sysUsed(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 sysHugePage(v unsafe.Pointer, n uintptr) {
95 }
96
97
98
99
100 func sysFree(v unsafe.Pointer, n uintptr, sysStat *sysMemStat) {
101 sysStat.add(-int64(n))
102 r := stdcall3(_VirtualFree, uintptr(v), 0, _MEM_RELEASE)
103 if r == 0 {
104 print("runtime: VirtualFree of ", n, " bytes failed with errno=", getlasterror(), "\n")
105 throw("runtime: failed to release pages")
106 }
107 }
108
109 func sysFault(v unsafe.Pointer, n uintptr) {
110
111 sysUnused(v, n)
112 }
113
114 func sysReserve(v unsafe.Pointer, n uintptr) unsafe.Pointer {
115
116
117
118 v = unsafe.Pointer(stdcall4(_VirtualAlloc, uintptr(v), n, _MEM_RESERVE, _PAGE_READWRITE))
119 if v != nil {
120 return v
121 }
122
123
124 return unsafe.Pointer(stdcall4(_VirtualAlloc, 0, n, _MEM_RESERVE, _PAGE_READWRITE))
125 }
126
127 func sysMap(v unsafe.Pointer, n uintptr, sysStat *sysMemStat) {
128 sysStat.add(int64(n))
129 }
130
View as plain text