Common: Add Buffer type

This commit is contained in:
TellowKrinkle
2026-10-07 14:10:46 -04:00
committed by Ty
parent d3a1eee5e7
commit a511890a68
3 changed files with 82 additions and 4 deletions
+74
View File
@@ -0,0 +1,74 @@
// SPDX-FileCopyrightText: 2002-2026 PCSX2 Dev Team
// SPDX-License-Identifier: GPL-3.0+
#pragma once
#include "Pcsx2Types.h"
#include <cstdlib>
#include <cstring>
namespace Common
{
/// Like `std::vector<u8>` but doesn't zero bytes on resize
class Buffer
{
void* ptr_;
size_t size_;
size_t cap_;
public:
constexpr Buffer(): ptr_(nullptr), size_(0), cap_(0) {}
explicit Buffer(size_t size): ptr_(malloc(size)), size_(size), cap_(size) {}
~Buffer() { if (ptr_) free(ptr_); }
Buffer(const Buffer& other): Buffer(other.size_)
{
memcpy(ptr_, other.ptr_, other.size_);
}
Buffer(Buffer&& other): ptr_(other.ptr_), size_(other.size_), cap_(other.cap_)
{
other.ptr_ = nullptr;
other.size_ = 0;
other.cap_ = 0;
}
Buffer& operator=(const Buffer& other)
{
resize(other.size_);
memcpy(ptr_, other.ptr_, other.size_);
return *this;
}
Buffer& operator=(Buffer&& other)
{
if (this != &other)
{
if (ptr_) free(ptr_);
ptr_ = other.ptr_;
size_ = other.size_;
cap_ = other.cap_;
other.ptr_ = nullptr;
other.size_ = 0;
other.cap_ = 0;
}
return *this;
}
template <typename T = void> const T* get() const { return static_cast<T*>(ptr_); }
template <typename T = void> T* get() { return static_cast<T*>(ptr_); }
size_t capacity() const { return cap_; }
size_t size() const { return size_; }
void resize(size_t size)
{
size_ = size;
reserve(size);
}
void reserve(size_t size)
{
if (cap_ >= size)
return;
cap_ = size;
ptr_ = realloc(ptr_, size);
}
};
} // namespace Common
+4
View File
@@ -42,6 +42,7 @@ target_sources(common PRIVATE
Assertions.h
boost_spsc_queue.hpp
BitUtils.h
Buffer.h
ByteSwap.h
Console.h
CrashHandler.h
@@ -64,6 +65,8 @@ target_sources(common PRIVATE
MD5Digest.h
MRCHelpers.h
Path.h
Pcsx2Defs.h
Pcsx2Types.h
PrecompiledHeader.h
ProgressCallback.h
ReadbackSpinManager.h
@@ -83,6 +86,7 @@ target_sources(common PRIVATE
WindowInfo.h
WrappedMemCopy.h
YAML.h
ZipHelpers.h
)
if(ARCH_X86)
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: GPL-3.0+
#include "common/AlignedMalloc.h"
#include "common/Buffer.h"
#include "common/Console.h"
#include "common/HashCombine.h"
#include "common/FileSystem.h"
@@ -692,10 +693,9 @@ std::optional<GSTextureReplacements::ReplacementTexture> GSTextureReplacements::
ReplacementTextureLoader loader = GetLoader(stat.name);
if (!loader)
return std::nullopt;
void* buf = malloc(stat.size);
ScopedGuard cleanup([buf]{ free(buf); });
Common::Buffer buf(stat.size);
auto zff = zip_fopen_index_managed(zf.get(), file.archive_idx, 0);
if (!zff || static_cast<u64>(zip_fread(zff.get(), buf, stat.size)) != stat.size)
if (!zff || static_cast<u64>(zip_fread(zff.get(), buf.get(), stat.size)) != stat.size)
{
if (!zff && zip_get_error(zf.get())->zip_err == ZIP_ER_COMPNOTSUPP)
Host::AddKeyedOSDMessage("ZipCompressionNotSupported",
@@ -705,7 +705,7 @@ std::optional<GSTextureReplacements::ReplacementTexture> GSTextureReplacements::
Console.Warning("Failed to load replacement texture %s from zip file %s: %s", stat.name, file.path.c_str(), zip_strerror(zf.get()));
return std::nullopt;
}
MemoryFile memfile(buf, stat.size);
MemoryFile memfile(buf.get(), stat.size);
if (!loader(memfile, stat.name, &rtex, only_base_image))
{
Console.Warning("Failed to load replacement texture %s", stat.name);