- GHOST/Wayland: Provide fallback for systems without posix_fallocate()

Index: intern/ghost/intern/GHOST_SystemWayland.cc
--- intern/ghost/intern/GHOST_SystemWayland.cc.orig
+++ intern/ghost/intern/GHOST_SystemWayland.cc
@@ -2610,6 +2610,36 @@ static size_t ghost_wl_shm_format_as_size(enum wl_shm_
   }
 }
 
+static int ghost_wl_shm_allocate(int fd, int32_t size)
+{
+#ifdef HAVE_POSIX_FALLOCATE
+  return posix_fallocate(fd, 0, size);
+#else
+  if (ftruncate(fd, size) != 0) {
+    return errno;
+  }
+
+  /* Touch one byte per page so allocation failures surface as errors
+   * here instead of SIGBUS later. */
+  const long page = sysconf(_SC_PAGESIZE);
+  if (page <= 0) {
+    return EINVAL;
+  }
+
+  const char zero = 0;
+  for (off_t off = 0; off < size; off += page) {
+    const ssize_t ret = pwrite(fd, &zero, 1, off);
+    if (ret == -1) {
+      return errno;
+    }
+    if (ret != 1) {
+      return EIO;
+    }
+  }
+  return 0;
+#endif
+}
+
 /**
  * Return a #wl_buffer, ready to have it's data filled in or nullptr in case of failure.
  * The caller is responsible for calling `unmap(buffer_data, buffer_size)`.
@@ -2628,7 +2658,7 @@ static wl_buffer *ghost_wl_buffer_create_for_image(wl_
   if (fd >= 0) {
     const int32_t buffer_stride = size_xy[0] * ghost_wl_shm_format_as_size(format);
     const int32_t buffer_size = buffer_stride * size_xy[1];
-    if (posix_fallocate(fd, 0, buffer_size) == 0) {
+    if (ghost_wl_shm_allocate(fd, buffer_size) == 0) {
       void *buffer_data = mmap(nullptr, buffer_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
       if (buffer_data != MAP_FAILED) {
         wl_shm_pool *pool = wl_shm_create_pool(shm, fd, buffer_size);
