github.com/ledgerwatch/erigon-lib@v1.0.0/mmap/mmap_windows.go (about) 1 /* 2 Copyright 2021 Erigon contributors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package mmap 18 19 import ( 20 "os" 21 "unsafe" 22 23 "golang.org/x/sys/windows" 24 ) 25 26 const MaxMapSize = 0xFFFFFFFFFFFF 27 28 func Mmap(f *os.File, size int) ([]byte, *[MaxMapSize]byte, error) { 29 // Open a file mapping handle. 30 sizelo := uint32(size >> 32) 31 sizehi := uint32(size) & 0xffffffff 32 h, errno := windows.CreateFileMapping(windows.Handle(f.Fd()), nil, windows.PAGE_READONLY, sizelo, sizehi, nil) 33 if h == 0 { 34 return nil, nil, os.NewSyscallError("CreateFileMapping", errno) 35 } 36 37 // Create the memory map. 38 addr, errno := windows.MapViewOfFile(h, windows.FILE_MAP_READ, 0, 0, uintptr(size)) 39 if addr == 0 { 40 return nil, nil, os.NewSyscallError("MapViewOfFile", errno) 41 } 42 43 // Close mapping handle. 44 if err := windows.CloseHandle(windows.Handle(h)); err != nil { 45 return nil, nil, os.NewSyscallError("CloseHandle", err) 46 } 47 48 // Convert to a byte array. 49 mmapHandle2 := ((*[MaxMapSize]byte)(unsafe.Pointer(addr))) 50 return mmapHandle2[:size], mmapHandle2, nil 51 } 52 53 func MadviseSequential(mmapHandle1 []byte) error { return nil } 54 func MadviseNormal(mmapHandle1 []byte) error { return nil } 55 func MadviseWillNeed(mmapHandle1 []byte) error { return nil } 56 func MadviseRandom(mmapHandle1 []byte) error { return nil } 57 58 func Munmap(_ []byte, mmapHandle2 *[MaxMapSize]byte) error { 59 if mmapHandle2 == nil { 60 return nil 61 } 62 63 addr := (uintptr)(unsafe.Pointer(&mmapHandle2[0])) 64 if err := windows.UnmapViewOfFile(addr); err != nil { 65 return os.NewSyscallError("UnmapViewOfFile", err) 66 } 67 return nil 68 }