github.com/sunvim/utils@v0.1.0/fs/os_mmap_unix.go (about) 1 //go:build !windows 2 3 package fs 4 5 import ( 6 "os" 7 "syscall" 8 "unsafe" 9 ) 10 11 func Mmap(f *os.File, mappingSize int64) ([]byte, error) { 12 p, err := syscall.Mmap(int(f.Fd()), 0, int(mappingSize), syscall.PROT_READ, syscall.MAP_SHARED|syscall.MAP_POPULATE) 13 return p, err 14 } 15 16 func Munmap(data []byte) error { 17 return syscall.Munmap(data) 18 } 19 20 func MadviceRandom(data []byte) error { 21 _, _, errno := syscall.Syscall(syscall.SYS_MADVISE, uintptr(unsafe.Pointer(&data[0])), uintptr(len(data)), uintptr(syscall.MADV_RANDOM)) 22 if errno != 0 { 23 return errno 24 } 25 return nil 26 } 27 28 func (f *osMMapFile) Truncate(size int64) error { 29 if err := f.File.Truncate(size); err != nil { 30 return err 31 } 32 f.size = size 33 return f.mremap() 34 }