Skip to content

Commit

Permalink
Reset StringMap's NumTombstones on clears and rehashes.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
stoklund committed Mar 30, 2011
1 parent e10fff6 commit 03ef449
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/llvm/ADT/StringMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ class StringMap : public StringMapImpl {
--NumTombstones;
Bucket.Item = KeyValue;
++NumItems;
assert(NumItems + NumTombstones <= NumBuckets);

RehashTable();
return true;
Expand All @@ -348,6 +349,7 @@ class StringMap : public StringMapImpl {
}

NumItems = 0;
NumTombstones = 0;
}

/// GetOrCreateValue - Look up the specified key in the table. If a value
Expand All @@ -367,6 +369,7 @@ class StringMap : public StringMapImpl {
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.
Expand Down
3 changes: 3 additions & 0 deletions lib/Support/StringMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ StringMapEntryBase *StringMapImpl::RemoveKey(StringRef Key) {
TheTable[Bucket].Item = getTombstoneVal();
--NumItems;
++NumTombstones;
assert(NumItems + NumTombstones <= NumBuckets);

return Result;
}

Expand Down Expand Up @@ -224,4 +226,5 @@ void StringMapImpl::RehashTable() {

TheTable = NewTableArray;
NumBuckets = NewSize;
NumTombstones = 0;
}

0 comments on commit 03ef449

Please sign in to comment.