|
4 | 4 | #include "common/printing.h" |
5 | 5 |
|
6 | 6 | #include <stdlib.h> |
| 7 | +#include <string.h> |
7 | 8 |
|
8 | 9 | #define FF_CACHE_VERSION_NAME "cacheversion" |
9 | 10 | #define FF_CACHE_VERSION_EXTENSION "ffv" |
10 | 11 |
|
11 | 12 | #define FF_CACHE_VALUE_EXTENSION "ffcv" |
12 | 13 | #define FF_CACHE_SPLIT_EXTENSION "ffcs" |
13 | 14 |
|
14 | | -#define FF_CACHE_EXTENSION_DATA "ffcd" |
| 15 | +#define FF_CACHE_EXTENSION_V1 "ffc1" |
15 | 16 |
|
16 | 17 | static void getCacheFilePath(const FFinstance* instance, const char* moduleName, const char* extension, FFstrbuf* buffer) |
17 | 18 | { |
@@ -211,24 +212,113 @@ void ffPrintAndWriteToCache(FFinstance* instance, const char* moduleName, const |
211 | 212 | ffCacheClose(&cache); |
212 | 213 | } |
213 | 214 |
|
214 | | -void ffCachingWriteData(const FFinstance* instance, const char* moduleName, size_t dataSize, const void* data) |
| 215 | +typedef struct FFCacheRead |
215 | 216 | { |
| 217 | + FFstrbuf data; |
| 218 | + uint32_t position; |
| 219 | +} FFCacheRead; |
| 220 | + |
| 221 | +static bool cacheReadStrbuf(FFCacheRead* cacheRead, FFstrbuf* strbuf) |
| 222 | +{ |
| 223 | + if(cacheRead->position >= cacheRead->data.length) |
| 224 | + return false; |
| 225 | + |
| 226 | + ffStrbufAppendS(strbuf, cacheRead->data.chars + cacheRead->position); |
| 227 | + cacheRead->position += strbuf->length + 1; // skip the null byte too |
| 228 | + return true; |
| 229 | +} |
| 230 | + |
| 231 | +static bool cacheReadData(FFCacheRead* cacheRead, size_t dataSize, void* data) |
| 232 | +{ |
| 233 | + if(cacheRead->position + dataSize > cacheRead->data.length) |
| 234 | + return false; |
| 235 | + |
| 236 | + memcpy(data, cacheRead->data.chars + cacheRead->position, dataSize); |
| 237 | + cacheRead->position += (uint32_t) dataSize; |
| 238 | + return true; |
| 239 | +} |
| 240 | + |
| 241 | +static bool cacheResetStrbuf(FFCache* cache, FFstrbuf* strbuf) |
| 242 | +{ |
| 243 | + FF_UNUSED(cache); |
| 244 | + ffStrbufClear(strbuf); |
| 245 | + return true; |
| 246 | +} |
| 247 | + |
| 248 | +static bool cacheResetData(FFCache* cacheRead, size_t dataSize, void* data) |
| 249 | +{ |
| 250 | + FF_UNUSED(cacheRead, dataSize, data); |
| 251 | + return true; |
| 252 | +} |
| 253 | + |
| 254 | +bool ffCacheRead(const FFinstance* instance, void* obj, const char* cacheName, FFCacheMethodCallback callback) |
| 255 | +{ |
| 256 | + if(instance->config.recache) |
| 257 | + return false; |
| 258 | + |
| 259 | + FFCacheRead cache; |
| 260 | + bool result; |
| 261 | + |
216 | 262 | FFstrbuf path; |
217 | 263 | ffStrbufInitA(&path, 128); |
218 | | - getCacheFilePath(instance, moduleName, FF_CACHE_EXTENSION_DATA, &path); |
219 | | - ffWriteFileData(path.chars, dataSize, data); |
| 264 | + getCacheFilePath(instance, cacheName, FF_CACHE_EXTENSION_V1, &path); |
| 265 | + |
| 266 | + cache.position = 0; |
| 267 | + |
| 268 | + ffStrbufInitA(&cache.data, 256); |
| 269 | + result = ffAppendFileBuffer(path.chars, &cache.data); |
| 270 | + |
| 271 | + if(result) |
| 272 | + result = callback(obj, &cache, (FFCacheMethodStrbuf) cacheReadStrbuf, (FFCacheMethodData) cacheReadData); |
| 273 | + |
| 274 | + if(result) |
| 275 | + result = cache.position == cache.data.length; |
| 276 | + |
| 277 | + if(!result) |
| 278 | + callback(obj, NULL, (FFCacheMethodStrbuf) cacheResetStrbuf, (FFCacheMethodData) cacheResetData); |
| 279 | + |
| 280 | + ffStrbufDestroy(&cache.data); |
220 | 281 | ffStrbufDestroy(&path); |
| 282 | + |
| 283 | + return result; |
221 | 284 | } |
222 | 285 |
|
223 | | -bool ffCachingReadData(const FFinstance* instance, const char* moduleName, size_t dataSize, void* data) |
| 286 | +typedef struct FFCacheWrite |
224 | 287 | { |
225 | | - if(instance->config.recache) |
226 | | - return false; |
| 288 | + FFstrbuf data; |
| 289 | +} FFCacheWrite; |
| 290 | + |
| 291 | +static bool cacheWriteStrbuf(FFCacheWrite* cacheWrite, FFstrbuf* strbuf) |
| 292 | +{ |
| 293 | + ffStrbufEnsureFree(&cacheWrite->data, strbuf->length); |
| 294 | + memcpy(cacheWrite->data.chars + cacheWrite->data.length, strbuf->chars, strbuf->length +1); //Copy the nullbyte too |
| 295 | + cacheWrite->data.length += strbuf->length + 1; |
| 296 | + return true; |
| 297 | +} |
| 298 | + |
| 299 | +static bool cacheWriteData(FFCacheWrite* cacheWrite, size_t dataSize, const void* data) |
| 300 | +{ |
| 301 | + ffStrbufEnsureFree(&cacheWrite->data, (uint32_t) dataSize); |
| 302 | + memcpy(cacheWrite->data.chars + cacheWrite->data.length, data, dataSize); |
| 303 | + cacheWrite->data.length += (uint32_t) dataSize; |
| 304 | + return true; |
| 305 | +} |
| 306 | + |
| 307 | +void ffCacheWrite(const FFinstance* instance, void* obj, const char* cacheName, FFCacheMethodCallback callback) |
| 308 | +{ |
| 309 | + if(!instance->config.cacheSave) |
| 310 | + return; |
| 311 | + |
| 312 | + FFCacheWrite cache; |
227 | 313 |
|
228 | 314 | FFstrbuf path; |
229 | 315 | ffStrbufInitA(&path, 128); |
230 | | - getCacheFilePath(instance, moduleName, FF_CACHE_EXTENSION_DATA, &path); |
231 | | - ssize_t result = ffReadFileData(path.chars, dataSize, data); |
| 316 | + getCacheFilePath(instance, cacheName, FF_CACHE_EXTENSION_V1, &path); |
| 317 | + |
| 318 | + ffStrbufInitA(&cache.data, 256); |
| 319 | + callback(obj, &cache, (FFCacheMethodStrbuf) cacheWriteStrbuf, (FFCacheMethodData) cacheWriteData); |
| 320 | + ffWriteFileBuffer(path.chars, &cache.data); |
| 321 | + |
| 322 | + ffStrbufDestroy(&cache.data); |
232 | 323 | ffStrbufDestroy(&path); |
233 | | - return result > 0 && (size_t) result == dataSize; |
234 | 324 | } |
0 commit comments