-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmmap.go
66 lines (53 loc) · 1.43 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
64
65
66
// Copyright (c) Efficient Go Authors
// Licensed under the Apache License 2.0.
package mmap
import (
"os"
"github.com/efficientgo/core/errors"
"github.com/efficientgo/core/merrors"
"golang.org/x/sys/unix"
)
// Wrapper for using memory mapping.
// Read more in "Efficient Go"; Example 5-1.
type MemoryMap struct {
f *os.File // nil if anonymous.
b []byte
}
func OpenFileBacked(path string, size int) (mf *MemoryMap, _ error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
b, err := unix.Mmap(int(f.Fd()), 0, size, unix.PROT_READ, unix.MAP_SHARED)
if err != nil {
return nil, merrors.New(f.Close(), err).Err()
}
return &MemoryMap{f: f, b: b}, nil
}
func OpenAnonymous(size int) (mf *MemoryMap, _ error) {
b, err := unix.Mmap(0, 0, size, unix.PROT_READ|unix.PROT_WRITE, unix.MAP_PRIVATE|unix.MAP_ANON)
if err != nil {
return nil, err
}
return &MemoryMap{f: nil, b: b}, nil
}
func (f *MemoryMap) Close() error {
errs := merrors.New()
errs.Add(unix.Munmap(f.b))
if f.f != nil {
errs.Add(f.f.Close())
}
return errs.Err()
}
func (f *MemoryMap) Bytes() []byte { return f.b }
func (f *MemoryMap) File() *os.File { return f.f }
func (f *MemoryMap) Advise(advise int) error {
if f.f != nil {
// TODO(bwplotka): Provide table what works in SHARED mode.
return errors.New("Most of madvise calls works ony on MAP_ANON mappings.")
}
if err := unix.Madvise(f.b, advise); err != nil {
return err
}
return nil
}