-
Notifications
You must be signed in to change notification settings - Fork 646
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e9ce18
commit c2577db
Showing
5 changed files
with
123 additions
and
18 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package bolt | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
) | ||
|
||
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 syscall.Flock(int(f.Fd()), syscall.LOCK_EX) | ||
} | ||
|
||
// funlock releases an advisory lock on a file descriptor. | ||
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) | ||
} | ||
|
||
// munmap unmaps a pointer from a file. | ||
func munmap(b []byte) error { | ||
return syscall.Munmap(b) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,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 | ||
} |
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