diff options
author | Chris Lattner <sabre@nondot.org> | 2007-04-04 00:29:37 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2007-04-04 00:29:37 +0000 |
commit | 2376346a8e42b61f62643ebd977b1c586e338ee6 (patch) | |
tree | 4d9caad8dc4a0d30c81e62bf297058cc668d9951 /llvm/lib/Support | |
parent | 07d353c9d7b8fb39d813a6d4f118c950d52f1986 (diff) | |
download | bcm5719-llvm-2376346a8e42b61f62643ebd977b1c586e338ee6.tar.gz bcm5719-llvm-2376346a8e42b61f62643ebd977b1c586e338ee6.zip |
Extend StringMap to support being initialized as completely empty. When
initialized this way, they do not do a malloc to allocate their buckets.
llvm-svn: 35642
Diffstat (limited to 'llvm/lib/Support')
-rw-r--r-- | llvm/lib/Support/StringMap.cpp | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/llvm/lib/Support/StringMap.cpp b/llvm/lib/Support/StringMap.cpp index a84cd9dbc64..caf9ba350ef 100644 --- a/llvm/lib/Support/StringMap.cpp +++ b/llvm/lib/Support/StringMap.cpp @@ -16,10 +16,25 @@ using namespace llvm; StringMapImpl::StringMapImpl(unsigned InitSize, unsigned itemSize) { + ItemSize = itemSize; + + // If a size is specified, initialize the table with that many buckets. + if (InitSize) { + init(InitSize); + return; + } + + // Otherwise, initialize it with zero buckets to avoid the allocation. + TheTable = 0; + NumBuckets = 0; + NumItems = 0; + NumTombstones = 0; +} + +void StringMapImpl::init(unsigned InitSize) { assert((InitSize & (InitSize-1)) == 0 && "Init Size must be a power of 2 or zero!"); NumBuckets = InitSize ? InitSize : 16; - ItemSize = itemSize; NumItems = 0; NumTombstones = 0; @@ -52,8 +67,12 @@ static unsigned HashString(const char *Start, const char *End) { /// case, the FullHashValue field of the bucket will be set to the hash value /// of the string. unsigned StringMapImpl::LookupBucketFor(const char *NameStart, - const char *NameEnd) { + const char *NameEnd) { unsigned HTSize = NumBuckets; + if (HTSize == 0) { // Hash table unallocated so far? + init(16); + HTSize = NumBuckets; + } unsigned FullHashValue = HashString(NameStart, NameEnd); unsigned BucketNo = FullHashValue & (HTSize-1); @@ -110,6 +129,7 @@ unsigned StringMapImpl::LookupBucketFor(const char *NameStart, /// This does not modify the map. int StringMapImpl::FindKey(const char *KeyStart, const char *KeyEnd) const { unsigned HTSize = NumBuckets; + if (HTSize == 0) return -1; // Really empty table? unsigned FullHashValue = HashString(KeyStart, KeyEnd); unsigned BucketNo = FullHashValue & (HTSize-1); |