Adds Load with unique_ptr to VFS

Also replaces some usages of Load with shared_ptr by unique_ptr.
This commit is contained in:
Vladislav Belov
2026-09-10 16:39:29 +02:00
parent aecd2b4245
commit 58e2c59466
11 changed files with 51 additions and 14 deletions
+3 -3
View File
@@ -202,7 +202,7 @@ bool CFont::AddFontFromPath(const OsPath& fontPath)
return false;
}
std::shared_ptr<u8> fontData;
std::unique_ptr<u8[], AlignedDeleter> fontData;
size_t fontDataSize;
if (g_VFS->LoadFile(fontPath, fontData, fontDataSize) != 0)
{
@@ -223,7 +223,7 @@ bool CFont::AddFontFromPath(const OsPath& fontPath)
}
// Keep the font data alive.
m_FontsData.push_back(fontData);
m_FontsData.push_back(std::move(fontData));
// Set the font size.
if (FT_Error error{FT_Set_Char_Size(face, 0, FloatToF26Dot6(m_FontSize), 0 , 0)})
@@ -233,7 +233,7 @@ bool CFont::AddFontFromPath(const OsPath& fontPath)
}
// Get the height of the font.
if(m_Faces.empty())
if (m_Faces.empty())
m_Height = FPosF26Dot6ToFloat(face->size->metrics.height);
// Add the fallback font to the list.
+3 -2
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@@ -19,6 +19,7 @@
#define INCLUDED_FONT
#include "graphics/Texture.h"
#include "lib/allocators/shared_ptr.h"
#include "lib/code_annotation.h"
#include "lib/os_path.h"
#include "lib/types.h"
@@ -190,7 +191,7 @@ private:
std::reference_wrapper<const std::array<float, 256>> m_GammaCorrectionLUT;
FT_Library m_FreeType;
std::vector<std::shared_ptr<u8>> m_FontsData;
std::vector<std::unique_ptr<u8[], AlignedDeleter>> m_FontsData;
std::vector<UniqueFTFace> m_Faces;
UniqueFTStroker m_Stroker{nullptr, &ftStrokerDeleter};
+1 -1
View File
@@ -82,7 +82,7 @@ class TestMeshManager : public CxxTest::TestSuite
void copyFile(const VfsPath& src, const VfsPath& dst)
{
// Copy a file into the mod directory, so we can work on it:
std::shared_ptr<u8> data; size_t size = 0;
std::unique_ptr<u8[], AlignedDeleter> data; size_t size = 0;
TS_ASSERT_OK(g_VFS->LoadFile(src, data, size));
TS_ASSERT_OK(g_VFS->CreateFile(dst, {data.get(), size}));
}
+23
View File
@@ -180,6 +180,29 @@ public:
return INFO::OK;
}
Status LoadFile(const VfsPath& pathname, std::unique_ptr<u8[], AlignedDeleter>& fileContents, size_t& size) final
{
std::lock_guard<std::mutex> lock(vfs_mutex);
VfsDirectory* directory; VfsFile* file;
// per 2010-05-01 meeting, this shouldn't raise 'scary error
// dialogs', which might fail to display the culprit pathname
// instead, callers should log the error, including pathname.
RETURN_STATUS_IF_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, &file));
size = file->Size();
fileContents.reset(reinterpret_cast<u8*>(rtl_AllocateAligned(size, maxSectorSize)));
if (!fileContents)
WARN_RETURN(ERR::NO_MEM);
RETURN_STATUS_IF_ERR(file->Loader()->Load(file->Name(), {fileContents.get(), file->Size()}));
stats_io_user_request(size);
m_trace->NotifyLoad(pathname, size);
return INFO::OK;
}
std::wstring TextRepresentation() const final
{
std::lock_guard<std::mutex> lock(vfs_mutex);
+11
View File
@@ -28,6 +28,7 @@
#ifndef INCLUDED_VFS
#define INCLUDED_VFS
#include "lib/allocators/shared_ptr.h"
#include "lib/file/file_system.h" // CFileInfo
#include "lib/file/vfs/vfs_path.h"
#include "lib/os_path.h"
@@ -160,6 +161,16 @@ struct IVFS
**/
virtual Status LoadFile(const VfsPath& pathname, std::shared_ptr<u8>& fileContents, size_t& size) = 0;
/**
* Read an entire file into memory.
*
* @param pathname
* @param fileContents receives a unique pointer to the contents.
* @param size receives the size [bytes] of the file contents.
* @return Status.
**/
virtual Status LoadFile(const VfsPath& pathname, std::unique_ptr<u8[], AlignedDeleter>& fileContents, size_t& size) = 0;
/**
* @return a string representation of all files and directories.
**/
+1 -1
View File
@@ -629,7 +629,7 @@ void CConsole::LoadHistory()
if (!VfsFileExists(m_HistoryFile))
return;
std::shared_ptr<u8> buf; size_t buflen;
std::unique_ptr<u8[], AlignedDeleter> buf; size_t buflen;
if (g_VFS->LoadFile(m_HistoryFile, buf, buflen) < 0)
return;
+1 -1
View File
@@ -311,7 +311,7 @@ bool CConfigDB::Reload(EConfigNamespace ns)
std::lock_guard<std::recursive_mutex> s(m_Mutex);
std::shared_ptr<u8> buffer;
std::unique_ptr<u8[], AlignedDeleter> buffer;
size_t buflen;
{
// Handle missing files quietly
+3 -2
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@@ -30,6 +30,7 @@
#ifndef INCLUDED_FILEPACKER
#define INCLUDED_FILEPACKER
#include "lib/allocators/shared_ptr.h"
#include "lib/file/io/write_buffer.h"
#include "lib/file/vfs/vfs_path.h"
#include "lib/types.h"
@@ -144,7 +145,7 @@ public:
private:
// the data read from file and used during unpack operations
std::shared_ptr<u8> m_buf;
std::unique_ptr<u8[], AlignedDeleter> m_buf;
size_t m_bufSize;
size_t m_unpackPos; /// current unpack position in stream
+3 -2
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@@ -18,6 +18,7 @@
#ifndef INCLUDED_PS_FILESYSTEM
#define INCLUDED_PS_FILESYSTEM
#include "lib/allocators/shared_ptr.h"
#include "lib/file/vfs/vfs.h"
#include "lib/file/vfs/vfs_path.h"
#include "lib/status.h"
@@ -99,7 +100,7 @@ public:
CStr8 DecodeUTF8() const;
private:
std::shared_ptr<u8> m_Buffer;
std::unique_ptr<u8[], AlignedDeleter> m_Buffer;
size_t m_BufferSize;
};
+1 -1
View File
@@ -198,7 +198,7 @@ void CVideoMode::CCursor::SetCursor(const CStrW& name)
int hotspotX = 0, hotspotY = 0;
{
const VfsPath pathHotspotName = pathBaseName.ChangeExtension(L".txt");
std::shared_ptr<u8> buffer;
std::unique_ptr<u8[], AlignedDeleter> buffer;
size_t size;
if (g_VFS->LoadFile(pathHotspotName, buffer, size) != INFO::OK)
{
+1 -1
View File
@@ -39,7 +39,7 @@ class TestSimulation2 : public CxxTest::TestSuite
{
void copyFile(const VfsPath& src, const VfsPath& dst)
{
std::shared_ptr<u8> data;
std::unique_ptr<u8[], AlignedDeleter> data;
size_t size = 0;
TS_ASSERT_OK(g_VFS->LoadFile(src, data, size));
TS_ASSERT_OK(g_VFS->CreateFile(dst, {data.get(), size}));