-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
21 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
} |