github.com/database64128/shadowsocks-go@v1.10.2-0.20240315062903-143a773533f1/mmap/mmap_unix_generic.go (about)

     1  //go:build unix && ((!freebsd && !linux) || (!amd64 && !arm64 && !loong64 && !mips64 && !mips64le && !ppc64 && !ppc64le && !riscv64 && !sparc64))
     2  
     3  package mmap
     4  
     5  import (
     6  	"os"
     7  	"unsafe"
     8  
     9  	"golang.org/x/sys/unix"
    10  )
    11  
    12  func readFile(f *os.File, size int64) (uintptr, error) {
    13  	data, err := unix.Mmap(int(f.Fd()), 0, int(size), unix.PROT_READ, unix.MAP_SHARED)
    14  	if err != nil {
    15  		return 0, err
    16  	}
    17  	return *(*uintptr)(unsafe.Pointer(&data)), nil
    18  }
    19  
    20  func unmap(addr uintptr, length int) error {
    21  	b := sliceHeader{
    22  		data: addr,
    23  		len:  length,
    24  		cap:  length,
    25  	}
    26  	return unix.Munmap(*(*[]byte)(unsafe.Pointer(&b)))
    27  }