Title: [261467] trunk/Source
Revision
261467
Author
da...@apple.com
Date
2020-05-10 23:14:56 -0700 (Sun, 10 May 2020)

Log Message

Add copy constructor and assignment operator to Ref<>
https://bugs.webkit.org/show_bug.cgi?id=211705

Reviewed by Sam Weinig.

Source/WebCore:

* dom/BoundaryPoint.h: As a test of the change to Ref, remove the explicit
copy and move constructors and assignment operators, relying on the defaults
instead, which are now exactly what we want.

Source/WTF:

As recently discussed in some WebKit bug patch review, we think that Ref and RefPtr
should have the same design, except for whether the value can be null. Ref had a
more ambitious approach to avoiding reference count churn, requiring a
call to copyRef to make any copying explicit, partly by analogy with raw C
references, which also can't be copied (but they can't be moved either). We choose
to change Ref to match RefPtr and to take the risk of additional churn. This makes
it easier to use Ref in contexts like collection classes and structures. An
alternative would be to go the other direction, and make RefPtr require a call to
copyRef: seems like an obviously worse option.

* wtf/Ref.h: Add the copy constructor and assignment operator.
These follow a similar pattern to the move constructor and assignment operator.
Also add a deprecation comment before copyRef, which we will eventually remove.

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (261466 => 261467)


--- trunk/Source/WTF/ChangeLog	2020-05-11 05:16:16 UTC (rev 261466)
+++ trunk/Source/WTF/ChangeLog	2020-05-11 06:14:56 UTC (rev 261467)
@@ -1,3 +1,24 @@
+2020-05-10  Darin Adler  <da...@apple.com>
+
+        Add copy constructor and assignment operator to Ref<>
+        https://bugs.webkit.org/show_bug.cgi?id=211705
+
+        Reviewed by Sam Weinig.
+
+        As recently discussed in some WebKit bug patch review, we think that Ref and RefPtr
+        should have the same design, except for whether the value can be null. Ref had a
+        more ambitious approach to avoiding reference count churn, requiring a
+        call to copyRef to make any copying explicit, partly by analogy with raw C
+        references, which also can't be copied (but they can't be moved either). We choose
+        to change Ref to match RefPtr and to take the risk of additional churn. This makes
+        it easier to use Ref in contexts like collection classes and structures. An
+        alternative would be to go the other direction, and make RefPtr require a call to
+        copyRef: seems like an obviously worse option.
+
+        * wtf/Ref.h: Add the copy constructor and assignment operator.
+        These follow a similar pattern to the move constructor and assignment operator.
+        Also add a deprecation comment before copyRef, which we will eventually remove.
+
 2020-05-10  Tetsuharu Ohzeki  <tetsuharu.ohz...@gmail.com>
 
         Use alias template to define `match_constness_t` in Wtf/wtp/TypeCasts.h

Modified: trunk/Source/WTF/wtf/Ref.h (261466 => 261467)


--- trunk/Source/WTF/wtf/Ref.h	2020-05-11 05:16:16 UTC (rev 261466)
+++ trunk/Source/WTF/wtf/Ref.h	2020-05-11 06:14:56 UTC (rev 261467)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013-2019 Apple Inc. All rights reserved.
+ * Copyright (C) 2013-2020 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -67,10 +67,18 @@
         object.ref();
     }
 
-    // Use copyRef() instead.
-    Ref(const Ref& other) = delete;
-    template<typename X, typename Y> Ref(const Ref<X, Y>& other) = delete;
+    Ref(const Ref& other)
+        : m_ptr(other.ptr())
+    {
+        m_ptr->ref();
+    }
 
