github.com/flower-corp/rosedb@v1.1.2-0.20230117132829-21dc4f7b319a/mmap/mmap_unix.go (about) 1 // +build !windows,!darwin,!plan9,!linux 2 3 package mmap 4 5 import ( 6 "os" 7 8 "golang.org/x/sys/unix" 9 ) 10 11 // Mmap uses the mmap system call to memory-map a file. If writable is true, 12 // memory protection of the pages is set so that they may be written to as well. 13 func mmap(fd *os.File, writable bool, size int64) ([]byte, error) { 14 mtype := unix.PROT_READ 15 if writable { 16 mtype |= unix.PROT_WRITE 17 } 18 return unix.Mmap(int(fd.Fd()), 0, int(size), mtype, unix.MAP_SHARED) 19 } 20 21 // Munmap unmaps a previously mapped slice. 22 func munmap(b []byte) error { 23 return unix.Munmap(b) 24 } 25 26 // Madvise uses the madvise system call to give advise about the use of memory 27 // when using a slice that is memory-mapped to a file. Set the readahead flag to 28 // false if page references are expected in random order. 29 func madvise(b []byte, readahead bool) error { 30 flags := unix.MADV_NORMAL 31 if !readahead { 32 flags = unix.MADV_RANDOM 33 } 34 return unix.Madvise(b, flags) 35 } 36 37 func msync(b []byte) error { 38 return unix.Msync(b, unix.MS_SYNC) 39 }