Source file src/cmd/vendor/golang.org/x/telemetry/internal/mmap/mmap_unix.go

     1  // Copyright 2011 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build unix && (!solaris || go1.20)
     6  
     7  package mmap
     8  
     9  import (
    10  	"fmt"
    11  	"io/fs"
    12  	"os"
    13  	"syscall"
    14  )
    15  
    16  func mmapFile(f *os.File) (*Data, error) {
    17  	st, err := f.Stat()
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  	size := st.Size()
    22  	pagesize := int64(os.Getpagesize())
    23  	if int64(int(size+(pagesize-1))) != size+(pagesize-1) {
    24  		return nil, fmt.Errorf("%s: too large for mmap", f.Name())
    25  	}
    26  	n := int(size)
    27  	if n == 0 {
    28  		return &Data{f, nil, nil}, nil
    29  	}
    30  	mmapLength := int(((size + pagesize - 1) / pagesize) * pagesize) // round up to page size
    31  	data, err := syscall.Mmap(int(f.Fd()), 0, mmapLength, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
    32  	if err != nil {
    33  		return nil, &fs.PathError{Op: "mmap", Path: f.Name(), Err: err}
    34  	}
    35  	return &Data{f, data[:n], nil}, nil
    36  }
    37  
    38  func munmapFile(d *Data) error {
    39  	if len(d.Data) == 0 {
    40  		return nil
    41  	}
    42  	err := syscall.Munmap(d.Data)
    43  	if err != nil {
    44  		return &fs.PathError{Op: "munmap", Path: d.f.Name(), Err: err}
    45  	}
    46  	return nil
    47  }
    48  

View as plain text