Skip to content

Modernize Windows mmap code #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 1, 2025
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
93 changes: 55 additions & 38 deletions mmap_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,77 +33,94 @@ package maxminddb
import (
"errors"
"os"
"reflect"
"sync"
"unsafe"

"golang.org/x/sys/windows"
)

type memoryMap []byte

// Windows
var (
handleLock sync.Mutex
handleMap = map[uintptr]windows.Handle{}
)

func mmap(fd int, length int) (data []byte, err error) {
h, errno := windows.CreateFileMapping(windows.Handle(fd), nil,
uint32(windows.PAGE_READONLY), 0, uint32(length), nil)
if h == 0 {
return nil, os.NewSyscallError("CreateFileMapping", errno)
// mmap maps a file into memory and returns a byte slice.
func mmap(fd int, length int) ([]byte, error) {
// Create a file mapping
handle, err := windows.CreateFileMapping(
windows.Handle(fd),
nil,
windows.PAGE_READONLY,
0,
uint32(length),
nil,
)
if err != nil {
return nil, os.NewSyscallError("CreateFileMapping", err)
}

addr, errno := windows.MapViewOfFile(h, uint32(windows.FILE_MAP_READ), 0,
0, uintptr(length))
if addr == 0 {
return nil, os.NewSyscallError("MapViewOfFile", errno)
// Map the file into memory
addr, err := windows.MapViewOfFile(
handle,
windows.FILE_MAP_READ,
0,
0,
uintptr(length),
)
if err != nil {
windows.CloseHandle(handle)
return nil, os.NewSyscallError("MapViewOfFile", err)
}

// Store the handle in the map
handleLock.Lock()
handleMap[addr] = h
handleMap[addr] = handle
handleLock.Unlock()

m := memoryMap{}
dh := m.header()
dh.Data = addr
dh.Len = length
dh.Cap = dh.Len

return m, nil
}

func (m *memoryMap) header() *reflect.SliceHeader {
return (*reflect.SliceHeader)(unsafe.Pointer(m))
data := unsafe.Slice((*byte)(unsafe.Pointer(addr)), length)
return data, nil
}

func flush(addr, len uintptr) error {
errno := windows.FlushViewOfFile(addr, len)
return os.NewSyscallError("FlushViewOfFile", errno)
// flush ensures changes to a memory-mapped region are written to disk.
func flush(addr, length uintptr) error {
err := windows.FlushViewOfFile(addr, length)
if err != nil {
return os.NewSyscallError("FlushViewOfFile", err)
}
return nil
}

func munmap(b []byte) (err error) {
m := memoryMap(b)
dh := m.header()

addr := dh.Data
length := uintptr(dh.Len)
// munmap unmaps a memory-mapped file and releases associated resources.
func munmap(b []byte) error {
// Convert slice to base address and length
data := unsafe.SliceData(b)
addr := uintptr(unsafe.Pointer(data))
length := uintptr(len(b))

flush(addr, length)
err = windows.UnmapViewOfFile(addr)
if err != nil {
// Flush the memory region
if err := flush(addr, length); err != nil {
return err
}

// Unmap the memory
if err := windows.UnmapViewOfFile(addr); err != nil {
return os.NewSyscallError("UnmapViewOfFile", err)
}

// Remove the handle from the map and close it
handleLock.Lock()
defer handleLock.Unlock()

handle, ok := handleMap[addr]
if !ok {
// should be impossible; we would've errored above
return errors.New("unknown base address")
}
delete(handleMap, addr)

e := windows.CloseHandle(windows.Handle(handle))
return os.NewSyscallError("CloseHandle", e)
if err := windows.CloseHandle(handle); err != nil {
return os.NewSyscallError("CloseHandle", err)
}
return nil
}
Loading