Skip to content

Commit 188cc3b

Browse files
committed
Refactor to use provided unix functions
golang.org/x/sys/unix provides functions for syscalls like msync that syscall didn't previously provide. This lets us simplify a little bit and simultaneously support more platforms that have different constant names.
1 parent 0833494 commit 188cc3b

File tree

3 files changed

+27
-37
lines changed

3 files changed

+27
-37
lines changed

mmap.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -81,25 +81,27 @@ func (m *MMap) header() *reflect.SliceHeader {
8181
return (*reflect.SliceHeader)(unsafe.Pointer(m))
8282
}
8383

84+
func (m *MMap) addrLen() (uintptr, uintptr) {
85+
header := m.header()
86+
return header.Data, uintptr(header.Len)
87+
}
88+
8489
// Lock keeps the mapped region in physical memory, ensuring that it will not be
8590
// swapped out.
8691
func (m MMap) Lock() error {
87-
dh := m.header()
88-
return lock(dh.Data, uintptr(dh.Len))
92+
return m.lock()
8993
}
9094

9195
// Unlock reverses the effect of Lock, allowing the mapped region to potentially
9296
// be swapped out.
9397
// If m is already unlocked, aan error will result.
9498
func (m MMap) Unlock() error {
95-
dh := m.header()
96-
return unlock(dh.Data, uintptr(dh.Len))
99+
return m.unlock()
97100
}
98101

99102
// Flush synchronizes the mapping's contents to the file's contents on disk.
100103
func (m MMap) Flush() error {
101-
dh := m.header()
102-
return flush(dh.Data, uintptr(dh.Len))
104+
return m.flush()
103105
}
104106

105107
// Unmap deletes the memory mapped region, flushes any remaining changes, and sets
@@ -109,8 +111,7 @@ func (m MMap) Flush() error {
109111
// Unmap should only be called on the slice value that was originally returned from
110112
// a call to Map. Calling Unmap on a derived slice may cause errors.
111113
func (m *MMap) Unmap() error {
112-
dh := m.header()
113-
err := unmap(dh.Data, uintptr(dh.Len))
114+
err := m.unmap()
114115
*m = nil
115116
return err
116117
}

mmap_unix.go

+8-24
Original file line numberDiff line numberDiff line change
@@ -34,34 +34,18 @@ func mmap(len int, inprot, inflags, fd uintptr, off int64) ([]byte, error) {
3434
return b, nil
3535
}
3636

37-
func flush(addr, len uintptr) error {
38-
_, _, errno := unix.Syscall(unix.SYS_MSYNC, addr, len, unix.MS_SYNC)
39-
if errno != 0 {
40-
return unix.Errno(errno)
41-
}
42-
return nil
37+
func (m MMap) flush() error {
38+
return unix.Msync([]byte(m), unix.MS_SYNC)
4339
}
4440

45-
func lock(addr, len uintptr) error {
46-
_, _, errno := unix.Syscall(unix.SYS_MLOCK, addr, len, 0)
47-
if errno != 0 {
48-
return unix.Errno(errno)
49-
}
50-
return nil
41+
func (m MMap) lock() error {
42+
return unix.Mlock([]byte(m))
5143
}
5244

53-
func unlock(addr, len uintptr) error {
54-
_, _, errno := unix.Syscall(unix.SYS_MUNLOCK, addr, len, 0)
55-
if errno != 0 {
56-
return unix.Errno(errno)
57-
}
58-
return nil
45+
func (m MMap) unlock() error {
46+
return unix.Munlock([]byte(m))
5947
}
6048

61-
func unmap(addr, len uintptr) error {
62-
_, _, errno := unix.Syscall(unix.SYS_MUNMAP, addr, len, 0)
63-
if errno != 0 {
64-
return unix.Errno(errno)
65-
}
66-
return nil
49+
func (m MMap) unmap() error {
50+
return unix.Munmap([]byte(m))
6751
}

mmap_windows.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ func mmap(len int, prot, flags, hfile uintptr, off int64) ([]byte, error) {
8181
return m, nil
8282
}
8383

84-
func flush(addr, len uintptr) error {
84+
func (m MMap) flush() error {
85+
addr, len := m.addrLen()
8586
errno := windows.FlushViewOfFile(addr, len)
8687
if errno != nil {
8788
return os.NewSyscallError("FlushViewOfFile", errno)
@@ -99,21 +100,25 @@ func flush(addr, len uintptr) error {
99100
return os.NewSyscallError("FlushFileBuffers", errno)
100101
}
101102

102-
func lock(addr, len uintptr) error {
103+
func (m MMap) lock() error {
104+
addr, len := m.addrLen()
103105
errno := windows.VirtualLock(addr, len)
104106
return os.NewSyscallError("VirtualLock", errno)
105107
}
106108

107-
func unlock(addr, len uintptr) error {
109+
func (m MMap) unlock() error {
110+
addr, len := m.addrLen()
108111
errno := windows.VirtualUnlock(addr, len)
109112
return os.NewSyscallError("VirtualUnlock", errno)
110113
}
111114

112-
func unmap(addr, len uintptr) (err error) {
113-
err = flush(addr, len)
115+
func (m MMap) unmap() error {
116+
err := m.flush()
114117
if err != nil {
115118
return err
116119
}
120+
121+
addr := m.header().Data
117122
// Lock the UnmapViewOfFile along with the handleMap deletion.
118123
// As soon as we unmap the view, the OS is free to give the
119124
// same addr to another new map. We don't want another goroutine

0 commit comments

Comments
 (0)