llvm.org GIT mirror llvm / ef484a3
Revert "Fix allocations of SmallVector and SmallPtrSet so they are more prone to" This reverts commit 617330909f0c26a3f2ab8601a029b9bdca48aa61. It broke the bots: /home/clangbuild2/clang-ppc64-2/llvm.src/unittests/ADT/SmallVectorTest.cpp:150: PushPopTest /home/clangbuild2/clang-ppc64-2/llvm.src/unittests/ADT/SmallVectorTest.cpp:118: Failure Value of: v[i].getValue() Actual: 0 Expected: value Which is: 2 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@178334 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 6 years ago
4 changed file(s) with 33 addition(s) and 81 deletion(s). Raw diff Collapse all Expand all
5353 /// then the set is in 'small mode'.
5454 const void **CurArray;
5555 /// CurArraySize - The allocated size of CurArray, always a power of two.
56 /// Note that CurArray points to an array that has CurArraySize+1 elements in
57 /// it, so that the end iterator actually points to valid memory.
5658 unsigned CurArraySize;
5759
5860 // If small, this is # elts allocated consecutively
6567 SmallArray(SmallStorage), CurArray(SmallStorage), CurArraySize(SmallSize) {
6668 assert(SmallSize && (SmallSize & (SmallSize-1)) == 0 &&
6769 "Initial size must be a power of two!");
70 // The end pointer, always valid, is set to a valid element to help the
71 // iterator.
72 CurArray[SmallSize] = 0;
6873 clear();
6974 }
7075 ~SmallPtrSetImpl();
141146 class SmallPtrSetIteratorImpl {
142147 protected:
143148 const void *const *Bucket;
144 const void *const *End;
145149 public:
146 explicit SmallPtrSetIteratorImpl(const void *const *BP, const void*const *E)
147 : Bucket(BP), End(E) {
148 AdvanceIfNotValid();
150 explicit SmallPtrSetIteratorImpl(const void *const *BP) : Bucket(BP) {
151 AdvanceIfNotValid();
149152 }
150153
151154 bool operator==(const SmallPtrSetIteratorImpl &RHS) const {
160163 /// that is. This is guaranteed to stop because the end() bucket is marked
161164 /// valid.
162165 void AdvanceIfNotValid() {
163 assert(Bucket <= End);
164 while (Bucket != End &&
165 (*Bucket == SmallPtrSetImpl::getEmptyMarker() ||
166 *Bucket == SmallPtrSetImpl::getTombstoneMarker()))
166 while (*Bucket == SmallPtrSetImpl::getEmptyMarker() ||
167 *Bucket == SmallPtrSetImpl::getTombstoneMarker())
167168 ++Bucket;
168169 }
169170 };
180181 typedef std::ptrdiff_t difference_type;
181182 typedef std::forward_iterator_tag iterator_category;
182183
183 explicit SmallPtrSetIterator(const void *const *BP, const void *const *E)
184 : SmallPtrSetIteratorImpl(BP, E) {}
184 explicit SmallPtrSetIterator(const void *const *BP)
185 : SmallPtrSetIteratorImpl(BP) {}
185186
186187 // Most methods provided by baseclass.
187188
188189 const PtrTy operator*() const {
189 assert(Bucket < End);
190190 return PtrTraits::getFromVoidPointer(const_cast(*Bucket));
191191 }
192192
235235 class SmallPtrSet : public SmallPtrSetImpl {
236236 // Make sure that SmallSize is a power of two, round up if not.
237237 enum { SmallSizePowTwo = RoundUpToPowerOfTwo::Val };
238 /// SmallStorage - Fixed size storage used in 'small mode'.
239 const void *SmallStorage[SmallSizePowTwo];
238 /// SmallStorage - Fixed size storage used in 'small mode'. The extra element
239 /// ensures that the end iterator actually points to valid memory.
240 const void *SmallStorage[SmallSizePowTwo+1];
240241 typedef PointerLikeTypeTraits PtrTraits;
241242 public:
242243 SmallPtrSet() : SmallPtrSetImpl(SmallStorage, SmallSizePowTwo) {}
273274 typedef SmallPtrSetIterator iterator;
274275 typedef SmallPtrSetIterator const_iterator;
275276 inline iterator begin() const {
276 return iterator(CurArray, CurArray+CurArraySize);
277 return iterator(CurArray);
277278 }
278279 inline iterator end() const {
279 return iterator(CurArray+CurArraySize, CurArray+CurArraySize);
280 return iterator(CurArray+CurArraySize);
280281 }
281282
282283 // Allow assignment from any smallptrset with the same element type even if it
1515
1616 #include "llvm/Support/AlignOf.h"
1717 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/MathExtras.h"
1918 #include "llvm/Support/type_traits.h"
2019 #include
2120 #include
267266 void SmallVectorTemplateBase::grow(size_t MinSize) {
268267 size_t CurCapacity = this->capacity();
269268 size_t CurSize = this->size();
270 // Always grow, even from zero.
271 size_t NewCapacity = NextPowerOf2(2*CurCapacity+(CurCapacity==0));
269 size_t NewCapacity = 2*CurCapacity + 1; // Always grow, even from zero.
272270 if (NewCapacity < MinSize)
273271 NewCapacity = MinSize;
274272 T *NewElts = static_cast(malloc(NewCapacity*sizeof(T)));
2828 NumElements = NumTombstones = 0;
2929
3030 // Install the new array. Clear all the buckets to empty.
31 CurArray = (const void**)malloc(sizeof(void*) * CurArraySize);
31 CurArray = (const void**)malloc(sizeof(void*) * (CurArraySize+1));
3232 assert(CurArray && "Failed to allocate memory?");
3333 memset(CurArray, -1, CurArraySize*sizeof(void*));
34
35 // The end pointer, always valid, is set to a valid element to help the
36 // iterator.
37 CurArray[CurArraySize] = 0;
3438 }
3539
3640 bool SmallPtrSetImpl::insert_imp(const void * Ptr) {
134138 bool WasSmall = isSmall();
135139
136140 // Install the new array. Clear all the buckets to empty.
137 CurArray = (const void**)malloc(sizeof(void*) * NewSize);
141 CurArray = (const void**)malloc(sizeof(void*) * (NewSize+1));
138142 assert(CurArray && "Failed to allocate memory?");
139143 CurArraySize = NewSize;
140144 memset(CurArray, -1, NewSize*sizeof(void*));
145
146 // The end pointer, always valid, is set to a valid element to help the
147 // iterator.
148 CurArray[NewSize] = 0;
141149
142150 // Copy over all the elements.
143151 if (WasSmall) {
171179 CurArray = SmallArray;
172180 // Otherwise, allocate new heap space (unless we were the same size)
173181 } else {
174 CurArray = (const void**)malloc(sizeof(void*) * that.CurArraySize);
182 CurArray = (const void**)malloc(sizeof(void*) * (that.CurArraySize+1));
175183 assert(CurArray && "Failed to allocate memory?");
176184 }
177185
179187 CurArraySize = that.CurArraySize;
180188
181189 // Copy over the contents from the other set
182 memcpy(CurArray, that.CurArray, sizeof(void*)*CurArraySize);
190 memcpy(CurArray, that.CurArray, sizeof(void*)*(CurArraySize+1));
183191
184192 NumElements = that.NumElements;
185193 NumTombstones = that.NumTombstones;
191199 if (isSmall() && RHS.isSmall())
192200 assert(CurArraySize == RHS.CurArraySize &&
193201 "Cannot assign sets with different small sizes");
194
202
195203 // If we're becoming small, prepare to insert into our stack space
196204 if (RHS.isSmall()) {
197205 if (!isSmall())
200208 // Otherwise, allocate new heap space (unless we were the same size)
201209 } else if (CurArraySize != RHS.CurArraySize) {
202210 if (isSmall())
203 CurArray = (const void**)malloc(sizeof(void*) * RHS.CurArraySize);
211 CurArray = (const void**)malloc(sizeof(void*) * (RHS.CurArraySize+1));
204212 else
205 CurArray = (const void**)realloc(CurArray, sizeof(void*)*RHS.CurArraySize);
213 CurArray = (const void**)realloc(CurArray, sizeof(void*)*(RHS.CurArraySize+1));
206214 assert(CurArray && "Failed to allocate memory?");
207215 }
208216
210218 CurArraySize = RHS.CurArraySize;
211219
212220 // Copy over the contents from the other set
213 memcpy(CurArray, RHS.CurArray, sizeof(void*)*CurArraySize);
221 memcpy(CurArray, RHS.CurArray, sizeof(void*)*(CurArraySize+1));
214222
215223 NumElements = RHS.NumElements;
216224 NumTombstones = RHS.NumTombstones;
1616 using namespace llvm;
1717
1818 // SmallPtrSet swapping test.
19 TEST(SmallPtrSetTest, GrowthTest) {
20 int i;
21 int buf[8];
22 for(i=0; i<8; ++i) buf[i]=0;
23
24
25 SmallPtrSet s;
26 typedef SmallPtrSet::iterator iter;
27
28 s.insert(&buf[0]);
29 s.insert(&buf[1]);
30 s.insert(&buf[2]);
31 s.insert(&buf[3]);
32 EXPECT_EQ(4U, s.size());
33
34 i = 0;
35 for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
36 (**I)++;
37 EXPECT_EQ(4, i);
38 for(i=0; i<8; ++i)
39 EXPECT_EQ(i<4?1:0,buf[i]);
40
41 s.insert(&buf[4]);
42 s.insert(&buf[5]);
43 s.insert(&buf[6]);
44 s.insert(&buf[7]);
45
46 i = 0;
47 for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
48 (**I)++;
49 EXPECT_EQ(8, i);
50 s.erase(&buf[4]);
51 s.erase(&buf[5]);
52 s.erase(&buf[6]);
53 s.erase(&buf[7]);
54 EXPECT_EQ(4U, s.size());
55
56 i = 0;
57 for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
58 (**I)++;
59 EXPECT_EQ(4, i);
60 for(i=0; i<8; ++i)
61 EXPECT_EQ(i<4?3:1,buf[i]);
62
63 s.clear();
64 for(i=0; i<8; ++i) buf[i]=0;
65 for(i=0; i<128; ++i) s.insert(&buf[i%8]); // test repeated entires
66 EXPECT_EQ(8U, s.size());
67 for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
68 (**I)++;
69 for(i=0; i<8; ++i)
70 EXPECT_EQ(1,buf[i]);
71 }
72
73
7419 TEST(SmallPtrSetTest, SwapTest) {
7520 int buf[10];
7621