aboutsummaryrefslogtreecommitdiff
path: root/bolt_windows.go
blob: 9962f03aa6c0441c2b310d66ef85b4fa9a543019 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package bolt

import (
	"os"
	"syscall"
	"unsafe"
)

var odirect int

// fdatasync flushes written data to a file descriptor.
func fdatasync(f *os.File) error {
	return f.Sync()
}

// flock acquires an advisory lock on a file descriptor.
func flock(f *os.File) error {
	return nil
}

// funlock releases an advisory lock on a file descriptor.
func funlock(f *os.File) error {
	return nil
}

// mmap memory maps a file to a byte slice.
// Based on: https://github.com/edsrzf/mmap-go
func mmap(f *os.File, sz int) ([]byte, error) {
	// Open a file mapping handle.
	sizelo, sizehi := uint32(sz>>32), uint32(sz&0xffffffff)
	h, errno := syscall.CreateFileMapping(syscall.Handle(f.Fd()), nil, syscall.PAGE_READONLY, sizehi, sizelo, nil)
	if h == 0 {
		return nil, os.NewSyscallError("CreateFileMapping", errno)
	}

	// Create the memory map.
	addr, errno := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, uintptr(sz))
	if addr == 0 {
		return nil, os.NewSyscallError("MapViewOfFile", errno)
	}

	// Close mapping handle.
	if err := syscall.CloseHandle(syscall.Handle(h)); err != nil {
		return nil, os.NewSyscallError("CloseHandle", err)
	}

	// Convert to a byte slice.
	b := ((*[0xFFFFFFF]byte)(unsafe.Pointer(addr)))[0:sz]
	return b, nil
}

// munmap unmaps a pointer from a file.
// Based on: https://github.com/edsrzf/mmap-go
func munmap(b []byte) error {
	addr := (uintptr)(unsafe.Pointer(&b[0]))
	if err := syscall.UnmapViewOfFile(addr); err != nil {
		return os.NewSyscallError("UnmapViewOfFile", err)
	}
	return nil
}