forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autosave.c
288 lines (239 loc) · 6.54 KB
/
autosave.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2016 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <boolean.h>
#include <string/string_list.h>
#include <rthreads/rthreads.h>
#include "autosave.h"
#include "configuration.h"
#include "msg_hash.h"
#include "runloop.h"
#include "libretro_version_1.h"
#include "verbosity.h"
/* Autosave support. */
struct autosave_st
{
autosave_t **list;
unsigned num;
};
struct autosave
{
volatile bool quit;
slock_t *lock;
slock_t *cond_lock;
scond_t *cond;
sthread_t *thread;
void *buffer;
const void *retro_buffer;
const char *path;
size_t bufsize;
unsigned interval;
};
static struct autosave_st autosave_state;
/**
* autosave_lock:
* @handle : pointer to autosave object
*
* Locks autosave.
**/
static void autosave_lock(autosave_t *handle)
{
slock_lock(handle->lock);
}
/**
* autosave_unlock:
* @handle : pointer to autosave object
*
* Unlocks autosave.
**/
static void autosave_unlock(autosave_t *handle)
{
slock_unlock(handle->lock);
}
/**
* autosave_thread:
* @data : pointer to autosave object
*
* Callback function for (threaded) autosave.
**/
static void autosave_thread(void *data)
{
bool first_log = true;
autosave_t *save = (autosave_t*)data;
while (!save->quit)
{
bool differ;
autosave_lock(save);
differ = memcmp(save->buffer, save->retro_buffer,
save->bufsize) != 0;
if (differ)
memcpy(save->buffer, save->retro_buffer, save->bufsize);
autosave_unlock(save);
if (differ)
{
/* Should probably deal with this more elegantly. */
FILE *file = fopen(save->path, "wb");
if (file)
{
bool failed = false;
/* Avoid spamming down stderr ... */
if (first_log)
{
RARCH_LOG("Autosaving SRAM to \"%s\", will continue to check every %u seconds ...\n",
save->path, save->interval);
first_log = false;
}
else
RARCH_LOG("SRAM changed ... autosaving ...\n");
failed |= fwrite(save->buffer, 1, save->bufsize, file)
!= save->bufsize;
failed |= fflush(file) != 0;
failed |= fclose(file) != 0;
if (failed)
RARCH_WARN("Failed to autosave SRAM. Disk might be full.\n");
}
}
slock_lock(save->cond_lock);
if (!save->quit)
scond_wait_timeout(save->cond, save->cond_lock,
save->interval * 1000000LL);
slock_unlock(save->cond_lock);
}
}
/**
* autosave_new:
* @path : path to autosave file
* @data : pointer to buffer
* @size : size of @data buffer
* @interval : interval at which saves should be performed.
*
* Create and initialize autosave object.
*
* Returns: pointer to new autosave_t object if successful, otherwise
* NULL.
**/
autosave_t *autosave_new(const char *path, const void *data, size_t size,
unsigned interval)
{
autosave_t *handle = (autosave_t*)calloc(1, sizeof(*handle));
if (!handle)
return NULL;
handle->bufsize = size;
handle->interval = interval;
handle->path = path;
handle->buffer = malloc(size);
handle->retro_buffer = data;
if (!handle->buffer)
{
free(handle);
return NULL;
}
memcpy(handle->buffer, handle->retro_buffer, handle->bufsize);
handle->lock = slock_new();
handle->cond_lock = slock_new();
handle->cond = scond_new();
handle->thread = sthread_create(autosave_thread, handle);
return handle;
}
/**
* autosave_free:
* @handle : pointer to autosave object
*
* Frees autosave object.
**/
void autosave_free(autosave_t *handle)
{
if (!handle)
return;
slock_lock(handle->cond_lock);
handle->quit = true;
slock_unlock(handle->cond_lock);
scond_signal(handle->cond);
sthread_join(handle->thread);
slock_free(handle->lock);
slock_free(handle->cond_lock);
scond_free(handle->cond);
free(handle->buffer);
free(handle);
}
/**
* lock_autosave:
*
* Lock autosave.
**/
void lock_autosave(void)
{
unsigned i;
for (i = 0; i < autosave_state.num; i++)
{
if (autosave_state.list[i])
autosave_lock(autosave_state.list[i]);
}
}
/**
* unlock_autosave:
*
* Unlocks autosave.
**/
void unlock_autosave(void)
{
unsigned i;
for (i = 0; i < autosave_state.num; i++)
{
if (autosave_state.list[i])
autosave_unlock(autosave_state.list[i]);
}
}
void autosave_event_init(void)
{
unsigned i;
settings_t *settings = config_get_ptr();
global_t *global = global_get_ptr();
if (settings->autosave_interval < 1 || !global->savefiles)
return;
if (!(autosave_state.list = (autosave_t**)calloc(global->savefiles->size,
sizeof(*autosave_state.list))))
return;
autosave_state.num = global->savefiles->size;
for (i = 0; i < global->savefiles->size; i++)
{
retro_ctx_memory_info_t mem_info;
const char *path = global->savefiles->elems[i].data;
unsigned type = global->savefiles->elems[i].attr.i;
mem_info.id = type;
core_ctl(CORE_CTL_RETRO_GET_MEMORY, &mem_info);
if (mem_info.size <= 0)
continue;
autosave_state.list[i] = autosave_new(path,
mem_info.data,
mem_info.size,
settings->autosave_interval);
if (!autosave_state.list[i])
RARCH_WARN("%s\n", msg_hash_to_str(MSG_AUTOSAVE_FAILED));
}
}
void autosave_event_deinit(void)
{
unsigned i;
for (i = 0; i < autosave_state.num; i++)
autosave_free(autosave_state.list[i]);
if (autosave_state.list)
free(autosave_state.list);
autosave_state.list = NULL;
autosave_state.num = 0;
}