-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
memorymapper_unix.go
55 lines (46 loc) · 1.1 KB
/
memorymapper_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
//go:build linux || darwin
package common
import (
"fmt"
"os"
"syscall"
)
type MemoryMapper struct {
FilePath string
Size int64
Data []byte
}
func NewMemoryMapper(filePath string) (*MemoryMapper, error) {
result := &MemoryMapper{
FilePath: filePath,
}
file, err := os.OpenFile(filePath, os.O_RDONLY, 0644)
if err != nil {
return nil, err
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
return nil, fmt.Errorf("error getting file info: %v", err)
}
result.Size = fileInfo.Size()
data, err := syscall.Mmap(int(file.Fd()), 0, int(result.Size), syscall.PROT_READ, syscall.MAP_SHARED)
if err != nil {
return nil, fmt.Errorf("error mapping file into memory: %v", err)
}
result.Data = data
return result, nil
}
func (mm *MemoryMapper) Unmap() error {
// TODO: Check this part again, it gives this error while trying to unmap: "runtime error: invalid memory address or nil pointer dereference"
/*
if mm.Data == nil {
return nil
}
err := syscall.Munmap(mm.Data)
if err != nil {
return fmt.Errorf("error unmapping file from memory: %v", err)
}
*/
return nil
}