From db830643d21e7c33ec7deb875d45a108290c7100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 15 Feb 2020 19:46:00 +0100 Subject: [PATCH] shm: prefer posix_fallocate over ftruncate --- shm.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/shm.c b/shm.c index 3f3ccf21..33095e74 100644 --- a/shm.c +++ b/shm.c @@ -6,6 +6,7 @@ #include #include #include +#include #include @@ -123,9 +124,19 @@ shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie) size = stride * height; LOG_DBG("cookie=%lx: allocating new buffer: %zu KB", cookie, size / 1024); - if (ftruncate(pool_fd, size) == -1) { - LOG_ERRNO("failed to truncate SHM pool"); - goto err; + int err = posix_fallocate(pool_fd, 0, size); + if (err != 0) { + static bool failure_logged = false; + + if (!failure_logged) { + failure_logged = true; + LOG_ERRNO_P("failed to fallocate", err); + } + + if (ftruncate(pool_fd, size) == -1) { + LOG_ERRNO("failed to truncate SHM pool"); + goto err; + } } mmapped = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, pool_fd, 0);