mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-20 15:35:03 +02:00
With truncate() the OS might not actually allocate the space leading to a SIGBUS if /dev/shm runs out of space when actually using the mmap. By using fallocate we ensure that once the SHM mmap is created it wont fail
30 lines
568 B
Go
30 lines
568 B
Go
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
|
|
|
|
package shm
|
|
|
|
import (
|
|
"fmt"
|
|
"syscall"
|
|
"unsafe"
|
|
)
|
|
|
|
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: int64(size),
|
|
}
|
|
|
|
for {
|
|
if _, _, err = syscall.Syscall(syscall.SYS_FCNTL, uintptr(out.f.Fd()), syscall.F_PREALLOCATE, uintptr(unsafe.Pointer(store))); !errors.Is(err, unix.EINTR) {
|
|
if err != 0 {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
}
|