Skip to content

Commit

Permalink
feat(mmap-go): make fallocate work on both linux and darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
symbiont-stevan-andjelkovic committed Jan 12, 2022
1 parent 850448b commit a7933c6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build darwin
// +build darwin

package main

import (
Expand All @@ -7,22 +10,22 @@ import (
)

// based on https://github.com/coilhq/tigerbeetle/blob/7e12fccc4859f035cd26af29b8e9f9749a0899a3/src/storage.zig#L430
func MacFallocate(file *os.File, offset int64, length int64) error {
func fallocate(fd int, _mode uint32, offset int64, length int64) error {
store := syscall.Fstore_t{
Flags: syscall.F_ALLOCATECONTIG | syscall.F_ALLOCATEALL,
Posmode: syscall.F_PEOFPOSMODE,
Offset: 0,
Length: offset + length,
Bytesalloc: 0,
}
_, _, err := syscall.Syscall(syscall.SYS_FCNTL, file.Fd(), syscall.F_PREALLOCATE, uintptr(unsafe.Pointer(&store)))
_, _, err := syscall.Syscall(syscall.SYS_FCNTL, fd, syscall.F_PREALLOCATE, uintptr(unsafe.Pointer(&store)))
if err != 0 {
store.Flags = syscall.F_ALLOCATEALL
_, _, err = syscall.Syscall(syscall.SYS_FCNTL, file.Fd(), syscall.F_PREALLOCATE, uintptr(unsafe.Pointer(&store)))
_, _, err = syscall.Syscall(syscall.SYS_FCNTL, fd, syscall.F_PREALLOCATE, uintptr(unsafe.Pointer(&store)))
if err != 0 {
return err
}
}

return syscall.Ftruncate(int(file.Fd()), store.Length)
return syscall.Ftruncate(fd, store.Length)
}
12 changes: 12 additions & 0 deletions src/mmap-go-prototype/fallocate_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build linux
// +build linux

package main

import (
"syscall"
)

func fallocate(fd int, mode uint32, offset int64, length int64) error {
return syscall.Fallocate(fd, mode, offset, length)
}
3 changes: 3 additions & 0 deletions src/mmap-go-prototype/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/symbiont-io/detsys-testkit/src/mmap-go-prototype

go 1.16
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ func NewMmap(f string) *Mmap {

// XXX: Passing `FALLOC_FL_ZERO_RANGE` as mode would zero the file, but
// I couldn't find this constant in `syscall`...
// mode := uint32(0) // syscall.F_PREALLOCATE
mode := uint32(0) // syscall.F_PREALLOCATE

//if err := syscall.Fallocate(int(file.Fd()), mode, 0, int64(pageSize)); err != nil {
if err := MacFallocate(file, 0, int64(pageSize)); err != nil {
if err := fallocate(int(file.Fd()), mode, 0, int64(pageSize)); err != nil {
panic(err)
}

Expand Down

0 comments on commit a7933c6

Please sign in to comment.