-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoverlay.go
More file actions
48 lines (41 loc) · 1.02 KB
/
overlay.go
File metadata and controls
48 lines (41 loc) · 1.02 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
package blockdev
import "sync"
// overlay is the copy-on-write block store: reads expose the stored slice
// (callers must not mutate), puts copy so callers can reuse their buffer.
type overlay struct {
mu sync.RWMutex
m map[int64][]byte
}
func newOverlay() *overlay {
return &overlay{m: make(map[int64][]byte)}
}
// Returned slice aliases the store — caller must not mutate.
func (o *overlay) get(block int64) ([]byte, bool) {
o.mu.RLock()
defer o.mu.RUnlock()
data, ok := o.m[block]
return data, ok
}
// Copies data so the caller can reuse its buffer immediately.
func (o *overlay) put(block int64, data []byte) {
buf := make([]byte, len(data))
copy(buf, data)
o.mu.Lock()
o.m[block] = buf
o.mu.Unlock()
}
// fn must not mutate data; return false to stop early.
func (o *overlay) each(fn func(block int64, data []byte) bool) {
o.mu.RLock()
defer o.mu.RUnlock()
for block, data := range o.m {
if !fn(block, data) {
return
}
}
}
func (o *overlay) count() int {
o.mu.RLock()
defer o.mu.RUnlock()
return len(o.m)
}