Skip to content

Commit

Permalink
FileSystem: Swap vector for DynamicHeapArray
Browse files Browse the repository at this point in the history
No point zeroing it out.
  • Loading branch information
stenzek committed Aug 25, 2024
1 parent a2aa040 commit 707453b
Show file tree
Hide file tree
Showing 32 changed files with 112 additions and 84 deletions.
10 changes: 5 additions & 5 deletions src/common/file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1401,9 +1401,9 @@ s64 FileSystem::GetPathFileSize(const char* Path)
return sd.Size;
}

std::optional<std::vector<u8>> FileSystem::ReadBinaryFile(const char* filename, Error* error)
std::optional<DynamicHeapArray<u8>> FileSystem::ReadBinaryFile(const char* filename, Error* error)
{
std::optional<std::vector<u8>> ret;
std::optional<DynamicHeapArray<u8>> ret;

ManagedCFilePtr fp = OpenManagedCFile(filename, "rb", error);
if (!fp)
Expand All @@ -1413,9 +1413,9 @@ std::optional<std::vector<u8>> FileSystem::ReadBinaryFile(const char* filename,
return ret;
}

std::optional<std::vector<u8>> FileSystem::ReadBinaryFile(std::FILE* fp, Error* error)
std::optional<DynamicHeapArray<u8>> FileSystem::ReadBinaryFile(std::FILE* fp, Error* error)
{
std::optional<std::vector<u8>> ret;
std::optional<DynamicHeapArray<u8>> ret;

if (FSeek64(fp, 0, SEEK_END) != 0) [[unlikely]]
{
Expand Down Expand Up @@ -1445,7 +1445,7 @@ std::optional<std::vector<u8>> FileSystem::ReadBinaryFile(std::FILE* fp, Error*
return ret;
}

ret = std::vector<u8>(static_cast<size_t>(size));
ret = DynamicHeapArray<u8>(static_cast<size_t>(size));
if (size > 0 && std::fread(ret->data(), 1u, static_cast<size_t>(size), fp) != static_cast<size_t>(size)) [[unlikely]]
{
Error::SetErrno(error, "fread() failed: ", errno);
Expand Down
5 changes: 3 additions & 2 deletions src/common/file_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#pragma once

#include "heap_array.h"
#include "types.h"

#include <cstdio>
Expand Down Expand Up @@ -171,8 +172,8 @@ class POSIXLock
};
#endif

std::optional<std::vector<u8>> ReadBinaryFile(const char* filename, Error* error = nullptr);
std::optional<std::vector<u8>> ReadBinaryFile(std::FILE* fp, Error* error = nullptr);
std::optional<DynamicHeapArray<u8>> ReadBinaryFile(const char* filename, Error* error = nullptr);
std::optional<DynamicHeapArray<u8>> ReadBinaryFile(std::FILE* fp, Error* error = nullptr);
std::optional<std::string> ReadFileToString(const char* filename, Error* error = nullptr);
std::optional<std::string> ReadFileToString(std::FILE* fp, Error* error = nullptr);
bool WriteBinaryFile(const char* filename, const void* data, size_t data_length, Error* error = nullptr);
Expand Down
24 changes: 13 additions & 11 deletions src/common/heap_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#pragma once

#include "common/assert.h"

#include <algorithm>
#include <cassert>
#include <cstdlib>
Expand Down Expand Up @@ -117,18 +115,20 @@ class FixedHeapArray
{
#ifdef _MSC_VER
m_data = static_cast<T*>(_aligned_malloc(SIZE * sizeof(T), ALIGNMENT));
if (!m_data)
Panic("Memory allocation failed.");
assert(m_data);
if (!m_data) [[unlikely]]
std::abort();
#else
if (posix_memalign(reinterpret_cast<void**>(&m_data), ALIGNMENT, SIZE * sizeof(T)) != 0)
Panic("Memory allocation failed.");
if (posix_memalign(reinterpret_cast<void**>(&m_data), ALIGNMENT, SIZE * sizeof(T)) != 0) [[unlikely]]
std::abort();
#endif
}
else
{
m_data = static_cast<T*>(std::malloc(SIZE * sizeof(T)));
if (!m_data)
Panic("Memory allocation failed.");
assert(m_data);
if (!m_data) [[unlikely]]
std::abort();
}
}
void deallocate()
Expand Down Expand Up @@ -377,11 +377,12 @@ class DynamicHeapArray
{
#ifdef _MSC_VER
m_data = static_cast<T*>(_aligned_realloc(prev_ptr, size * sizeof(T), alignment));
assert(m_data);
if (!m_data) [[unlikely]]
Panic("Memory allocation failed.");
std::abort();
#else
if (posix_memalign(reinterpret_cast<void**>(&m_data), alignment, size * sizeof(T)) != 0) [[unlikely]]
Panic("Memory allocation failed.");
std::abort();

if (prev_ptr)
{
Expand All @@ -393,8 +394,9 @@ class DynamicHeapArray
else
{
m_data = static_cast<T*>(std::realloc(prev_ptr, size * sizeof(T)));
assert(m_data);
if (!m_data) [[unlikely]]
Panic("Memory allocation failed.");
std::abort();
}

m_size = size;
Expand Down
2 changes: 1 addition & 1 deletion src/core/bios.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ std::optional<BIOS::Image> BIOS::LoadImageFromFile(const char* filename, Error*
}

// We want to hash the whole file. That means reading the whole thing in, if it's a larger BIOS (PS2).
std::optional<std::vector<u8>> data = FileSystem::ReadBinaryFile(fp.get(), error);
std::optional<DynamicHeapArray<u8>> data = FileSystem::ReadBinaryFile(fp.get(), error);
if (!data.has_value() || data->size() < BIOS_SIZE)
return ret;

Expand Down
3 changes: 2 additions & 1 deletion src/core/bios.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "types.h"

#include "common/heap_array.h"
#include "common/small_string.h"

#include <array>
Expand Down Expand Up @@ -52,7 +53,7 @@ struct Image
{
const ImageInfo* info;
ImageInfo::Hash hash;
std::vector<u8> data;
DynamicHeapArray<u8> data;
};

#pragma pack(push, 1)
Expand Down
8 changes: 4 additions & 4 deletions src/core/bus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1034,15 +1034,15 @@ bool Bus::SideloadEXE(const std::string& path, Error* error)
if (const std::string libps_path = Path::BuildRelativePath(path, "libps.exe");
FileSystem::FileExists(libps_path.c_str()))
{
const std::optional<std::vector<u8>> exe_data = FileSystem::ReadBinaryFile(libps_path.c_str(), error);
okay = (exe_data.has_value() && InjectExecutable(exe_data.value(), false, error));
const std::optional<DynamicHeapArray<u8>> exe_data = FileSystem::ReadBinaryFile(libps_path.c_str(), error);
okay = (exe_data.has_value() && InjectExecutable(exe_data->cspan(), false, error));
if (!okay)
Error::AddPrefix(error, "Failed to load libps.exe: ");
}
if (okay)
{
const std::optional<std::vector<u8>> exe_data = FileSystem::ReadBinaryFile(System::GetExeOverride().c_str(), error);
okay = (exe_data.has_value() && InjectExecutable(exe_data.value(), true, error));
const std::optional<DynamicHeapArray<u8>> exe_data = FileSystem::ReadBinaryFile(System::GetExeOverride().c_str(), error);
okay = (exe_data.has_value() && InjectExecutable(exe_data->cspan(), true, error));
if (!okay)
Error::AddPrefixFmt(error, "Failed to load {}: ", Path::GetFileName(path));
}
Expand Down
1 change: 1 addition & 0 deletions src/core/gdb_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "cpu_core_private.h"
#include "system.h"

#include "common/assert.h"
#include "common/log.h"
#include "common/small_string.h"
#include "common/string_util.h"
Expand Down
52 changes: 32 additions & 20 deletions src/core/memory_card_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ const T* GetFramePtr(const DataArray& data, u32 block, u32 frame)
}

static std::optional<u32> GetNextFreeBlock(const DataArray& data);
static bool ImportCardMCD(DataArray* data, const char* filename, std::vector<u8> file_data, Error* error);
static bool ImportCardGME(DataArray* data, const char* filename, std::vector<u8> file_data, Error* error);
static bool ImportCardVGS(DataArray* data, const char* filename, std::vector<u8> file_data, Error* error);
static bool ImportCardPSX(DataArray* data, const char* filename, std::vector<u8> file_data, Error* error);
static bool ImportCardMCD(DataArray* data, const char* filename, std::span<const u8> file_data, Error* error);
static bool ImportCardGME(DataArray* data, const char* filename, std::span<const u8> file_data, Error* error);
static bool ImportCardVGS(DataArray* data, const char* filename, std::span<const u8> file_data, Error* error);
static bool ImportCardPSX(DataArray* data, const char* filename, std::span<const u8> file_data, Error* error);
static bool ImportSaveWithDirectoryFrame(DataArray* data, const char* filename, const FILESYSTEM_STAT_DATA& sd,
Error* error);
static bool ImportRawSave(DataArray* data, const char* filename, const FILESYSTEM_STAT_DATA& sd, Error* error);
Expand Down Expand Up @@ -324,7 +324,7 @@ bool MemoryCardImage::ReadFile(const DataArray& data, const FileInfo& fi, std::v
return true;
}

bool MemoryCardImage::WriteFile(DataArray* data, std::string_view filename, const std::vector<u8>& buffer, Error* error)
bool MemoryCardImage::WriteFile(DataArray* data, std::string_view filename, const std::span<const u8> buffer, Error* error)
{
if (buffer.empty())
{
Expand Down Expand Up @@ -481,7 +481,7 @@ bool MemoryCardImage::UndeleteFile(DataArray* data, const FileInfo& fi)
return true;
}

bool MemoryCardImage::ImportCardMCD(DataArray* data, const char* filename, std::vector<u8> file_data, Error* error)
bool MemoryCardImage::ImportCardMCD(DataArray* data, const char* filename, std::span<const u8> file_data, Error* error)
{
if (file_data.size() != DATA_SIZE)
{
Expand All @@ -494,7 +494,7 @@ bool MemoryCardImage::ImportCardMCD(DataArray* data, const char* filename, std::
return true;
}

bool MemoryCardImage::ImportCardGME(DataArray* data, const char* filename, std::vector<u8> file_data, Error* error)
bool MemoryCardImage::ImportCardGME(DataArray* data, const char* filename, std::span<const u8> file_data, Error* error)
{
#pragma pack(push, 1)
struct GMEHeader
Expand Down Expand Up @@ -529,15 +529,27 @@ bool MemoryCardImage::ImportCardGME(DataArray* data, const char* filename, std::
{
WARNING_LOG("GME memory card '{}' is too small (got {} expected {}), padding with zeroes", filename,
file_data.size(), expected_size);
file_data.resize(expected_size);
if (file_data.size() > sizeof(GMEHeader))
{
const size_t present = file_data.size() - sizeof(GMEHeader);
std::memcpy(data->data(), file_data.data() + sizeof(GMEHeader), present);
std::memset(data->data() + present, 0, DATA_SIZE - present);
}
else
{
std::memset(data->data(), 0, DATA_SIZE);
}
}
else
{
// we don't actually care about the header, just skip over it
std::memcpy(data->data(), file_data.data() + sizeof(GMEHeader), DATA_SIZE);
}

// we don't actually care about the header, just skip over it
std::memcpy(data->data(), file_data.data() + sizeof(GMEHeader), DATA_SIZE);
return true;
}

bool MemoryCardImage::ImportCardVGS(DataArray* data, const char* filename, std::vector<u8> file_data, Error* error)
bool MemoryCardImage::ImportCardVGS(DataArray* data, const char* filename, std::span<const u8> file_data, Error* error)
{
constexpr u32 HEADER_SIZE = 64;
constexpr u32 EXPECTED_SIZE = HEADER_SIZE + DATA_SIZE;
Expand All @@ -560,7 +572,7 @@ bool MemoryCardImage::ImportCardVGS(DataArray* data, const char* filename, std::
return true;
}

bool MemoryCardImage::ImportCardPSX(DataArray* data, const char* filename, std::vector<u8> file_data, Error* error)
bool MemoryCardImage::ImportCardPSX(DataArray* data, const char* filename, std::span<const u8> file_data, Error* error)
{
constexpr u32 HEADER_SIZE = 256;
constexpr u32 EXPECTED_SIZE = HEADER_SIZE + DATA_SIZE;
Expand All @@ -583,7 +595,7 @@ bool MemoryCardImage::ImportCardPSX(DataArray* data, const char* filename, std::
return true;
}

bool MemoryCardImage::ImportCard(DataArray* data, const char* filename, std::vector<u8> file_data, Error* error)
bool MemoryCardImage::ImportCard(DataArray* data, const char* filename, std::span<const u8> file_data, Error* error)
{
const std::string_view extension = Path::GetExtension(filename);
if (extension.empty())
Expand All @@ -597,19 +609,19 @@ bool MemoryCardImage::ImportCard(DataArray* data, const char* filename, std::vec
StringUtil::EqualNoCase(extension, "psm") || StringUtil::EqualNoCase(extension, "ps") ||
StringUtil::EqualNoCase(extension, "ddf"))
{
return ImportCardMCD(data, filename, std::move(file_data), error);
return ImportCardMCD(data, filename, file_data, error);
}
else if (StringUtil::EqualNoCase(extension, "gme"))
{
return ImportCardGME(data, filename, std::move(file_data), error);
return ImportCardGME(data, filename, file_data, error);
}
else if (StringUtil::EqualNoCase(extension, "mem") || StringUtil::EqualNoCase(extension, "vgs"))
{
return ImportCardVGS(data, filename, std::move(file_data), error);
return ImportCardVGS(data, filename, file_data, error);
}
else if (StringUtil::EqualNoCase(extension, "psx"))
{
return ImportCardPSX(data, filename, std::move(file_data), error);
return ImportCardPSX(data, filename, file_data, error);
}
else
{
Expand All @@ -620,11 +632,11 @@ bool MemoryCardImage::ImportCard(DataArray* data, const char* filename, std::vec

bool MemoryCardImage::ImportCard(DataArray* data, const char* filename, Error* error)
{
std::optional<std::vector<u8>> file_data = FileSystem::ReadBinaryFile(filename, error);
std::optional<DynamicHeapArray<u8>> file_data = FileSystem::ReadBinaryFile(filename, error);
if (!file_data.has_value())
return false;

return ImportCard(data, filename, std::move(file_data.value()), error);
return ImportCard(data, filename, file_data->cspan(), error);
}

bool MemoryCardImage::ExportSave(DataArray* data, const FileInfo& fi, const char* filename, Error* error)
Expand Down Expand Up @@ -724,7 +736,7 @@ bool MemoryCardImage::ImportRawSave(DataArray* data, const char* filename, const
if (save_name.length() > DirectoryFrame::FILE_NAME_LENGTH)
save_name.erase(DirectoryFrame::FILE_NAME_LENGTH);

std::optional<std::vector<u8>> blocks = FileSystem::ReadBinaryFile(filename, error);
std::optional<DynamicHeapArray<u8>> blocks = FileSystem::ReadBinaryFile(filename, error);
if (!blocks.has_value())
return false;

Expand Down
5 changes: 3 additions & 2 deletions src/core/memory_card_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma once

#include <array>
#include <span>
#include <string>
#include <string_view>
#include <vector>
Expand Down Expand Up @@ -51,11 +52,11 @@ bool IsValid(const DataArray& data);
u32 GetFreeBlockCount(const DataArray& data);
std::vector<FileInfo> EnumerateFiles(const DataArray& data, bool include_deleted);
bool ReadFile(const DataArray& data, const FileInfo& fi, std::vector<u8>* buffer, Error* error);
bool WriteFile(DataArray* data, std::string_view filename, const std::vector<u8>& buffer, Error* error);
bool WriteFile(DataArray* data, std::string_view filename, std::span<const u8> buffer, Error* error);
bool DeleteFile(DataArray* data, const FileInfo& fi, bool clear_sectors);
bool UndeleteFile(DataArray* data, const FileInfo& fi);
bool ImportCard(DataArray* data, const char* filename, Error* error);
bool ImportCard(DataArray* data, const char* filename, std::vector<u8> file_data, Error* error);
bool ImportCard(DataArray* data, const char* filename, std::span<const u8> file_data, Error* error);
bool ExportSave(DataArray* data, const FileInfo& fi, const char* filename, Error* error);
bool ImportSave(DataArray* data, const char* filename, Error* error);
} // namespace MemoryCardImage
4 changes: 3 additions & 1 deletion src/core/pine_server.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team, Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: LGPL-3.0+
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
// NOTE: File has been rewritten completely compared to the original, only the enums remain.

#include "pine_server.h"
#include "cpu_core.h"
Expand All @@ -11,6 +12,7 @@

#include "util/sockets.h"

#include "common/assert.h"
#include "common/binary_reader_writer.h"
#include "common/error.h"
#include "common/file_system.h"
Expand Down
6 changes: 3 additions & 3 deletions src/core/pine_server.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team, Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: LGPL-3.0+
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)

/* A reference client implementation for interfacing with PINE is available
* here: https://code.govanify.com/govanify/pine/ */
// A reference client implementation for interfacing with PINE is available here:
// https://code.govanify.com/govanify/pine/

#pragma once

Expand Down
2 changes: 1 addition & 1 deletion src/core/psf_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ float PSFLoader::File::GetTagFloat(const char* tag_name, float default_value) co

bool PSFLoader::File::Load(const char* path, Error* error)
{
std::optional<std::vector<u8>> file_data(FileSystem::ReadBinaryFile(path, error));
std::optional<DynamicHeapArray<u8>> file_data(FileSystem::ReadBinaryFile(path, error));
if (!file_data.has_value() || file_data->empty())
return false;

Expand Down
4 changes: 2 additions & 2 deletions src/core/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -900,12 +900,12 @@ bool System::GetGameDetailsFromImage(CDImage* cdi, std::string* out_id, GameHash

System::GameHash System::GetGameHashFromFile(const char* path)
{
const std::optional<std::vector<u8>> data = FileSystem::ReadBinaryFile(path);
const std::optional<DynamicHeapArray<u8>> data = FileSystem::ReadBinaryFile(path);
if (!data)
return 0;

const std::string display_name = FileSystem::GetDisplayNameFromPath(path);
return GetGameHashFromBuffer(display_name, data.value(), IsoReader::ISOPrimaryVolumeDescriptor{}, 0);
return GetGameHashFromBuffer(display_name, data->cspan(), IsoReader::ISOPrimaryVolumeDescriptor{}, 0);
}

std::string System::GetExecutableNameForImage(IsoReader& iso, bool strip_subdirectories)
Expand Down
1 change: 1 addition & 0 deletions src/duckstation-qt/autoupdaterdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "util/http_downloader.h"

#include "common/assert.h"
#include "common/error.h"
#include "common/file_system.h"
#include "common/log.h"
Expand Down
1 change: 1 addition & 0 deletions src/duckstation-qt/gamelistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "core/system.h"

#include "common/assert.h"
#include "common/file_system.h"
#include "common/path.h"
#include "common/string_util.h"
Expand Down
Loading

0 comments on commit 707453b

Please sign in to comment.