mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-08-08 19:25:47 +00:00
[Allocator] Make BumpPtrAllocator movable and move assignable.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206372 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -138,9 +138,6 @@ template <typename AllocatorT = MallocAllocator, size_t SlabSize = 4096,
|
|||||||
class BumpPtrAllocatorImpl
|
class BumpPtrAllocatorImpl
|
||||||
: public AllocatorBase<
|
: public AllocatorBase<
|
||||||
BumpPtrAllocatorImpl<AllocatorT, SlabSize, SizeThreshold>> {
|
BumpPtrAllocatorImpl<AllocatorT, SlabSize, SizeThreshold>> {
|
||||||
BumpPtrAllocatorImpl(const BumpPtrAllocatorImpl &) LLVM_DELETED_FUNCTION;
|
|
||||||
void operator=(const BumpPtrAllocatorImpl &) LLVM_DELETED_FUNCTION;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static_assert(SizeThreshold <= SlabSize,
|
static_assert(SizeThreshold <= SlabSize,
|
||||||
"The SizeThreshold must be at most the SlabSize to ensure "
|
"The SizeThreshold must be at most the SlabSize to ensure "
|
||||||
@@ -153,11 +150,43 @@ public:
|
|||||||
BumpPtrAllocatorImpl(T &&Allocator)
|
BumpPtrAllocatorImpl(T &&Allocator)
|
||||||
: CurPtr(nullptr), End(nullptr), BytesAllocated(0),
|
: CurPtr(nullptr), End(nullptr), BytesAllocated(0),
|
||||||
Allocator(std::forward<T &&>(Allocator)) {}
|
Allocator(std::forward<T &&>(Allocator)) {}
|
||||||
|
|
||||||
|
// Manually implement a move constructor as we must clear the old allocators
|
||||||
|
// slabs as a matter of correctness.
|
||||||
|
BumpPtrAllocatorImpl(BumpPtrAllocatorImpl &&Old)
|
||||||
|
: CurPtr(Old.CurPtr), End(Old.End), Slabs(std::move(Old.Slabs)),
|
||||||
|
CustomSizedSlabs(std::move(Old.CustomSizedSlabs)),
|
||||||
|
BytesAllocated(Old.BytesAllocated),
|
||||||
|
Allocator(std::move(Old.Allocator)) {
|
||||||
|
Old.CurPtr = Old.End = nullptr;
|
||||||
|
Old.BytesAllocated = 0;
|
||||||
|
Old.Slabs.clear();
|
||||||
|
Old.CustomSizedSlabs.clear();
|
||||||
|
}
|
||||||
|
|
||||||
~BumpPtrAllocatorImpl() {
|
~BumpPtrAllocatorImpl() {
|
||||||
DeallocateSlabs(Slabs.begin(), Slabs.end());
|
DeallocateSlabs(Slabs.begin(), Slabs.end());
|
||||||
DeallocateCustomSizedSlabs();
|
DeallocateCustomSizedSlabs();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BumpPtrAllocatorImpl &operator=(BumpPtrAllocatorImpl &&RHS) {
|
||||||
|
DeallocateSlabs(Slabs.begin(), Slabs.end());
|
||||||
|
DeallocateCustomSizedSlabs();
|
||||||
|
|
||||||
|
CurPtr = RHS.CurPtr;
|
||||||
|
End = RHS.End;
|
||||||
|
BytesAllocated = RHS.BytesAllocated;
|
||||||
|
Slabs = std::move(RHS.Slabs);
|
||||||
|
CustomSizedSlabs = std::move(RHS.CustomSizedSlabs);
|
||||||
|
Allocator = std::move(RHS.Allocator);
|
||||||
|
|
||||||
|
RHS.CurPtr = RHS.End = nullptr;
|
||||||
|
RHS.BytesAllocated = 0;
|
||||||
|
RHS.Slabs.clear();
|
||||||
|
RHS.CustomSizedSlabs.clear();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
/// \brief Deallocate all but the current slab and reset the current pointer
|
/// \brief Deallocate all but the current slab and reset the current pointer
|
||||||
/// to the beginning of it, freeing all memory allocated so far.
|
/// to the beginning of it, freeing all memory allocated so far.
|
||||||
void Reset() {
|
void Reset() {
|
||||||
|
@@ -29,6 +29,21 @@ TEST(AllocatorTest, Basics) {
|
|||||||
EXPECT_EQ(2, b[9]);
|
EXPECT_EQ(2, b[9]);
|
||||||
EXPECT_EQ(3, *c);
|
EXPECT_EQ(3, *c);
|
||||||
EXPECT_EQ(1U, Alloc.GetNumSlabs());
|
EXPECT_EQ(1U, Alloc.GetNumSlabs());
|
||||||
|
|
||||||
|
BumpPtrAllocator Alloc2 = std::move(Alloc);
|
||||||
|
EXPECT_EQ(0U, Alloc.GetNumSlabs());
|
||||||
|
EXPECT_EQ(1U, Alloc2.GetNumSlabs());
|
||||||
|
|
||||||
|
// Make sure the old pointers still work. These are especially interesting
|
||||||
|
// under ASan or Valgrind.
|
||||||
|
EXPECT_EQ(1, *a);
|
||||||
|
EXPECT_EQ(2, b[0]);
|
||||||
|
EXPECT_EQ(2, b[9]);
|
||||||
|
EXPECT_EQ(3, *c);
|
||||||
|
|
||||||
|
Alloc = std::move(Alloc2);
|
||||||
|
EXPECT_EQ(0U, Alloc2.GetNumSlabs());
|
||||||
|
EXPECT_EQ(1U, Alloc.GetNumSlabs());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allocate enough bytes to create three slabs.
|
// Allocate enough bytes to create three slabs.
|
||||||
|
Reference in New Issue
Block a user