Module: Mesa Branch: main Commit: 5ee3212078e12f28ada17c2db033363029443f4a URL: http://cgit.freedesktop.org/mesa/mesa/commit/?id=5ee3212078e12f28ada17c2db033363029443f4a
Author: Yonggang Luo <[email protected]> Date: Tue Mar 29 00:41:50 2022 +0800 util: Add os_create_anonymous_file support on win32 Signed-off-by: Yonggang Luo <[email protected]> Reviewed-by: Jesse Natalie <[email protected]> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18017> --- 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 e2d9265194d..983ff75ac02 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 <unistd.h> #include <fcntl.h> #include <errno.h> @@ -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 <windows.h> +#include <io.h> + +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 790537b7aee..bdd74205d90 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 <sys/types.h> +#include <stdint.h> -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
