summaryrefslogtreecommitdiff
path: root/indra/newview/lltexturecache.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'indra/newview/lltexturecache.cpp')
-rw-r--r--indra/newview/lltexturecache.cpp94
1 files changed, 58 insertions, 36 deletions
diff --git a/indra/newview/lltexturecache.cpp b/indra/newview/lltexturecache.cpp
index 6a213309a0..8632890bbb 100644
--- a/indra/newview/lltexturecache.cpp
+++ b/indra/newview/lltexturecache.cpp
@@ -36,6 +36,7 @@
// Included to allow LLTextureCache::purgeTextures() to pause watchdog timeout
#include "llappviewer.h"
+#include "llmemory.h"
// Cache organization:
// cache/texture.entries
@@ -113,7 +114,7 @@ public:
~LLTextureCacheWorker()
{
llassert_always(!haveWork());
- delete[] mReadData;
+ FREE_MEM(LLImageBase::getPrivatePool(), mReadData);
}
// override this interface
@@ -215,7 +216,7 @@ bool LLTextureCacheLocalFileWorker::doRead()
mDataSize = 0;
return true;
}
- mReadData = new U8[mDataSize];
+ mReadData = (U8*)ALLOCATE_MEM(LLImageBase::getPrivatePool(), mDataSize);
mBytesRead = -1;
mBytesToRead = mDataSize;
setPriority(LLWorkerThread::PRIORITY_LOW | mPriority);
@@ -233,7 +234,7 @@ bool LLTextureCacheLocalFileWorker::doRead()
// << " Bytes: " << mDataSize << " Offset: " << mOffset
// << " / " << mDataSize << llendl;
mDataSize = 0; // failed
- delete[] mReadData;
+ FREE_MEM(LLImageBase::getPrivatePool(), mReadData);
mReadData = NULL;
}
return true;
@@ -248,7 +249,7 @@ bool LLTextureCacheLocalFileWorker::doRead()
{
mDataSize = local_size;
}
- mReadData = new U8[mDataSize];
+ mReadData = (U8*)ALLOCATE_MEM(LLImageBase::getPrivatePool(), mDataSize);
S32 bytes_read = LLAPRFile::readEx(mFileName, mReadData, mOffset, mDataSize, mCache->getLocalAPRFilePool());
@@ -258,7 +259,7 @@ bool LLTextureCacheLocalFileWorker::doRead()
// << " Bytes: " << mDataSize << " Offset: " << mOffset
// << " / " << mDataSize << llendl;
mDataSize = 0;
- delete[] mReadData;
+ FREE_MEM(LLImageBase::getPrivatePool(), mReadData);
mReadData = NULL;
}
else
@@ -326,6 +327,7 @@ bool LLTextureCacheRemoteWorker::doRead()
// First state / stage : find out if the file is local
if (mState == INIT)
{
+#if 0
std::string filename = mCache->getLocalFileName(mID);
// Is it a JPEG2000 file?
{
@@ -360,6 +362,11 @@ bool LLTextureCacheRemoteWorker::doRead()
}
// Determine the next stage: if we found a file, then LOCAL else CACHE
mState = (local_size > 0 ? LOCAL : CACHE);
+
+ llassert_always(mState == CACHE) ;
+#else
+ mState = CACHE;
+#endif
}
// Second state / stage : if the file is local, load it and leave
@@ -371,7 +378,7 @@ bool LLTextureCacheRemoteWorker::doRead()
mDataSize = local_size;
}
// Allocate read buffer
- mReadData = new U8[mDataSize];
+ mReadData = (U8*)ALLOCATE_MEM(LLImageBase::getPrivatePool(), mDataSize);
S32 bytes_read = LLAPRFile::readEx(local_filename,
mReadData, mOffset, mDataSize, mCache->getLocalAPRFilePool());
if (bytes_read != mDataSize)
@@ -380,7 +387,7 @@ bool LLTextureCacheRemoteWorker::doRead()
<< " Bytes: " << mDataSize << " Offset: " << mOffset
<< " / " << mDataSize << llendl;
mDataSize = 0;
- delete[] mReadData;
+ FREE_MEM(LLImageBase::getPrivatePool(), mReadData);
mReadData = NULL;
}
else
@@ -423,7 +430,7 @@ bool LLTextureCacheRemoteWorker::doRead()
S32 size = TEXTURE_CACHE_ENTRY_SIZE - mOffset;
size = llmin(size, mDataSize);
// Allocate the read buffer
- mReadData = new U8[size];
+ mReadData = (U8*)ALLOCATE_MEM(LLImageBase::getPrivatePool(), size);
S32 bytes_read = LLAPRFile::readEx(mCache->mHeaderDataFileName,
mReadData, offset, size, mCache->getLocalAPRFilePool());
if (bytes_read != size)
@@ -431,7 +438,7 @@ bool LLTextureCacheRemoteWorker::doRead()
llwarns << "LLTextureCacheWorker: " << mID
<< " incorrect number of bytes read from header: " << bytes_read
<< " / " << size << llendl;
- delete[] mReadData;
+ FREE_MEM(LLImageBase::getPrivatePool(), mReadData);
mReadData = NULL;
mDataSize = -1; // failed
done = true;
@@ -461,7 +468,7 @@ bool LLTextureCacheRemoteWorker::doRead()
S32 data_offset, file_size, file_offset;
// Reserve the whole data buffer first
- U8* data = new U8[mDataSize];
+ U8* data = (U8*)ALLOCATE_MEM(LLImageBase::getPrivatePool(), mDataSize);
// Set the data file pointers taking the read offset into account. 2 cases:
if (mOffset < TEXTURE_CACHE_ENTRY_SIZE)
@@ -474,7 +481,7 @@ bool LLTextureCacheRemoteWorker::doRead()
// Copy the raw data we've been holding from the header cache into the new sized buffer
llassert_always(mReadData);
memcpy(data, mReadData, data_offset);
- delete[] mReadData;
+ FREE_MEM(LLImageBase::getPrivatePool(), mReadData);
mReadData = NULL;
}
else
@@ -500,7 +507,7 @@ bool LLTextureCacheRemoteWorker::doRead()
llwarns << "LLTextureCacheWorker: " << mID
<< " incorrect number of bytes read from body: " << bytes_read
<< " / " << file_size << llendl;
- delete[] mReadData;
+ FREE_MEM(LLImageBase::getPrivatePool(), mReadData);
mReadData = NULL;
mDataSize = -1; // failed
done = true;
@@ -592,11 +599,11 @@ bool LLTextureCacheRemoteWorker::doWrite()
{
// We need to write a full record in the header cache so, if the amount of data is smaller
// than a record, we need to transfer the data to a buffer padded with 0 and write that
- U8* padBuffer = new U8[TEXTURE_CACHE_ENTRY_SIZE];
+ U8* padBuffer = (U8*)ALLOCATE_MEM(LLImageBase::getPrivatePool(), TEXTURE_CACHE_ENTRY_SIZE);
memset(padBuffer, 0, TEXTURE_CACHE_ENTRY_SIZE); // Init with zeros
memcpy(padBuffer, mWriteData, mDataSize); // Copy the write buffer
bytes_written = LLAPRFile::writeEx(mCache->mHeaderDataFileName, padBuffer, offset, size, mCache->getLocalAPRFilePool());
- delete [] padBuffer;
+ FREE_MEM(LLImageBase::getPrivatePool(), padBuffer);
}
else
{
@@ -692,7 +699,7 @@ void LLTextureCacheWorker::finishWork(S32 param, bool completed)
}
else
{
- delete[] mReadData;
+ FREE_MEM(LLImageBase::getPrivatePool(), mReadData);
mReadData = NULL;
}
}
@@ -753,7 +760,7 @@ LLTextureCache::~LLTextureCache()
//////////////////////////////////////////////////////////////////////////////
//virtual
-S32 LLTextureCache::update(U32 max_time_ms)
+S32 LLTextureCache::update(F32 max_time_ms)
{
static LLFrameTimer timer ;
static const F32 MAX_TIME_INTERVAL = 300.f ; //seconds.
@@ -943,7 +950,7 @@ S64 LLTextureCache::initCache(ELLPath location, S64 max_size, BOOL texture_cache
max_size -= sCacheMaxTexturesSize;
LL_INFOS("TextureCache") << "Headers: " << sCacheMaxEntries
- << " Textures size: " << sCacheMaxTexturesSize/(1024*1024) << " MB" << LL_ENDL;
+ << " Textures size: " << sCacheMaxTexturesSize / (1024 * 1024) << " MB" << LL_ENDL;
setDirNames(location);
@@ -1413,22 +1420,21 @@ void LLTextureCache::readHeaderCache()
}
}
}
- if (num_entries > sCacheMaxEntries)
+ if (num_entries - empty_entries > sCacheMaxEntries)
{
// Special case: cache size was reduced, need to remove entries
// Note: After we prune entries, we will call this again and create the LRU
- U32 entries_to_purge = (num_entries-empty_entries) - sCacheMaxEntries;
+ U32 entries_to_purge = (num_entries - empty_entries) - sCacheMaxEntries;
llinfos << "Texture Cache Entries: " << num_entries << " Max: " << sCacheMaxEntries << " Empty: " << empty_entries << " Purging: " << entries_to_purge << llendl;
- if (entries_to_purge > 0)
+ // We can exit the following loop with the given condition, since if we'd reach the end of the lru set we'd have:
+ // purge_list.size() = lru.size() = num_entries - empty_entries = entries_to_purge + sCacheMaxEntries >= entries_to_purge
+ // So, it's certain that iter will never reach lru.end() first.
+ std::set<lru_data_t>::iterator iter = lru.begin();
+ while (purge_list.size() < entries_to_purge)
{
- for (std::set<lru_data_t>::iterator iter = lru.begin(); iter != lru.end(); ++iter)
- {
- purge_list.insert(iter->second);
- if (purge_list.size() >= entries_to_purge)
- break;
- }
+ purge_list.insert(iter->second);
+ ++iter;
}
- llassert_always(purge_list.size() >= entries_to_purge);
}
else
{
@@ -1508,12 +1514,12 @@ void LLTextureCache::purgeAllTextures(bool purge_directories)
{
const char* subdirs = "0123456789abcdef";
std::string delem = gDirUtilp->getDirDelimiter();
- std::string mask = delem + "*";
+ std::string mask = "*";
for (S32 i=0; i<16; i++)
{
std::string dirname = mTexturesDirName + delem + subdirs[i];
llinfos << "Deleting files in directory: " << dirname << llendl;
- gDirUtilp->deleteFilesInDir(dirname,mask);
+ gDirUtilp->deleteFilesInDir(dirname, mask);
if (purge_directories)
{
LLFile::rmdir(dirname);
@@ -1592,7 +1598,7 @@ void LLTextureCache::purgeTextures(bool validate)
if (validate)
{
validate_idx = gSavedSettings.getU32("CacheValidateCounter");
- U32 next_idx = (++validate_idx) % 256;
+ U32 next_idx = (validate_idx + 1) % 256;
gSavedSettings.setU32("CacheValidateCounter", next_idx);
LL_DEBUGS("TextureCache") << "TEXTURE CACHE: Validating: " << validate_idx << LL_ENDL;
}
@@ -1635,8 +1641,8 @@ void LLTextureCache::purgeTextures(bool validate)
{
purge_count++;
LL_DEBUGS("TextureCache") << "PURGING: " << filename << LL_ENDL;
- removeEntry(idx, entries[idx], filename) ;
cache_size -= entries[idx].mBodySize;
+ removeEntry(idx, entries[idx], filename) ;
}
}
@@ -1650,7 +1656,7 @@ void LLTextureCache::purgeTextures(bool validate)
LL_INFOS("TextureCache") << "TEXTURE CACHE:"
<< " PURGED: " << purge_count
<< " ENTRIES: " << num_entries
- << " CACHE SIZE: " << mTexturesSizeTotal / 1024*1024 << " MB"
+ << " CACHE SIZE: " << mTexturesSizeTotal / (1024 * 1024) << " MB"
<< llendl;
}
@@ -1858,18 +1864,34 @@ void LLTextureCache::removeCachedTexture(const LLUUID& id)
//called after mHeaderMutex is locked.
void LLTextureCache::removeEntry(S32 idx, Entry& entry, std::string& filename)
{
+ bool file_maybe_exists = true; // Always attempt to remove when idx is invalid.
+
if(idx >= 0) //valid entry
{
+ if (entry.mBodySize == 0) // Always attempt to remove when mBodySize > 0.
+ {
+ if (LLAPRFile::isExist(filename, getLocalAPRFilePool())) // Sanity check. Shouldn't exist when body size is 0.
+ {
+ LL_WARNS("TextureCache") << "Entry has body size of zero but file " << filename << " exists. Deleting this file, too." << LL_ENDL;
+ }
+ else
+ {
+ file_maybe_exists = false;
+ }
+ }
+ mTexturesSizeTotal -= entry.mBodySize;
+
entry.mImageSize = -1;
entry.mBodySize = 0;
mHeaderIDMap.erase(entry.mID);
- mTexturesSizeMap.erase(entry.mID);
-
- mTexturesSizeTotal -= entry.mBodySize;
+ mTexturesSizeMap.erase(entry.mID);
mFreeList.insert(idx);
}
- LLAPRFile::remove(filename, getLocalAPRFilePool());
+ if (file_maybe_exists)
+ {
+ LLAPRFile::remove(filename, getLocalAPRFilePool());
+ }
}
bool LLTextureCache::removeFromCache(const LLUUID& id)