+    template<typename X, typename Y> Ref(const Ref<X, Y>& other)
+        : m_ptr(other.ptr())
+    {
+        m_ptr->ref();
+    }
+
     Ref(Ref&& other)
         : m_ptr(&other.leakRef())
     {
@@ -88,9 +96,8 @@
     Ref& operator=(Ref&&);
     template<typename X, typename Y> Ref& operator=(Ref<X, Y>&&);
 
-    // Use copyRef() and the move assignment operators instead.
-    Ref& operator=(const Ref&) = delete;
-    template<typename X, typename Y> Ref& operator=(const Ref<X, Y>&) = delete;
+    Ref& operator=(const Ref&);
+    template<typename X, typename Y> Ref& operator=(const Ref<X, Y>&);
 
     template<typename X, typename Y> void swap(Ref<X, Y>&);
 
@@ -124,6 +131,7 @@
 
     template<typename X, typename Y> Ref<T, PtrTraits> replace(Ref<X, Y>&&) WARN_UNUSED_RETURN;
 
+    // The following function is deprecated.
     Ref copyRef() && = delete;
     Ref copyRef() const & WARN_UNUSED_RETURN { return Ref(*m_ptr); }
 
@@ -188,7 +196,32 @@
 }
 
 template<typename T, typename U>
+inline Ref<T, U>& Ref<T, U>::operator=(const Ref& reference)
+{
+#if ASAN_ENABLED
+    if (__asan_address_is_poisoned(this))
+        __asan_unpoison_memory_region(this, sizeof(*this));
+#endif
+    Ref copiedReference = reference;
+    swap(copiedReference);
+    return *this;
+}
+
+template<typename T, typename U>
 template<typename X, typename Y>
+inline Ref<T, U>& Ref<T, U>::operator=(const Ref<X, Y>& reference)
+{
+#if ASAN_ENABLED
+    if (__asan_address_is_poisoned(this))
+        __asan_unpoison_memory_region(this, sizeof(*this));
+#endif
+    Ref copiedReference = reference;
+    swap(copiedReference);
+    return *this;
+}
+
+template<typename T, typename U>
+template<typename X, typename Y>
 inline void Ref<T, U>::swap(Ref<X, Y>& other)
 {
     U::swap(m_ptr, other.m_ptr);

Modified: trunk/Source/WebCore/ChangeLog (261466 => 261467)


--- trunk/Source/WebCore/ChangeLog	2020-05-11 05:16:16 UTC (rev 261466)
+++ trunk/Source/WebCore/ChangeLog	2020-05-11 06:14:56 UTC (rev 261467)
@@ -1,3 +1,14 @@
+2020-05-10  Darin Adler  <da...@apple.com>
+
+        Add copy constructor and assignment operator to Ref<>
+        https://bugs.webkit.org/show_bug.cgi?id=211705
+
+        Reviewed by Sam Weinig.
+
+        * dom/BoundaryPoint.h: As a test of the change to Ref, remove the explicit
+        copy and move constructors and assignment operators, relying on the defaults
+        instead, which are now exactly what we want.
+
 2020-05-10  Michael Catanzaro  <mcatanz...@gnome.org>
 
         Update user agent quirk for bankofamerica.com

Modified: trunk/Source/WebCore/dom/BoundaryPoint.h (261466 => 261467)


--- trunk/Source/WebCore/dom/BoundaryPoint.h	2020-05-11 05:16:16 UTC (rev 261466)
+++ trunk/Source/WebCore/dom/BoundaryPoint.h	2020-05-11 06:14:56 UTC (rev 261467)
@@ -35,12 +35,6 @@
 
     BoundaryPoint(Ref<Node>&&, unsigned);
 
-    // Unlike Ref, we allow copying a BoundaryPoint without an explicit copyRef().
-    BoundaryPoint(const BoundaryPoint&);
-    BoundaryPoint(BoundaryPoint&&) = default;
-    BoundaryPoint& operator=(const BoundaryPoint&);
-    BoundaryPoint& operator=(BoundaryPoint&&) = default;
-
     Document& document() const;
 };
 
@@ -55,19 +49,6 @@
 {
 }
 
-inline BoundaryPoint::BoundaryPoint(const BoundaryPoint& other)
-    : container(other.container.copyRef())
-    , offset(other.offset)
-{
-}
-
-inline BoundaryPoint& BoundaryPoint::operator=(const BoundaryPoint& other)
-{
-    container = other.container.copyRef();
-    offset = other.offset;
-    return *this;
-}
-
 inline Document& BoundaryPoint::document() const
 {
     return container->document();
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to