Reset StringMap's NumTombstones on clears and rehashes.
authorJakob Stoklund Olesen <stoklund@2pi.dk>
Wed, 30 Mar 2011 18:32:51 +0000 (18:32 +0000)
committerJakob Stoklund Olesen <stoklund@2pi.dk>
Wed, 30 Mar 2011 18:32:51 +0000 (18:32 +0000)
StringMap was not properly updating NumTombstones after a clear or rehash.

This was not fatal until now because the table was growing faster than
NumTombstones could, but with the previous change of preventing infinite
growth of the table the invariant (NumItems + NumTombstones <= NumBuckets)
stopped being observed, causing infinite loops in certain situations.

Patch by José Fonseca!

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@128567 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/StringMap.h
lib/Support/StringMap.cpp

index f3d6b9f4849996e36069503ba6e2be8ff49167e5..907c72d685e0fd5bfd6e98434c363a6934f2140a 100644 (file)
@@ -329,6 +329,7 @@ public:
       --NumTombstones;
     Bucket.Item = KeyValue;
     ++NumItems;
+    assert(NumItems + NumTombstones <= NumBuckets);
 
     RehashTable();
     return true;
@@ -348,6 +349,7 @@ public:
     }
 
     NumItems = 0;
+    NumTombstones = 0;
   }
 
   /// GetOrCreateValue - Look up the specified key in the table.  If a value
@@ -367,6 +369,7 @@ public:
     if (Bucket.Item == getTombstoneVal())
       --NumTombstones;
     ++NumItems;
+    assert(NumItems + NumTombstones <= NumBuckets);
 
     // Fill in the bucket for the hash table.  The FullHashValue was already
     // filled in by LookupBucketFor.
index f193aa42a447a02d74ee612fef1cdc8ecfe46fb2..a1ac512fa244cfb115952384d1fa1c1dfa0e1d11 100644 (file)
@@ -169,6 +169,8 @@ StringMapEntryBase *StringMapImpl::RemoveKey(StringRef Key) {
   TheTable[Bucket].Item = getTombstoneVal();
   --NumItems;
   ++NumTombstones;
+  assert(NumItems + NumTombstones <= NumBuckets);
+
   return Result;
 }
 
@@ -224,4 +226,5 @@ void StringMapImpl::RehashTable() {
   
   TheTable = NewTableArray;
   NumBuckets = NewSize;
+  NumTombstones = 0;
 }