-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmmap.go
63 lines (52 loc) · 1.48 KB
/
mmap.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
// Copyright (c) 2020 Meng Huang (mhboy@outlook.com)
// This package is licensed under a MIT license that can be found in the LICENSE file.
// Package mmap provides a way to memory-map a file.
package mmap
import (
"os"
)
// Prot is the protection flag.
type Prot int
const (
// READ represents the read prot
READ Prot = 1 << iota
// WRITE represents the write prot
WRITE
// COPY represents the copy prot
COPY
// EXEC represents the exec prot
EXEC
)
// Fd returns the integer file descriptor referencing the open file.
// The file descriptor is valid only until f.Close is called or f is garbage collected.
func Fd(f *os.File) int {
return int(f.Fd())
}
// Fsize returns the file size.
func Fsize(f *os.File) int {
cursor, _ := f.Seek(0, os.SEEK_CUR)
ret, _ := f.Seek(0, os.SEEK_END)
f.Seek(cursor, os.SEEK_SET)
return int(ret)
}
// ProtFlags returns prot and flags by Prot p.
func ProtFlags(p Prot) (prot int, flags int) {
return protFlags(p)
}
// Open opens a mmap
func Open(fd int, offset int64, length int, p Prot) (data []byte, err error) {
prot, flags := ProtFlags(p)
return Mmap(fd, offset, length, prot, flags)
}
//Mmap calls the mmap system call.
func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
return mmap(fd, offset, length, prot, flags)
}
// Msync calls the msync system call.
func Msync(b []byte) (err error) {
return msync(b)
}
// Munmap calls the munmap system call.
func Munmap(b []byte) (err error) {
return munmap(b)
}