...
1
2
3
4
5
6
7 package base
8
9 import (
10 "internal/unsafeheader"
11 "os"
12 "runtime"
13 "syscall"
14 "unsafe"
15 )
16
17
18
19
20
21
22 func MapFile(f *os.File, offset, length int64) (string, error) {
23
24
25 x := offset & int64(os.Getpagesize()-1)
26 offset -= x
27 length += x
28
29 buf, err := syscall.Mmap(int(f.Fd()), offset, int(length), syscall.PROT_READ, syscall.MAP_SHARED)
30 runtime.KeepAlive(f)
31 if err != nil {
32 return "", err
33 }
34
35 buf = buf[x:]
36 pSlice := (*unsafeheader.Slice)(unsafe.Pointer(&buf))
37
38 var res string
39 pString := (*unsafeheader.String)(unsafe.Pointer(&res))
40
41 pString.Data = pSlice.Data
42 pString.Len = pSlice.Len
43
44 return res, nil
45 }
46
View as plain text