github.com/outcaste-io/ristretto@v0.2.3/z/mmap_darwin.go (about) 1 /* 2 * Copyright 2019 Dgraph Labs, Inc. and Contributors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package z 18 19 import ( 20 "os" 21 "syscall" 22 "unsafe" 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 // This is required because the unix package does not support the madvise system call on OS X. 43 func madvise(b []byte, readahead bool) error { 44 advice := unix.MADV_NORMAL 45 if !readahead { 46 advice = unix.MADV_RANDOM 47 } 48 49 _, _, e1 := syscall.Syscall(syscall.SYS_MADVISE, uintptr(unsafe.Pointer(&b[0])), 50 uintptr(len(b)), uintptr(advice)) 51 if e1 != 0 { 52 return e1 53 } 54 return nil 55 } 56 57 func msync(b []byte) error { 58 return unix.Msync(b, unix.MS_SYNC) 59 }