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