1 // Copyright 2009 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_CONTAINERS_LINKED_LIST_H_ 6 #define BASE_CONTAINERS_LINKED_LIST_H_ 7 8 #include "base/base_export.h" 9 #include "base/memory/raw_ptr_exclusion.h" 10 11 // Simple LinkedList type. (See the Q&A section to understand how this 12 // differs from std::list). 13 // 14 // To use, start by declaring the class which will be contained in the linked 15 // list, as extending LinkNode (this gives it next/previous pointers). 16 // 17 // class MyNodeType : public LinkNode<MyNodeType> { 18 // ... 19 // }; 20 // 21 // Next, to keep track of the list's head/tail, use a LinkedList instance: 22 // 23 // LinkedList<MyNodeType> list; 24 // 25 // To add elements to the list, use any of LinkedList::Append, 26 // LinkNode::InsertBefore, or LinkNode::InsertAfter: 27 // 28 // LinkNode<MyNodeType>* n1 = ...; 29 // LinkNode<MyNodeType>* n2 = ...; 30 // LinkNode<MyNodeType>* n3 = ...; 31 // 32 // list.Append(n1); 33 // list.Append(n3); 34 // n2->InsertBefore(n3); 35 // 36 // Lastly, to iterate through the linked list forwards: 37 // 38 // for (LinkNode<MyNodeType>* node = list.head(); 39 // node != list.end(); 40 // node = node->next()) { 41 // MyNodeType* value = node->value(); 42 // ... 43 // } 44 // 45 // Or to iterate the linked list backwards: 46 // 47 // for (LinkNode<MyNodeType>* node = list.tail(); 48 // node != list.end(); 49 // node = node->previous()) { 50 // MyNodeType* value = node->value(); 51 // ... 52 // } 53 // 54 // Questions and Answers: 55 // 56 // Q. Should I use std::list or base::LinkedList? 57 // 58 // A. The main reason to use base::LinkedList over std::list is 59 // performance. If you don't care about the performance differences 60 // then use an STL container, as it makes for better code readability. 61 // 62 // Comparing the performance of base::LinkedList<T> to std::list<T*>: 63 // 64 // * Erasing an element of type T* from base::LinkedList<T> is 65 // an O(1) operation. Whereas for std::list<T*> it is O(n). 66 // That is because with std::list<T*> you must obtain an 67 // iterator to the T* element before you can call erase(iterator). 68 // 69 // * Insertion operations with base::LinkedList<T> never require 70 // heap allocations. 71 // 72 // Q. How does base::LinkedList implementation differ from std::list? 73 // 74 // A. Doubly-linked lists are made up of nodes that contain "next" and 75 // "previous" pointers that reference other nodes in the list. 76 // 77 // With base::LinkedList<T>, the type being inserted already reserves 78 // space for the "next" and "previous" pointers (base::LinkNode<T>*). 79 // Whereas with std::list<T> the type can be anything, so the implementation 80 // needs to glue on the "next" and "previous" pointers using 81 // some internal node type. 82 83 namespace base { 84 85 namespace internal { 86 87 // Base class for LinkNode<T> type 88 class BASE_EXPORT LinkNodeBase { 89 public: 90 void RemoveFromList(); 91 92 protected: 93 LinkNodeBase(); 94 LinkNodeBase(LinkNodeBase* previous, LinkNodeBase* next); 95 LinkNodeBase(LinkNodeBase&& rhs); 96 LinkNodeBase(const LinkNodeBase&) = delete; 97 ~LinkNodeBase() = default; 98 99 LinkNodeBase& operator=(const LinkNodeBase&) = delete; 100 101 // Calling these with |e| as a different LinkNode type as |this| is 102 // unsafe. These are protected and only called from LinkNode<T> to 103 // ensure safety. 104 void InsertBeforeBase(LinkNodeBase* e); 105 void InsertAfterBase(LinkNodeBase* e); 106 previous_base()107 LinkNodeBase* previous_base() const { return previous_; } next_base()108 LinkNodeBase* next_base() const { return next_; } 109 110 private: 111 // `previous_` and `next_` are not a raw_ptr<...> for performance reasons: 112 // on-stack pointer + a large number of non-PA pointees through WeakLinkNode + 113 // based on analysis of sampling profiler data and tab_search:top100:2020. 114 RAW_PTR_EXCLUSION LinkNodeBase* previous_ = nullptr; 115 RAW_PTR_EXCLUSION LinkNodeBase* next_ = nullptr; 116 }; 117 118 } // namespace internal 119 120 template <typename T> 121 class LinkNode : public internal::LinkNodeBase { 122 public: 123 LinkNode() = default; LinkNode(LinkNode<T> * previous,LinkNode<T> * next)124 LinkNode(LinkNode<T>* previous, LinkNode<T>* next) 125 : internal::LinkNodeBase(previous, next) {} 126 127 LinkNode(LinkNode<T>&&) = default; 128 129 LinkNode(const LinkNode&) = delete; 130 LinkNode& operator=(const LinkNode&) = delete; 131 132 // Insert |this| into the linked list, before |e|. |this| must not 133 // already be in a list. InsertBefore(LinkNode<T> * e)134 void InsertBefore(LinkNode<T>* e) { InsertBeforeBase(e); } 135 136 // Insert |this| into the linked list, after |e|. |this| must not 137 // already be in a list. InsertAfter(LinkNode<T> * e)138 void InsertAfter(LinkNode<T>* e) { InsertAfterBase(e); } 139 previous()140 LinkNode<T>* previous() const { 141 return static_cast<LinkNode<T>*>(previous_base()); 142 } 143 next()144 LinkNode<T>* next() const { return static_cast<LinkNode<T>*>(next_base()); } 145 146 // Cast from the node-type to the value type. value()147 const T* value() const { 148 return static_cast<const T*>(this); 149 } 150 value()151 T* value() { 152 return static_cast<T*>(this); 153 } 154 }; 155 156 template <typename T> 157 class LinkedList { 158 public: 159 // The "root" node is self-referential, and forms the basis of a circular 160 // list (root_.next() will point back to the start of the list, 161 // and root_->previous() wraps around to the end of the list). LinkedList()162 LinkedList() : root_(&root_, &root_) {} 163 LinkedList(const LinkedList&) = delete; 164 LinkedList& operator=(const LinkedList&) = delete; 165 166 // Appends |e| to the end of the linked list. Append(LinkNode<T> * e)167 void Append(LinkNode<T>* e) { 168 e->InsertBefore(&root_); 169 } 170 head()171 LinkNode<T>* head() const { 172 return root_.next(); 173 } 174 tail()175 LinkNode<T>* tail() const { 176 return root_.previous(); 177 } 178 end()179 const LinkNode<T>* end() const { 180 return &root_; 181 } 182 empty()183 bool empty() const { return head() == end(); } 184 185 private: 186 LinkNode<T> root_; 187 }; 188 189 } // namespace base 190 191 #endif // BASE_CONTAINERS_LINKED_LIST_H_ 192