|
27 | 27 | static std::string lastStateFile; |
28 | 28 | static time_t lastStateTime; |
29 | 29 |
|
| 30 | +#if defined(__ANDROID__) |
| 31 | + #include <android/api-level.h> |
| 32 | + // fmemopen was added in Marshmallow (API 23) |
| 33 | + #if __ANDROID_API__ >= 23 |
| 34 | + #define HAS_FMEMOPEN |
| 35 | + #endif |
| 36 | +#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__GLIBC__) |
| 37 | + // Standard POSIX platforms usually have fmemopen |
| 38 | + #define HAS_FMEMOPEN |
| 39 | +#endif |
| 40 | + |
| 41 | +#ifdef HAS_FMEMOPEN |
| 42 | +const u32 QUICKSAVE_DEFAULT_SIZE = 32 * 1024 * 1024; // 32 MB |
| 43 | +static u8 quicksave_buf[QUICKSAVE_DEFAULT_SIZE] = {0}; |
| 44 | +#endif |
| 45 | + |
30 | 46 | struct SavestateHeader |
31 | 47 | { |
32 | 48 | void init() |
@@ -182,8 +198,23 @@ void dc_savestate(int index, const u8 *pngData, u32 pngSize) |
182 | 198 | ser = Serializer(data, ser.size()); |
183 | 199 | dc_serialize(ser); |
184 | 200 |
|
185 | | - std::string filename = hostfs::getSavestatePath(index, true); |
186 | | - FILE *f = nowide::fopen(filename.c_str(), "wb"); |
| 201 | + FILE *f = nullptr; |
| 202 | + std::string filename = ""; |
| 203 | +#ifdef HAS_FMEMOPEN |
| 204 | + if (index == -2) |
| 205 | + { |
| 206 | + // in-ram savestate |
| 207 | + filename = "RAM"; |
| 208 | + f = fmemopen(quicksave_buf, QUICKSAVE_DEFAULT_SIZE, "wb"); |
| 209 | + } |
| 210 | + else |
| 211 | +#endif |
| 212 | + { |
| 213 | + // regular file savestate |
| 214 | + filename = hostfs::getSavestatePath(index, true); |
| 215 | + f = nowide::fopen(filename.c_str(), "wb"); |
| 216 | + } |
| 217 | + |
187 | 218 | if (f == nullptr) |
188 | 219 | { |
189 | 220 | WARN_LOG(SAVESTATE, "Failed to save state - could not open %s for writing", filename.c_str()); |
@@ -235,8 +266,23 @@ void dc_loadstate(int index) |
235 | 266 | return; |
236 | 267 | u32 total_size = 0; |
237 | 268 |
|
238 | | - std::string filename = hostfs::getSavestatePath(index, false); |
239 | | - FILE *f = hostfs::storage().openFile(filename, "rb"); |
| 269 | + FILE *f = nullptr; |
| 270 | + std::string filename = ""; |
| 271 | +#ifdef HAS_FMEMOPEN |
| 272 | + if (index == -2) |
| 273 | + { |
| 274 | + // in-ram savestate |
| 275 | + filename = "RAM"; |
| 276 | + f = fmemopen(quicksave_buf, QUICKSAVE_DEFAULT_SIZE, "rb"); |
| 277 | + } |
| 278 | + else |
| 279 | +#endif |
| 280 | + { |
| 281 | + // regular file savestate |
| 282 | + filename = hostfs::getSavestatePath(index, false); |
| 283 | + f = hostfs::storage().openFile(filename, "rb"); |
| 284 | + } |
| 285 | + |
240 | 286 | if (f == nullptr) |
241 | 287 | { |
242 | 288 | WARN_LOG(SAVESTATE, "Failed to load state - could not open %s for reading", filename.c_str()); |
|
0 commit comments