alloc save memory instead return error

This commit is contained in:
mailwl 2025-05-16 17:38:57 +03:00
parent d5a4413d60
commit 289e9fb5ab
3 changed files with 18 additions and 12 deletions

View File

@ -10,6 +10,7 @@
#include <utility>
#include <fmt/format.h>
#include "boost/icl/concept/interval.hpp"
#include "common/elf_info.h"
#include "common/logging/log.h"
#include "common/path_util.h"
@ -43,6 +44,7 @@ struct SlotData {
static std::mutex g_slot_mtx;
static std::unordered_map<u32, SlotData> g_attached_slots;
static size_t g_memory_size = 0;
void PersistMemory(u32 slot_id, bool lock) {
std::unique_lock lck{g_slot_mtx, std::defer_lock};
@ -190,25 +192,24 @@ void SaveSFO(u32 slot_id) {
}
}
Error ReadMemory(u32 slot_id, void* buf, size_t buf_size, int64_t offset) {
void ReadMemory(u32 slot_id, void* buf, size_t buf_size, int64_t offset) {
std::lock_guard lk{g_slot_mtx};
auto& data = g_attached_slots[slot_id];
auto& memory = data.memory_cache;
if (memory.empty()) { // Load file
memory.resize(g_memory_size);
memset(memory.data(), 0, g_memory_size);
IOFile f{data.folder_path / FilenameSaveDataMemory, Common::FS::FileAccessMode::Read};
if (!f.IsOpen()) {
return Error::NOT_FOUND;
if (f.IsOpen()) {
f.Seek(0);
f.ReadSpan(std::span{memory});
}
memory.resize(f.GetSize());
f.Seek(0);
f.ReadSpan(std::span{memory});
}
s64 read_size = buf_size;
if (read_size + offset > memory.size()) {
read_size = memory.size() - offset;
}
std::memcpy(buf, memory.data() + offset, read_size);
return Error::OK;
}
void WriteMemory(u32 slot_id, void* buf, size_t buf_size, int64_t offset) {
@ -224,4 +225,8 @@ void WriteMemory(u32 slot_id, void* buf, size_t buf_size, int64_t offset) {
Backup::OrbisSaveDataEventType::__DO_NOT_SAVE);
}
void SetMemorySize(size_t memory_size) {
g_memory_size = memory_size;
}
} // namespace Libraries::SaveData::SaveMemory

View File

@ -37,8 +37,10 @@ void SetIcon(u32 slot_id, void* buf = nullptr, size_t buf_size = 0);
// Save now or wait for the background thread to save
void SaveSFO(u32 slot_id);
Error ReadMemory(u32 slot_id, void* buf, size_t buf_size, int64_t offset);
void ReadMemory(u32 slot_id, void* buf, size_t buf_size, int64_t offset);
void WriteMemory(u32 slot_id, void* buf, size_t buf_size, int64_t offset);
void SetMemorySize(size_t memory_size);
} // namespace Libraries::SaveData::SaveMemory

View File

@ -1139,10 +1139,7 @@ Error PS4_SYSV_ABI sceSaveDataGetSaveDataMemory2(OrbisSaveDataMemoryGet2* getPar
LOG_DEBUG(Lib_SaveData, "called");
auto data = getParam->data;
if (data != nullptr) {
auto result = SaveMemory::ReadMemory(slot_id, data->buf, data->bufSize, data->offset);
if (Error::OK != result) {
return result;
}
SaveMemory::ReadMemory(slot_id, data->buf, data->bufSize, data->offset);
}
auto param = getParam->param;
if (param != nullptr) {
@ -1568,6 +1565,8 @@ Error PS4_SYSV_ABI sceSaveDataSetupSaveDataMemory2(const OrbisSaveDataMemorySetu
slot_id = setupParam->slotId;
}
SaveMemory::SetMemorySize(setupParam->memorySize);
const auto& save_path = SaveMemory::GetSavePath(setupParam->userId, slot_id, g_game_serial);
for (const auto& instance : g_mount_slots) {
if (instance.has_value() && instance->GetSavePath() == save_path) {