Only autosave when the data actually changed.

This commit is contained in:
Themaister 2011-09-17 10:57:55 +02:00
parent 38e7fc6659
commit efe3f943d5
2 changed files with 24 additions and 16 deletions

View File

@ -48,9 +48,13 @@ static int autosave_thread(void *data)
while (!save->quit) while (!save->quit)
{ {
autosave_lock(save); autosave_lock(save);
bool differ = memcmp(save->buffer, save->snes_buffer, save->bufsize) != 0;
if (differ)
memcpy(save->buffer, save->snes_buffer, save->bufsize); memcpy(save->buffer, save->snes_buffer, save->bufsize);
autosave_unlock(save); autosave_unlock(save);
if (differ)
{
// Should probably deal with this more elegantly. // Should probably deal with this more elegantly.
FILE *file = fopen(save->path, "wb"); FILE *file = fopen(save->path, "wb");
if (file) if (file)
@ -58,9 +62,11 @@ static int autosave_thread(void *data)
// Avoid spamming down stderr ... :) // Avoid spamming down stderr ... :)
if (first_log) if (first_log)
{ {
SSNES_LOG("Autosaving SRAM to \"%s\", will continue to autosave every %u seconds ...\n", save->path, save->interval); SSNES_LOG("Autosaving SRAM to \"%s\", will continue to check every %u seconds ...\n", save->path, save->interval);
first_log = false; first_log = false;
} }
else
SSNES_LOG("SRAM changed ... autosaving ...\n");
bool failed = false; bool failed = false;
failed |= fwrite(save->buffer, 1, save->bufsize, file) != save->bufsize; failed |= fwrite(save->buffer, 1, save->bufsize, file) != save->bufsize;
@ -69,6 +75,7 @@ static int autosave_thread(void *data)
if (failed) if (failed)
SSNES_WARN("Failed to autosave SRAM! Disk might be full.\n"); SSNES_WARN("Failed to autosave SRAM! Disk might be full.\n");
} }
}
SDL_mutexP(save->cond_lock); SDL_mutexP(save->cond_lock);
if (!save->quit) if (!save->quit)
@ -96,6 +103,7 @@ autosave_t *autosave_new(const char *path, const void *data, size_t size, unsign
free(handle); free(handle);
return NULL; return NULL;
} }
memcpy(handle->buffer, handle->snes_buffer, handle->bufsize);
handle->lock = SDL_CreateMutex(); handle->lock = SDL_CreateMutex();
handle->cond_lock = SDL_CreateMutex(); handle->cond_lock = SDL_CreateMutex();