Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Support] Recycler: Implement move constructor #120555

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions llvm/include/llvm/Support/Recycler.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class Recycler {
// clear() before deleting the Recycler.
assert(!FreeList && "Non-empty recycler deleted!");
}
Recycler(const Recycler &) = delete;
Recycler(Recycler &&Other)
: FreeList(std::exchange(Other.FreeList, nullptr)) {}
Recycler() = default;

/// clear - Release all the tracked allocations to the allocator. The
/// recycler must be free of any tracked allocations before being
Expand Down
1 change: 1 addition & 0 deletions llvm/unittests/Support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ add_llvm_unittest(SupportTests
PerThreadBumpPtrAllocatorTest.cpp
ProcessTest.cpp
ProgramTest.cpp
RecyclerTest.cpp
RegexTest.cpp
ReverseIterationTest.cpp
ReplaceFileTest.cpp
Expand Down
47 changes: 47 additions & 0 deletions llvm/unittests/Support/RecyclerTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//===--- unittest/Support/RecyclerTest.cpp --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/Support/Recycler.h"
#include "llvm/Support/AllocatorBase.h"
#include "gtest/gtest.h"

using namespace llvm;

namespace {

struct Object8 {
char Data[8];
};

class DecoratedMallocAllocator : public MallocAllocator {
public:
int DeallocCount = 0;

template <typename T> void Deallocate(T *Ptr) {
DeallocCount++;
MallocAllocator::Deallocate(Ptr);
}
};

TEST(RecyclerTest, MoveConstructor) {
DecoratedMallocAllocator Allocator;
Recycler<Object8> R;
Object8 *A1 = R.Allocate(Allocator);
Object8 *A2 = R.Allocate(Allocator);
R.Deallocate(Allocator, A1);
R.Deallocate(Allocator, A2);
Recycler<Object8> R2(std::move(R));
Object8 *A3 = R2.Allocate(Allocator);
R2.Deallocate(Allocator, A3);
R.clear(Allocator); // Should not deallocate anything as it was moved from.
EXPECT_EQ(Allocator.DeallocCount, 0);
R2.clear(Allocator);
EXPECT_EQ(Allocator.DeallocCount, 2);
}

} // end anonymous namespace
Loading