11package util
22
33import (
4- "context"
54 "os"
65 "reflect"
76 "unsafe"
87
9- "github.com/tetratelabs/wazero/api"
108 "golang.org/x/sys/windows"
119)
1210
@@ -16,14 +14,21 @@ type MappedRegion struct {
1614 addr uintptr
1715}
1816
19- func MapRegion (ctx context.Context , mod api.Module , f * os.File , offset int64 , size int32 ) (* MappedRegion , error ) {
20- h , err := windows .CreateFileMapping (windows .Handle (f .Fd ()), nil , windows .PAGE_READWRITE , 0 , 0 , nil )
17+ func MapRegion (f * os.File , offset int64 , size int32 ) (* MappedRegion , error ) {
18+ maxSize := offset + int64 (size )
19+ h , err := windows .CreateFileMapping (
20+ windows .Handle (f .Fd ()), nil , windows .PAGE_READWRITE ,
21+ uint32 (maxSize >> 32 ), uint32 (maxSize ), nil )
2122 if h == 0 {
2223 return nil , err
2324 }
2425
26+ const allocationGranularity = 64 * 1024
27+ align := offset % allocationGranularity
28+ offset -= align
29+
2530 a , err := windows .MapViewOfFile (h , windows .FILE_MAP_WRITE ,
26- uint32 (offset >> 32 ), uint32 (offset ), uintptr (size ))
31+ uint32 (offset >> 32 ), uint32 (offset ), uintptr (size )+ uintptr ( align ) )
2732 if a == 0 {
2833 windows .CloseHandle (h )
2934 return nil , err
@@ -32,9 +37,9 @@ func MapRegion(ctx context.Context, mod api.Module, f *os.File, offset int64, si
3237 ret := & MappedRegion {Handle : h , addr : a }
3338 // SliceHeader, although deprecated, avoids a go vet warning.
3439 sh := (* reflect .SliceHeader )(unsafe .Pointer (& ret .Data ))
40+ sh .Data = a + uintptr (align )
3541 sh .Len = int (size )
3642 sh .Cap = int (size )
37- sh .Data = a
3843 return ret , nil
3944}
4045
0 commit comments