-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_windows.go
73 lines (58 loc) · 1.46 KB
/
memory_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//go:build windows && malloc_syscall
package memory
import (
"fmt"
"unsafe"
"golang.org/x/sys/windows"
)
// DefaultAllocator is ...
var DefaultAllocator = NewAllocator()
// pointer is ...
type pointer struct {
Pointer uintptr
}
func Alloc[T any](n int) (pointer, []T, error) {
size := n * int(unsafe.Sizeof(*(new(T))))
ptr, err := DefaultAllocator.Alloc(size)
if err != nil {
return ptr, nil, err
}
return ptr, unsafe.Slice((*T)(unsafe.Pointer(ptr.Pointer)), n), nil
}
func Free(p pointer) error {
return DefaultAllocator.Free(p)
}
type Allocator struct {
*windows.LazyDLL
AllocProc *windows.LazyProc
FreeProc *windows.LazyProc
}
func NewAllocator() Allocator {
kernel32 := windows.NewLazySystemDLL("kernel32.dll")
virtualAlloc := kernel32.NewProc("VirtualAlloc")
virtualFree := kernel32.NewProc("VirtualFree")
return Allocator{LazyDLL: kernel32, AllocProc: virtualAlloc, FreeProc: virtualFree}
}
func (alloc Allocator) Alloc(n int) (pointer, error) {
ptr, _, err := alloc.AllocProc.Call(
0,
uintptr(n),
uintptr(0x1000), // MEM_COMMIT
uintptr(0x04), // PAGE_READWRITE
)
if ptr == 0 {
return pointer{}, fmt.Errorf("failed to make VirtualAlloc allocator: %w", err)
}
return pointer{Pointer: ptr}, nil
}
func (alloc Allocator) Free(p pointer) error {
r1, _, err := alloc.FreeProc.Call(
p.Pointer,
0,
uintptr(0x8000), // MEM_RELEASE
)
if r1 == 0 {
return fmt.Errorf("failed to make VirtualFree allocator: %w", err)
}
return nil
}