From 48d949150cd445ce1e801a7a8ee67597a965f14b Mon Sep 17 00:00:00 2001
From: Xiaohong Bao <bao@lindenlab.com>
Date: Wed, 20 Jul 2011 16:05:19 -0600
Subject: [PATCH] add a debug setting "MemoryPrivatePoolEnabled" to turn on/off
 private memory pool.

---
 indra/llcommon/llmemory.cpp             | 51 +++++++++++++------------
 indra/llcommon/llmemory.h               |  6 ++-
 indra/newview/app_settings/settings.xml | 13 ++++++-
 indra/newview/llappviewer.cpp           |  2 +
 4 files changed, 45 insertions(+), 27 deletions(-)

diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp
index eb55bdae849..0d36009fc42 100644
--- a/indra/llcommon/llmemory.cpp
+++ b/indra/llcommon/llmemory.cpp
@@ -61,10 +61,6 @@ BOOL LLMemory::sEnableMemoryFailurePrevention = FALSE;
 LLPrivateMemoryPoolManager::mem_allocation_info_t LLPrivateMemoryPoolManager::sMemAllocationTracker;
 #endif
 
-#ifndef _USE_PRIVATE_MEM_POOL_
-#define _USE_PRIVATE_MEM_POOL_ 1
-#endif
-
 //static
 void LLMemory::initClass()
 {
@@ -1386,7 +1382,7 @@ void LLPrivateMemoryPool::freeMem(void* addr)
 	{
 		return ;
 	}
-
+	
 	lock() ;
 	
 	LLMemoryChunk* chunk = findChunk((char*)addr) ;
@@ -1789,7 +1785,7 @@ bool LLPrivateMemoryPool::fillHashTable(U16 start, U16 end, LLMemoryChunk* chunk
 //--------------------------------------------------------------------
 LLPrivateMemoryPoolManager* LLPrivateMemoryPoolManager::sInstance = NULL ;
 
-LLPrivateMemoryPoolManager::LLPrivateMemoryPoolManager() 
+LLPrivateMemoryPoolManager::LLPrivateMemoryPoolManager(BOOL enabled) 
 {
 	mPoolList.resize(LLPrivateMemoryPool::MAX_TYPES) ;
 
@@ -1797,6 +1793,8 @@ LLPrivateMemoryPoolManager::LLPrivateMemoryPoolManager()
 	{
 		mPoolList[i] = NULL ;
 	}
+
+	mPrivatePoolEnabled = enabled ;
 }
 
 LLPrivateMemoryPoolManager::~LLPrivateMemoryPoolManager() 
@@ -1838,13 +1836,21 @@ LLPrivateMemoryPoolManager::~LLPrivateMemoryPoolManager()
 #endif
 }
 
+//static 
+void LLPrivateMemoryPoolManager::initClass(BOOL enabled) 
+{
+	llassert_always(!sInstance) ;
+
+	sInstance = new LLPrivateMemoryPoolManager(enabled) ;
+}
+
 //static 
 LLPrivateMemoryPoolManager* LLPrivateMemoryPoolManager::getInstance() 
 {
-	if(!sInstance)
-	{
-		sInstance = new LLPrivateMemoryPoolManager() ;
-	}
+	//if(!sInstance)
+	//{
+	//	sInstance = new LLPrivateMemoryPoolManager(FALSE) ;
+	//}
 	return sInstance ;
 }
 	
@@ -1860,6 +1866,11 @@ void LLPrivateMemoryPoolManager::destroyClass()
 
 LLPrivateMemoryPool* LLPrivateMemoryPoolManager::newPool(S32 type) 
 {
+	if(!mPrivatePoolEnabled)
+	{
+		return NULL ;
+	}
+
 	if(!mPoolList[type])
 	{
 		mPoolList[type] = new LLPrivateMemoryPool(type) ;
@@ -1870,7 +1881,7 @@ LLPrivateMemoryPool* LLPrivateMemoryPoolManager::newPool(S32 type)
 
 void LLPrivateMemoryPoolManager::deletePool(LLPrivateMemoryPool* pool) 
 {
-	if(pool->isEmpty())
+	if(pool && pool->isEmpty())
 	{
 		mPoolList[pool->getType()] = NULL ;
 		delete pool;
@@ -1907,7 +1918,7 @@ char* LLPrivateMemoryPoolManager::allocate(LLPrivateMemoryPool* poolp, U32 size,
 	{
 		p = poolp->allocate(size) ;
 	}
-
+	
 	if(p)
 	{
 		char num[16] ;
@@ -1924,18 +1935,14 @@ char* LLPrivateMemoryPoolManager::allocate(LLPrivateMemoryPool* poolp, U32 size,
 //static 
 char* LLPrivateMemoryPoolManager::allocate(LLPrivateMemoryPool* poolp, U32 size) 
 {
-#if _USE_PRIVATE_MEM_POOL_
-	if(!poolp)
+	if(poolp)
 	{
-		return (char*)malloc(size) ;
+		return poolp->allocate(size) ;		
 	}
 	else
 	{
-		return poolp->allocate(size) ;
+		return (char*)malloc(size) ;
 	}
-#else
-	return (char*)malloc(size) ;
-#endif
 }
 #endif
 
@@ -1951,7 +1958,6 @@ void  LLPrivateMemoryPoolManager::freeMem(LLPrivateMemoryPool* poolp, void* addr
 	sMemAllocationTracker.erase((char*)addr) ;
 #endif
 
-#if _USE_PRIVATE_MEM_POOL_
 	if(poolp)
 	{
 		poolp->freeMem(addr) ;
@@ -1959,10 +1965,7 @@ void  LLPrivateMemoryPoolManager::freeMem(LLPrivateMemoryPool* poolp, void* addr
 	else
 	{
 		free(addr) ;
-	}
-#else
-	free(addr) ;
-#endif
+	}	
 }
 
 //--------------------------------------------------------------------
diff --git a/indra/llcommon/llmemory.h b/indra/llcommon/llmemory.h
index 26488423a36..f9099da6123 100644
--- a/indra/llcommon/llmemory.h
+++ b/indra/llcommon/llmemory.h
@@ -367,11 +367,12 @@ class LL_COMMON_API LLPrivateMemoryPool
 class LL_COMMON_API LLPrivateMemoryPoolManager
 {
 private:
-	LLPrivateMemoryPoolManager() ;
+	LLPrivateMemoryPoolManager(BOOL enabled) ;
 	~LLPrivateMemoryPoolManager() ;
 
-public:
+public:	
 	static LLPrivateMemoryPoolManager* getInstance() ;
+	static void initClass(BOOL enabled) ;
 	static void destroyClass() ;
 
 	LLPrivateMemoryPool* newPool(S32 type) ;
@@ -380,6 +381,7 @@ class LL_COMMON_API LLPrivateMemoryPoolManager
 private:
 	static LLPrivateMemoryPoolManager* sInstance ;
 	std::vector<LLPrivateMemoryPool*> mPoolList ;
+	BOOL mPrivatePoolEnabled;
 
 public:
 	//debug and statistics info.
diff --git a/indra/newview/app_settings/settings.xml b/indra/newview/app_settings/settings.xml
index 8ff53412978..9c065537e5f 100644
--- a/indra/newview/app_settings/settings.xml
+++ b/indra/newview/app_settings/settings.xml
@@ -5561,7 +5561,7 @@
     <key>Comment</key>
     <string>If set, the viewer will quit to avoid crash when memory failure happens</string>
     <key>Persist</key>
-    <integer>0</integer>
+    <integer>1</integer>
     <key>Type</key>
     <string>Boolean</string>
     <key>Value</key>
@@ -5578,6 +5578,17 @@
         <key>Value</key>
             <real>600.0</real>
         </map>
+    <key>MemoryPrivatePoolEnabled</key>
+    <map>
+      <key>Comment</key>
+      <string>Enable the private memory pool management</string>
+      <key>Persist</key>
+      <integer>1</integer>
+      <key>Type</key>
+      <string>Boolean</string>
+      <key>Value</key>
+      <integer>0</integer>
+    </map>
     <key>MemProfiling</key>
     <map>
       <key>Comment</key>
diff --git a/indra/newview/llappviewer.cpp b/indra/newview/llappviewer.cpp
index 86b34ac3273..156c76e84c3 100644
--- a/indra/newview/llappviewer.cpp
+++ b/indra/newview/llappviewer.cpp
@@ -686,6 +686,8 @@ bool LLAppViewer::init()
 	//set the max heap size.
 	initMaxHeapSize() ;
 
+	LLPrivateMemoryPoolManager::initClass((BOOL)gSavedSettings.getBOOL("MemoryPrivatePoolEnabled")) ;
+
 	// write Google Breakpad minidump files to our log directory
 	std::string logdir = gDirUtilp->getExpandedFilename(LL_PATH_LOGS, "");
 	logdir += gDirUtilp->getDirDelimiter();
-- 
GitLab