gitee.com/quant1x/gox@v1.21.2/cache/mmap_unix.go (about)

     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  //go:build darwin || dragonfly || freebsd || linux || openbsd || solaris || netbsd
     6  // +build darwin dragonfly freebsd linux openbsd solaris netbsd
     7  
     8  package cache
     9  
    10  import (
    11  	"golang.org/x/sys/unix"
    12  )
    13  
    14  func mmap(len int, inprot, inflags, fd uintptr, off int64) ([]byte, error) {
    15  	flags := unix.MAP_SHARED
    16  	prot := unix.PROT_READ
    17  	switch {
    18  	case inprot&COPY != 0:
    19  		prot |= unix.PROT_WRITE
    20  		flags = unix.MAP_PRIVATE
    21  	case inprot&RDWR != 0:
    22  		prot |= unix.PROT_WRITE
    23  	}
    24  	if inprot&EXEC != 0 {
    25  		prot |= unix.PROT_EXEC
    26  	}
    27  	if inflags&ANON != 0 {
    28  		flags |= unix.MAP_ANON
    29  	}
    30  
    31  	b, err := unix.Mmap(int(fd), off, len, prot, flags)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  	return b, nil
    36  }
    37  
    38  func (m MMap) flush() error {
    39  	return unix.Msync([]byte(m), unix.MS_SYNC)
    40  }
    41  
    42  func (m MMap) lock() error {
    43  	return unix.Mlock([]byte(m))
    44  }
    45  
    46  func (m MMap) unlock() error {
    47  	return unix.Munlock([]byte(m))
    48  }
    49  
    50  func (m MMap) unmap() error {
    51  	return unix.Munmap([]byte(m))
    52  }