-
Notifications
You must be signed in to change notification settings - Fork 0
/
close_map.go
56 lines (46 loc) · 1.01 KB
/
close_map.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
package mmap
// Lock the map before calling closeDirects
func (m *Map) closeDirects() {
for addr, direct := range m.direct {
*direct = nil
delete(m.direct, addr)
}
}
// Lock map before calling closeReaders
func (m *Map) closeReaders() {
for _, reader := range m.readers {
reader.close()
}
}
// Lock map before calling closeWriters
func (m *Map) closeWriters() {
for _, writer := range m.writers {
writer.close()
}
}
// Close closes the memory map and returns an error if any.
func (m *Map) Close() error {
m.Lock()
defer m.Unlock()
if m.data == nil {
return nil
}
m.closeDirects()
m.closeWriters()
m.closeReaders()
err := m.unmap()
if err != nil {
return errors.Wrap(err, "unmap error during close").Set("name", m.name)
}
return nil
}
// Lock map and close all direct, writers, and readers before calling.
func (m *Map) unmap() error {
data := m.data
m.data = nil
err := munmap(data)
if err != nil {
return errors.Wrap(err, "error unmapping memory").Set("name", m.name)
}
return nil
}