Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/Vfs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,18 @@ namespace zenkit {
w->write(catalog.data(), catalog.size());
}

#ifndef _ZK_WITH_MMAP
/// Maximum number of bytes to request from a stream in a single read (256 MB).
static constexpr std::streamsize VFS_READ_CHUNK_SIZE = 256 * 1024 * 1024;

/// Reads up to size bytes from the stream into data.
/// A single read of 2 GiB or more fails outright on some platforms, so we split the read into chunks.
static void vfs_read_chunked(std::istream& stream, std::byte* data, std::streamsize size) {
for (std::streamsize off = 0; off < size && stream; off += stream.gcount())
stream.read((char*) data + off, std::min(VFS_READ_CHUNK_SIZE, size - off));
}
#endif // _ZK_WITH_MMAP

void Vfs::mount_disk(std::filesystem::path const& host, VfsOverwriteBehavior overwrite) {
#ifdef _ZK_WITH_MMAP
auto& mem = _m_data_mapped.emplace_back(host);
Expand All @@ -413,7 +425,7 @@ namespace zenkit {
stream.seekg(0);

auto& data = _m_data.emplace_back(new std::byte[(size_t) size]);
stream.read((char*) data.get(), size);
vfs_read_chunked(stream, data.get(), size);
this->mount_disk(data.get(), (size_t) size, overwrite);
#endif
}
Expand Down Expand Up @@ -564,7 +576,7 @@ namespace zenkit {
stream.seekg(0);

auto& data = _m_data.emplace_back(new std::byte[(size_t) size]);
stream.read((char*) data.get(), size);
vfs_read_chunked(stream, data.get(), size);

parent->create(VfsNode::file(path.filename().string(),
VfsFileDescriptor {data.get(), static_cast<size_t>(size), false},
Expand Down
2 changes: 1 addition & 1 deletion support/BuildSupport.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function(bs_check_win32_mmap _MMAP_AVAIL)
check_c_source_compiles("
#include <windows.h>
int main(int argc, char** argv) {
HANDLE hFile = CreateFile(L\"test.txt\", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
HANDLE hFile = CreateFileW(L\"test.txt\", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
DWORD dwFileSize = GetFileSize(hFile, NULL);
HANDLE hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, dwFileSize, NULL);
LPVOID lpFileBase = MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
Expand Down