From b594d3b04d3095f15750436910debdd5a602a872 Mon Sep 17 00:00:00 2001
From: Xiaohong Bao <bao@lindenlab.com>
Date: Tue, 10 May 2011 21:02:20 -0600
Subject: [PATCH] add debug mode to track the memory allocation/deallocation.

---
 indra/llcommon/llmemory.cpp       | 117 +++++++++++++++++++++++++++---
 indra/llcommon/llmemory.h         |  24 +++++-
 indra/llimage/llimage.cpp         |  36 ++-------
 indra/llimage/llimage.h           |   3 +-
 indra/llimage/llimagedxt.cpp      |   2 +-
 indra/llimage/llimagej2c.cpp      |   4 +-
 indra/llrender/llvertexbuffer.cpp |  24 +++---
 indra/newview/lltexturecache.cpp  |  30 ++++----
 indra/newview/lltexturefetch.cpp  |  14 ++--
 9 files changed, 173 insertions(+), 81 deletions(-)

diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp
index dfc00b5e0ae..8f65107e470 100644
--- a/indra/llcommon/llmemory.cpp
+++ b/indra/llcommon/llmemory.cpp
@@ -57,6 +57,10 @@ U32 LLMemory::sAllocatedPageSizeInKB = 0 ;
 U32 LLMemory::sMaxHeapSizeInKB = U32_MAX ;
 BOOL LLMemory::sEnableMemoryFailurePrevention = FALSE;
 
