github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/mmap/mmap_unix.go (about) 1 // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 //go:build darwin || dragonfly || freebsd || linux || openbsd || solaris || netbsd 16 // +build darwin dragonfly freebsd linux openbsd solaris netbsd 17 18 package mmap 19 20 import ( 21 "golang.org/x/sys/unix" 22 ) 23 24 func mmapfd(len int, inprot, inflags, fd uintptr, off int64) ([]byte, error) { 25 flags := unix.MAP_SHARED 26 prot := unix.PROT_READ 27 switch { 28 case inprot© != 0: 29 prot |= unix.PROT_WRITE 30 flags = unix.MAP_PRIVATE 31 case inprot&RDWR != 0: 32 prot |= unix.PROT_WRITE 33 } 34 if inprot&EXEC != 0 { 35 prot |= unix.PROT_EXEC 36 } 37 if inflags&ANON != 0 { 38 flags |= unix.MAP_ANON 39 } 40 41 b, err := unix.Mmap(int(fd), off, len, prot, flags) 42 if err != nil { 43 return nil, err 44 } 45 return b, nil 46 } 47 48 func (m Mbuf) flush() error { 49 return unix.Msync([]byte(m), unix.MS_SYNC) 50 } 51 52 func (m Mbuf) lock() error { 53 return unix.Mlock([]byte(m)) 54 } 55 56 func (m Mbuf) unlock() error { 57 return unix.Munlock([]byte(m)) 58 } 59 60 func (m Mbuf) unmap() error { 61 return unix.Munmap([]byte(m)) 62 }