-
Notifications
You must be signed in to change notification settings - Fork 24
/
mmapcache.go
104 lines (85 loc) · 1.72 KB
/
mmapcache.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
package main
import (
"fmt"
"os"
"sync"
"syscall"
)
type mmapData struct {
file *os.File
data []byte
count int
sync.Mutex
}
func (md *mmapData) Decrement() bool {
md.Lock()
defer md.Unlock()
md.count--
if md.count == 0 {
return true
} else {
return false
}
}
type mmapCache struct {
files map[string]*mmapData
sync.Mutex
}
func NewMmapCache() *mmapCache {
return &mmapCache{files: make(map[string]*mmapData)}
}
func (mc *mmapCache) MapFile(filename string, count int) (*mmapData, error) {
mc.Lock()
defer mc.Unlock()
// File is already open and mmapped
md, ok := mc.files[filename]
if ok {
return md, nil
}
// Open the file
file, err := os.Open(filename)
if err != nil {
return nil, err
}
// Stat the file
st, err := file.Stat()
if err != nil {
return nil, err
}
size := int(st.Size())
// Mmap the file
data, err := syscall.Mmap(int(file.Fd()), 0, (size+4095) & ^4095, syscall.PROT_READ, syscall.MAP_SHARED)
if err != nil {
return nil, err
}
// Cache the information and return the mmap
mc.files[filename] = &mmapData{file: file, data: data, count: count}
return mc.files[filename], nil
}
func (mc *mmapCache) CloseFile(filename string) error {
mc.Lock()
defer mc.Unlock()
// Make sure the file is open first
md, ok := mc.files[filename]
if !ok {
mc.Unlock()
return fmt.Errorf("File '%s' is not open", filename)
}
// Make sure it has a 0 count
if md.count > 0 {
return fmt.Errorf("File '%s' does not have a 0 reference count", filename)
}
// Unmap the file
err := syscall.Munmap(md.data)
if err != nil {
return err
}
// Close the file
err = md.file.Close()
if err != nil {
return err
}
// Remove the file from the map
delete(mc.files, filename)
return nil
}