-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmmap_unix.go
executable file
·59 lines (49 loc) · 1.22 KB
/
mmap_unix.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
// 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 darwin linux dragonfly freebsd netbsd openbsd
package mmap
import (
"os"
"syscall"
"unsafe"
)
const (
PROT_READ = syscall.PROT_READ
PROT_WRITE = syscall.PROT_WRITE
PROT_EXEC = syscall.PROT_EXEC
MAP_SHARED = syscall.MAP_SHARED
MAP_PRIVATE = syscall.MAP_PRIVATE
MAP_COPY = MAP_PRIVATE
)
// Offset returns the valid offset.
func Offset(offset int64) int64 {
pageSize := int64(os.Getpagesize())
return offset / pageSize * pageSize
}
func protFlags(p Prot) (prot int, flags int) {
prot = PROT_READ
flags = MAP_SHARED
if p&WRITE != 0 {
prot |= PROT_WRITE
}
if p© != 0 {
flags = MAP_COPY
}
if p&EXEC != 0 {
prot |= PROT_EXEC
}
return
}
func mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
return syscall.Mmap(fd, offset, length, prot, flags)
}
func msync(b []byte) (err error) {
_, _, errno := syscall.Syscall(syscall.SYS_MSYNC, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)), syscall.MS_SYNC)
if errno != 0 {
err = syscall.Errno(errno)
}
return
}
func munmap(b []byte) (err error) {
return syscall.Munmap(b)
}