Title: [261440] trunk/Source/WTF
Revision
261440
Author
[email protected]
Date
2020-05-09 15:29:50 -0700 (Sat, 09 May 2020)

Log Message

Add iterator checking to ListHashSet
https://bugs.webkit.org/show_bug.cgi?id=211669

Reviewed by Anders Carlsson.

HashSet and HashMap have iterator checking in debug builds.
Add similar checking to ListHashSet, controlled by the same
macro, CHECK_HASHTABLE_ITERATORS. Use WeakPtr to make the
implementation simple.

* wtf/ListHashSet.h: Make ListHashSet and ListHashSetNode derive
from CanMakeWeakPtr. Add m_weakSet and m_weakPosition members to
ListHashSetConstIterator, and assert their values at the appropriate
times so we will get a breakpoint or crash.

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (261439 => 261440)


--- trunk/Source/WTF/ChangeLog	2020-05-09 20:15:39 UTC (rev 261439)
+++ trunk/Source/WTF/ChangeLog	2020-05-09 22:29:50 UTC (rev 261440)
@@ -1,3 +1,20 @@
+2020-05-09  Darin Adler  <[email protected]>
+
+        Add iterator checking to ListHashSet
+        https://bugs.webkit.org/show_bug.cgi?id=211669
+
+        Reviewed by Anders Carlsson.
+
+        HashSet and HashMap have iterator checking in debug builds.
+        Add similar checking to ListHashSet, controlled by the same
+        macro, CHECK_HASHTABLE_ITERATORS. Use WeakPtr to make the
+        implementation simple.
+
+        * wtf/ListHashSet.h: Make ListHashSet and ListHashSetNode derive
+        from CanMakeWeakPtr. Add m_weakSet and m_weakPosition members to
+        ListHashSetConstIterator, and assert their values at the appropriate
+        times so we will get a breakpoint or crash.
+
 2020-05-08  Darin Adler  <[email protected]>
 
         Streamline MarkupAccumulator to improve efficiency a bit

Modified: trunk/Source/WTF/wtf/ListHashSet.h (261439 => 261440)


--- trunk/Source/WTF/wtf/ListHashSet.h	2020-05-09 20:15:39 UTC (rev 261439)
+++ trunk/Source/WTF/wtf/ListHashSet.h	2020-05-09 22:29:50 UTC (rev 261440)
@@ -23,6 +23,10 @@
 
 #include <wtf/HashSet.h>
 
+#if CHECK_HASHTABLE_ITERATORS
+#include <wtf/WeakPtr.h>
+#endif
+
 namespace WTF {
 
 // ListHashSet: Just like HashSet, this class provides a Set
@@ -45,7 +49,11 @@
 template<typename HashArg> struct ListHashSetNodeHashFunctions;
 template<typename HashArg> struct ListHashSetTranslator;
 
-template<typename ValueArg, typename HashArg = typename DefaultHash<ValueArg>::Hash> class ListHashSet final {
+template<typename ValueArg, typename HashArg = typename DefaultHash<ValueArg>::Hash> class ListHashSet final
+#if CHECK_HASHTABLE_ITERATORS
+    : public CanMakeWeakPtr<ListHashSet<ValueArg, HashArg>>
+#endif
+{
     WTF_MAKE_FAST_ALLOCATED;
 private:
     typedef ListHashSetNode<ValueArg> Node;
@@ -164,11 +172,14 @@
     Node* m_tail { nullptr };
 };
 
-template<typename ValueArg> struct ListHashSetNode {
-    WTF_MAKE_FAST_ALLOCATED;
-public:
-    template<typename T>
-    ListHashSetNode(T&& value)
+template<typename ValueArg> struct ListHashSetNode
+#if CHECK_HASHTABLE_ITERATORS
+    : CanMakeWeakPtr<ListHashSetNode<ValueArg>>
+#endif
+{
+    WTF_MAKE_STRUCT_FAST_ALLOCATED;
+
+    template<typename T> ListHashSetNode(T&& value)
         : m_value(std::forward<T>(value))
     {
     }
@@ -247,6 +258,10 @@
     ListHashSetConstIterator(const ListHashSetType* set, Node* position)
         : m_set(set)
         , m_position(position)
+#if CHECK_HASHTABLE_ITERATORS
+        , m_weakSet(makeWeakPtr(set))
+        , m_weakPosition(makeWeakPtr(position))
+#endif
     {
     }
 
@@ -263,6 +278,9 @@
 
     const ValueType* get() const
     {
+#if CHECK_HASHTABLE_ITERATORS
+        ASSERT(m_weakPosition);
+#endif
         return std::addressof(m_position->m_value);
     }
 
@@ -271,8 +289,14 @@
 
     const_iterator& operator++()
     {
+#if CHECK_HASHTABLE_ITERATORS
+        ASSERT(m_weakPosition);
+#endif
         ASSERT(m_position);
         m_position = m_position->m_next;
+#if CHECK_HASHTABLE_ITERATORS
+        m_weakPosition = makeWeakPtr(m_position);
+#endif
         return *this;
     }
 
@@ -280,11 +304,18 @@
 
     const_iterator& operator--()
     {
+#if CHECK_HASHTABLE_ITERATORS
+        ASSERT(m_weakSet);
+        m_weakPosition.get();
+#endif
         ASSERT(m_position != m_set->m_head);
         if (!m_position)
             m_position = m_set->m_tail;
         else
             m_position = m_position->m_prev;
+#if CHECK_HASHTABLE_ITERATORS
+        m_weakPosition = makeWeakPtr(m_position);
+#endif
         return *this;
     }
 
@@ -303,8 +334,12 @@
 private:
     Node* node() { return m_position; }
 
-    const ListHashSetType* m_set;
-    Node* m_position;
+    const ListHashSetType* m_set { nullptr };
+    Node* m_position { nullptr };
+#if CHECK_HASHTABLE_ITERATORS
+    WeakPtr<const ListHashSetType> m_weakSet;
+    WeakPtr<Node> m_weakPosition;
+#endif
 };
 
 template<typename HashFunctions>
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to