llvm.org GIT mirror llvm / d66a3fe
[Allocator] Make BumpPtrAllocator movable and move assignable. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206372 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 6 years ago
2 changed file(s) with 47 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
137137 class BumpPtrAllocatorImpl
138138 : public AllocatorBase<
139139 BumpPtrAllocatorImpl> {
140 BumpPtrAllocatorImpl(const BumpPtrAllocatorImpl &) LLVM_DELETED_FUNCTION;
141 void operator=(const BumpPtrAllocatorImpl &) LLVM_DELETED_FUNCTION;
142
143140 public:
144141 static_assert(SizeThreshold <= SlabSize,
145142 "The SizeThreshold must be at most the SlabSize to ensure "
152149 BumpPtrAllocatorImpl(T &&Allocator)
153150 : CurPtr(nullptr), End(nullptr), BytesAllocated(0),
154151 Allocator(std::forward(Allocator)) {}
152
153 // Manually implement a move constructor as we must clear the old allocators
154 // slabs as a matter of correctness.
155 BumpPtrAllocatorImpl(BumpPtrAllocatorImpl &&Old)
156 : CurPtr(Old.CurPtr), End(Old.End), Slabs(std::move(Old.Slabs)),
157 CustomSizedSlabs(std::move(Old.CustomSizedSlabs)),
158 BytesAllocated(Old.BytesAllocated),
159 Allocator(std::move(Old.Allocator)) {
160 Old.CurPtr = Old.End = nullptr;
161 Old.BytesAllocated = 0;
162 Old.Slabs.clear();
163 Old.CustomSizedSlabs.clear();
164 }
165
155166 ~BumpPtrAllocatorImpl() {
156167 DeallocateSlabs(Slabs.begin(), Slabs.end());
157168 DeallocateCustomSizedSlabs();
169 }
170
171 BumpPtrAllocatorImpl &operator=(BumpPtrAllocatorImpl &&RHS) {
172 DeallocateSlabs(Slabs.begin(), Slabs.end());
173 DeallocateCustomSizedSlabs();
174
175 CurPtr = RHS.CurPtr;
176 End = RHS.End;
177 BytesAllocated = RHS.BytesAllocated;
178 Slabs = std::move(RHS.Slabs);
179 CustomSizedSlabs = std::move(RHS.CustomSizedSlabs);
180 Allocator = std::move(RHS.Allocator);
181
182 RHS.CurPtr = RHS.End = nullptr;
183 RHS.BytesAllocated = 0;
184 RHS.Slabs.clear();
185 RHS.CustomSizedSlabs.clear();
186 return *this;
158187 }
159188
160189 /// \brief Deallocate all but the current slab and reset the current pointer
2727 EXPECT_EQ(2, b[0]);
2828 EXPECT_EQ(2, b[9]);
2929 EXPECT_EQ(3, *c);
30 EXPECT_EQ(1U, Alloc.GetNumSlabs());
31
32 BumpPtrAllocator Alloc2 = std::move(Alloc);
33 EXPECT_EQ(0U, Alloc.GetNumSlabs());
34 EXPECT_EQ(1U, Alloc2.GetNumSlabs());
35
36 // Make sure the old pointers still work. These are especially interesting
37 // under ASan or Valgrind.
38 EXPECT_EQ(1, *a);
39 EXPECT_EQ(2, b[0]);
40 EXPECT_EQ(2, b[9]);
41 EXPECT_EQ(3, *c);
42
43 Alloc = std::move(Alloc2);
44 EXPECT_EQ(0U, Alloc2.GetNumSlabs());
3045 EXPECT_EQ(1U, Alloc.GetNumSlabs());
3146 }
3247