Permalink
Browse files

Merge pull request #10052 from unknownbrackets/savestate-oom

Avoid Android OOM crashes on save state
  • Loading branch information...
hrydgard committed Nov 5, 2017
2 parents 79208f1 + 349b36f commit 3c591ad9ca6acf972379c91a2ac8685b8892e764
Showing with 41 additions and 52 deletions.
  1. +38 −42 Common/ChunkFile.cpp
  2. +3 −10 Common/ChunkFile.h
View
@@ -15,6 +15,7 @@
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#include <cstdlib>
#include <cstring>
#include <snappy-c.h>
@@ -271,64 +272,59 @@ CChunkFileReader::Error CChunkFileReader::SaveFile(const std::string &filename,
INFO_LOG(SAVESTATE, "ChunkReader: Writing %s", filename.c_str());
File::IOFile pFile(filename, "wb");
if (!pFile)
{
if (!pFile) {
ERROR_LOG(SAVESTATE, "ChunkReader: Error opening file for write");
delete[] buffer;
free(buffer);
return ERROR_BAD_FILE;
}
bool compress = true;
// Make sure we can allocate a buffer to compress before compressing.
size_t write_len = snappy_max_compressed_length(sz);
u8 *compressed_buffer = (u8 *)malloc(write_len);
u8 *write_buffer = buffer;
if (!compressed_buffer) {
ERROR_LOG(SAVESTATE, "ChunkReader: Unable to allocate compressed buffer");
// We'll save uncompressed. Better than not saving...
write_len = sz;
} else {
snappy_compress((const char *)buffer, sz, (char *)compressed_buffer, &write_len);
free(buffer);
write_buffer = compressed_buffer;
}
// Create header
SChunkHeader header;
header.Compress = compress ? 1 : 0;
header.Compress = compressed_buffer ? 1 : 0;
header.Revision = REVISION_CURRENT;
header.ExpectedSize = (u32)sz;
header.ExpectedSize = (u32)write_len;
header.UncompressedSize = (u32)sz;
truncate_cpy(header.GitVersion, gitVersion);
// Setup the fixed-length title.
char titleFixed[128];
truncate_cpy(titleFixed, title.c_str());
// Write to file
if (compress) {
size_t comp_len = snappy_max_compressed_length(sz);
u8 *compressed_buffer = new u8[comp_len];
snappy_compress((const char *)buffer, sz, (char *)compressed_buffer, &comp_len);
delete [] buffer;
header.ExpectedSize = (u32)comp_len;
if (!pFile.WriteArray(&header, 1)) {
ERROR_LOG(SAVESTATE, "ChunkReader: Failed writing header");
return ERROR_BAD_FILE;
}
if (!pFile.WriteArray(titleFixed, sizeof(titleFixed))) {
ERROR_LOG(SAVESTATE, "ChunkReader: Failed writing title");
return ERROR_BAD_FILE;
}
if (!pFile.WriteBytes(&compressed_buffer[0], comp_len)) {
ERROR_LOG(SAVESTATE, "ChunkReader: Failed writing compressed data");
return ERROR_BAD_FILE;
} else {
INFO_LOG(SAVESTATE, "Savestate: Compressed %i bytes into %i", (int)sz, (int)comp_len);
}
delete [] compressed_buffer;
} else {
if (!pFile.WriteArray(&header, 1))
{
ERROR_LOG(SAVESTATE, "ChunkReader: Failed writing header");
delete[] buffer;
return ERROR_BAD_FILE;
}
if (!pFile.WriteBytes(&buffer[0], sz))
{
ERROR_LOG(SAVESTATE, "ChunkReader: Failed writing data");
delete[] buffer;
return ERROR_BAD_FILE;
}
delete [] buffer;
// Now let's start writing out the file...
if (!pFile.WriteArray(&header, 1)) {
ERROR_LOG(SAVESTATE, "ChunkReader: Failed writing header");
free(write_buffer);
return ERROR_BAD_FILE;
}
if (!pFile.WriteArray(titleFixed, sizeof(titleFixed))) {
ERROR_LOG(SAVESTATE, "ChunkReader: Failed writing title");
free(write_buffer);
return ERROR_BAD_FILE;
}
if (!pFile.WriteBytes(write_buffer, write_len)) {
ERROR_LOG(SAVESTATE, "ChunkReader: Failed writing compressed data");
free(write_buffer);
return ERROR_BAD_FILE;
} else if (sz != write_len) {
INFO_LOG(SAVESTATE, "Savestate: Compressed %i bytes into %i", (int)sz, (int)write_len);
}
free(write_buffer);
INFO_LOG(SAVESTATE, "ChunkReader: Done writing %s", filename.c_str());
return ERROR_NONE;
View
@@ -29,6 +29,7 @@
// + Sections can be versioned for backwards/forwards compatibility
// - Serialization code for anything complex has to be manually written.
#include <cstdlib>
#include <map>
#include <unordered_map>
#include <deque>
@@ -646,17 +647,9 @@ class CChunkFileReader
{
// Get data
size_t const sz = MeasurePtr(_class);
u8 *buffer = nullptr;
#if PPSSPP_PLATFORM(ANDROID)
buffer = new u8[sz];
#else
try {
buffer = new u8[sz];
}
catch (std::bad_alloc e) {
u8 *buffer = (u8 *)malloc(sz);
if (!buffer)
return ERROR_BAD_ALLOC;
}
#endif
Error error = SavePtr(buffer, _class);
// SaveFile takes ownership of buffer

0 comments on commit 3c591ad

Please sign in to comment.