Browse Source

[chore] Fix windows building

mmap-fixews
lizzie 2 weeks ago
committed by Caio Oliveira
parent
commit
bfc6c48365
  1. 41
      src/common/fs/file.cpp
  2. 10
      src/common/fs/file.h

41
src/common/fs/file.cpp

@ -249,9 +249,9 @@ FileType IOFile::GetType() const {
return file_type;
}
#ifdef __unix__
int PlatformMapReadOnly(IOFile& io, const char* path) {
io.mmap_fd = open(path., O_RDONLY);
#if defined(__unix__)
static int PlatformMapReadOnly(IOFile& io, const char* path) {
io.mmap_fd = open(path, O_RDONLY);
if (io.mmap_fd > 0) {
struct stat st;
fstat(io.mmap_fd, &st);
@ -294,28 +294,30 @@ int PlatformMapReadOnly(IOFile& io, const char* path) {
close(io.mmap_fd);
io.mmap_fd = -1;
} else {
posix_madvise(mmap_base, io.mmap_size, POSIX_MADV_WILLNEED);
posix_madvise(io.mmap_base, io.mmap_size, POSIX_MADV_WILLNEED);
}
}
return io.mmap_fd;
}
void PlatformUnmap(IOFile& io) {
static void PlatformUnmap(IOFile& io) {
if (io.mmap_fd != -1) {
munmap(io.mmap_base, io.mmap_size);
close(io.mmap_fd);
io.mmap_fd = -1;
}
}
#elif defined(__APPLE__)
// NO IMPLEMENTATION YET
#else
int PlatformMapReadOnly(IOFile& io, const char* path) {
io.file_handle = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, nullptr);
static int PlatformMapReadOnly(IOFile& io, const wchar_t* path) {
io.file_handle = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, nullptr);
if (HANDLE(io.file_handle) != INVALID_HANDLE_VALUE) {
io.mapping_handle = CreateFileMappingW(file_handle, nullptr, PAGE_READONLY, 0, 0, nullptr);
io.mapping_handle = CreateFileMappingW(HANDLE(io.file_handle), nullptr, PAGE_READONLY, 0, 0, nullptr);
if (io.mapping_handle) {
io.mmap_base = (char const*)MapViewOfFile(HANDLE(io.mapping_handle), FILE_MAP_READ, 0, 0, 0);
io.mmap_base = (u8*)MapViewOfFile(HANDLE(io.mapping_handle), FILE_MAP_READ, 0, 0, 0);
if (io.mmap_base) {
_LARGE_INTEGER pvalue;
GetFileSizeEx(file_handle, &pvalue);
GetFileSizeEx(io.file_handle, &pvalue);
io.mmap_size = uint32_t(pvalue.QuadPart);
} else {
CloseHandle(io.mapping_handle);
@ -329,7 +331,7 @@ int PlatformMapReadOnly(IOFile& io, const char* path) {
}
return 0;
}
void PlatformUnmap(IOFile& io) {
static void PlatformUnmap(IOFile& io) {
if(io.mapping_handle) {
if(io.mmap_base)
UnmapViewOfFile(HANDLE(io.mmap_base));
@ -347,10 +349,19 @@ void IOFile::Open(const fs::path& path, FileAccessMode mode, FileType type, File
file_type = type;
errno = 0;
#ifdef _WIN32
if (flag != FileShareFlag::ShareNone) {
file = _wfsopen(path.c_str(), AccessModeToWStr(mode, type), ToWindowsFileShareFlag(flag));
} else {
_wfopen_s(&file, path.c_str(), AccessModeToWStr(mode, type));
// TODO: this probably can use better logic but oh well I'm not a windowser
file_handle = nullptr;
if (type == FileType::BinaryFile && mode == FileAccessMode::Read) {
if (PlatformMapReadOnly(*this, path.c_str()) == -1) {
LOG_ERROR(Common_Filesystem, "Error mmap'ing file"); //: {}", path.c_str());
}
}
if (file_handle == nullptr) {
if (flag != FileShareFlag::ShareNone) {
file = _wfsopen(path.c_str(), AccessModeToWStr(mode, type), ToWindowsFileShareFlag(flag));
} else {
_wfopen_s(&file, path.c_str(), AccessModeToWStr(mode, type));
}
}
#elif ANDROID
if (Android::IsContentUri(path)) {

10
src/common/fs/file.h

@ -433,22 +433,24 @@ public:
*/
[[nodiscard]] s64 Tell() const;
private:
#ifdef _WIN32
inline bool IsMappedFile() const noexcept { return file_handle != nullptr; }
#else // POSIX
inline bool IsMappedFile() const noexcept { return mmap_fd != -1; }
#endif
std::filesystem::path file_path;
FileAccessMode file_access_mode{};
FileType file_type{};
std::FILE* file = nullptr;
// Any decent system should have mmap() for files
// Systems with artifical mmap() limitations should simply change the logic within file.cpp
// and reduce the threshold for which the mmap() is set to
#ifdef _WIN32
void *mapping_handle = nullptr;
void *file_handle = nullptr;
bool IsMappedFile() { return file_handle != nullptr; }
#else // POSIX
int mmap_fd = -1;
bool IsMappedFile() { return mmap_fd != -1; }
#endif
u8* mmap_base = nullptr;
size_t mmap_size = 0;

Loading…
Cancel
Save