Skip to content

Commit bc51a2e

Browse files
authored
[Support] Recycler: Implement move constructor (#120555)
Discovered missing while running RAGreedy through the NPM which relies on moving analyses to the cache.
1 parent a774e7f commit bc51a2e

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

llvm/include/llvm/Support/Recycler.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ class Recycler {
6060
// clear() before deleting the Recycler.
6161
assert(!FreeList && "Non-empty recycler deleted!");
6262
}
63+
Recycler(const Recycler &) = delete;
64+
Recycler(Recycler &&Other)
65+
: FreeList(std::exchange(Other.FreeList, nullptr)) {}
66+
Recycler() = default;
6367

6468
/// clear - Release all the tracked allocations to the allocator. The
6569
/// recycler must be free of any tracked allocations before being

llvm/unittests/Support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ add_llvm_unittest(SupportTests
6969
PerThreadBumpPtrAllocatorTest.cpp
7070
ProcessTest.cpp
7171
ProgramTest.cpp
72+
RecyclerTest.cpp
7273
RegexTest.cpp
7374
ReverseIterationTest.cpp
7475
ReplaceFileTest.cpp
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===--- unittest/Support/RecyclerTest.cpp --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/Support/Recycler.h"
10+
#include "llvm/Support/AllocatorBase.h"
11+
#include "gtest/gtest.h"
12+
13+
using namespace llvm;
14+
15+
namespace {
16+
17+
struct Object8 {
18+
char Data[8];
19+
};
20+
21+
class DecoratedMallocAllocator : public MallocAllocator {
22+
public:
23+
int DeallocCount = 0;
24+
25+
template <typename T> void Deallocate(T *Ptr) {
26+
DeallocCount++;
27+
MallocAllocator::Deallocate(Ptr);
28+
}
29+
};
30+
31+
TEST(RecyclerTest, MoveConstructor) {
32+
DecoratedMallocAllocator Allocator;
33+
Recycler<Object8> R;
34+
Object8 *A1 = R.Allocate(Allocator);
35+
Object8 *A2 = R.Allocate(Allocator);
36+
R.Deallocate(Allocator, A1);
37+
R.Deallocate(Allocator, A2);
38+
Recycler<Object8> R2(std::move(R));
39+
Object8 *A3 = R2.Allocate(Allocator);
40+
R2.Deallocate(Allocator, A3);
41+
R.clear(Allocator); // Should not deallocate anything as it was moved from.
42+
EXPECT_EQ(Allocator.DeallocCount, 0);
43+
R2.clear(Allocator);
44+
EXPECT_EQ(Allocator.DeallocCount, 2);
45+
}
46+
47+
} // end anonymous namespace

0 commit comments

Comments
 (0)