From 6619bf33b0adf1b3212d66211cce492eb3e55b08 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 23 Sep 2023 10:39:10 +0530 Subject: [PATCH] Using fcntl() based fallocate on darwin doesnt work with file descriptors returned by shm_open --- kitty/shm.py | 5 ++++- tools/utils/shm/fallocate_darwin.go | 29 ----------------------------- tools/utils/shm/fallocate_other.go | 2 +- tools/utils/shm/shm_test.go | 2 +- 4 files changed, 6 insertions(+), 32 deletions(-) delete mode 100644 tools/utils/shm/fallocate_darwin.go diff --git a/kitty/shm.py b/kitty/shm.py index f82e62ff0..3ed23fed7 100644 --- a/kitty/shm.py +++ b/kitty/shm.py @@ -82,7 +82,10 @@ class SharedMemory: self._name = name try: if flags & os.O_CREAT and size: - os.posix_fallocate(self._fd, 0, size) + if hasattr(os, 'posix_fallocate'): + os.posix_fallocate(self._fd, 0, size) + else: + os.ftruncate(self._fd, size) self.stats = os.fstat(self._fd) size = self.stats.st_size self._mmap = mmap.mmap(self._fd, size, access=mmap.ACCESS_READ if readonly else mmap.ACCESS_WRITE) diff --git a/tools/utils/shm/fallocate_darwin.go b/tools/utils/shm/fallocate_darwin.go deleted file mode 100644 index eeb5c8a85..000000000 --- a/tools/utils/shm/fallocate_darwin.go +++ /dev/null @@ -1,29 +0,0 @@ -// License: GPLv3 Copyright: 2023, Kovid Goyal, - -package shm - -import ( - "errors" - "fmt" - "syscall" - "unsafe" - - "golang.org/x/sys/unix" -) - -var _ = fmt.Print - -func Fallocate_simple(fd int, size int64) (err error) { - store := &syscall.Fstore_t{ - Flags: syscall.F_ALLOCATEALL, - Posmode: syscall.F_PEOFPOSMODE, - Offset: 0, - Length: size, - } - - for { - if _, _, err = syscall.Syscall(syscall.SYS_FCNTL, uintptr(fd), syscall.F_PREALLOCATE, uintptr(unsafe.Pointer(store))); !errors.Is(err, unix.EINTR) { - return err - } - } -} diff --git a/tools/utils/shm/fallocate_other.go b/tools/utils/shm/fallocate_other.go index 4481a592a..4b62b9c34 100644 --- a/tools/utils/shm/fallocate_other.go +++ b/tools/utils/shm/fallocate_other.go @@ -1,6 +1,6 @@ // License: GPLv3 Copyright: 2023, Kovid Goyal, -//go:build !darwin && !linux +//go:build !linux package shm diff --git a/tools/utils/shm/shm_test.go b/tools/utils/shm/shm_test.go index 5115bdb74..ade640dcb 100644 --- a/tools/utils/shm/shm_test.go +++ b/tools/utils/shm/shm_test.go @@ -16,7 +16,7 @@ import ( var _ = fmt.Print func TestSHM(t *testing.T) { - force_use_of_fallocate = runtime.GOOS == "darwin" || runtime.GOOS == "linux" + force_use_of_fallocate = runtime.GOOS == "linux" defer func() { force_use_of_fallocate = false }() data := make([]byte, 13347) _, _ = rand.Read(data)