Skip to content

Commit

Permalink
for memory backed files, let FResourceFile::Read return a reference t…
Browse files Browse the repository at this point in the history
…o the backing store instead of copying the data.
  • Loading branch information
coelckers committed Dec 16, 2023
1 parent d45ca81 commit 0861461
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
16 changes: 13 additions & 3 deletions src/common/filesystem/include/fs_files.h
Expand Up @@ -40,6 +40,7 @@
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <functional>
#include <vector>
#include "fs_swap.h"
Expand Down Expand Up @@ -82,11 +83,20 @@ class FileData
public:
using value_type = uint8_t;
FileData() { memory = nullptr; length = 0; owned = true; }
FileData(const void* memory_, size_t len)
FileData(const void* memory_, size_t len, bool own = true)
{
length = len;
memory = allocate(len);
if (memory_) memcpy(memory, memory_, len);
if (own)
{
length = len;
memory = allocate(len);
if (memory_) memcpy(memory, memory_, len);
}
else
{
memory = (void*)memory_;
owned = false;
}
}
uint8_t* writable() const { return owned? (uint8_t*)memory : nullptr; }
const void* data() const { return memory; }
Expand Down
6 changes: 1 addition & 5 deletions src/common/filesystem/include/resourcefile.h
Expand Up @@ -190,11 +190,7 @@ class FResourceFile
return (entry < NumLumps) ? Entries[entry].FileName : nullptr;
}

virtual FileData Read(int entry)
{
auto fr = GetEntryReader(entry, READER_SHARED, 0);
return fr.Read(entry < NumLumps ? Entries[entry].Length : 0);
}
virtual FileData Read(int entry);

virtual FCompressedBuffer GetRawData(uint32_t entry);

Expand Down
15 changes: 15 additions & 0 deletions src/common/filesystem/source/resourcefile.cpp
Expand Up @@ -709,6 +709,21 @@ FileReader FResourceFile::GetEntryReader(uint32_t entry, int readertype, int rea
return fr;
}

FileData FResourceFile::Read(int entry)
{
if (!(Entries[entry].Flags & RESFF_COMPRESSED))
{
auto buf = Reader.GetBuffer();
// if this is backed by a memory buffer, we can just return a reference to the backing store.
if (buf != nullptr)
{
return FileData(buf + Entries[entry].Position, Entries[entry].Length, false);
}
}

auto fr = GetEntryReader(entry, READER_SHARED, 0);
return fr.Read(entry < NumLumps ? Entries[entry].Length : 0);
}



Expand Down

0 comments on commit 0861461

Please sign in to comment.