-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmmap_windows.go
139 lines (123 loc) · 3.04 KB
/
mmap_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) 2020 Meng Huang (mhboy@outlook.com)
// This package is licensed under a MIT license that can be found in the LICENSE file.
// +build windows
package mmap
import (
"os"
"sync"
"syscall"
"unsafe"
)
const (
PAGE_READONLY = syscall.PAGE_READONLY
PAGE_READWRITE = syscall.PAGE_READWRITE
PAGE_WRITECOPY = syscall.PAGE_WRITECOPY
PAGE_EXECUTE_READ = syscall.PAGE_EXECUTE_READ
PAGE_EXECUTE_READWRITE = syscall.PAGE_EXECUTE_READWRITE
PAGE_EXECUTE_WRITECOPY = syscall.PAGE_EXECUTE_WRITECOPY
FILE_MAP_COPY = syscall.FILE_MAP_COPY
FILE_MAP_WRITE = syscall.FILE_MAP_WRITE
FILE_MAP_READ = syscall.FILE_MAP_READ
FILE_MAP_EXECUTE = syscall.FILE_MAP_EXECUTE
)
// Offset returns the valid offset.
func Offset(offset int64) int64 {
pageSize := int64(os.Getpagesize() * 16)
return offset / pageSize * pageSize
}
func protFlags(p Prot) (prot int, flags int) {
prot = PAGE_READONLY
flags = FILE_MAP_READ
if p&WRITE != 0 {
prot = PAGE_READWRITE
flags = FILE_MAP_WRITE
}
if p© != 0 {
prot = PAGE_WRITECOPY
flags = FILE_MAP_COPY
}
if p&EXEC != 0 {
prot <<= 4
flags |= FILE_MAP_EXECUTE
}
return
}
type mmapper struct {
sync.Mutex
active map[*byte][]byte
}
func (m *mmapper) Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
if length <= 0 {
return nil, syscall.EINVAL
}
handle, err := syscall.CreateFileMapping(syscall.Handle(fd), nil, uint32(prot), uint32((offset+int64(length))>>32), uint32((offset+int64(length))&0xFFFFFFFF), nil)
if err != nil {
return nil, err
}
addr, err := syscall.MapViewOfFile(handle, uint32(flags), uint32(offset>>32), uint32(offset&0xFFFFFFFF), uintptr(length))
if err != nil {
return nil, err
}
err = syscall.CloseHandle(syscall.Handle(handle))
if err != nil {
return nil, err
}
var sl = struct {
addr uintptr
len int
cap int
}{addr, length, length}
b := *(*[]byte)(unsafe.Pointer(&sl))
p := &b[cap(b)-1]
m.Lock()
defer m.Unlock()
m.active[p] = b
return b, nil
}
func (m *mmapper) Msync(b []byte) (err error) {
slice := (*struct {
addr uintptr
len int
cap int
})(unsafe.Pointer(&b))
p := &b[cap(b)-1]
m.Lock()
data := m.active[p]
m.Unlock()
if data == nil || &b[0] != &data[0] {
return syscall.EINVAL
}
return syscall.FlushViewOfFile(slice.addr, uintptr(slice.len))
}
func (m *mmapper) Munmap(data []byte) (err error) {
if len(data) == 0 || len(data) != cap(data) {
return syscall.EINVAL
}
p := &data[cap(data)-1]
m.Lock()
b := m.active[p]
m.Unlock()
if b == nil || &b[0] != &data[0] {
return syscall.EINVAL
}
err = syscall.UnmapViewOfFile(uintptr(unsafe.Pointer(&b[0])))
if err != nil {
return err
}
m.Lock()
delete(m.active, p)
m.Unlock()
return nil
}
var mapper = &mmapper{
active: make(map[*byte][]byte),
}
func mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
return mapper.Mmap(fd, offset, length, prot, flags)
}
func msync(b []byte) (err error) {
return mapper.Msync(b)
}
func munmap(b []byte) (err error) {
return mapper.Munmap(b)
}