Memory management hygiene: Use compare() instead of substr(), and put a few more things intrinsically in the memory pool.


git-svn-id: https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/tools/glslang@23467 e7fa87d3-cd2b-0410-9028-fcbf551c1848
diff --git a/README.txt b/README.txt
index b9650ec..e50c902 100644
--- a/README.txt
+++ b/README.txt
@@ -127,4 +127,20 @@
  -  Reduction of the tree to a linear byte-code style low-level intermediate
     representation is likely a good way to generate fully optimized code.
 
- -  There is currently some dead linker-type code still lying around.
+ -  There is currently some dead old-style linker-type code still lying around.
+
+ - Memory pool: parsing uses types derived from C++ std types, using a 
+   custom allocator that puts them in a memory pool.  This makes allocation
+   of individual container/contents just few cycles and deallocation free.
+   This pool is popped after the AST is made and processed.
+
+   The use is simple: if you are going to call "new", there are three cases:
+
+    - the object comes from the pool (its base class has the macro
+      POOL_ALLOCATOR_NEW_DELETE in it) and you do not have to call delete
+
+    - it is a TString, in which case call NewPoolTString(), which gets 
+      it from the pool, and there is no corresponding delete
+
+    - the object does not come from the pool, and you have to do normal
+      C++ memory management of what you 'new'
diff --git a/StandAlone/StandAlone.cpp b/StandAlone/StandAlone.cpp
index 6cc2a73..db9428e 100644
--- a/StandAlone/StandAlone.cpp
+++ b/StandAlone/StandAlone.cpp
@@ -254,7 +254,7 @@
     if (name.size() < 5)
         return false;
 
-    if (name.substr(name.size() - 5, std::string::npos) == ".conf") {
+    if (name.compare(name.size() - 5, 5, ".conf") == 0) {
         ConfigFile = name;
         return true;
     }
diff --git a/glslang/Include/Common.h b/glslang/Include/Common.h
index cdabe09..8c4ccf2 100644
--- a/glslang/Include/Common.h
+++ b/glslang/Include/Common.h
@@ -91,7 +91,7 @@
 // Pool version of string.
 //
 typedef pool_allocator<char> TStringAllocator;
-typedef std::basic_string <char, std::char_traits<char>, TStringAllocator > TString;
+typedef std::basic_string <char, std::char_traits<char>, TStringAllocator> TString;
 inline TString* NewPoolTString(const char* s)
 {
 	void* memory = GetThreadPoolAllocator().allocate(sizeof(TString));
@@ -103,6 +103,8 @@
 //
 template <class T> class TVector : public std::vector<T, pool_allocator<T> > {
 public:
+    POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator())
+
     typedef typename std::vector<T, pool_allocator<T> >::size_type size_type;
     TVector() : std::vector<T, pool_allocator<T> >() {}
     TVector(const pool_allocator<T>& a) : std::vector<T, pool_allocator<T> >(a) {}
diff --git a/glslang/Include/ConstantUnion.h b/glslang/Include/ConstantUnion.h
index 53a5e93..1e1d18a 100644
--- a/glslang/Include/ConstantUnion.h
+++ b/glslang/Include/ConstantUnion.h
@@ -421,19 +421,20 @@
 public:
     POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator())
 
-    TConstUnionArray() { unionArray = 0; }
+    TConstUnionArray() : unionArray(0) { }
+    virtual ~TConstUnionArray() { }
 
     explicit TConstUnionArray(int size)
     {
         if (size == 0)
             unionArray = 0;
         else
-            unionArray = new TVector<TConstUnion>(size);
+            unionArray =  new TConstUnionVector(size);
     }
     TConstUnionArray(const TConstUnionArray& a) : unionArray(a.unionArray) { }
     TConstUnionArray(const TConstUnionArray& a, int start, int size)
     {
-        unionArray = new TVector<TConstUnion>(size);
+        unionArray = new TConstUnionVector(size);
         for (int i = 0; i < size; ++i)
             (*unionArray)[i] = a[start + i];
     }
@@ -441,7 +442,7 @@
     // Use this constructor for a smear operation
     TConstUnionArray(int size, const TConstUnion& val)
     {
-        unionArray = new TVector<TConstUnion>(size, val);
+        unionArray = new TConstUnionVector(size, val);
     }
 
     TConstUnion& operator[](int index) { return (*unionArray)[index]; }
@@ -462,7 +463,8 @@
     bool empty() const { return unionArray == 0; }
 
 protected:
-    TVector<TConstUnion>* unionArray;
+    typedef TVector<TConstUnion> TConstUnionVector;
+    TConstUnionVector* unionArray;
 };
 
 } // end namespace glslang
