2018-05-19 18:19:46 +05:30
|
|
|
// 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 22:10:30 +05:30
|
|
|
"golang.org/x/sys/unix"
|
2018-05-19 18:19:46 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
func mmap(len int, inprot, inflags, fd uintptr, off int64) ([]byte, error) {
|
2019-08-23 22:10:30 +05:30
|
|
|
flags := unix.MAP_SHARED
|
|
|
|
prot := unix.PROT_READ
|
2018-05-19 18:19:46 +05:30
|
|
|
switch {
|
|
|
|
case inprot© != 0:
|
2019-08-23 22:10:30 +05:30
|
|
|
prot |= unix.PROT_WRITE
|
|
|
|
flags = unix.MAP_PRIVATE
|
2018-05-19 18:19:46 +05:30
|
|
|
case inprot&RDWR != 0:
|
2019-08-23 22:10:30 +05:30
|
|
|
prot |= unix.PROT_WRITE
|
2018-05-19 18:19:46 +05:30
|
|
|
}
|
|
|
|
if inprot&EXEC != 0 {
|
2019-08-23 22:10:30 +05:30
|
|
|
prot |= unix.PROT_EXEC
|
2018-05-19 18:19:46 +05:30
|
|
|
}
|
|
|
|
if inflags&ANON != 0 {
|
2019-08-23 22:10:30 +05:30
|
|
|
flags |= unix.MAP_ANON
|
2018-05-19 18:19:46 +05:30
|
|
|
}
|
|
|
|
|
2019-08-23 22:10:30 +05:30
|
|
|
b, err := unix.Mmap(int(fd), off, len, prot, flags)
|
2018-05-19 18:19:46 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2019-08-23 22:10:30 +05:30
|
|
|
func (m MMap) flush() error {
|
|
|
|
return unix.Msync([]byte(m), unix.MS_SYNC)
|
2018-05-19 18:19:46 +05:30
|
|
|
}
|
|
|
|
|
2019-08-23 22:10:30 +05:30
|
|
|
func (m MMap) lock() error {
|
|
|
|
return unix.Mlock([]byte(m))
|
2018-05-19 18:19:46 +05:30
|
|
|
}
|
|
|
|
|
2019-08-23 22:10:30 +05:30
|
|
|
func (m MMap) unlock() error {
|
|
|
|
return unix.Munlock([]byte(m))
|
2018-05-19 18:19:46 +05:30
|
|
|
}
|
|
|
|
|
2019-08-23 22:10:30 +05:30
|
|
|
func (m MMap) unmap() error {
|
|
|
|
return unix.Munmap([]byte(m))
|
2018-05-19 18:19:46 +05:30
|
|
|
}
|