From 5fede412058aa26ace9e5ac62aae19dc0e2586ad Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 23 Sep 2023 10:55:15 +0530 Subject: [PATCH] Always use fallocate() on Linux for SHM creation --- tools/utils/shm/fallocate_other.go | 4 ++-- tools/utils/shm/shm.go | 9 ++++----- tools/utils/shm/shm_test.go | 3 --- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/tools/utils/shm/fallocate_other.go b/tools/utils/shm/fallocate_other.go index 4b62b9c34..3aa7ee532 100644 --- a/tools/utils/shm/fallocate_other.go +++ b/tools/utils/shm/fallocate_other.go @@ -5,12 +5,12 @@ package shm import ( + "errors" "fmt" - "golang.org/x/sys/unix" ) var _ = fmt.Print func Fallocate_simple(fd int, size int64) (err error) { - return unix.ENOSYS + return errors.ErrUnsupported } diff --git a/tools/utils/shm/shm.go b/tools/utils/shm/shm.go index 36f0931e4..beadb832a 100644 --- a/tools/utils/shm/shm.go +++ b/tools/utils/shm/shm.go @@ -92,16 +92,15 @@ func CreateTemp(pattern string, size uint64) (MMap, error) { return create_temp(pattern, size) } -var force_use_of_fallocate bool = false - func truncate_or_unlink(ans *os.File, size uint64) (err error) { fd := int(ans.Fd()) - if err = Fallocate_simple(fd, int64(size)); err != nil { - if force_use_of_fallocate { + sz := int64(size) + if err = Fallocate_simple(fd, sz); err != nil { + if !errors.Is(err, errors.ErrUnsupported) { return err } for { - err = unix.Ftruncate(fd, int64(size)) + err = unix.Ftruncate(fd, sz) if !errors.Is(err, unix.EINTR) { break } diff --git a/tools/utils/shm/shm_test.go b/tools/utils/shm/shm_test.go index ade640dcb..1cd2b7de8 100644 --- a/tools/utils/shm/shm_test.go +++ b/tools/utils/shm/shm_test.go @@ -9,15 +9,12 @@ import ( "io/fs" "os" "reflect" - "runtime" "testing" ) var _ = fmt.Print func TestSHM(t *testing.T) { - force_use_of_fallocate = runtime.GOOS == "linux" - defer func() { force_use_of_fallocate = false }() data := make([]byte, 13347) _, _ = rand.Read(data) mm, err := CreateTemp("test-kitty-shm-", uint64(len(data)))