1// Copyright 2011 Evan Shaw. All rights reserved. 2// Use of this source code is governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5// +build darwin dragonfly freebsd linux openbsd solaris netbsd 6 7package mmap 8 9import ( 10 "golang.org/x/sys/unix" 11) 12 13func mmap(len int, inprot, inflags, fd uintptr, off int64) ([]byte, error) { 14 flags := unix.MAP_SHARED 15 prot := unix.PROT_READ 16 switch { 17 case inprot© != 0: 18 prot |= unix.PROT_WRITE 19 flags = unix.MAP_PRIVATE 20 case inprot&RDWR != 0: 21 prot |= unix.PROT_WRITE 22 } 23 if inprot&EXEC != 0 { 24 prot |= unix.PROT_EXEC 25 } 26 if inflags&ANON != 0 { 27 flags |= unix.MAP_ANON 28 } 29 30 b, err := unix.Mmap(int(fd), off, len, prot, flags) 31 if err != nil { 32 return nil, err 33 } 34 return b, nil 35} 36 37func (m MMap) flush() error { 38 return unix.Msync([]byte(m), unix.MS_SYNC) 39} 40 41func (m MMap) lock() error { 42 return unix.Mlock([]byte(m)) 43} 44 45func (m MMap) unlock() error { 46 return unix.Munlock([]byte(m)) 47} 48 49func (m MMap) unmap() error { 50 return unix.Munmap([]byte(m)) 51} 52