Revert this code back to what it was

This commit is contained in:
twinaphex 2016-09-22 16:28:45 +02:00
parent 693a9660f3
commit 2c29e599e8

View File

@ -324,112 +324,6 @@ void autosave_deinit(void)
}
#endif
static unsigned content_allocate_save_blocks(struct sram_block **blocks)
{
unsigned i;
unsigned num_blocks = 0;
settings_t *settings = config_get_ptr();
/* Checking of SRAM overwrite, the backing up of it and
flushing. */
if (settings->block_sram_overwrite && task_save_files
&& task_save_files->size)
{
RARCH_LOG("%s.\n",
msg_hash_to_str(MSG_BLOCKING_SRAM_OVERWRITE));
*blocks = (struct sram_block*)
calloc(task_save_files->size, sizeof(*blocks));
if (*blocks)
{
num_blocks = task_save_files->size;
for (i = 0; i < num_blocks; i++)
{
struct sram_block *block = (struct sram_block*)&blocks[i];
block->type = task_save_files->elems[i].attr.i;
}
}
}
for (i = 0; i < num_blocks; i++)
{
retro_ctx_memory_info_t mem_info;
struct sram_block *block = (struct sram_block*)&blocks[i];
mem_info.id = block->type;
core_get_memory(&mem_info);
block->size = mem_info.size;
}
for (i = 0; i < num_blocks; i++)
{
struct sram_block *block = (struct sram_block*)&blocks[i];
if (block->size)
block->data = malloc(block->size);
}
/* Backup current SRAM which is overwritten by unserialize. */
for (i = 0; i < num_blocks; i++)
{
retro_ctx_memory_info_t mem_info;
void *dst = NULL;
const void *src = NULL;
struct sram_block *block = (struct sram_block*)&blocks[i];
if (!block->data)
continue;
mem_info.id = block->type;
core_get_memory(&mem_info);
src = mem_info.data;
dst = block->data;
if (src)
memcpy(dst, src, block->size);
}
return num_blocks;
}
static void content_flush_save_blocks(struct sram_block **blocks,
unsigned num_blocks)
{
unsigned i;
/* Flush back. */
for (i = 0; i < num_blocks; i++)
{
retro_ctx_memory_info_t mem_info;
const void *src = NULL;
void *dst = NULL;
struct sram_block *block = (struct sram_block*)&blocks[i];
if (!block->data)
continue;
mem_info.id = block->type;
core_get_memory(&mem_info);
src = block->data;
dst = mem_info.data;
if (dst)
memcpy(dst, src, block->size);
}
for (i = 0; i < num_blocks; i++)
{
struct sram_block *block = (struct sram_block*)&blocks[i];
if (!block || !block->data)
continue;
free(block->data);
}
}
/**
* undo_load_state:
* Revert to the state before a state was loaded.
@ -438,12 +332,14 @@ static void content_flush_save_blocks(struct sram_block **blocks,
**/
bool content_undo_load_state(void)
{
unsigned i;
retro_ctx_serialize_info_t serial_info;
size_t temp_data_size;
bool ret = false;
void* temp_data = NULL;
unsigned num_blocks = 0;
void* temp_data = NULL;
struct sram_block *blocks = NULL;
settings_t *settings = config_get_ptr();
RARCH_LOG("%s: \"%s\".\n",
msg_hash_to_str(MSG_LOADING_STATE),
@ -454,7 +350,55 @@ bool content_undo_load_state(void)
undo_load_buf.size,
msg_hash_to_str(MSG_BYTES));
num_blocks = content_allocate_save_blocks(&blocks);
/* TODO/FIXME - This checking of SRAM overwrite, the backing up of it and
its flushing could all be in their own functions... */
if (settings->block_sram_overwrite && task_save_files
&& task_save_files->size)
{
RARCH_LOG("%s.\n",
msg_hash_to_str(MSG_BLOCKING_SRAM_OVERWRITE));
blocks = (struct sram_block*)
calloc(task_save_files->size, sizeof(*blocks));
if (blocks)
{
num_blocks = task_save_files->size;
for (i = 0; i < num_blocks; i++)
blocks[i].type = task_save_files->elems[i].attr.i;
}
}
for (i = 0; i < num_blocks; i++)
{
retro_ctx_memory_info_t mem_info;
mem_info.id = blocks[i].type;
core_get_memory(&mem_info);
blocks[i].size = mem_info.size;
}
for (i = 0; i < num_blocks; i++)
if (blocks[i].size)
blocks[i].data = malloc(blocks[i].size);
/* Backup current SRAM which is overwritten by unserialize. */
for (i = 0; i < num_blocks; i++)
{
if (blocks[i].data)
{
retro_ctx_memory_info_t mem_info;
const void *ptr = NULL;
mem_info.id = blocks[i].type;
core_get_memory(&mem_info);
ptr = mem_info.data;
if (ptr)
memcpy(blocks[i].data, ptr, blocks[i].size);
}
}
/* We need to make a temporary copy of the buffer, to allow the swap below */
temp_data = malloc(undo_load_buf.size);
@ -474,7 +418,26 @@ bool content_undo_load_state(void)
free(temp_data);
temp_data = NULL;
content_flush_save_blocks(&blocks, num_blocks);
/* Flush back. */
for (i = 0; i < num_blocks; i++)
{
if (blocks[i].data)
{
retro_ctx_memory_info_t mem_info;
void *ptr = NULL;
mem_info.id = blocks[i].type;
core_get_memory(&mem_info);
ptr = mem_info.data;
if (ptr)
memcpy(ptr, blocks[i].data, blocks[i].size);
}
}
for (i = 0; i < num_blocks; i++)
free(blocks[i].data);
free(blocks);
if (!ret)
@ -624,12 +587,14 @@ bool content_save_state(const char *path, bool save_to_disk)
**/
bool content_load_state(const char *path, bool load_to_backup_buffer)
{
unsigned i;
ssize_t size;
retro_ctx_serialize_info_t serial_info;
unsigned num_blocks = 0;
void *buf = NULL;
struct sram_block *blocks = NULL;
bool ret = filestream_read_file(path, &buf, &size);
settings_t *settings = config_get_ptr();
RARCH_LOG("%s: \"%s\".\n",
msg_hash_to_str(MSG_LOADING_STATE),
@ -666,7 +631,54 @@ bool content_load_state(const char *path, bool load_to_backup_buffer)
return true;
}
num_blocks = content_allocate_save_blocks(&blocks);
if (settings->block_sram_overwrite && task_save_files
&& task_save_files->size)
{
RARCH_LOG("%s.\n",
msg_hash_to_str(MSG_BLOCKING_SRAM_OVERWRITE));
blocks = (struct sram_block*)
calloc(task_save_files->size, sizeof(*blocks));
if (blocks)
{
num_blocks = task_save_files->size;
for (i = 0; i < num_blocks; i++)
blocks[i].type = task_save_files->elems[i].attr.i;
}
}
for (i = 0; i < num_blocks; i++)
{
retro_ctx_memory_info_t mem_info;
mem_info.id = blocks[i].type;
core_get_memory(&mem_info);
blocks[i].size = mem_info.size;
}
for (i = 0; i < num_blocks; i++)
if (blocks[i].size)
blocks[i].data = malloc(blocks[i].size);
/* Backup current SRAM which is overwritten by unserialize. */
for (i = 0; i < num_blocks; i++)
{
if (blocks[i].data)
{
retro_ctx_memory_info_t mem_info;
const void *ptr = NULL;
mem_info.id = blocks[i].type;
core_get_memory(&mem_info);
ptr = mem_info.data;
if (ptr)
memcpy(blocks[i].data, ptr, blocks[i].size);
}
}
serial_info.data_const = buf;
serial_info.size = size;
@ -676,7 +688,26 @@ bool content_load_state(const char *path, bool load_to_backup_buffer)
ret = core_unserialize(&serial_info);
content_flush_save_blocks(&blocks, num_blocks);
/* Flush back. */
for (i = 0; i < num_blocks; i++)
{
if (blocks[i].data)
{
retro_ctx_memory_info_t mem_info;
void *ptr = NULL;
mem_info.id = blocks[i].type;
core_get_memory(&mem_info);
ptr = mem_info.data;
if (ptr)
memcpy(ptr, blocks[i].data, blocks[i].size);
}
}
for (i = 0; i < num_blocks; i++)
free(blocks[i].data);
free(blocks);
if (!ret)