From 5ee3212078e12f28ada17c2db033363029443f4a Mon Sep 17 00:00:00 2001 From: Yonggang Luo Date: Tue, 29 Mar 2022 00:41:50 +0800 Subject: [PATCH] util: Add os_create_anonymous_file support on win32 Signed-off-by: Yonggang Luo Reviewed-by: Jesse Natalie Part-of: --- src/util/anon_file.c | 20 +++++++++++++++++--- src/util/anon_file.h | 5 +++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/util/anon_file.c b/src/util/anon_file.c index e2d9265194d2f..983ff75ac02ab 100644 --- a/src/util/anon_file.c +++ b/src/util/anon_file.c @@ -27,9 +27,10 @@ * Based on weston shared/os-compatibility.c */ -#ifndef _WIN32 #include "anon_file.h" +#ifndef _WIN32 + #include #include #include @@ -112,7 +113,7 @@ create_tmpfile_cloexec(char *tmpname) * SCM_RIGHTS methods. */ int -os_create_anonymous_file(off_t size, const char *debug_name) +os_create_anonymous_file(int64_t size, const char *debug_name) { int fd, ret; #if defined(HAVE_MEMFD_CREATE) @@ -155,7 +156,7 @@ os_create_anonymous_file(off_t size, const char *debug_name) if (fd < 0) return -1; - ret = ftruncate(fd, size); + ret = ftruncate(fd, (off_t)size); if (ret < 0) { close(fd); return -1; @@ -163,4 +164,17 @@ os_create_anonymous_file(off_t size, const char *debug_name) return fd; } +#else + +#include +#include + +int +os_create_anonymous_file(int64_t size, const char *debug_name) +{ + (void)debug_name; + HANDLE h = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, + PAGE_READWRITE, (size >> 32), size & 0xFFFFFFFF, NULL); + return _open_osfhandle((intptr_t)h, 0); +} #endif diff --git a/src/util/anon_file.h b/src/util/anon_file.h index 790537b7aee6f..bdd74205d90e1 100644 --- a/src/util/anon_file.h +++ b/src/util/anon_file.h @@ -26,8 +26,9 @@ #ifndef _ANON_FILE_H_ #define _ANON_FILE_H_ -#include +#include -int os_create_anonymous_file(off_t size, const char *debug_name); +/* On win32, off_t is only 32 bit, so always using 64 bit size */ +int os_create_anonymous_file(int64_t size, const char *debug_name); #endif