2018-05-19 08:49:46 -04:00
|
|
|
// Copyright 2011 Evan Shaw. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// +build darwin dragonfly freebsd linux openbsd solaris netbsd
|
|
|
|
|
|
|
|
package mmap
|
|
|
|
|
|
|
|
import (
|
2019-08-23 12:40:30 -04:00
|
|
|
"golang.org/x/sys/unix"
|
2018-05-19 08:49:46 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func mmap(len int, inprot, inflags, fd uintptr, off int64) ([]byte, error) {
|
2019-08-23 12:40:30 -04:00
|
|
|
flags := unix.MAP_SHARED
|
|
|
|
prot := unix.PROT_READ
|
2018-05-19 08:49:46 -04:00
|
|
|
switch {
|
|
|
|
case inprot© != 0:
|
2019-08-23 12:40:30 -04:00
|
|
|
prot |= unix.PROT_WRITE
|
|
|
|
flags = unix.MAP_PRIVATE
|
2018-05-19 08:49:46 -04:00
|
|
|
case inprot&RDWR != 0:
|
2019-08-23 12:40:30 -04:00
|
|
|
prot |= unix.PROT_WRITE
|
2018-05-19 08:49:46 -04:00
|
|
|
}
|
|
|
|
if inprot&EXEC != 0 {
|
2019-08-23 12:40:30 -04:00
|
|
|
prot |= unix.PROT_EXEC
|
2018-05-19 08:49:46 -04:00
|
|
|
}
|
|
|
|
if inflags&ANON != 0 {
|
2019-08-23 12:40:30 -04:00
|
|
|
flags |= unix.MAP_ANON
|
2018-05-19 08:49:46 -04:00
|
|
|
}
|
|
|
|
|
2019-08-23 12:40:30 -04:00
|
|
|
b, err := unix.Mmap(int(fd), off, len, prot, flags)
|
2018-05-19 08:49:46 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2019-08-23 12:40:30 -04:00
|
|
|
func (m MMap) flush() error {
|
|
|
|
return unix.Msync([]byte(m), unix.MS_SYNC)
|
2018-05-19 08:49:46 -04:00
|
|
|
}
|
|
|
|
|
2019-08-23 12:40:30 -04:00
|
|
|
func (m MMap) lock() error {
|
|
|
|
return unix.Mlock([]byte(m))
|
2018-05-19 08:49:46 -04:00
|
|
|
}
|
|
|
|
|
2019-08-23 12:40:30 -04:00
|
|
|
func (m MMap) unlock() error {
|
|
|
|
return unix.Munlock([]byte(m))
|
2018-05-19 08:49:46 -04:00
|
|
|
}
|
|
|
|
|
2019-08-23 12:40:30 -04:00
|
|
|
func (m MMap) unmap() error {
|
|
|
|
return unix.Munmap([]byte(m))
|
2018-05-19 08:49:46 -04:00
|
|
|
}
|