[llvm] [Support] Recycler: Implement move constructor (PR #120555)
Akshat Oke via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 6 22:18:03 PST 2025
https://github.com/optimisan updated https://github.com/llvm/llvm-project/pull/120555
>From 7ff8b4956e334277c675f98d99d6ef11ba19d6d2 Mon Sep 17 00:00:00 2001
From: Akshat Oke <Akshat.Oke at amd.com>
Date: Thu, 19 Dec 2024 06:57:46 +0000
Subject: [PATCH 1/2] [Support] Recycler: Implement move constructor
---
llvm/include/llvm/Support/Recycler.h | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/llvm/include/llvm/Support/Recycler.h b/llvm/include/llvm/Support/Recycler.h
index bbd9ae321ae30c6..693c6559ff2fdc3 100644
--- a/llvm/include/llvm/Support/Recycler.h
+++ b/llvm/include/llvm/Support/Recycler.h
@@ -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
>From e6fab53bc2d3514e2f5d610c2a054cdf8f4b92b2 Mon Sep 17 00:00:00 2001
From: Akshat Oke <Akshat.Oke at amd.com>
Date: Wed, 1 Jan 2025 06:35:09 +0000
Subject: [PATCH 2/2] Add test
---
llvm/unittests/Support/CMakeLists.txt | 1 +
llvm/unittests/Support/RecyclerTest.cpp | 47 +++++++++++++++++++++++++
2 files changed, 48 insertions(+)
create mode 100644 llvm/unittests/Support/RecyclerTest.cpp
diff --git a/llvm/unittests/Support/CMakeLists.txt b/llvm/unittests/Support/CMakeLists.txt
index d64f89847aa8e7c..6de816582644203 100644
--- a/llvm/unittests/Support/CMakeLists.txt
+++ b/llvm/unittests/Support/CMakeLists.txt
@@ -69,6 +69,7 @@ add_llvm_unittest(SupportTests
PerThreadBumpPtrAllocatorTest.cpp
ProcessTest.cpp
ProgramTest.cpp
+ RecyclerTest.cpp
RegexTest.cpp
ReverseIterationTest.cpp
ReplaceFileTest.cpp
diff --git a/llvm/unittests/Support/RecyclerTest.cpp b/llvm/unittests/Support/RecyclerTest.cpp
new file mode 100644
index 000000000000000..a33506b47ebeae3
--- /dev/null
+++ b/llvm/unittests/Support/RecyclerTest.cpp
@@ -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
More information about the llvm-commits
mailing list