only call CreateFileMapping if size > 0

This commit is contained in:
jacob 2024-04-03 22:59:18 -05:00
parent 100fdd264d
commit 7eb63bc80f

View File

@ -422,24 +422,25 @@ u64 sys_file_size(struct sys_file file)
struct sys_file_map sys_file_map_open_read(struct sys_file file)
{
HANDLE map_handle = CreateFileMappingW(
(HANDLE)file.handle,
NULL,
PAGE_READONLY,
0,
0,
NULL
);
if (!map_handle) {
ASSERT(false);
return (struct sys_file_map) { 0 };
}
u64 size = sys_file_size(file);
u8 *base_ptr = NULL;
HANDLE map_handle = 0;
if (size > 0) {
map_handle = CreateFileMappingW(
(HANDLE)file.handle,
NULL,
PAGE_READONLY,
0,
0,
NULL
);
if (!map_handle) {
ASSERT(false);
return (struct sys_file_map) { 0 };
}
base_ptr = MapViewOfFile(
map_handle,
FILE_MAP_READ,
@ -456,7 +457,6 @@ struct sys_file_map sys_file_map_open_read(struct sys_file file)
}
} else {
/* File is empty */
CloseHandle(map_handle);
return (struct sys_file_map) { 0 };
}
@ -468,8 +468,12 @@ struct sys_file_map sys_file_map_open_read(struct sys_file file)
void sys_file_map_close(struct sys_file_map map)
{
UnmapViewOfFile(map.mapped_memory.data);
CloseHandle((HANDLE)map.handle);
if (map.mapped_memory.data) {
UnmapViewOfFile(map.mapped_memory.data);
}
if (map.handle) {
CloseHandle((HANDLE)map.handle);
}
}
struct buffer sys_file_map_data(struct sys_file_map map)