forked from grandecola/mmap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmmap_page.go
39 lines (32 loc) · 889 Bytes
/
mmap_page.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
package mmap
import (
"syscall"
"unsafe"
)
// Advise provides hints to kernel regarding the use of memory mapped region.
func (m *File) Advise(advice int) error {
_, _, err := syscall.Syscall(syscall.SYS_MADVISE,
uintptr(unsafe.Pointer(&m.data[0])), uintptr(m.length), uintptr(advice))
if err != 0 {
return err
}
return nil
}
// Lock locks all the mapped memory to RAM, preventing the pages from swapping out.
func (m *File) Lock() error {
_, _, err := syscall.Syscall(syscall.SYS_MLOCK,
uintptr(unsafe.Pointer(&m.data[0])), uintptr(m.length), 0)
if err != 0 {
return err
}
return nil
}
// Unlock unlocks the mapped memory from RAM, enabling swapping out of RAM if required.
func (m *File) Unlock() error {
_, _, err := syscall.Syscall(syscall.SYS_MUNLOCK,
uintptr(unsafe.Pointer(&m.data[0])), uintptr(m.length), 0)
if err != 0 {
return err
}
return nil
}