diff --git a/glslang/Include/Types.h b/glslang/Include/Types.h
index 1d341c9..d4d9fc8 100644
--- a/glslang/Include/Types.h
+++ b/glslang/Include/Types.h
@@ -150,20 +150,8 @@
 };
 typedef TVector<TTypeLoc> TTypeList;
 
-inline TTypeList* NewPoolTTypeList()
-{
-	void* memory = GetThreadPoolAllocator().allocate(sizeof(TTypeList));
-	return new(memory) TTypeList;
-}
-
 typedef TVector<TString*> TIdentifierList;
 
-inline TIdentifierList* NewPoolTIdentifierList()
-{
-    void* memory = GetThreadPoolAllocator().allocate(sizeof(TIdentifierList));
-    return new(memory) TIdentifierList;
-}
-
 //
 // TODO: memory: TArraySizes can be replaced by something smaller.
 // Almost all arrays could be handled by two sizes each fitting
@@ -175,6 +163,8 @@
 // is used, it will be containing at least one size.
 
 struct TArraySizes {
+    POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator())
+
     TArraySizes() : maxArraySize(0) { }
     int getSize() { return sizes.front(); }  // TArraySizes only exists if there is at least one dimension
     void setSize(int s) { sizes.push_back(s); }
@@ -185,12 +175,6 @@
     int maxArraySize; // for tracking maximum referenced index, before an explicit size is given
 };
 
-inline TArraySizes* NewPoolTArraySizes()
-{
-    void* memory = GetThreadPoolAllocator().allocate(sizeof(TArraySizes));
-    return new(memory) TArraySizes;
-}
-
 //
 // TPublicType (coming up after some dependent declarations)
 // is a workaround for a problem with the yacc stack,  It can't have
