github.com/flower-corp/rosedb@v1.1.2-0.20230117132829-21dc4f7b319a/mmap/mmap_darwin.go (about)

     1  package mmap
     2  
     3  import (
     4  	"os"
     5  	"syscall"
     6  	"unsafe"
     7  
     8  	"golang.org/x/sys/unix"
     9  )
    10  
    11  // Mmap uses the mmap system call to memory-map a file. If writable is true,
    12  // memory protection of the pages is set so that they may be written to as well.
    13  func mmap(fd *os.File, writable bool, size int64) ([]byte, error) {
    14  	mtype := unix.PROT_READ
    15  	if writable {
    16  		mtype |= unix.PROT_WRITE
    17  	}
    18  	return unix.Mmap(int(fd.Fd()), 0, int(size), mtype, unix.MAP_SHARED)
    19  }
    20  
    21  // Munmap unmaps a previously mapped slice.
    22  func munmap(b []byte) error {
    23  	return unix.Munmap(b)
    24  }
    25  
    26  // This is required because the unix package does not support the madvise system call on OS X.
    27  func madvise(b []byte, readahead bool) error {
    28  	advice := unix.MADV_NORMAL
    29  	if !readahead {
    30  		advice = unix.MADV_RANDOM
    31  	}
    32  
    33  	_, _, e1 := syscall.Syscall(syscall.SYS_MADVISE, uintptr(unsafe.Pointer(&b[0])),
    34  		uintptr(len(b)), uintptr(advice))
    35  	if e1 != 0 {
    36  		return e1
    37  	}
    38  	return nil
    39  }
    40  
    41  func msync(b []byte) error {
    42  	return unix.Msync(b, unix.MS_SYNC)
    43  }