llvm.org GIT mirror llvm / 03af6d4
Use size_t to represent the size of a StringMapEntry length and alignment rather than unsigned. Patch by Matt Davis. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@322305 91177308-0d34-0410-b5e6-96231b3b80d8 Aaron Ballman 1 year, 9 months ago
2 changed file(s) with 52 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
3636
3737 /// StringMapEntryBase - Shared base class of StringMapEntry instances.
3838 class StringMapEntryBase {
39 unsigned StrLen;
40
41 public:
42 explicit StringMapEntryBase(unsigned Len) : StrLen(Len) {}
43
44 unsigned getKeyLength() const { return StrLen; }
39 size_t StrLen;
40
41 public:
42 explicit StringMapEntryBase(size_t Len) : StrLen(Len) {}
43
44 size_t getKeyLength() const { return StrLen; }
4545 };
4646
4747 /// StringMapImpl - This is the base class of StringMap that is shared among
126126 public:
127127 ValueTy second;
128128
129 explicit StringMapEntry(unsigned strLen)
129 explicit StringMapEntry(size_t strLen)
130130 : StringMapEntryBase(strLen), second() {}
131131 template
132 StringMapEntry(unsigned strLen, InitTy &&... InitVals)
132 StringMapEntry(size_t strLen, InitTy &&... InitVals)
133133 : StringMapEntryBase(strLen), second(std::forward(InitVals)...) {}
134134 StringMapEntry(StringMapEntry &E) = delete;
135135
154154 template
155155 static StringMapEntry *Create(StringRef Key, AllocatorTy &Allocator,
156156 InitTy &&... InitVals) {
157 unsigned KeyLength = Key.size();
157 size_t KeyLength = Key.size();
158158
159159 // Allocate a new item with space for the string at the end and a null
160160 // terminator.
161 unsigned AllocSize = static_cast(sizeof(StringMapEntry))+
162 KeyLength+1;
163 unsigned Alignment = alignof(StringMapEntry);
161 size_t AllocSize = sizeof(StringMapEntry) + KeyLength + 1;
162 size_t Alignment = alignof(StringMapEntry);
164163
165164 StringMapEntry *NewItem =
166165 static_cast(Allocator.Allocate(AllocSize,Alignment));
202201 template
203202 void Destroy(AllocatorTy &Allocator) {
204203 // Free memory referenced by the item.
205 unsigned AllocSize =
206 static_cast(sizeof(StringMapEntry)) + getKeyLength() + 1;
204 size_t AllocSize = sizeof(StringMapEntry) + getKeyLength() + 1;
207205 this->~StringMapEntry();
208206 Allocator.Deallocate(static_cast(this), AllocSize);
209207 }
1111 #include "llvm/ADT/Twine.h"
1212 #include "llvm/Support/DataTypes.h"
1313 #include "gtest/gtest.h"
14 #include
1415 #include
1516 using namespace llvm;
1617
491492 EXPECT_EQ(42, Map["abcd"].Data);
492493 }
493494
495 // Test that StringMapEntryBase can handle size_t wide sizes.
496 TEST(StringMapCustomTest, StringMapEntryBaseSize) {
497 size_t LargeValue;
498
499 // Test that the entry can represent max-unsigned.
500 if (sizeof(size_t) <= sizeof(unsigned))
501 LargeValue = std::numeric_limits::max();
502 else
503 LargeValue = std::numeric_limits::max() + 1ULL;
504 StringMapEntryBase LargeBase(LargeValue);
505 EXPECT_EQ(LargeValue, LargeBase.getKeyLength());
506
507 // Test that the entry can hold at least max size_t.
508 LargeValue = std::numeric_limits::max();
509 StringMapEntryBase LargerBase(LargeValue);
510 LargeValue = std::numeric_limits::max();
511 EXPECT_EQ(LargeValue, LargerBase.getKeyLength());
512 }
513
514 // Test that StringMapEntry can handle size_t wide sizes.
515 TEST(StringMapCustomTest, StringMapEntrySize) {
516 size_t LargeValue;
517
518 // Test that the entry can represent max-unsigned.
519 if (sizeof(size_t) <= sizeof(unsigned))
520 LargeValue = std::numeric_limits::max();
521 else
522 LargeValue = std::numeric_limits::max() + 1ULL;
523 StringMapEntry LargeEntry(LargeValue);
524 StringRef Key = LargeEntry.getKey();
525 EXPECT_EQ(LargeValue, Key.size());
526
527 // Test that the entry can hold at least max size_t.
528 LargeValue = std::numeric_limits::max();
529 StringMapEntry LargerEntry(LargeValue);
530 Key = LargerEntry.getKey();
531 EXPECT_EQ(LargeValue, Key.size());
532 }
533
494534 } // end anonymous namespace