Skip to content

Commit

Permalink
Modernize Windows mmap code (again)
Browse files Browse the repository at this point in the history
Closes #162.
  • Loading branch information
database64128 committed Jan 20, 2025
1 parent 2fabca0 commit 2a6b0bf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 105 deletions.
29 changes: 0 additions & 29 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,3 @@ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.

--------------------------------------------------------------------

The file mmap_windows.go is governed by the following license:

Copyright (c) 2011, Evan Shaw <[email protected]>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,4 @@ with your changes.

## License ##

This is free software, primarily licensed under the ISC License. The file
`mmap_windows.go` is licensed under a BSD-style license by Evan Shaw.
This is free software, primarily licensed under the ISC License.
94 changes: 20 additions & 74 deletions mmap_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,14 @@

package maxminddb

// Windows support largely borrowed from mmap-go.
//
// Copyright (c) 2011, Evan Shaw <[email protected]>
// All rights reserved.

// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of the copyright holder nor the
// names of its contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.

// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import (
"errors"
"os"
"sync"
"unsafe"

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

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

// mmap maps a file into memory and returns a byte slice.
func mmap(fd int, length int) ([]byte, error) {
// Create a file mapping
Expand All @@ -53,74 +19,54 @@ func mmap(fd int, length int) ([]byte, error) {
nil,
windows.PAGE_READONLY,
0,
uint32(length),
0,
nil,
)
if err != nil {
return nil, os.NewSyscallError("CreateFileMapping", err)
}
defer windows.CloseHandle(handle)

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

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

data := unsafe.Slice((*byte)(unsafe.Pointer(addr)), length)
return data, nil
}

// 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)
// When there's not enough address space for the whole file (e.g. large
// files on 32-bit systems), MapViewOfFile may return a partial mapping.
// Query the region size and fail on partial mappings.
var info windows.MemoryBasicInformation
if err := windows.VirtualQuery(addrUintptr, &info, unsafe.Sizeof(info)); err != nil {
_ = windows.UnmapViewOfFile(addrUintptr)
return nil, os.NewSyscallError("VirtualQuery", err)
}
return nil
if info.RegionSize < uintptr(length) {
_ = windows.UnmapViewOfFile(addrUintptr)
return nil, errors.New("file too large")
}

// Workaround for unsafeptr check in go vet, see
// https://github.com/golang/go/issues/58625
addr := *(*unsafe.Pointer)(unsafe.Pointer(&addrUintptr))
return unsafe.Slice((*byte)(addr), length), nil
}

// 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 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)

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

0 comments on commit 2a6b0bf

Please sign in to comment.