From 4f8305de5f30909d3ae3fb99a8ee00a52975bc17 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 20 Aug 2023 02:04:19 +0200 Subject: [PATCH] - use the lump cache directly for FileData. This was the final puzzle piece to allow using memory mapped files. :) --- src/common/filesystem/filesystem.cpp | 23 ------------------- src/common/filesystem/filesystem.h | 34 ++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/src/common/filesystem/filesystem.cpp b/src/common/filesystem/filesystem.cpp index 6c67b9f455..097b6ffe5a 100644 --- a/src/common/filesystem/filesystem.cpp +++ b/src/common/filesystem/filesystem.cpp @@ -1518,29 +1518,6 @@ bool FileSystem::CreatePathlessCopy(const char *name, int id, int /*flags*/) return true; } -// FileData ----------------------------------------------------------------- - -FileData::FileData (const FileData ©) -{ - Block = copy.Block; -} - -FileData &FileData::operator = (const FileData ©) -{ - Block = copy.Block; - return *this; -} - -FileData::FileData (FResourceLump* lump) -{ - auto size = lump->LumpSize; - Block.resize(1 + size); - memcpy(Block.data(), lump->Lock(), size); - Block[size] = 0; - lump->Unlock(); -} - - //========================================================================== // // PrintLastError diff --git a/src/common/filesystem/filesystem.h b/src/common/filesystem/filesystem.h index eb3b119a6a..29b9b8b4ec 100644 --- a/src/common/filesystem/filesystem.h +++ b/src/common/filesystem/filesystem.h @@ -28,18 +28,34 @@ union LumpShortName class FileData { public: - FileData () = default; - FileData (const FileData ©); - FileData &operator= (const FileData ©); - const void *GetMem () { return Block.size() <= 1 ? NULL : (void *)Block.data(); } - size_t GetSize () { return Block.size(); } - const char* GetString () const { return (const char*)Block.data(); } - const uint8_t* GetBytes() const { return Block.data(); } + FileData() { lump = nullptr; } + const void *GetMem () { return lump->Cache; } + size_t GetSize () { return lump->LumpSize; } + const char* GetString () const { return (const char*)lump->Cache; } + const uint8_t* GetBytes() const { return (const uint8_t*)lump->Cache; } + + FileData& operator = (const FileData& copy) = delete; + + FileData(const FileData& copy) + { + lump = copy.lump; + lump->Lock(); + } + + ~FileData() + { + if (lump) lump->Unlock(); + } + private: - FileData (FResourceLump* lump); + FileData(FResourceLump* nlump) + { + lump = nlump; + if (lump) lump->Lock(); + } - std::vector Block; + FResourceLump* lump; friend class FileSystem; };