aboutsummaryrefslogtreecommitdiff
path: root/bolt_linux.go
diff options
context:
space:
mode:
authorBen Johnson <benbjohnson@yahoo.com>2014-06-12 09:23:30 -0600
committerBen Johnson <benbjohnson@yahoo.com>2014-06-12 09:23:30 -0600
commit1c97a490dda8e8b8d3805fa6debccc34f32b540d (patch)
tree7d0a35f8becb520361016cb7c6551afe64c7ebf1 /bolt_linux.go
parentAdd Windows support. (diff)
downloaddedo-1c97a490dda8e8b8d3805fa6debccc34f32b540d.tar.gz
dedo-1c97a490dda8e8b8d3805fa6debccc34f32b540d.tar.xz
Add Windows support.
This commit adds Windows support to Bolt. Windows memory maps return an address instead of a byte slice so the DB.data field had to be refactored to be a pointer to a large byte array.
Diffstat (limited to 'bolt_linux.go')
-rw-r--r--bolt_linux.go32
1 files changed, 26 insertions, 6 deletions
diff --git a/bolt_linux.go b/bolt_linux.go
index 9aba955..761a83e 100644
--- a/bolt_linux.go
+++ b/bolt_linux.go
@@ -3,6 +3,7 @@ package bolt
import (
"os"
"syscall"
+ "unsafe"
)
var odirect = syscall.O_DIRECT
@@ -22,12 +23,31 @@ func funlock(f *os.File) error {
return syscall.Flock(int(f.Fd()), syscall.LOCK_UN)
}
-// mmap memory maps a file to a byte slice.
-func mmap(f *os.File, sz int) ([]byte, error) {
- return syscall.Mmap(int(f.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED)
+// mmap memory maps a DB's data file.
+func mmap(db *DB, sz int) error {
+ b, err := syscall.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED)
+ if err != nil {
+ return err
+ }
+
+ // Save the original byte slice and convert to a byte array pointer.
+ db.dataref = b
+ db.data = (*[maxMapSize]byte)(unsafe.Pointer(&b[0]))
+ db.datasz = sz
+ return nil
}
-// munmap unmaps a pointer from a file.
-func munmap(b []byte) error {
- return syscall.Munmap(b)
+// munmap unmaps a DB's data file from memory.
+func munmap(db *DB) error {
+ // Ignore the unmap if we have no mapped data.
+ if db.dataref == nil {
+ return nil
+ }
+
+ // Unmap using the original byte slice.
+ err := syscall.Munmap(db.dataref)
+ db.dataref = nil
+ db.data = nil
+ db.datasz = 0
+ return err
}