util: Add os_create_anonymous_file support on win32

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
Reviewed-by: Jesse Natalie <jenatali@microsoft.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18017>
This commit is contained in:
Yonggang Luo 2022-03-29 00:41:50 +08:00 committed by Marge Bot
parent 24b9ad7cd5
commit 5ee3212078
2 changed files with 20 additions and 5 deletions

View File

@ -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

View File

@ -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