diff options
author | Chris Lattner <sabre@nondot.org> | 2007-02-11 00:10:26 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2007-02-11 00:10:26 +0000 |
commit | 9cc2d3dce87c2f4666a5d6a97321d865ade930da (patch) | |
tree | a6cec3de501806e5bd3dfd03a2ca5be202ad6045 | |
parent | b508987e84c5a735464d5633be83a636fa6ea4b0 (diff) | |
download | external_llvm-9cc2d3dce87c2f4666a5d6a97321d865ade930da.zip external_llvm-9cc2d3dce87c2f4666a5d6a97321d865ade930da.tar.gz external_llvm-9cc2d3dce87c2f4666a5d6a97321d865ade930da.tar.bz2 |
Split StringMapEntry construction out of StringMap, into StringMapEntry.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@34170 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/llvm/ADT/StringMap.h | 83 |
1 files changed, 55 insertions, 28 deletions
diff --git a/include/llvm/ADT/StringMap.h b/include/llvm/ADT/StringMap.h index 375472c..c47d9ae 100644 --- a/include/llvm/ADT/StringMap.h +++ b/include/llvm/ADT/StringMap.h @@ -94,6 +94,58 @@ public: /// value. The string data is always stored immediately after the /// StringMapEntry object. const char *getKeyData() const {return reinterpret_cast<const char*>(this+1);} + + /// Create - Create a StringMapEntry for the specified key and default + /// construct the value. + template<typename AllocatorTy> + static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd, + AllocatorTy &Allocator) { + unsigned KeyLength = KeyEnd-KeyStart; + + // Okay, the item doesn't already exist, and 'Bucket' is the bucket to fill + // in. Allocate a new item with space for the string at the end and a null + // terminator. + unsigned AllocSize = sizeof(StringMapEntry)+KeyLength+1; + +#ifdef __GNUC__ + unsigned Alignment = __alignof__(StringMapEntry); +#else + // FIXME: ugly. + unsigned Alignment = 8; +#endif + StringMapEntry *NewItem = + static_cast<StringMapEntry*>(Allocator.Allocate(AllocSize, Alignment)); + + // Default construct the value. + new (NewItem) StringMapEntry(KeyLength); + + // Copy the string information. + char *StrBuffer = const_cast<char*>(NewItem->getKeyData()); + memcpy(StrBuffer, KeyStart, KeyLength); + StrBuffer[KeyLength] = 0; // Null terminate for convenience of clients. + return NewItem; + } + + /// Create - Create a StringMapEntry with normal malloc/free. + static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd) { + MallocAllocator A; + return Create(KeyStart, KeyEnd, A); + } + + /// Destroy - Destroy this StringMapEntry, releasing memory back to the + /// specified allocator. + template<typename AllocatorTy> + void Destroy(AllocatorTy &Allocator) { + // Free memory referenced by the item. + this->~StringMapEntry(); + Allocator.Deallocate(this); + } + + /// Destroy this object, releasing memory back to the malloc allocator. + void Destroy() { + MallocAllocator A; + Destroy(A); + } }; @@ -129,31 +181,9 @@ public: if (Bucket.Item) return *static_cast<MapEntryTy*>(Bucket.Item); - unsigned KeyLength = KeyEnd-KeyStart; - - // Okay, the item doesn't already exist, and 'Bucket' is the bucket to fill - // in. Allocate a new item with space for the string at the end and a null - // terminator. - unsigned AllocSize = sizeof(MapEntryTy)+KeyLength+1; - -#ifdef __GNUC__ - unsigned Alignment = __alignof__(MapEntryTy); -#else - // FIXME: ugly. - unsigned Alignment = 8; -#endif - MapEntryTy *NewItem = - static_cast<MapEntryTy*>(Allocator.Allocate(AllocSize, Alignment)); - - // Default construct the value. - new (NewItem) MapEntryTy(KeyLength); + MapEntryTy *NewItem = MapEntryTy::Create(KeyStart, KeyEnd, Allocator); ++NumItems; - // Copy the string information. - char *StrBuffer = const_cast<char*>(NewItem->getKeyData()); - memcpy(StrBuffer, KeyStart, KeyLength); - StrBuffer[KeyLength] = 0; // Null terminate for convenience of clients. - // Fill in the bucket for the hash table. The FullHashValue was already // filled in by LookupBucketFor. Bucket.Item = NewItem; @@ -166,11 +196,8 @@ public: ~StringMap() { for (ItemBucket *I = TheTable, *E = TheTable+NumBuckets; I != E; ++I) { - if (MapEntryTy *Id = static_cast<MapEntryTy*>(I->Item)) { - // Free memory referenced by the item. - Id->~MapEntryTy(); - Allocator.Deallocate(Id); - } + if (MapEntryTy *Id = static_cast<MapEntryTy*>(I->Item)) + Id->Destroy(Allocator); } delete [] TheTable; } |