Skip to content
This repository has been archived by the owner on Aug 13, 2019. It is now read-only.

Use the right sized byte array for large indexes #301

Merged
merged 1 commit into from
Mar 14, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions fileutil/mmap_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ import (
"unsafe"
)

func mmap(f *os.File, sz int) ([]byte, error) {
low, high := uint32(sz), uint32(sz>>32)
const maxMapSize = 0xFFFFFFFFFFFF // 256TB

func mmap(f *os.File, size int) ([]byte, error) {
low, high := uint32(size), uint32(size>>32)
h, errno := syscall.CreateFileMapping(syscall.Handle(f.Fd()), nil, syscall.PAGE_READONLY, high, low, nil)
if h == 0 {
return nil, os.NewSyscallError("CreateFileMapping", errno)
}

addr, errno := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, uintptr(sz))
addr, errno := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, uintptr(size))
if addr == 0 {
return nil, os.NewSyscallError("MapViewOfFile", errno)
}
Expand All @@ -35,7 +37,7 @@ func mmap(f *os.File, sz int) ([]byte, error) {
return nil, os.NewSyscallError("CloseHandle", err)
}

return (*[1 << 30]byte)(unsafe.Pointer(addr))[:sz], nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sz is just size? Can we name it like that? (maybe just for master)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I have stupid question. I have never used mmap before, but... why don't we cast to just [sz]byte here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking the same thing and tried it but casting to an array requires a constant for the array size as this is evaluated at compile time

  • return (*[sz]byte)(unsafe.Pointer(addr))[:sz], nil = non-constant array bound

return (*[maxMapSize]byte)(unsafe.Pointer(addr))[:size], nil
}

func munmap(b []byte) error {
Expand Down