This repository was archived by the owner on Jan 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmmap.go
More file actions
84 lines (70 loc) · 1.89 KB
/
mmap.go
File metadata and controls
84 lines (70 loc) · 1.89 KB
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
package memiavl
import (
"os"
"github.com/ledgerwatch/erigon-lib/mmap"
"github.com/sei-protocol/sei-db/common/errors"
"golang.org/x/sys/unix"
)
// MmapFile manage the resources of a mmap-ed file
type MmapFile struct {
file *os.File
data []byte
// mmap handle for windows (this is used to close mmap)
handle *[mmap.MaxMapSize]byte
}
// Open openes the file and create the mmap.
// the mmap is created with flags: PROT_READ, MAP_SHARED, MADV_RANDOM.
func NewMmap(path string) (*MmapFile, error) {
return newMmapInternal(path, true)
}
// NewMmapNoPreload opens the file and creates mmap without prefetching hints
// Used for small/inactive trees to avoid unnecessary OS prefetching
func NewMmapNoPreload(path string) (*MmapFile, error) {
return newMmapInternal(path, false)
}
func newMmapInternal(path string, withPrefetch bool) (*MmapFile, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
data, handle, err := Mmap(file)
if err != nil {
_ = file.Close()
return nil, err
}
// Apply madvise hints based on prefetch flag
if len(data) > 0 {
if withPrefetch {
// Override default MADV_RANDOM with SEQUENTIAL + WILLNEED to favor prefetching
_ = unix.Madvise(data, unix.MADV_SEQUENTIAL)
_ = unix.Madvise(data, unix.MADV_WILLNEED)
}
}
return &MmapFile{
file: file,
data: data,
handle: handle,
}, nil
}
func (m *MmapFile) PrepareForRandomRead() {
_ = unix.Madvise(m.data, unix.MADV_RANDOM)
}
// Close closes the file and mmap handles
func (m *MmapFile) Close() error {
var err error
if m.handle != nil {
err = mmap.Munmap(m.data, m.handle)
}
return errors.Join(err, m.file.Close())
}
// Data returns the mmap-ed buffer
func (m *MmapFile) Data() []byte {
return m.data
}
func Mmap(f *os.File) ([]byte, *[mmap.MaxMapSize]byte, error) {
fi, err := f.Stat()
if err != nil || fi.Size() == 0 {
return nil, nil, err
}
return mmap.Mmap(f, int(fi.Size()))
}