0
0
Fork 0
mirror of https://github.com/ZDoom/gzdoom.git synced 2025-03-22 10:51:10 +00:00

for memory backed files, let FResourceFile::Read return a reference to the backing store instead of copying the data.

This commit is contained in:
Christoph Oelckers 2023-12-16 10:54:31 +01:00
parent d45ca8151f
commit 08614613be
3 changed files with 29 additions and 8 deletions
src/common/filesystem

View file

@ -40,6 +40,7 @@
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <functional>
#include <vector>
#include "fs_swap.h"
@ -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; }

View file

@ -190,11 +190,7 @@ public:
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);

View file

@ -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);
}