You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1019 B
52 lines
1019 B
7 years ago
|
// 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 (
|
||
5 years ago
|
"golang.org/x/sys/unix"
|
||
7 years ago
|
)
|
||
|
|
||
|
func mmap(len int, inprot, inflags, fd uintptr, off int64) ([]byte, error) {
|
||
5 years ago
|
flags := unix.MAP_SHARED
|
||
|
prot := unix.PROT_READ
|
||
7 years ago
|
switch {
|
||
|
case inprot© != 0:
|
||
5 years ago
|
prot |= unix.PROT_WRITE
|
||
|
flags = unix.MAP_PRIVATE
|
||
7 years ago
|
case inprot&RDWR != 0:
|
||
5 years ago
|
prot |= unix.PROT_WRITE
|
||
7 years ago
|
}
|
||
|
if inprot&EXEC != 0 {
|
||
5 years ago
|
prot |= unix.PROT_EXEC
|
||
7 years ago
|
}
|
||
|
if inflags&ANON != 0 {
|
||
5 years ago
|
flags |= unix.MAP_ANON
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
b, err := unix.Mmap(int(fd), off, len, prot, flags)
|
||
7 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return b, nil
|
||
|
}
|
||
|
|
||
5 years ago
|
func (m MMap) flush() error {
|
||
|
return unix.Msync([]byte(m), unix.MS_SYNC)
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
func (m MMap) lock() error {
|
||
|
return unix.Mlock([]byte(m))
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
func (m MMap) unlock() error {
|
||
|
return unix.Munlock([]byte(m))
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
func (m MMap) unmap() error {
|
||
|
return unix.Munmap([]byte(m))
|
||
7 years ago
|
}
|