Skip to content

Commit 02e23be

Browse files
authored
Merge pull request #7156 from RetroSven/master
push save state into background for cores that need the emulator to be running during serialization
2 parents f16ec70 + 75abd1f commit 02e23be

File tree

1 file changed

+80
-62
lines changed

1 file changed

+80
-62
lines changed

Diff for: tasks/task_save.c

+80-62
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,33 @@ static void task_save_handler_finished(retro_task_t *task,
555555
free(state);
556556
}
557557

558+
void* get_serialized_data(const char *path, size_t serial_size)
559+
{
560+
retro_ctx_serialize_info_t serial_info;
561+
bool ret = false;
562+
void *data = NULL;
563+
564+
data = malloc(serial_size);
565+
566+
if (!data)
567+
return NULL;
568+
569+
RARCH_LOG("%s: %d %s.\n",
570+
msg_hash_to_str(MSG_STATE_SIZE),
571+
(int)serial_size,
572+
msg_hash_to_str(MSG_BYTES));
573+
574+
serial_info.data = data;
575+
serial_info.size = serial_size;
576+
ret = core_serialize(&serial_info);
577+
if ( !ret )
578+
{
579+
free(data) ;
580+
return NULL ;
581+
}
582+
return data ;
583+
}
584+
558585
/**
559586
* task_save_handler:
560587
* @task : the task being worked on
@@ -576,9 +603,22 @@ static void task_save_handler(retro_task_t *task)
576603
return;
577604
}
578605

606+
if (!state->data)
607+
{
608+
state->data = get_serialized_data(state->path, state->size) ;
609+
}
610+
579611
remaining = MIN(state->size - state->written, SAVE_STATE_CHUNK);
580-
written = (int)intfstream_write(state->file,
581-
(uint8_t*)state->data + state->written, remaining);
612+
613+
if ( state->data )
614+
{
615+
written = (int)intfstream_write(state->file,
616+
(uint8_t*)state->data + state->written, remaining);
617+
}
618+
else
619+
{
620+
written = 0 ;
621+
}
582622

583623
state->written += written;
584624

@@ -1134,6 +1174,7 @@ static void task_push_load_and_save_state(const char *path, void *data,
11341174
free(task);
11351175
}
11361176

1177+
11371178
/**
11381179
* content_save_state:
11391180
* @path : path of saved state that shall be written to.
@@ -1144,85 +1185,62 @@ static void task_push_load_and_save_state(const char *path, void *data,
11441185
**/
11451186
bool content_save_state(const char *path, bool save_to_disk, bool autosave)
11461187
{
1147-
retro_ctx_serialize_info_t serial_info;
1188+
//retro_ctx_serialize_info_t serial_info;
11481189
retro_ctx_size_info_t info;
11491190
bool ret = false;
11501191
void *data = NULL;
11511192

11521193
core_serialize_size(&info);
11531194

1154-
RARCH_LOG("%s: \"%s\".\n",
1155-
msg_hash_to_str(MSG_SAVING_STATE),
1156-
path);
1157-
1158-
if (info.size == 0)
1159-
return false;
1160-
1161-
data = malloc(info.size);
1162-
1163-
if (!data)
1164-
return false;
1165-
1166-
RARCH_LOG("%s: %d %s.\n",
1167-
msg_hash_to_str(MSG_STATE_SIZE),
1168-
(int)info.size,
1169-
msg_hash_to_str(MSG_BYTES));
1170-
1171-
serial_info.data = data;
1172-
serial_info.size = info.size;
1173-
ret = core_serialize(&serial_info);
1174-
1175-
if (ret)
1195+
if (save_to_disk)
11761196
{
1177-
if (save_to_disk)
1197+
if (filestream_exists(path) && !autosave)
11781198
{
1179-
if (filestream_exists(path) && !autosave)
1180-
{
1181-
/* Before overwritting the savestate file, load it into a buffer
1182-
to allow undo_save_state() to work */
1183-
/* TODO/FIXME - Use msg_hash_to_str here */
1184-
RARCH_LOG("%s ...\n",
1185-
msg_hash_to_str(MSG_FILE_ALREADY_EXISTS_SAVING_TO_BACKUP_BUFFER));
1199+
/* Before overwritting the savestate file, load it into a buffer
1200+
to allow undo_save_state() to work */
1201+
/* TODO/FIXME - Use msg_hash_to_str here */
1202+
RARCH_LOG("%s ...\n",
1203+
msg_hash_to_str(MSG_FILE_ALREADY_EXISTS_SAVING_TO_BACKUP_BUFFER));
11861204

1187-
task_push_load_and_save_state(path, data, info.size, true, autosave);
1188-
}
1189-
else
1190-
task_push_save_state(path, data, info.size, autosave);
1205+
task_push_load_and_save_state(path, data, info.size, true, autosave);
11911206
}
11921207
else
1208+
task_push_save_state(path, data, info.size, autosave);
1209+
}
1210+
else
1211+
{
1212+
data = get_serialized_data(path, info.size) ;
1213+
if ( data == NULL )
11931214
{
1194-
/* save_to_disk is false, which means we are saving the state
1195-
in undo_load_buf to allow content_undo_load_state() to restore it */
1196-
1197-
/* If we were holding onto an old state already, clean it up first */
1198-
if (undo_load_buf.data)
1199-
{
1200-
free(undo_load_buf.data);
1201-
undo_load_buf.data = NULL;
1202-
}
1215+
RARCH_ERR("%s \"%s\".\n",
1216+
msg_hash_to_str(MSG_FAILED_TO_SAVE_STATE_TO),
1217+
path);
1218+
return false ;
1219+
}
1220+
/* save_to_disk is false, which means we are saving the state
1221+
in undo_load_buf to allow content_undo_load_state() to restore it */
12031222

1204-
undo_load_buf.data = malloc(info.size);
1205-
if (!undo_load_buf.data)
1206-
{
1207-
free(data);
1208-
return false;
1209-
}
1223+
/* If we were holding onto an old state already, clean it up first */
1224+
if (undo_load_buf.data)
1225+
{
1226+
free(undo_load_buf.data);
1227+
undo_load_buf.data = NULL;
1228+
}
12101229

1211-
memcpy(undo_load_buf.data, data, info.size);
1230+
undo_load_buf.data = malloc(info.size);
1231+
if (!undo_load_buf.data)
1232+
{
12121233
free(data);
1213-
undo_load_buf.size = info.size;
1214-
strlcpy(undo_load_buf.path, path, sizeof(undo_load_buf.path));
1234+
return false;
12151235
}
1216-
}
1217-
else
1218-
{
1236+
1237+
memcpy(undo_load_buf.data, data, info.size);
12191238
free(data);
1220-
RARCH_ERR("%s \"%s\".\n",
1221-
msg_hash_to_str(MSG_FAILED_TO_SAVE_STATE_TO),
1222-
path);
1239+
undo_load_buf.size = info.size;
1240+
strlcpy(undo_load_buf.path, path, sizeof(undo_load_buf.path));
12231241
}
12241242

1225-
return ret;
1243+
return true;
12261244
}
12271245

12281246
/**

0 commit comments

Comments
 (0)