Memory management hygiene: Use compare() instead of substr(), and put a few more...
authorJohn Kessenich <cepheus@frii.com>
Fri, 11 Oct 2013 16:28:43 +0000 (16:28 +0000)
committerJohn Kessenich <cepheus@frii.com>
Fri, 11 Oct 2013 16:28:43 +0000 (16:28 +0000)
git-svn-id: https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/tools/glslang@23467 e7fa87d3-cd2b-0410-9028-fcbf551c1848

README.txt
StandAlone/StandAlone.cpp
glslang/Include/Common.h
glslang/Include/ConstantUnion.h
glslang/Include/Types.h
glslang/MachineIndependent/Initialize.cpp
glslang/MachineIndependent/ParseHelper.cpp
glslang/MachineIndependent/SymbolTable.cpp
glslang/MachineIndependent/SymbolTable.h
glslang/MachineIndependent/glslang.y

index b9650ec873fa98a31286d74287247a569a7c52ad..e50c9026d2a7813943568135c4743a4e5714568b 100644 (file)
@@ -127,4 +127,20 @@ Basic Internal Operation
  -  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'
index 6cc2a7350cf76cff36c4e7afd06777d2e21bd53b..db9428e9aafbdd96be4f386938b096b548a70641 100644 (file)
@@ -254,7 +254,7 @@ bool SetConfigFile(const std::string& name)
     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;
     }
index cdabe09f4f33a5d787c8f45baae95feac685e86a..8c4ccf2c953ee035d4ac0b8b910f517a3f4c0410 100644 (file)
@@ -91,7 +91,7 @@ namespace glslang {
 // 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 @@ inline TString* NewPoolTString(const char* s)
 //
 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) {}
index 53a5e930a2e51a25820858872a4199c85a179d3d..1e1d18a5d278c23b85502c30a539f36d580d1325 100644 (file)
@@ -421,19 +421,20 @@ class TConstUnionArray {
 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 @@ public:
     // 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 @@ public:
     bool empty() const { return unionArray == 0; }
 
 protected:
-    TVector<TConstUnion>* unionArray;
+    typedef TVector<TConstUnion> TConstUnionVector;
+    TConstUnionVector* unionArray;
 };
 
 } // end namespace glslang
index 1d341c95ef18233c276f6200cd001ab5758201b7..d4d9fc8b206002c274b78d3b44ef9c584528c14d 100644 (file)
@@ -150,20 +150,8 @@ struct TTypeLoc {
 };
 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 @@ inline TIdentifierList* NewPoolTIdentifierList()
 // 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 @@ protected:
     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 @@ public:
         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 @@ public:
     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); }
index ed947544b320b6e90395c6342ea0ce6ef4f0dd71..a44b48062f254cffa31fd25aaad925f6b1890136 100644 (file)
@@ -1692,7 +1692,7 @@ void IdentifyBuiltIns(int version, EProfile profile, EShLanguage language, TSymb
         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));
index e37c41d67646f3362c3e784d6e46ebb8ad2600c7..d981df3b1798859d5859518c5e3f048b587a37f5 100644 (file)
@@ -1204,7 +1204,7 @@ void TParseContext::globalCheck(TSourceLoc loc, bool global, const char* token)
 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 @@ void TParseContext::nonInitConstCheck(TSourceLoc loc, TString& identifier, TType
 //
 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 @@ void TParseContext::addBlock(TSourceLoc loc, TTypeList& typeList, const TString*
 
     // 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)) {
index a80c6c556059c3cab74bfa2959a424ee15d62b5d..67fccaecc5ffbeda625747bce0605374a93ab53c 100644 (file)
@@ -198,7 +198,7 @@ void TSymbolTableLevel::relateToOperator(const char* name, TOperator op)
     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
index 50bb1052238f49f88ced9e882a162a4440cd2e9b..a0db19579146dd5ff168f7bb687722a89c27e2fa 100644 (file)
@@ -85,7 +85,7 @@ public:
     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 @@ public:
             // 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 @@ public:
         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;
         }
index 17f3b27bcf14c679031fb07178b3ec1bb7d189ca..d89b937d539768991e1f0fabd650d37fd97e322d 100644 (file)
@@ -800,7 +800,7 @@ block_structure
 \r
 identifier_list\r
     : COMMA IDENTIFIER {\r
-        $$ = NewPoolTIdentifierList();\r
+        $$ = new TIdentifierList;\r
         $$->push_back($2.string);\r
     }\r
     | identifier_list COMMA IDENTIFIER {\r
@@ -1291,12 +1291,12 @@ type_specifier
 array_specifier\r
     : LEFT_BRACKET RIGHT_BRACKET {\r
         $$.loc = $1.loc;\r
-        $$.arraySizes = NewPoolTArraySizes();\r
+        $$.arraySizes = new TArraySizes;\r
         $$.arraySizes->setSize(0);\r
     }\r
     | LEFT_BRACKET constant_expression RIGHT_BRACKET {\r
         $$.loc = $1.loc;\r
-        $$.arraySizes = NewPoolTArraySizes();\r
+        $$.arraySizes = new TArraySizes;\r
 \r
         int size;\r
         parseContext.arraySizeCheck($2->getLoc(), $2, size);\r
@@ -2051,7 +2051,7 @@ struct_declaration
 \r
 struct_declarator_list\r
     : struct_declarator {\r
-        $$ = NewPoolTTypeList();\r
+        $$ = new TTypeList;\r
         $$->push_back($1);\r
     }\r
     | struct_declarator_list COMMA struct_declarator {\r