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