github.com/outcaste-io/ristretto@v0.2.3/z/mmap_unix.go (about)

     1  //go:build !windows && !darwin && !plan9 && !linux && !wasip1
     2  // +build !windows,!darwin,!plan9,!linux,!wasip1
     3  
     4  /*
     5   * Copyright 2019 Dgraph Labs, Inc. and Contributors
     6   *
     7   * Licensed under the Apache License, Version 2.0 (the "License");
     8   * you may not use this file except in compliance with the License.
     9   * You may obtain a copy of the License at
    10   *
    11   *     http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   * Unless required by applicable law or agreed to in writing, software
    14   * distributed under the License is distributed on an "AS IS" BASIS,
    15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16   * See the License for the specific language governing permissions and
    17   * limitations under the License.
    18   */
    19  
    20  package z
    21  
    22  import (
    23  	"os"
    24  
    25  	"golang.org/x/sys/unix"
    26  )
    27  
    28  // Mmap uses the mmap system call to memory-map a file. If writable is true,
    29  // memory protection of the pages is set so that they may be written to as well.
    30  func mmap(fd *os.File, writable bool, size int64) ([]byte, error) {
    31  	mtype := unix.PROT_READ
    32  	if writable {
    33  		mtype |= unix.PROT_WRITE
    34  	}
    35  	return unix.Mmap(int(fd.Fd()), 0, int(size), mtype, unix.MAP_SHARED)
    36  }
    37  
    38  // Munmap unmaps a previously mapped slice.
    39  func munmap(b []byte) error {
    40  	return unix.Munmap(b)
    41  }
    42  
    43  // Madvise uses the madvise system call to give advise about the use of memory
    44  // when using a slice that is memory-mapped to a file. Set the readahead flag to
    45  // false if page references are expected in random order.
    46  func madvise(b []byte, readahead bool) error {
    47  	flags := unix.MADV_NORMAL
    48  	if !readahead {
    49  		flags = unix.MADV_RANDOM
    50  	}
    51  	return unix.Madvise(b, flags)
    52  }
    53  
    54  func msync(b []byte) error {
    55  	return unix.Msync(b, unix.MS_SYNC)
    56  }