@@ -5,26 +5,10 @@ package alloc
5
5
import (
6
6
"fmt"
7
7
"math"
8
- "syscall"
9
8
"unsafe"
10
9
11
10
"github.com/tetratelabs/wazero/experimental"
12
- )
13
-
14
- var (
15
- kernel32 = syscall .NewLazyDLL ("kernel32.dll" )
16
- procVirtualAlloc = kernel32 .NewProc ("VirtualAlloc" )
17
- procVirtualFree = kernel32 .NewProc ("VirtualFree" )
18
- )
19
-
20
- const (
21
- windows_MEM_COMMIT uintptr = 0x00001000
22
- windows_MEM_RESERVE uintptr = 0x00002000
23
- windows_MEM_RELEASE uintptr = 0x00008000
24
- windows_PAGE_READWRITE uintptr = 0x00000004
25
-
26
- // https://cs.opensource.google/go/x/sys/+/refs/tags/v0.20.0:windows/syscall_windows.go;l=131
27
- windows_PAGE_SIZE uint64 = 4096
11
+ "golang.org/x/sys/windows"
28
12
)
29
13
30
14
func Allocator () experimental.MemoryAllocator {
@@ -33,7 +17,7 @@ func Allocator() experimental.MemoryAllocator {
33
17
34
18
func virtualAllocator (cap , max uint64 ) experimental.LinearMemory {
35
19
// Round up to the page size.
36
- rnd := windows_PAGE_SIZE - 1
20
+ rnd := uint64 ( windows . Getpagesize () - 1 )
37
21
max = (max + rnd ) &^ rnd
38
22
cap = (cap + rnd ) &^ rnd
39
23
@@ -45,15 +29,15 @@ func virtualAllocator(cap, max uint64) experimental.LinearMemory {
45
29
46
30
// Reserve, but don't commit, max bytes of address space, to ensure we won't need to move it.
47
31
// This does not commit memory.
48
- r , _ , err := procVirtualAlloc . Call (0 , uintptr (max ), windows_MEM_RESERVE , windows_PAGE_READWRITE )
49
- if r == 0 {
32
+ r , err := windows . VirtualAlloc (0 , uintptr (max ), windows . MEM_RESERVE , windows . PAGE_READWRITE )
33
+ if err != nil {
50
34
panic (fmt .Errorf ("alloc_windows: failed to reserve memory: %w" , err ))
51
35
}
52
36
53
37
// Commit the initial cap bytes of memory.
54
- r , _ , err = procVirtualAlloc . Call (r , uintptr (cap ), windows_MEM_COMMIT , windows_PAGE_READWRITE )
55
- if r == 0 {
56
- _ , _ , _ = procVirtualFree . Call (r , 0 , windows_MEM_RELEASE )
38
+ r , err = windows . VirtualAlloc (r , uintptr (cap ), windows . MEM_COMMIT , windows . PAGE_READWRITE )
39
+ if err != nil {
40
+ _ = windows . VirtualFree (r , 0 , windows . MEM_RELEASE )
57
41
panic (fmt .Errorf ("alloc_windows: failed to commit initial memory: %w" , err ))
58
42
}
59
43
@@ -74,12 +58,12 @@ func (m *virtualMemory) Reallocate(size uint64) []byte {
74
58
res := uint64 (cap (m .buf ))
75
59
if com < size && size < res {
76
60
// Round up to the page size.
77
- rnd := windows_PAGE_SIZE - 1
61
+ rnd := uint64 ( windows . Getpagesize () - 1 )
78
62
new := (size + rnd ) &^ rnd
79
63
80
64
// Commit additional memory up to new bytes.
81
- r , _ , err := procVirtualAlloc . Call (m .addr , uintptr (new ), windows_MEM_COMMIT , windows_PAGE_READWRITE )
82
- if r == 0 {
65
+ _ , err := windows . VirtualAlloc (m .addr , uintptr (new ), windows . MEM_COMMIT , windows . PAGE_READWRITE )
66
+ if err != nil {
83
67
panic (fmt .Errorf ("alloc_windows: failed to commit memory: %w" , err ))
84
68
}
85
69
@@ -91,8 +75,8 @@ func (m *virtualMemory) Reallocate(size uint64) []byte {
91
75
}
92
76
93
77
func (m * virtualMemory ) Free () {
94
- r , _ , err := procVirtualFree . Call (m .addr , 0 , windows_MEM_RELEASE )
95
- if r == 0 {
78
+ err := windows . VirtualFree (m .addr , 0 , windows . MEM_RELEASE )
79
+ if err != nil {
96
80
panic (fmt .Errorf ("alloc_windows: failed to release memory: %w" , err ))
97
81
}
98
82
m .addr = 0
0 commit comments