Moved the global-pool-allocator to TCompiler so that all memory allocated by the compiler can be de-allocated. Earlier the global-pool-allocator kept accumulating memory from all compilers (symbol-table in particular). The memory was only de-allocated when gpu-process exited or ShFinalize() was called. This was a problem for Chromium which keeps the GPU process around for the browser session. Now the memory is de-allocated as soon as the compiler is deleted, which happens when a tab is closed.
BUG=58808 (crbug.com)
Review URL: http://codereview.appspot.com/3280041

git-svn-id: https://angleproject.googlecode.com/svn/trunk@489 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/compiler/Compiler.cpp b/src/compiler/Compiler.cpp
index 322595b..11acfae 100644
--- a/src/compiler/Compiler.cpp
+++ b/src/compiler/Compiler.cpp
@@ -8,10 +8,11 @@
 #include "compiler/ParseHelper.h"
 #include "compiler/ShHandle.h"
 
-static bool InitializeSymbolTable(
-        const TBuiltInStrings& builtInStrings,
-        ShShaderType type, ShShaderSpec spec, const ShBuiltInResources& resources,
-        TInfoSink& infoSink, TSymbolTable& symbolTable)
+namespace {
+bool InitializeSymbolTable(
+    const TBuiltInStrings& builtInStrings,
+    ShShaderType type, ShShaderSpec spec, const ShBuiltInResources& resources,
+    TInfoSink& infoSink, TSymbolTable& symbolTable)
 {
     TIntermediate intermediate(infoSink);
     TExtensionBehavior extBehavior;
@@ -49,6 +50,34 @@
     return true;
 }
 
+class TScopedPoolAllocator {
+public:
+    TScopedPoolAllocator(TPoolAllocator* allocator, bool pushPop)
+        : mAllocator(allocator), mPushPopAllocator(pushPop) {
+        if (mPushPopAllocator) mAllocator->push();
+        SetGlobalPoolAllocator(mAllocator);
+    }
+    ~TScopedPoolAllocator() {
+        SetGlobalPoolAllocator(NULL);
+        if (mPushPopAllocator) mAllocator->pop();
+    }
+
+private:
+    TPoolAllocator* mAllocator;
+    bool mPushPopAllocator;
+};
+}  // namespace
+
+TShHandleBase::TShHandleBase() {
+    allocator.push();
+    SetGlobalPoolAllocator(&allocator);
+}
+
+TShHandleBase::~TShHandleBase() {
+    SetGlobalPoolAllocator(NULL);
+    allocator.popAll();
+}
+
 TCompiler::TCompiler(ShShaderType type, ShShaderSpec spec)
     : shaderType(type),
       shaderSpec(spec) 
@@ -61,11 +90,13 @@
 
 bool TCompiler::Init(const ShBuiltInResources& resources)
 {
+    TScopedPoolAllocator scopedAlloc(&allocator, false);
+
     // Generate built-in symbol table.
     if (!InitBuiltInSymbolTable(resources))
         return false;
-
     InitExtensionBehavior(resources, extensionBehavior);
+
     return true;
 }
 
@@ -73,6 +104,7 @@
                         const int numStrings,
                         int compileOptions)
 {
+    TScopedPoolAllocator scopedAlloc(&allocator, true);
     clearResults();
 
     if (numStrings == 0)