@@ -484,12 +468,12 @@
         shallowCopy(copyOf);
 
         if (arraySizes) {
-            arraySizes = NewPoolTArraySizes();
+            arraySizes = new TArraySizes;
             *arraySizes = *copyOf.arraySizes;
         }
 
 		if (structure) {
-			structure = NewPoolTTypeList();
+			structure = new TTypeList;
     		TStructureMapIterator iter;
 			for (unsigned int i = 0; i < copyOf.structure->size(); ++i) {
 				TTypeLoc typeLoc;
@@ -604,7 +588,7 @@
     void setArraySizes(TArraySizes* s)
     {
         // For when we don't want distinct types sharing the same descriptor.
-        arraySizes = NewPoolTArraySizes();
+        arraySizes = new TArraySizes;
         *arraySizes = *s;
     }
     void setArraySizes(const TType& type) { setArraySizes(type.arraySizes); }
diff --git a/glslang/MachineIndependent/Initialize.cpp b/glslang/MachineIndependent/Initialize.cpp
index ed94754..a44b480 100644
--- a/glslang/MachineIndependent/Initialize.cpp
+++ b/glslang/MachineIndependent/Initialize.cpp
@@ -1692,7 +1692,7 @@
         if (version < FirstProfileVersion || profile == ECompatibilityProfile || (! ForwardCompatibility && profile != EEsProfile && version < 420)) {
             TPrecisionQualifier pq = profile == EEsProfile ? EpqMedium : EpqNone;
             TType fragData(EbtFloat, EvqFragColor, pq, 4);
-            TArraySizes* arraySizes = NewPoolTArraySizes();
+            TArraySizes* arraySizes = new TArraySizes;
             arraySizes->setSize(resources.maxDrawBuffers);
             fragData.setArraySizes(arraySizes);
             symbolTable.insert(*new TVariable(NewPoolTString("gl_FragData"), fragData));
diff --git a/glslang/MachineIndependent/ParseHelper.cpp b/glslang/MachineIndependent/ParseHelper.cpp
index e37c41d..d981df3 100644
--- a/glslang/MachineIndependent/ParseHelper.cpp
+++ b/glslang/MachineIndependent/ParseHelper.cpp
@@ -1204,7 +1204,7 @@
 bool TParseContext::reservedErrorCheck(TSourceLoc loc, const TString& identifier)
 {
     if (! symbolTable.atBuiltInLevel()) {
-        if (identifier.substr(0, 3) == TString("gl_")) {
+        if (identifier.compare(0, 3, "gl_") == 0) {
             error(loc, "reserved built-in name", "gl_", "");
 
             return true;
@@ -1826,7 +1826,7 @@
 //
 TVariable* TParseContext::redeclareBuiltin(TSourceLoc loc, const TString& identifier, bool& newDeclaration)
 {
-    if (profile == EEsProfile || identifier.substr(0, 3) != TString("gl_") || symbolTable.atBuiltInLevel())
+    if (profile == EEsProfile || identifier.compare(0, 3, "gl_") != 0 || symbolTable.atBuiltInLevel())
         return 0;
 
     // Potentially redeclaring a built-in variable...
@@ -2416,7 +2416,7 @@
 
     // make an anonymous variable if no name was provided
     if (! instanceName)
-        instanceName = new TString("");
+        instanceName = NewPoolTString("");
 
     TVariable* variable = new TVariable(instanceName, blockType);
     if (! symbolTable.insert(*variable)) {
diff --git a/glslang/MachineIndependent/SymbolTable.cpp b/glslang/MachineIndependent/SymbolTable.cpp
index a80c6c5..67fccae 100644
--- a/glslang/MachineIndependent/SymbolTable.cpp
+++ b/glslang/MachineIndependent/SymbolTable.cpp
@@ -198,7 +198,7 @@
     while (candidate != level.end()) {
         const TString& candidateName = (*candidate).first;
         TString::size_type parenAt = candidateName.find_first_of('(');
-        if (parenAt != candidateName.npos && candidateName.substr(0, parenAt) == name) {
+        if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0) {
             TFunction* function = (*candidate).second->getAsFunction();
             function->relateToOperator(op);
         } else
diff --git a/glslang/MachineIndependent/SymbolTable.h b/glslang/MachineIndependent/SymbolTable.h
index 50bb105..a0db195 100644
--- a/glslang/MachineIndependent/SymbolTable.h
+++ b/glslang/MachineIndependent/SymbolTable.h
@@ -85,7 +85,7 @@
     virtual ~TSymbol() { }
 
     const TString& getName() const { return *name; }
-    void changeName(const char* buf) { name = new TString(buf); }
+    void changeName(const TString* newName) { name = newName; }
     virtual const TString& getMangledName() const { return getName(); }
     virtual TFunction* getAsFunction() { return 0; }
     virtual const TFunction* getAsFunction() const { return 0; }
@@ -257,7 +257,7 @@
             // Give it a name and insert its members in the symbol table, pointing to the container.
             char buf[20];
             snprintf(buf, 20, "__anon__%d", anonId++);
-            symbol.changeName(buf);
+            symbol.changeName(NewPoolTString(buf));
 
             bool isOkay = true;
             const TTypeList& types = *symbol.getAsVariable()->getType().getStruct();
@@ -307,7 +307,7 @@
         if (candidate != level.end()) {
             const TString& candidateName = (*candidate).first;
             TString::size_type parenAt = candidateName.find_first_of('(');
-            if (parenAt != candidateName.npos && candidateName.substr(0, parenAt) == name)
+            if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0)
 
                 return true;
         }
diff --git a/glslang/MachineIndependent/glslang.y b/glslang/MachineIndependent/glslang.y
index 17f3b27..d89b937 100644
--- a/glslang/MachineIndependent/glslang.y
+++ b/glslang/MachineIndependent/glslang.y
@@ -800,7 +800,7 @@
 

 identifier_list

     : COMMA IDENTIFIER {

-        $$ = NewPoolTIdentifierList();

+        $$ = new TIdentifierList;

         $$->push_back($2.string);

     }

     | identifier_list COMMA IDENTIFIER {

@@ -1291,12 +1291,12 @@
 array_specifier

     : LEFT_BRACKET RIGHT_BRACKET {

         $$.loc = $1.loc;

-        $$.arraySizes = NewPoolTArraySizes();

+        $$.arraySizes = new TArraySizes;

         $$.arraySizes->setSize(0);

     }

     | LEFT_BRACKET constant_expression RIGHT_BRACKET {

         $$.loc = $1.loc;

-        $$.arraySizes = NewPoolTArraySizes();

+        $$.arraySizes = new TArraySizes;

 

         int size;

         parseContext.arraySizeCheck($2->getLoc(), $2, size);

@@ -2051,7 +2051,7 @@
 

 struct_declarator_list

     : struct_declarator {

-        $$ = NewPoolTTypeList();

+        $$ = new TTypeList;

         $$->push_back($1);

     }

     | struct_declarator_list COMMA struct_declarator {