[llvm] [llvm][ADT] Implement `IntrusiveRefCntPtr::useCount()` (PR #65502)

Jan Svoboda via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 6 10:10:00 PDT 2023


https://github.com/jansvoboda11 updated https://github.com/llvm/llvm-project/pull/65502:

>From e41d54104cff710a9f6744bfcda9962386558b17 Mon Sep 17 00:00:00 2001
From: Jan Svoboda <jan_svoboda at apple.com>
Date: Fri, 1 Sep 2023 15:07:23 -0700
Subject: [PATCH] [llvm][ADT] Implement `IntrusiveRefCntPtr::useCount()`

This function has the same semantics as `std::shared_ptr<T>::use_count()`, and enables implementing copy-on-write semantics.
---
 llvm/include/llvm/ADT/IntrusiveRefCntPtr.h    |  9 +++++++++
 llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp | 13 +++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h b/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
index e41eb0639ce30eb..90349e02014dd66 100644
--- a/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
+++ b/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
@@ -93,6 +93,8 @@ template <class Derived> class RefCountedBase {
 #endif
 
 public:
+  unsigned UseCount() const { return RefCount; }
+
   void Retain() const { ++RefCount; }
 
   void Release() const {
@@ -124,6 +126,8 @@ template <class Derived> class ThreadSafeRefCountedBase {
 #endif
 
 public:
+  unsigned UseCount() const { return RefCount.load(std::memory_order_relaxed); }
+
   void Retain() const { RefCount.fetch_add(1, std::memory_order_relaxed); }
 
   void Release() const {
@@ -155,6 +159,7 @@ template <class Derived> class ThreadSafeRefCountedBase {
 /// Bar.h could use IntrusiveRefCntPtr<Foo>, although it still couldn't call any
 /// functions on Foo itself, because Foo would be an incomplete type.
 template <typename T> struct IntrusiveRefCntPtrInfo {
+  static unsigned useCount(const T *obj) { return obj->UseCount(); }
   static void retain(T *obj) { obj->Retain(); }
   static void release(T *obj) { obj->Release(); }
 };
@@ -213,6 +218,10 @@ template <typename T> class IntrusiveRefCntPtr {
 
   void resetWithoutRelease() { Obj = nullptr; }
 
+  unsigned useCount() const {
+    return Obj ? IntrusiveRefCntPtrInfo<T>::useCount(Obj) : 0;
+  }
+
 private:
   void retain() {
     if (Obj)
diff --git a/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp b/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp
index 45b8028aef20089..af2c3760bc99af2 100644
--- a/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp
+++ b/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp
@@ -140,4 +140,17 @@ TEST(IntrusiveRefCntPtr, InteropsWithConvertible) {
   EXPECT_EQ(P4, X4.get());
 }
 
+TEST(IntrusiveRefCntPtrTest, Unique) {
+  IntrusiveRefCntPtr<X> X1;
+  EXPECT_EQ(X1.useCount(), 0);
+  X1 = new X();
+  EXPECT_EQ(X1.useCount(), 1);
+  {
+    IntrusiveRefCntPtr<X> X2 = X1;
+    EXPECT_EQ(X1.useCount(), 2);
+    EXPECT_EQ(X2.useCount(), 2);
+  }
+  EXPECT_EQ(X1.useCount(), 1);
+}
+
 } // end namespace llvm



More information about the llvm-commits mailing list