util: add anon_file.h for all memfd/temp file usage
[mesa.git] / src / intel / vulkan / anv_allocator.c
index 48d41891cfb5c6ba35e7f1d985f0dd96e613180d..a6eeed79a02e35f3a9281e2de1f9a9503fd58946 100644 (file)
 #include <unistd.h>
 #include <limits.h>
 #include <assert.h>
-#include <linux/memfd.h>
 #include <sys/mman.h>
 
 #include "anv_private.h"
 
 #include "util/hash_table.h"
 #include "util/simple_mtx.h"
+#include "util/anon_file.h"
 
 #ifdef HAVE_VALGRIND
 #define VG_NOACCESS_READ(__ptr) ({                       \
@@ -111,14 +111,6 @@ struct anv_mmap_cleanup {
 
 #define ANV_MMAP_CLEANUP_INIT ((struct anv_mmap_cleanup){0})
 
-#ifndef HAVE_MEMFD_CREATE
-static inline int
-memfd_create(const char *name, unsigned int flags)
-{
-   return syscall(SYS_memfd_create, name, flags);
-}
-#endif
-
 static inline uint32_t
 ilog2_round_up(uint32_t value)
 {
@@ -152,15 +144,12 @@ anv_state_table_init(struct anv_state_table *table,
 
    table->device = device;
 
-   table->fd = memfd_create("state table", MFD_CLOEXEC);
-   if (table->fd == -1)
-      return vk_error(VK_ERROR_INITIALIZATION_FAILED);
-
    /* Just make it 2GB up-front.  The Linux kernel won't actually back it
     * with pages until we either map and fault on one of them or we use
     * userptr and send a chunk of it off to the GPU.
     */
-   if (ftruncate(table->fd, BLOCK_POOL_MEMFD_SIZE) == -1) {
+   table->fd = os_create_anonymous_file(BLOCK_POOL_MEMFD_SIZE, "state table");
+   if (table->fd == -1) {
       result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
       goto fail_fd;
    }
@@ -446,18 +435,13 @@ anv_block_pool_init(struct anv_block_pool *pool,
    anv_bo_init(pool->bo, 0, 0);
 
    if (!(pool->bo_flags & EXEC_OBJECT_PINNED)) {
-      pool->fd = memfd_create("block pool", MFD_CLOEXEC);
-      if (pool->fd == -1)
-         return vk_error(VK_ERROR_INITIALIZATION_FAILED);
-
       /* Just make it 2GB up-front.  The Linux kernel won't actually back it
        * with pages until we either map and fault on one of them or we use
        * userptr and send a chunk of it off to the GPU.
        */
-      if (ftruncate(pool->fd, BLOCK_POOL_MEMFD_SIZE) == -1) {
-         result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
-         goto fail_fd;
-      }
+      pool->fd = os_create_anonymous_file(BLOCK_POOL_MEMFD_SIZE, "block pool");
+      if (pool->fd == -1)
+         return vk_error(VK_ERROR_INITIALIZATION_FAILED);
    } else {
       pool->fd = -1;
    }