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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
#include "UnityPrefix.h"
#include "FileCache.h"
#include "Runtime/Utilities/algorithm_utility.h"
#include "Runtime/Utilities/Utility.h"
#include "Runtime/Utilities/PathNameUtility.h"
#if SUPPORT_SERIALIZE_WRITE
#include "Runtime/Utilities/FileUtilities.h"
#endif
using namespace std;
#define USE_OPEN_FILE_CACHE UNITY_EDITOR
#if USE_OPEN_FILE_CACHE
struct OpenFilesCache
{
enum { kOpenedFileCacheCount = 5 };
File* m_Cache[kOpenedFileCacheCount];
UInt32 m_TimeStamps[kOpenedFileCacheCount];
UInt32 m_TimeStamp;
OpenFilesCache ()
{
m_TimeStamp = 0;
for (int i=0;i<kOpenedFileCacheCount;i++)
{
m_Cache[i] = NULL;
m_TimeStamps[i] = 0;
}
}
void OpenCached (File* theFile, const std::string& thePath)
{
m_TimeStamp++;
// find cache, don't do anything if we are in the cache
for (int i=0;i<kOpenedFileCacheCount;i++)
{
if (theFile == m_Cache[i])
{
m_TimeStamps[i] = m_TimeStamp;
return;
}
}
// Find Least recently used cache entry
UInt32 lruTimeStamp = m_TimeStamps[0];
int lruIndex = 0;
for (int i=1;i<kOpenedFileCacheCount;i++)
{
if (m_TimeStamps[i] < lruTimeStamp)
{
lruTimeStamp = m_TimeStamps[i];
lruIndex = i;
}
}
// replace the least recently used cache entry
if (m_Cache[lruIndex] != NULL)
{
#if UNITY_OSX
m_Cache[lruIndex]->Lock (File::kNone, false);
#endif
m_Cache[lruIndex]->Close ();
}
m_Cache[lruIndex] = theFile;
m_TimeStamps[lruIndex] = m_TimeStamp;
if(!theFile->Open (thePath, File::kReadPermission, File::kSilentReturnOnOpenFail))
ErrorString(Format("Could not open file %s for read", thePath.c_str()));
#if UNITY_OSX
theFile->Lock (File::kShared, false);
#endif
}
void ForceCloseAll ()
{
// Find and close cache
for (int i=0;i<kOpenedFileCacheCount;i++)
{
if (m_Cache[i] != NULL)
ForceClose (m_Cache[i]);
}
}
void ForceClose (File* cachable)
{
// Find and close cache
for (int i=0;i<kOpenedFileCacheCount;i++)
{
if (m_Cache[i] == cachable)
{
#if UNITY_OSX
m_Cache[i]->Lock (File::kNone, false);
#endif
m_Cache[i]->Close ();
m_Cache[i] = NULL;
m_TimeStamps[i] = 0;
return;
}
}
}
};
OpenFilesCache gOpenFilesCache;
void ForceCloseAllOpenFileCaches ()
{
gOpenFilesCache.ForceCloseAll ();
}
#endif
CacheReaderBase::~CacheReaderBase ()
{}
CacheWriterBase::~CacheWriterBase ()
{}
FileCacherRead::FileCacherRead (const string& pathName, size_t cacheSize, size_t cacheCount)
{
m_RootHeader = GET_CURRENT_ALLOC_ROOT_HEADER();
m_Path = PathToAbsolutePath(pathName);
// Initialize Cache
m_MaxCacheCount = cacheCount;
m_CacheSize = cacheSize;
m_TimeStamp = 0;
// Get File size
m_FileSize = ::GetFileLength(m_Path);
#if !USE_OPEN_FILE_CACHE
if(!m_File.Open(m_Path, File::kReadPermission, File::kSilentReturnOnOpenFail))
ErrorString(Format("Could not open file %s for read", m_Path.c_str()));
// Make the file non-overwritable by the cache (to make caching behaviour imitate windows)
#if UNITY_OSX
m_File.Lock (File::kShared, false);
#endif
#endif
#if DEBUG_LINEAR_FILE_ACCESS
m_LastFileAccessPosition = 0;
#endif
}
FileCacherRead::~FileCacherRead ()
{
for (CacheBlocks::iterator i = m_CacheBlocks.begin ();i != m_CacheBlocks.end ();++i)
{
AssertIf (i->second.lockCount);
UNITY_FREE(kMemFile,i->second.data);
}
m_CacheBlocks.clear ();
#if USE_OPEN_FILE_CACHE
gOpenFilesCache.ForceClose (&m_File);
#else
// Make the file overwritable by the cache.
#if UNITY_OSX
m_File.Lock (File::kNone, false);
#endif
m_File.Close();
#endif
}
void FileCacherRead::ReadCacheBlock (CacheBlock& cacheBlock)
{
int block = cacheBlock.block;
// Watch out for not reading over eof
int readSize = min<int> (m_CacheSize, m_FileSize - block * m_CacheSize);
// load the data from disk
// only if the physical file contains any data for this block
if (readSize > 0)
{
#if USE_OPEN_FILE_CACHE
gOpenFilesCache.OpenCached (&m_File, m_Path);
#endif
#if DEBUG_LINEAR_FILE_ACCESS
size_t position = block * m_CacheSize;
#endif
m_File.Read (block * m_CacheSize, cacheBlock.data, readSize);
#if DEBUG_LINEAR_FILE_ACCESS
// printf_console("ACCESS: [%08x] %s %i bytes @ %i to %08x\n",this, __FUNCTION__, readSize, block * m_CacheSize, cacheBlock.data);
std::string fileName = GetLastPathNameComponent(m_Path);
if (position < m_LastFileAccessPosition && fileName != "mainData" && fileName != "unity default resources")
{
ErrorString(Format("File access: %s is not linear Reading: %d Seek position: %d", fileName.c_str(), position, m_LastFileAccessPosition));
}
m_LastFileAccessPosition = position + readSize;
#endif
}
}
void FileCacherRead::DirectRead (void* data, size_t position, size_t size)
{
// load the data from disk
// only if the physical file contains any data for this block
FatalErrorIf (m_FileSize - position < size);
#if USE_OPEN_FILE_CACHE
gOpenFilesCache.OpenCached (&m_File, m_Path);
#endif
m_File.Read (position, data, size);
#if DEBUG_LINEAR_FILE_ACCESS
// printf_console("ACCCESS: [%08x] %s %i bytes @ %i to %08x\n",this, __FUNCTION__, size, position, data);
std::string fileName = GetLastPathNameComponent(m_Path);
if (position < m_LastFileAccessPosition && fileName != "mainData" && fileName != "unity default resources")
{
ErrorString(Format("File access: %s is not linear Reading: %d Seek position: %d", fileName.c_str(), position, m_LastFileAccessPosition));
}
m_LastFileAccessPosition = position + size;
#endif
}
bool FileCacherRead::FreeSingleCache ()
{
unsigned lowestTimeStamp = -1;
CacheBlocks::iterator lowestCacheBlock = m_CacheBlocks.end ();
CacheBlocks::iterator i;
for (i=m_CacheBlocks.begin ();i != m_CacheBlocks.end ();i++)
{
if (i->second.lockCount == 0)
{
if (i->second.timeStamp < lowestTimeStamp)
{
lowestTimeStamp = i->second.timeStamp;
lowestCacheBlock = i;
}
}
}
if (lowestCacheBlock != m_CacheBlocks.end ())
{
CacheBlock& block = lowestCacheBlock->second;
UNITY_FREE(kMemFile,block.data);
m_CacheBlocks.erase (lowestCacheBlock);
return true;
}
else
return false;
}
FileCacherRead::CacheBlock& FileCacherRead::AllocateCacheBlock (int block)
{
AssertIf (m_CacheBlocks.count (block));
CacheBlock cacheBlock;
cacheBlock.block = block;
cacheBlock.lockCount = 0;
cacheBlock.timeStamp = 0;
cacheBlock.data = (UInt8*)UNITY_MALLOC(MemLabelId(kMemFileId, m_RootHeader),m_CacheSize);
m_CacheBlocks[block] = cacheBlock;
return m_CacheBlocks[block];
}
void FileCacherRead::LockCacheBlock (int block, UInt8** startPos, UInt8** endPos)
{
CacheBlock* newCacheBlock;
CacheBlocks::iterator i = m_CacheBlocks.find (block);
if (i != m_CacheBlocks.end ())
newCacheBlock = &i->second;
else
{
// Make room for a new cache block
if (m_CacheBlocks.size () >= m_MaxCacheCount)
FreeSingleCache ();
newCacheBlock = &AllocateCacheBlock (block);
ReadCacheBlock (*newCacheBlock);
}
AssertIf (newCacheBlock->block != block);
newCacheBlock->timeStamp = ++m_TimeStamp;
newCacheBlock->lockCount++;
*startPos = newCacheBlock->data;
*endPos = newCacheBlock->data + min<int> (m_FileSize - block * GetCacheSize (), GetCacheSize ());
}
void FileCacherRead::UnlockCacheBlock (int block)
{
AssertIf (!m_CacheBlocks.count (block));
CacheBlock& cacheBlock = m_CacheBlocks.find (block)->second;
cacheBlock.lockCount--;
if (cacheBlock.lockCount == 0)
{
// LockCacheBlock sometimes locks more caches than m_MaxCacheCount
// Thus we should Free them as soon as they become unlocked
if (m_CacheBlocks.size () > m_MaxCacheCount)
FreeSingleCache ();
}
}
std::string FileCacherRead::GetPathName() const
{
return m_Path;
}
#if SUPPORT_SERIALIZE_WRITE
FileCacherWrite::FileCacherWrite ()
{
m_Success = true;
m_Block = -1;
m_Locked = false;
m_CacheSize = 0;
m_DataCache = NULL;
}
void FileCacherWrite::InitWriteFile (const std::string& pathName, size_t cacheSize)
{
m_Path = PathToAbsolutePath(pathName);
m_Success = true;
m_File.Open(m_Path, File::kWritePermission);
// file we're writing to always is a temporary, non-indexable file
SetFileFlags(m_Path, kAllFileFlags, kFileFlagDontIndex|kFileFlagTemporary);
m_Block = -1;
m_Locked = false;
m_CacheSize = cacheSize;
m_DataCache = (UInt8*)UNITY_MALLOC (kMemFile, m_CacheSize);
}
bool FileCacherWrite::CompleteWriting (size_t size)
{
Assert(m_Block != -1);
size_t remainingData = size - (m_Block * m_CacheSize);
Assert(remainingData <= m_CacheSize);
m_Success &= m_File.Write(m_Block * m_CacheSize, m_DataCache, remainingData);
return m_Success;
}
bool FileCacherWrite::WriteHeaderAndCloseFile (void* data, size_t position, size_t size)
{
Assert(position == 0);
if (size != 0)
m_Success &= m_File.Write(position, data, size);
m_Success &= m_File.Close();
return m_Success;
}
FileCacherWrite::~FileCacherWrite()
{
if (m_DataCache)
{
UNITY_FREE (kMemFile, m_DataCache);
m_DataCache = NULL;
}
m_File.Close();
}
void FileCacherWrite::LockCacheBlock (int block, UInt8** startPos, UInt8** endPos)
{
AssertIf (block == -1);
Assert (block == m_Block || m_Block+1 == block );
Assert (!m_Locked);
if (m_Block != block)
{
AssertIf (m_Locked != 0);
if (m_Block != -1)
m_Success &= m_File.Write(m_DataCache, m_CacheSize);
m_Block = block;
}
*startPos = m_DataCache;
*endPos = m_DataCache + m_CacheSize;
m_Locked++;
}
void FileCacherWrite::UnlockCacheBlock (int block)
{
Assert (block == m_Block);
Assert (m_Locked);
m_Locked = false;
}
std::string FileCacherWrite::GetPathName() const
{
return m_Path;
}
#endif // SUPPORT_SERIALIZE_WRITE
MemoryCacherReadBlocks::MemoryCacherReadBlocks (UInt8** blocks, int size, size_t cacheBlockSize)
: m_CacheBlockSize(cacheBlockSize)
, m_Memory(blocks)
, m_FileSize(size)
{
}
MemoryCacherReadBlocks::~MemoryCacherReadBlocks ()
{
}
void MemoryCacherReadBlocks::LockCacheBlock (int block, UInt8** startPos, UInt8** endPos)
{
/// VERIFY OUT OF BOUNDS!!! ???
AssertIf(block > m_FileSize / m_CacheBlockSize);
*startPos = m_Memory[block];
*endPos = *startPos + min<int> (GetFileLength () - block * m_CacheBlockSize, m_CacheBlockSize);
}
void MemoryCacherReadBlocks::DirectRead (void* data, size_t position, size_t size)
{
ReadFileCache(*this, data, position, size);
}
void ReadFileCache (CacheReaderBase& cacher, void* data, size_t position, size_t size)
{
UInt8 *cacheStart, *cacheEnd;
UInt8 *from, *fromClamped, *to, *toClamped;
int block = position / cacher.GetCacheSize ();
int lastBlock = (position + size - 1) / cacher.GetCacheSize ();
while (block <= lastBlock)
{
cacher.LockCacheBlock (block, &cacheStart, &cacheEnd);
// copy data from oldblock and unlock
from = cacheStart + (position - block * cacher.GetCacheSize ());
fromClamped = clamp (from, cacheStart, cacheEnd);
to = cacheStart + (position + size - block * cacher.GetCacheSize ());
toClamped = clamp<UInt8*> (to, cacheStart, cacheEnd);
memcpy ((UInt8*)data + (fromClamped - from), fromClamped, toClamped - fromClamped);
cacher.UnlockCacheBlock (block);
block++;
}
}
|