github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/go/mmap/mmap_windows.go (about)

     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  package mmap
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  	"syscall"
    11  	"unsafe"
    12  
    13  	"github.com/go-asm/go/syscall/windows"
    14  )
    15  
    16  func mmapFile(f *os.File) (Data, error) {
    17  	st, err := f.Stat()
    18  	if err != nil {
    19  		return Data{}, err
    20  	}
    21  	size := st.Size()
    22  	if size == 0 {
    23  		return Data{f, nil}, nil
    24  	}
    25  	h, err := syscall.CreateFileMapping(syscall.Handle(f.Fd()), nil, syscall.PAGE_READONLY, 0, 0, nil)
    26  	if err != nil {
    27  		return Data{}, fmt.Errorf("CreateFileMapping %s: %w", f.Name(), err)
    28  	}
    29  
    30  	addr, err := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, 0)
    31  	if err != nil {
    32  		return Data{}, fmt.Errorf("MapViewOfFile %s: %w", f.Name(), err)
    33  	}
    34  	var info windows.MemoryBasicInformation
    35  	err = windows.VirtualQuery(addr, &info, unsafe.Sizeof(info))
    36  	if err != nil {
    37  		return Data{}, fmt.Errorf("VirtualQuery %s: %w", f.Name(), err)
    38  	}
    39  	data := unsafe.Slice((*byte)(unsafe.Pointer(addr)), int(info.RegionSize))
    40  	return Data{f, data}, nil
    41  }