+#if __DEBUG_PRIVATE_MEM__
+LLPrivateMemoryPoolManager::mem_allocation_info_t LLPrivateMemoryPoolManager::sMemAllocationTracker;
+#endif
+
 //static
 void LLMemory::initClass()
 {
@@ -1431,8 +1435,14 @@ S32  LLPrivateMemoryPool::getChunkIndex(U32 size)
 void  LLPrivateMemoryPool::destroyPool()
 {
 	lock() ;
-	if(mNumOfChunks > 0)
+
+#if 0
+	if(mNumOfChunks > 0) 
 	{
+		//Warn:
+		//should crash here because there is memory leaking if reach here.
+		//
+
 		for(U32 i = 0 ; i < mHashFactor; i++)
 		{
 			while(mChunkHashList[i])
@@ -1441,11 +1451,19 @@ void  LLPrivateMemoryPool::destroyPool()
 			}
 		}
 	}
-	mChunkHashList.clear() ;
-	mHashFactor = 1 ;
+	
 	llassert_always(mNumOfChunks == 0) ;
 	llassert_always(mReservedPoolSize == 0) ;
+#endif
+
+	if(mNumOfChunks > 0)
+	{
+		llwarns << "There is some memory not freed when destroy the memory pool!" << llendl ;
+	}
 
+	mNumOfChunks = 0 ;
+	mChunkHashList.clear() ;
+	mHashFactor = 1 ;
 	for(S32 i = 0 ; i < SUPER_ALLOCATION ; i++)
 	{
 		mChunkList[i] = NULL ;
@@ -1750,6 +1768,21 @@ LLPrivateMemoryPoolManager::LLPrivateMemoryPoolManager()
 
 LLPrivateMemoryPoolManager::~LLPrivateMemoryPoolManager() 
 {
+
+#if __DEBUG_PRIVATE_MEM__
+	if(!sMemAllocationTracker.empty())
+	{
+		llwarns << "there is potential memory leaking here. The list of not freed memory blocks are from: " <<llendl ;
+
+		S32 k = 0 ;
+		for(mem_allocation_info_t::iterator iter = sMemAllocationTracker.begin() ; iter != sMemAllocationTracker.end() ; ++iter)
+		{
+			llinfos << k++ << ", " << iter->second << llendl ;
+		}
+		sMemAllocationTracker.clear() ;
+	}
+#endif
+
 #if 0
 	//all private pools should be released by their owners before reaching here.
 	for(S32 i = 0 ; i < LLPrivateMemoryPool::MAX_TYPES; i++)
@@ -1827,6 +1860,70 @@ void LLPrivateMemoryPoolManager::updateStatistics()
 	}
 }
 
+#if __DEBUG_PRIVATE_MEM__
+//static 
+char* LLPrivateMemoryPoolManager::allocate(LLPrivateMemoryPool* poolp, U32 size, const char* function, const int line) 
+{
+	char* p ;
+
+	if(!poolp)
+	{
+		p = new char[size] ;
+	}
+	else
+	{
+		p = poolp->allocate(size) ;
+	}
+
+	if(p)
+	{
+		char num[16] ;
+		sprintf(num, " line: %d ", line) ;
+		std::string str(function) ;
+		str += num; 
+
+		sMemAllocationTracker[p] = str ;
+	}
+
+	return p ;
+}	
+#else
+//static 
+char* LLPrivateMemoryPoolManager::allocate(LLPrivateMemoryPool* poolp, U32 size) 
+{
+	if(!poolp)
+	{
+		return new char[size] ;
+	}
+	else
+	{
+		return poolp->allocate(size) ;
+	}
+}
+#endif
+
+//static 
+void  LLPrivateMemoryPoolManager::freeMem(LLPrivateMemoryPool* poolp, void* addr) 
+{
+	if(!addr)
+	{
+		return ;
+	}
+
+#if __DEBUG_PRIVATE_MEM__
+	sMemAllocationTracker.erase((char*)addr) ;
+#endif
+
+	if(poolp)
+	{
+		poolp->freeMem(addr) ;
+	}
+	else
+	{
+		delete[] addr ;
+	}
+}
+
 //--------------------------------------------------------------------
 //class LLPrivateMemoryPoolTester
 //--------------------------------------------------------------------
@@ -1915,7 +2012,7 @@ void LLPrivateMemoryPoolTester::test(U32 min_size, U32 max_size, U32 stride, U32
 		for(j = 0 ; j < levels; j++) 
 		{
 			size = min_size + j * stride ;
-			p[i][j] = sPool->allocate(size) ;
+			p[i][j] = ALLOCATE_MEM(sPool, size) ;
 
 			total_allocated_size+= size ;
 
@@ -1931,7 +2028,7 @@ void LLPrivateMemoryPoolTester::test(U32 min_size, U32 max_size, U32 stride, U32
 				if(p[i][k])
 				{
 					llassert_always(*(U32*)p[i][k] == i && *((U32*)p[i][k] + 1) == k) ;
-					sPool->freeMem(p[i][k]) ;
+					FREE_MEM(sPool, p[i][k]) ;
 					total_allocated_size -= min_size + k * stride ;
 					p[i][k] = NULL ;
 				}
@@ -1952,7 +2049,7 @@ void LLPrivateMemoryPoolTester::test(U32 min_size, U32 max_size, U32 stride, U32
 			if(p[i][j])
 			{
 				llassert_always(*(U32*)p[i][j] == i && *((U32*)p[i][j] + 1) == j) ;
-				sPool->freeMem(p[i][j]) ;
+				FREE_MEM(sPool, p[i][j]) ;
 				total_allocated_size -= min_size + j * stride ;
 				p[i][j] = NULL ;
 			}
@@ -1977,7 +2074,7 @@ void LLPrivateMemoryPoolTester::testAndTime(U32 size, U32 times)
 	//allocation
 	for(U32 i = 0 ; i < times; i++)
 	{
-		p[i] = sPool->allocate(size) ;
+		p[i] = ALLOCATE_MEM(sPool, size) ;
 		if(!p[i])
 		{
 			llerrs << "allocation failed" << llendl ;
@@ -1986,7 +2083,7 @@ void LLPrivateMemoryPoolTester::testAndTime(U32 size, U32 times)
 	//de-allocation
 	for(U32 i = 0 ; i < times; i++)
 	{
-		sPool->freeMem(p[i]) ;
+		FREE_MEM(sPool, p[i]) ;
 		p[i] = NULL ;
 	}
 	llinfos << "time spent using customized memory pool: " << timer.getElapsedTimeF32() << llendl ;
@@ -2020,8 +2117,8 @@ void LLPrivateMemoryPoolTester::correctnessTest()
 	//to see if allocation is right.
 	
 	//edge case
-	char* p = sPool->allocate(0) ;
-	sPool->freeMem(p) ;
+	char* p = ALLOCATE_MEM(sPool, 0) ;
+	FREE_MEM(sPool, p) ;
 
 	//small sized
 	// [8 bytes, 2KB), each asks for 256 allocations and deallocations
diff --git a/indra/llcommon/llmemory.h b/indra/llcommon/llmemory.h
index 001ff9c1236..d50ae998232 100644
--- a/indra/llcommon/llmemory.h
+++ b/indra/llcommon/llmemory.h
@@ -34,6 +34,10 @@ extern S32 gDACount;
 extern void* ll_allocate (size_t size);
 extern void ll_release (void *p);
 
+#ifndef __DEBUG_PRIVATE_MEM__
+#define __DEBUG_PRIVATE_MEM__  0
+#endif
+
 class LL_COMMON_API LLMemory
 {
 public:
@@ -234,7 +238,6 @@ class LL_COMMON_API LLPrivateMemoryPool
 	LLPrivateMemoryPool(S32 type) ;
 	~LLPrivateMemoryPool() ;
 
-public:
 	char *allocate(U32 size) ;
 	void  freeMem(void* addr) ;
 	
@@ -314,8 +317,27 @@ class LL_COMMON_API LLPrivateMemoryPoolManager
 
 	U32 mTotalReservedSize ;
 	U32 mTotalAllocatedSize ;
+
+public:
+#if __DEBUG_PRIVATE_MEM__
+	static char* allocate(LLPrivateMemoryPool* poolp, U32 size, const char* function, const int line) ;	
+	
+	typedef std::map<char*, std::string> mem_allocation_info_t ;
+	static mem_allocation_info_t sMemAllocationTracker;
+#else
+	static char* allocate(LLPrivateMemoryPool* poolp, U32 size) ;	
+#endif
+	static void  freeMem(LLPrivateMemoryPool* poolp, void* addr) ;
 };
 
+//-------------------------------------------------------------------------------------
+#if __DEBUG_PRIVATE_MEM__
+#define ALLOCATE_MEM(poolp, size) LLPrivateMemoryPoolManager::allocate((poolp), (size), __FUNCTION__, __LINE__)
+#else
+#define ALLOCATE_MEM(poolp, size) LLPrivateMemoryPoolManager::allocate((poolp), (size))
+#endif
+#define FREE_MEM(poolp, addr) LLPrivateMemoryPoolManager::freeMem((poolp), (addr))
+//-------------------------------------------------------------------------------------
 //
 //the below singleton is used to test the private memory pool.
 //
diff --git a/indra/llimage/llimage.cpp b/indra/llimage/llimage.cpp
index eefcf0a9fbc..cfa4123b1e5 100644
--- a/indra/llimage/llimage.cpp
+++ b/indra/llimage/llimage.cpp
@@ -122,32 +122,6 @@ void LLImageBase::destroyPrivatePool()
 	}
 }
 
-//static
-char* LLImageBase::allocateMemory(S32 size) 
-{
-	if(sPrivatePoolp)
-	{
-		return sPrivatePoolp->allocate(size) ;
-	}
-	else
-	{
-		return new char[size];
-	}
-}
-
-//static
-void  LLImageBase::deleteMemory(void* p) 
-{
-	if(sPrivatePoolp)
-	{
-		sPrivatePoolp->freeMem(p) ;
-	}
-	else
-	{
-		delete[] (char*)p ;
-	}
-}
-
 // virtual
 void LLImageBase::dump()
 {
@@ -181,7 +155,7 @@ void LLImageBase::sanityCheck()
 // virtual
 void LLImageBase::deleteData()
 {
-	deleteMemory(mData) ;
+	FREE_MEM(sPrivatePoolp, mData) ;
 	mData = NULL;
 	mDataSize = 0;
 }
@@ -218,7 +192,7 @@ U8* LLImageBase::allocateData(S32 size)
 	{
 		deleteData(); // virtual
 		mBadBufferAllocation = false ;
-		mData = (U8*)allocateMemory(size);
+		mData = (U8*)ALLOCATE_MEM(sPrivatePoolp, size);
 		if (!mData)
 		{
 			llwarns << "allocate image data: " << size << llendl;
@@ -236,7 +210,7 @@ U8* LLImageBase::allocateData(S32 size)
 U8* LLImageBase::reallocateData(S32 size)
 {
 	LLMemType mt1(mMemType);
-	U8 *new_datap = (U8*)allocateMemory(size);
+	U8 *new_datap = (U8*)ALLOCATE_MEM(sPrivatePoolp, size);
 	if (!new_datap)
 	{
 		llwarns << "Out of memory in LLImageBase::reallocateData" << llendl;
@@ -246,7 +220,7 @@ U8* LLImageBase::reallocateData(S32 size)
 	{
 		S32 bytes = llmin(mDataSize, size);
 		memcpy(new_datap, mData, bytes);	/* Flawfinder: ignore */
-		deleteMemory(mData) ;
+		FREE_MEM(sPrivatePoolp, mData) ;
 	}
 	mData = new_datap;
 	mDataSize = size;
@@ -1601,7 +1575,7 @@ void LLImageFormatted::appendData(U8 *data, S32 size)
 			S32 newsize = cursize + size;
 			reallocateData(newsize);
 			memcpy(getData() + cursize, data, size);
-			deleteMemory(data);
+			FREE_MEM(LLImageBase::getPrivatePool(), data);
 		}
 	}
 }
diff --git a/indra/llimage/llimage.h b/indra/llimage/llimage.h
index ab20ccda9eb..10621623ad9 100644
--- a/indra/llimage/llimage.h
+++ b/indra/llimage/llimage.h
@@ -140,8 +140,7 @@ class LLImageBase : public LLThreadSafeRefCount
 	
 	static void createPrivatePool() ;
 	static void destroyPrivatePool() ;
-	static char* allocateMemory(S32 size) ;
-	static void  deleteMemory(void* p) ;	
+	static LLPrivateMemoryPool* getPrivatePool() {return sPrivatePoolp;}
 
 private:
 	U8 *mData;
diff --git a/indra/llimage/llimagedxt.cpp b/indra/llimage/llimagedxt.cpp
index 81be09a412c..2867f5e6f0a 100644
--- a/indra/llimage/llimagedxt.cpp
+++ b/indra/llimage/llimagedxt.cpp
@@ -429,7 +429,7 @@ bool LLImageDXT::convertToDXR()
 	S32 nmips = calcNumMips(width,height);
 	S32 total_bytes = getDataSize();
 	U8* olddata = getData();
-	U8* newdata = (U8*)allocateMemory(total_bytes);
+	U8* newdata = (U8*)ALLOCATE_MEM(LLImageBase::getPrivatePool(), total_bytes);
 	if (!newdata)
 	{
 		llerrs << "Out of memory in LLImageDXT::convertToDXR()" << llendl;
diff --git a/indra/llimage/llimagej2c.cpp b/indra/llimage/llimagej2c.cpp
index 1bdcba6eb57..78e5d58f148 100644
--- a/indra/llimage/llimagej2c.cpp
+++ b/indra/llimage/llimagej2c.cpp
@@ -372,14 +372,14 @@ BOOL LLImageJ2C::loadAndValidate(const std::string &filename)
 	}
 	else
 	{
-		U8 *data = (U8*)allocateMemory(file_size);
+		U8 *data = (U8*)ALLOCATE_MEM(LLImageBase::getPrivatePool(), file_size);
 		apr_size_t bytes_read = file_size;
 		apr_status_t s = apr_file_read(apr_file, data, &bytes_read); // modifies bytes_read	
 		infile.close() ;
 
 		if (s != APR_SUCCESS || (S32)bytes_read != file_size)
 		{
-			deleteMemory(data);
+			FREE_MEM(LLImageBase::getPrivatePool(), data);
 			setLastError("Unable to read entire file");
 			res = FALSE;
 		}
diff --git a/indra/llrender/llvertexbuffer.cpp b/indra/llrender/llvertexbuffer.cpp
index fd2a04373bd..67417aea439 100644
--- a/indra/llrender/llvertexbuffer.cpp
+++ b/indra/llrender/llvertexbuffer.cpp
@@ -544,7 +544,7 @@ void LLVertexBuffer::createGLBuffer()
 	{
 		static int gl_buffer_idx = 0;
 		mGLBuffer = ++gl_buffer_idx;
-		mMappedData = (U8*)sPrivatePoolp->allocate(size);
+		mMappedData = (U8*)ALLOCATE_MEM(sPrivatePoolp, size);
 		memset(mMappedData, 0, size);
 	}
 }
@@ -574,7 +574,7 @@ void LLVertexBuffer::createGLIndices()
 	}
 	else
 	{
-		mMappedIndexData = (U8*)sPrivatePoolp->allocate(size);
+		mMappedIndexData = (U8*)ALLOCATE_MEM(sPrivatePoolp, size);
 		memset(mMappedIndexData, 0, size);
 		static int gl_buffer_idx = 0;
 		mGLIndices = ++gl_buffer_idx;
@@ -598,7 +598,7 @@ void LLVertexBuffer::destroyGLBuffer()
 		}
 		else
 		{
-			sPrivatePoolp->freeMem(mMappedData) ;
+			FREE_MEM(sPrivatePoolp, mMappedData) ;
 			mMappedData = NULL;
 			mEmpty = TRUE;
 		}
@@ -627,7 +627,7 @@ void LLVertexBuffer::destroyGLIndices()
 		}
 		else
 		{
-			sPrivatePoolp->freeMem(mMappedIndexData) ;
+			FREE_MEM(sPrivatePoolp, mMappedIndexData) ;
 			mMappedIndexData = NULL;
 			mEmpty = TRUE;
 		}
@@ -759,7 +759,7 @@ void LLVertexBuffer::resizeBuffer(S32 newnverts, S32 newnindices)
 				if (!useVBOs())
 				{
 					U8* old = mMappedData;
-					mMappedData = (U8*)sPrivatePoolp->allocate(newsize);
+					mMappedData = (U8*)ALLOCATE_MEM(sPrivatePoolp, newsize);
 					if (old)
 					{	
 						memcpy(mMappedData, old, llmin(newsize, oldsize));
@@ -768,7 +768,7 @@ void LLVertexBuffer::resizeBuffer(S32 newnverts, S32 newnindices)
 							memset(mMappedData+oldsize, 0, newsize-oldsize);
 						}
 
-						sPrivatePoolp->freeMem(old);
+						FREE_MEM(sPrivatePoolp, old);
 					}
 					else
 					{
@@ -796,7 +796,7 @@ void LLVertexBuffer::resizeBuffer(S32 newnverts, S32 newnindices)
 				{
 					//delete old buffer, keep GL buffer for now
 					U8* old = mMappedIndexData;
-					mMappedIndexData = (U8*)sPrivatePoolp->allocate(new_index_size);
+					mMappedIndexData = (U8*)ALLOCATE_MEM(sPrivatePoolp, new_index_size);
 					
 					if (old)
 					{	
@@ -805,7 +805,7 @@ void LLVertexBuffer::resizeBuffer(S32 newnverts, S32 newnindices)
 						{
 							memset(mMappedIndexData+old_index_size, 0, new_index_size - old_index_size);
 						}
-						sPrivatePoolp->freeMem(old);
+						FREE_MEM(sPrivatePoolp, old);
 					}
 					else
 					{
@@ -852,8 +852,8 @@ void LLVertexBuffer::freeClientBuffer()
 {
 	if(useVBOs() && sDisableVBOMapping && (mMappedData || mMappedIndexData))
 	{
-		sPrivatePoolp->freeMem(mMappedData) ;
-		sPrivatePoolp->freeMem(mMappedIndexData) ;
+		FREE_MEM(sPrivatePoolp, mMappedData) ;
+		FREE_MEM(sPrivatePoolp, mMappedIndexData) ;
 		mMappedData = NULL ;
 		mMappedIndexData = NULL ;
 	}
@@ -864,7 +864,7 @@ void LLVertexBuffer::allocateClientVertexBuffer()
 	if(!mMappedData)
 	{
 		U32 size = getSize() ;
-		mMappedData = (U8*)sPrivatePoolp->allocate(size);
+		mMappedData = (U8*)ALLOCATE_MEM(sPrivatePoolp, size);
 		memset(mMappedData, 0, size);
 	}
 }
@@ -874,7 +874,7 @@ void LLVertexBuffer::allocateClientIndexBuffer()
 	if(!mMappedIndexData)
 	{
 		U32 size = getIndicesSize();
-		mMappedIndexData = (U8*)sPrivatePoolp->allocate(size);
+		mMappedIndexData = (U8*)ALLOCATE_MEM(sPrivatePoolp, size);
 		memset(mMappedIndexData, 0, size);
 	}
 }
diff --git a/indra/newview/lltexturecache.cpp b/indra/newview/lltexturecache.cpp
index 11dff69e0c6..b6e396a96f7 100644
--- a/indra/newview/lltexturecache.cpp
+++ b/indra/newview/lltexturecache.cpp
@@ -113,7 +113,7 @@ class LLTextureCacheWorker : public LLWorkerClass
 	~LLTextureCacheWorker()
 	{
 		llassert_always(!haveWork());
-		LLImageBase::deleteMemory(mReadData);
+		FREE_MEM(LLImageBase::getPrivatePool(), mReadData);
 	}
 
 	// override this interface
@@ -215,7 +215,7 @@ bool LLTextureCacheLocalFileWorker::doRead()
 			mDataSize = 0;
 			return true;
 		}
-		mReadData = (U8*)LLImageBase::allocateMemory(mDataSize);
+		mReadData = (U8*)ALLOCATE_MEM(LLImageBase::getPrivatePool(), mDataSize);
 		mBytesRead = -1;
 		mBytesToRead = mDataSize;
 		setPriority(LLWorkerThread::PRIORITY_LOW | mPriority);
@@ -233,7 +233,7 @@ bool LLTextureCacheLocalFileWorker::doRead()
 // 						<< " Bytes: " << mDataSize << " Offset: " << mOffset
 // 						<< " / " << mDataSize << llendl;
 				mDataSize = 0; // failed
-				LLImageBase::deleteMemory(mReadData);
+				FREE_MEM(LLImageBase::getPrivatePool(), mReadData);
 				mReadData = NULL;
 			}
 			return true;
@@ -248,7 +248,7 @@ bool LLTextureCacheLocalFileWorker::doRead()
 	{
 		mDataSize = local_size;
 	}
-	mReadData = (U8*)LLImageBase::allocateMemory(mDataSize);
+	mReadData = (U8*)ALLOCATE_MEM(LLImageBase::getPrivatePool(), mDataSize);
 	
 	S32 bytes_read = LLAPRFile::readEx(mFileName, mReadData, mOffset, mDataSize);
 
@@ -258,7 +258,7 @@ bool LLTextureCacheLocalFileWorker::doRead()
 // 				<< " Bytes: " << mDataSize << " Offset: " << mOffset
 // 				<< " / " << mDataSize << llendl;
 		mDataSize = 0;
-		LLImageBase::deleteMemory(mReadData);
+		FREE_MEM(LLImageBase::getPrivatePool(), mReadData);
 		mReadData = NULL;
 	}
 	else
@@ -377,7 +377,7 @@ bool LLTextureCacheRemoteWorker::doRead()
 			mDataSize = local_size;
 		}
 		// Allocate read buffer
-		mReadData = (U8*)LLImageBase::allocateMemory(mDataSize);
+		mReadData = (U8*)ALLOCATE_MEM(LLImageBase::getPrivatePool(), mDataSize);
 		S32 bytes_read = LLAPRFile::readEx(local_filename, mReadData, mOffset, mDataSize);
 		if (bytes_read != mDataSize)
 		{
@@ -385,7 +385,7 @@ bool LLTextureCacheRemoteWorker::doRead()
  					<< " Bytes: " << mDataSize << " Offset: " << mOffset
  					<< " / " << mDataSize << llendl;
 			mDataSize = 0;
-			LLImageBase::deleteMemory(mReadData);
+			FREE_MEM(LLImageBase::getPrivatePool(), mReadData);
 			mReadData = NULL;
 		}
 		else
@@ -428,14 +428,14 @@ bool LLTextureCacheRemoteWorker::doRead()
 		S32 size = TEXTURE_CACHE_ENTRY_SIZE - mOffset;
 		size = llmin(size, mDataSize);
 		// Allocate the read buffer
-		mReadData = (U8*)LLImageBase::allocateMemory(size);
+		mReadData = (U8*)ALLOCATE_MEM(LLImageBase::getPrivatePool(), size);
 		S32 bytes_read = LLAPRFile::readEx(mCache->mHeaderDataFileName, mReadData, offset, size);
 		if (bytes_read != size)
 		{
 			llwarns << "LLTextureCacheWorker: "  << mID
 					<< " incorrect number of bytes read from header: " << bytes_read
 					<< " / " << size << llendl;
-			LLImageBase::deleteMemory(mReadData);
+			FREE_MEM(LLImageBase::getPrivatePool(), mReadData);
 			mReadData = NULL;
 			mDataSize = -1; // failed
 			done = true;
@@ -465,7 +465,7 @@ bool LLTextureCacheRemoteWorker::doRead()
 			S32 data_offset, file_size, file_offset;
 			
 			// Reserve the whole data buffer first
-			U8* data = (U8*)LLImageBase::allocateMemory(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)
@@ -478,7 +478,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);
-				LLImageBase::deleteMemory(mReadData);
+				FREE_MEM(LLImageBase::getPrivatePool(), mReadData);
 				mReadData = NULL;
 			}
 			else
@@ -503,7 +503,7 @@ bool LLTextureCacheRemoteWorker::doRead()
 				llwarns << "LLTextureCacheWorker: "  << mID
 						<< " incorrect number of bytes read from body: " << bytes_read
 						<< " / " << file_size << llendl;
-				LLImageBase::deleteMemory(mReadData);
+				FREE_MEM(LLImageBase::getPrivatePool(), mReadData);
 				mReadData = NULL;
 				mDataSize = -1; // failed
 				done = true;
@@ -595,11 +595,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 = (U8*)LLImageBase::allocateMemory(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);
-			LLImageBase::deleteMemory(padBuffer);
+			FREE_MEM(LLImageBase::getPrivatePool(), padBuffer);
 		}
 		else
 		{
@@ -694,7 +694,7 @@ void LLTextureCacheWorker::finishWork(S32 param, bool completed)
 			}
 			else
 			{
-				LLImageBase::deleteMemory(mReadData);
+				FREE_MEM(LLImageBase::getPrivatePool(), mReadData);
 				mReadData = NULL;
 			}
 		}
