Using fcntl() based fallocate on darwin doesnt work with file descriptors returned by shm_open

This commit is contained in:
Kovid Goyal
2023-09-23 10:39:10 +05:30
parent 627c80125b
commit 6619bf33b0
4 changed files with 6 additions and 32 deletions

View File

@@ -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)

View File

@@ -1,29 +0,0 @@
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
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
}
}
}

View File

@@ -1,6 +1,6 @@
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
//go:build !darwin && !linux
//go:build !linux
package shm

View File

@@ -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)