2014-06-11 17:11:21 +00:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
2014-06-12 15:23:30 +00:00
|
|
|
"fmt"
|
2014-06-11 17:11:21 +00:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2014-06-12 15:23:30 +00:00
|
|
|
// mmap memory maps a DB's data file.
|
2014-06-11 17:11:21 +00:00
|
|
|
// Based on: https://github.com/edsrzf/mmap-go
|
2014-06-12 15:23:30 +00:00
|
|
|
func mmap(db *DB, sz int) error {
|
|
|
|
// Truncate the database to the size of the mmap.
|
|
|
|
if err := db.file.Truncate(int64(sz)); err != nil {
|
|
|
|
return fmt.Errorf("truncate: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-06-11 17:11:21 +00:00
|
|
|
// Open a file mapping handle.
|
2014-06-12 15:23:30 +00:00
|
|
|
sizelo := uint32(sz >> 32)
|
|
|
|
sizehi := uint32(sz & 0xffffffff)
|
|
|
|
h, errno := syscall.CreateFileMapping(syscall.Handle(db.file.Fd()), nil, syscall.PAGE_READONLY, sizelo, sizehi, nil)
|
2014-06-11 17:11:21 +00:00
|
|
|
if h == 0 {
|
2014-06-12 15:23:30 +00:00
|
|
|
return os.NewSyscallError("CreateFileMapping", errno)
|
2014-06-11 17:11:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the memory map.
|
|
|
|
addr, errno := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, uintptr(sz))
|
|
|
|
if addr == 0 {
|
2014-06-12 15:23:30 +00:00
|
|
|
return os.NewSyscallError("MapViewOfFile", errno)
|
2014-06-11 17:11:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close mapping handle.
|
|
|
|
if err := syscall.CloseHandle(syscall.Handle(h)); err != nil {
|
2014-06-12 15:23:30 +00:00
|
|
|
return os.NewSyscallError("CloseHandle", err)
|
2014-06-11 17:11:21 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 15:23:30 +00:00
|
|
|
// Convert to a byte array.
|
|
|
|
db.data = ((*[maxMapSize]byte)(unsafe.Pointer(addr)))
|
|
|
|
db.datasz = sz
|
|
|
|
|
|
|
|
return nil
|
2014-06-11 17:11:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// munmap unmaps a pointer from a file.
|
|
|
|
// Based on: https://github.com/edsrzf/mmap-go
|
2014-06-12 15:23:30 +00:00
|
|
|
func munmap(db *DB) error {
|
|
|
|
if db.data == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := (uintptr)(unsafe.Pointer(&db.data[0]))
|
2014-06-11 17:11:21 +00:00
|
|
|
if err := syscall.UnmapViewOfFile(addr); err != nil {
|
|
|
|
return os.NewSyscallError("UnmapViewOfFile", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|