diff --git a/indra/newview/lltexturefetch.cpp b/indra/newview/lltexturefetch.cpp
index 806f1304866..e9be45ffd0c 100644
--- a/indra/newview/lltexturefetch.cpp
+++ b/indra/newview/lltexturefetch.cpp
@@ -816,7 +816,7 @@ void LLTextureFetchWorker::setImagePriority(F32 priority)
 
 void LLTextureFetchWorker::resetFormattedData()
 {
-	LLImageBase::deleteMemory(mBuffer);
+	FREE_MEM(LLImageBase::getPrivatePool(), mBuffer);
 	mBuffer = NULL;
 	mBufferSize = 0;
 	if (mFormattedImage.notNull())
@@ -887,7 +887,7 @@ bool LLTextureFetchWorker::doWork(S32 param)
 		mSentRequest = UNSENT;
 		mDecoded  = FALSE;
 		mWritten  = FALSE;
-		LLImageBase::deleteMemory(mBuffer);
+		FREE_MEM(LLImageBase::getPrivatePool(), mBuffer);
 		mBuffer = NULL;
 		mBufferSize = 0;
 		mHaveAllData = FALSE;
@@ -1283,7 +1283,7 @@ bool LLTextureFetchWorker::doWork(S32 param)
 			llassert_always(mBufferSize == cur_size + mRequestedSize);
 			if(!mBufferSize)//no data received.
 			{
-				LLImageBase::deleteMemory(mBuffer); 
+				FREE_MEM(LLImageBase::getPrivatePool(), mBuffer); 
 				mBuffer = NULL;
 
 				//abort.
@@ -1311,7 +1311,7 @@ bool LLTextureFetchWorker::doWork(S32 param)
 				mFileSize = mBufferSize + 1 ; //flag the file is not fully loaded.
 			}
 			
-			U8* buffer = (U8*)LLImageBase::allocateMemory(mBufferSize);
+			U8* buffer = (U8*)ALLOCATE_MEM(LLImageBase::getPrivatePool(), mBufferSize);
 			if (cur_size > 0)
 			{
 				memcpy(buffer, mFormattedImage->getData(), cur_size);
@@ -1320,7 +1320,7 @@ bool LLTextureFetchWorker::doWork(S32 param)
 			// NOTE: setData releases current data and owns new data (buffer)
 			mFormattedImage->setData(buffer, mBufferSize);
 			// delete temp data
-			LLImageBase::deleteMemory(mBuffer); // Note: not 'buffer' (assigned in setData())
+			FREE_MEM(LLImageBase::getPrivatePool(), mBuffer); // Note: not 'buffer' (assigned in setData())
 			mBuffer = NULL;
 			mBufferSize = 0;
 			mLoadedDiscard = mRequestedDiscard;
@@ -1617,7 +1617,7 @@ bool LLTextureFetchWorker::processSimulatorPackets()
 			if (buffer_size > cur_size)
 			{
 				/// We have new data
-				U8* buffer = (U8*)LLImageBase::allocateMemory(buffer_size);
+				U8* buffer = (U8*)ALLOCATE_MEM(LLImageBase::getPrivatePool(), buffer_size);
 				S32 offset = 0;
 				if (cur_size > 0 && mFirstPacket > 0)
 				{
@@ -1669,7 +1669,7 @@ S32 LLTextureFetchWorker::callbackHttpGet(const LLChannelDescriptors& channels,
 		if (data_size > 0)
 		{
 			// *TODO: set the formatted image data here directly to avoid the copy
-			mBuffer = (U8*)LLImageBase::allocateMemory(data_size);
+			mBuffer = (U8*)ALLOCATE_MEM(LLImageBase::getPrivatePool(), data_size);
 			buffer->readAfter(channels.in(), NULL, mBuffer, data_size);
 			mBufferSize += data_size;
 			if (data_size < mRequestedSize && mRequestedDiscard == 0)
-- 
GitLab