1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-23 19:23:23 +01:00

Prevent infinite growth of SmallMap instances.

Rehash but don't grow when full of tombstones.

Patch by José Fonseca!

llvm-svn: 128565
This commit is contained in:
Jakob Stoklund Olesen 2011-03-30 18:32:44 +00:00
parent 492f97910d
commit 0daa5f56cb
2 changed files with 15 additions and 15 deletions

View File

@ -81,16 +81,6 @@ protected:
StringMapImpl(unsigned InitSize, unsigned ItemSize);
void RehashTable();
/// ShouldRehash - Return true if the table should be rehashed after a new
/// element was recently inserted.
bool ShouldRehash() const {
// If the hash table is now more than 3/4 full, or if fewer than 1/8 of
// the buckets are empty (meaning that many are filled with tombstones),
// grow the table.
return NumItems*4 > NumBuckets*3 ||
NumBuckets-(NumItems+NumTombstones) < NumBuckets/8;
}
/// LookupBucketFor - Look up the bucket that the specified string should end
/// up in. If it already exists as a key in the map, the Item pointer for the
/// specified bucket will be non-null. Otherwise, it will be null. In either
@ -340,8 +330,7 @@ public:
Bucket.Item = KeyValue;
++NumItems;
if (ShouldRehash())
RehashTable();
RehashTable();
return true;
}
@ -383,8 +372,7 @@ public:
// filled in by LookupBucketFor.
Bucket.Item = NewItem;
if (ShouldRehash())
RehashTable();
RehashTable();
return *NewItem;
}

View File

@ -177,7 +177,19 @@ StringMapEntryBase *StringMapImpl::RemoveKey(StringRef Key) {
/// RehashTable - Grow the table, redistributing values into the buckets with
/// the appropriate mod-of-hashtable-size.
void StringMapImpl::RehashTable() {
unsigned NewSize = NumBuckets*2;
unsigned NewSize;
// If the hash table is now more than 3/4 full, or if fewer than 1/8 of
// the buckets are empty (meaning that many are filled with tombstones),
// grow/rehash the table.
if (NumItems*4 > NumBuckets*3) {
NewSize = NumBuckets*2;
} else if (NumBuckets-(NumItems+NumTombstones) < NumBuckets/8) {
NewSize = NumBuckets;
} else {
return;
}
// Allocate one extra bucket which will always be non-empty. This allows the
// iterators to stop at end.
ItemBucket *NewTableArray =(ItemBucket*)calloc(NewSize+1, sizeof(ItemBucket));