forkjo/vendor/github.com/edsrzf/mmap-go/mmap_unix.go

52 lines
1,019 B
Go
Raw Normal View History

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