| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
/* A type-safe doubly-linked list class. */ |
| 8 |
|
| 9 |
/* |
| 10 |
* The classes LinkedList<T> and LinkedListElement<T> together form a |
| 11 |
* convenient, type-safe doubly-linked list implementation. |
| 12 |
* |
| 13 |
* The class T which will be inserted into the linked list must inherit from |
| 14 |
* LinkedListElement<T>. A given object may be in only one linked list at a |
| 15 |
* time. |
| 16 |
* |
| 17 |
* A LinkedListElement automatically removes itself from the list upon |
| 18 |
* destruction, and a LinkedList will fatally assert in debug builds if it's |
| 19 |
* non-empty when it's destructed. |
| 20 |
* |
| 21 |
* For example, you might use LinkedList in a simple observer list class as |
| 22 |
* follows. |
| 23 |
* |
| 24 |
* class Observer : public LinkedListElement<Observer> |
| 25 |
* { |
| 26 |
* public: |
| 27 |
* void observe(char* aTopic) { ... } |
| 28 |
* }; |
| 29 |
* |
| 30 |
* class ObserverContainer |
| 31 |
* { |
| 32 |
* private: |
| 33 |
* LinkedList<Observer> list; |
| 34 |
* |
| 35 |
* public: |
| 36 |
* void addObserver(Observer* aObserver) |
| 37 |
* { |
| 38 |
* // Will assert if |aObserver| is part of another list. |
| 39 |
* list.insertBack(aObserver); |
| 40 |
* } |
| 41 |
* |
| 42 |
* void removeObserver(Observer* aObserver) |
| 43 |
* { |
| 44 |
* // Will assert if |aObserver| is not part of some list. |
| 45 |
* aObserver.remove(); |
| 46 |
* // Or, will assert if |aObserver| is not part of |list| specifically. |
| 47 |
* // aObserver.removeFrom(list); |
| 48 |
* } |
| 49 |
* |
| 50 |
* void notifyObservers(char* aTopic) |
| 51 |
* { |
| 52 |
* for (Observer* o = list.getFirst(); o != nullptr; o = o->getNext()) { |
| 53 |
* o->observe(aTopic); |
| 54 |
* } |
| 55 |
* } |
| 56 |
* }; |
| 57 |
* |
| 58 |
* Additionally, the class AutoCleanLinkedList<T> is a LinkedList<T> that will |
| 59 |
* remove and delete each element still within itself upon destruction. Note |
| 60 |
* that because each element is deleted, elements must have been allocated |
| 61 |
* using |new|. |
| 62 |
*/ |
| 63 |
|
| 64 |
#ifndef mozilla_LinkedList_h |
| 65 |
#define mozilla_LinkedList_h |
| 66 |
|
| 67 |
#include <algorithm> |
| 68 |
#include <utility> |
| 69 |
|
| 70 |
#include "mozilla/Assertions.h" |
| 71 |
#include "mozilla/Attributes.h" |
| 72 |
#include "mozilla/MemoryReporting.h" |
| 73 |
#include "mozilla/RefPtr.h" |
| 74 |
|
| 75 |
#ifdef __cplusplus |
| 76 |
|
| 77 |
namespace mozilla { |
| 78 |
|
| 79 |
template <typename T> |
| 80 |
class LinkedListElement; |
| 81 |
|
| 82 |
namespace detail { |
| 83 |
|
| 84 |
/** |
| 85 |
* LinkedList supports refcounted elements using this adapter class. Clients |
| 86 |
* using LinkedList<RefPtr<T>> will get a data structure that holds a strong |
| 87 |
* reference to T as long as T is in the list. |
| 88 |
*/ |
| 89 |
template <typename T> |
| 90 |
struct LinkedListElementTraits { |
| 91 |
typedef T* RawType; |
| 92 |
typedef const T* ConstRawType; |
| 93 |
typedef T* ClientType; |
| 94 |
typedef const T* ConstClientType; |
| 95 |
|
| 96 |
// These static methods are called when an element is added to or removed from |
| 97 |
// a linked list. It can be used to keep track ownership in lists that are |
| 98 |
// supposed to own their elements. If elements are transferred from one list |
| 99 |
// to another, no enter or exit calls happen since the elements still belong |
| 100 |
// to a list. |
| 101 |
static void enterList(LinkedListElement<T>* elt) {} |
| 102 |
static void exitList(LinkedListElement<T>* elt) {} |
| 103 |
|
| 104 |
// This method is called when AutoCleanLinkedList cleans itself |
| 105 |
// during destruction. It can be used to call delete on elements if |
| 106 |
// the list is the sole owner. |
| 107 |
static void cleanElement(LinkedListElement<T>* elt) { delete elt->asT(); } |
| 108 |
}; |
| 109 |
|
| 110 |
template <typename T> |
| 111 |
struct LinkedListElementTraits<RefPtr<T>> { |
| 112 |
typedef T* RawType; |
| 113 |
typedef const T* ConstRawType; |
| 114 |
typedef RefPtr<T> ClientType; |
| 115 |
typedef RefPtr<const T> ConstClientType; |
| 116 |
|
| 117 |
static void enterList(LinkedListElement<RefPtr<T>>* elt) { |
| 118 |
elt->asT()->AddRef(); |
| 119 |
} |
| 120 |
static void exitList(LinkedListElement<RefPtr<T>>* elt) { |
| 121 |
elt->asT()->Release(); |
| 122 |
} |
| 123 |
static void cleanElement(LinkedListElement<RefPtr<T>>* elt) {} |
| 124 |
}; |
| 125 |
|
| 126 |
} /* namespace detail */ |
| 127 |
|
| 128 |
template <typename T> |
| 129 |
class LinkedList; |
| 130 |
|
| 131 |
template <typename T> |
| 132 |
class LinkedListElement { |
| 133 |
typedef typename detail::LinkedListElementTraits<T> Traits; |
| 134 |
typedef typename Traits::RawType RawType; |
| 135 |
typedef typename Traits::ConstRawType ConstRawType; |
| 136 |
typedef typename Traits::ClientType ClientType; |
| 137 |
typedef typename Traits::ConstClientType ConstClientType; |
| 138 |
|
| 139 |
/* |
| 140 |
* It's convenient that we return nullptr when getNext() or getPrevious() |
| 141 |
* hits the end of the list, but doing so costs an extra word of storage in |
| 142 |
* each linked list node (to keep track of whether |this| is the sentinel |
| 143 |
* node) and a branch on this value in getNext/getPrevious. |
| 144 |
* |
| 145 |
* We could get rid of the extra word of storage by shoving the "is |
| 146 |
* sentinel" bit into one of the pointers, although this would, of course, |
| 147 |
* have performance implications of its own. |
| 148 |
* |
| 149 |
* But the goal here isn't to win an award for the fastest or slimmest |
| 150 |
* linked list; rather, we want a *convenient* linked list. So we won't |
| 151 |
* waste time guessing which micro-optimization strategy is best. |
| 152 |
* |
| 153 |
* |
| 154 |
* Speaking of unnecessary work, it's worth addressing here why we wrote |
| 155 |
* mozilla::LinkedList in the first place, instead of using stl::list. |
| 156 |
* |
| 157 |
* The key difference between mozilla::LinkedList and stl::list is that |
| 158 |
* mozilla::LinkedList stores the mPrev/mNext pointers in the object itself, |
| 159 |
* while stl::list stores the mPrev/mNext pointers in a list element which |
| 160 |
* itself points to the object being stored. |
| 161 |
* |
| 162 |
* mozilla::LinkedList's approach makes it harder to store an object in more |
| 163 |
* than one list. But the upside is that you can call next() / prev() / |
| 164 |
* remove() directly on the object. With stl::list, you'd need to store a |
| 165 |
* pointer to its iterator in the object in order to accomplish this. Not |
| 166 |
* only would this waste space, but you'd have to remember to update that |
| 167 |
* pointer every time you added or removed the object from a list. |
| 168 |
* |
| 169 |
* In-place, constant-time removal is a killer feature of doubly-linked |
| 170 |
* lists, and supporting this painlessly was a key design criterion. |
| 171 |
*/ |
| 172 |
|
| 173 |
private: |
| 174 |
LinkedListElement* mNext; |
| 175 |
LinkedListElement* mPrev; |
| 176 |
const bool mIsSentinel; |
| 177 |
|
| 178 |
public: |
| 179 |
LinkedListElement() : mNext(this), mPrev(this), mIsSentinel(false) {} |
| 180 |
|
| 181 |
/* |
| 182 |
* Moves |aOther| into |*this|. If |aOther| is already in a list, then |
| 183 |
* |aOther| is removed from the list and replaced by |*this|. |
| 184 |
*/ |
| 185 |
LinkedListElement(LinkedListElement<T>&& aOther) |
| 186 |
: mIsSentinel(aOther.mIsSentinel) { |
| 187 |
adjustLinkForMove(std::move(aOther)); |
| 188 |
} |
| 189 |
|
| 190 |
LinkedListElement& operator=(LinkedListElement<T>&& aOther) { |
| 191 |
MOZ_ASSERT(mIsSentinel == aOther.mIsSentinel, "Mismatch NodeKind!"); |
| 192 |
MOZ_ASSERT(!isInList(), |
| 193 |
"Assigning to an element in a list messes up that list!"); |
| 194 |
adjustLinkForMove(std::move(aOther)); |
| 195 |
return *this; |
| 196 |
} |
| 197 |
|
| 198 |
~LinkedListElement() { |
| 199 |
if (!mIsSentinel && isInList()) { |
| 200 |
remove(); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
/* |
| 205 |
* Get the next element in the list, or nullptr if this is the last element |
| 206 |
* in the list. |
| 207 |
*/ |
| 208 |
RawType getNext() { return mNext->asT(); } |
| 209 |
ConstRawType getNext() const { return mNext->asT(); } |
| 210 |
|
| 211 |
/* |
| 212 |
* Get the previous element in the list, or nullptr if this is the first |
| 213 |
* element in the list. |
| 214 |
*/ |
| 215 |
RawType getPrevious() { return mPrev->asT(); } |
| 216 |
ConstRawType getPrevious() const { return mPrev->asT(); } |
| 217 |
|
| 218 |
/* |
| 219 |
* Insert aElem after this element in the list. |this| must be part of a |
| 220 |
* linked list when you call setNext(); otherwise, this method will assert. |
| 221 |
*/ |
| 222 |
void setNext(RawType aElem) { |
| 223 |
MOZ_ASSERT(isInList()); |
| 224 |
setNextUnsafe(aElem); |
| 225 |
} |
| 226 |
|
| 227 |
/* |
| 228 |
* Insert aElem before this element in the list. |this| must be part of a |
| 229 |
* linked list when you call setPrevious(); otherwise, this method will |
| 230 |
* assert. |
| 231 |
*/ |
| 232 |
void setPrevious(RawType aElem) { |
| 233 |
MOZ_ASSERT(isInList()); |
| 234 |
setPreviousUnsafe(aElem); |
| 235 |
} |
| 236 |
|
| 237 |
/* |
| 238 |
* Remove this element from the list which contains it. If this element is |
| 239 |
* not currently part of a linked list, this method asserts. |
| 240 |
*/ |
| 241 |
void remove() { |
| 242 |
MOZ_ASSERT(isInList()); |
| 243 |
|
| 244 |
mPrev->mNext = mNext; |
| 245 |
mNext->mPrev = mPrev; |
| 246 |
mNext = this; |
| 247 |
mPrev = this; |
| 248 |
|
| 249 |
Traits::exitList(this); |
| 250 |
} |
| 251 |
|
| 252 |
/* |
| 253 |
* Remove this element from the list containing it. Returns a pointer to the |
| 254 |
* element that follows this element (before it was removed). This method |
| 255 |
* asserts if the element does not belong to a list. Note: In a refcounted |
| 256 |
* list, |this| may be destroyed. |
| 257 |
*/ |
| 258 |
RawType removeAndGetNext() { |
| 259 |
RawType r = getNext(); |
| 260 |
remove(); |
| 261 |
return r; |
| 262 |
} |
| 263 |
|
| 264 |
/* |
| 265 |
* Remove this element from the list containing it. Returns a pointer to the |
| 266 |
* previous element in the containing list (before the removal). This method |
| 267 |
* asserts if the element does not belong to a list. Note: In a refcounted |
| 268 |
* list, |this| may be destroyed. |
| 269 |
*/ |
| 270 |
RawType removeAndGetPrevious() { |
| 271 |
RawType r = getPrevious(); |
| 272 |
remove(); |
| 273 |
return r; |
| 274 |
} |
| 275 |
|
| 276 |
/* |
| 277 |
* Identical to remove(), but also asserts in debug builds that this element |
| 278 |
* is in aList. |
| 279 |
*/ |
| 280 |
void removeFrom(const LinkedList<T>& aList) { |
| 281 |
aList.assertContains(asT()); |
| 282 |
remove(); |
| 283 |
} |
| 284 |
|
| 285 |
/* |
| 286 |
* Return true if |this| part is of a linked list, and false otherwise. |
| 287 |
*/ |
| 288 |
bool isInList() const { |
| 289 |
MOZ_ASSERT((mNext == this) == (mPrev == this)); |
| 290 |
return mNext != this; |
| 291 |
} |
| 292 |
|
| 293 |
private: |
| 294 |
friend class LinkedList<T>; |
| 295 |
friend struct detail::LinkedListElementTraits<T>; |
| 296 |
|
| 297 |
enum class NodeKind { Normal, Sentinel }; |
| 298 |
|
| 299 |
explicit LinkedListElement(NodeKind nodeKind) |
| 300 |
: mNext(this), mPrev(this), mIsSentinel(nodeKind == NodeKind::Sentinel) {} |
| 301 |
|
| 302 |
/* |
| 303 |
* Return |this| cast to T* if we're a normal node, or return nullptr if |
| 304 |
* we're a sentinel node. |
| 305 |
*/ |
| 306 |
RawType asT() { return mIsSentinel ? nullptr : static_cast<RawType>(this); } |
| 307 |
ConstRawType asT() const { |
| 308 |
return mIsSentinel ? nullptr : static_cast<ConstRawType>(this); |
| 309 |
} |
| 310 |
|
| 311 |
/* |
| 312 |
* Insert aElem after this element, but don't check that this element is in |
| 313 |
* the list. This is called by LinkedList::insertFront(). |
| 314 |
*/ |
| 315 |
void setNextUnsafe(RawType aElem) { |
| 316 |
LinkedListElement* listElem = static_cast<LinkedListElement*>(aElem); |
| 317 |
MOZ_RELEASE_ASSERT(!listElem->isInList()); |
| 318 |
|
| 319 |
listElem->mNext = this->mNext; |
| 320 |
listElem->mPrev = this; |
| 321 |
this->mNext->mPrev = listElem; |
| 322 |
this->mNext = listElem; |
| 323 |
|
| 324 |
Traits::enterList(aElem); |
| 325 |
} |
| 326 |
|
| 327 |
/* |
| 328 |
* Insert aElem before this element, but don't check that this element is in |
| 329 |
* the list. This is called by LinkedList::insertBack(). |
| 330 |
*/ |
| 331 |
void setPreviousUnsafe(RawType aElem) { |
| 332 |
LinkedListElement<T>* listElem = static_cast<LinkedListElement<T>*>(aElem); |
| 333 |
MOZ_RELEASE_ASSERT(!listElem->isInList()); |
| 334 |
|
| 335 |
listElem->mNext = this; |
| 336 |
listElem->mPrev = this->mPrev; |
| 337 |
this->mPrev->mNext = listElem; |
| 338 |
this->mPrev = listElem; |
| 339 |
|
| 340 |
Traits::enterList(aElem); |
| 341 |
} |
| 342 |
|
| 343 |
/* |
| 344 |
* Transfers the elements [aBegin, aEnd) before the "this" list element. |
| 345 |
*/ |
| 346 |
void transferBeforeUnsafe(LinkedListElement<T>& aBegin, |
| 347 |
LinkedListElement<T>& aEnd) { |
| 348 |
MOZ_RELEASE_ASSERT(!aBegin.mIsSentinel); |
| 349 |
if (!aBegin.isInList() || !aEnd.isInList()) { |
| 350 |
return; |
| 351 |
} |
| 352 |
|
| 353 |
auto otherPrev = aBegin.mPrev; |
| 354 |
|
| 355 |
aBegin.mPrev = this->mPrev; |
| 356 |
this->mPrev->mNext = &aBegin; |
| 357 |
this->mPrev = aEnd.mPrev; |
| 358 |
aEnd.mPrev->mNext = this; |
| 359 |
|
| 360 |
// Patch the gap in the source list |
| 361 |
otherPrev->mNext = &aEnd; |
| 362 |
aEnd.mPrev = otherPrev; |
| 363 |
} |
| 364 |
|
| 365 |
/* |
| 366 |
* Adjust mNext and mPrev for implementing move constructor and move |
| 367 |
* assignment. |
| 368 |
*/ |
| 369 |
void adjustLinkForMove(LinkedListElement<T>&& aOther) { |
| 370 |
if (!aOther.isInList()) { |
| 371 |
mNext = this; |
| 372 |
mPrev = this; |
| 373 |
return; |
| 374 |
} |
| 375 |
|
| 376 |
if (!mIsSentinel) { |
| 377 |
Traits::enterList(this); |
| 378 |
} |
| 379 |
|
| 380 |
MOZ_ASSERT(aOther.mNext->mPrev == &aOther); |
| 381 |
MOZ_ASSERT(aOther.mPrev->mNext == &aOther); |
| 382 |
|
| 383 |
/* |
| 384 |
* Initialize |this| with |aOther|'s mPrev/mNext pointers, and adjust those |
| 385 |
* element to point to this one. |
| 386 |
*/ |
| 387 |
mNext = aOther.mNext; |
| 388 |
mPrev = aOther.mPrev; |
| 389 |
|
| 390 |
mNext->mPrev = this; |
| 391 |
mPrev->mNext = this; |
| 392 |
|
| 393 |
/* |
| 394 |
* Adjust |aOther| so it doesn't think it's in a list. This makes it |
| 395 |
* safely destructable. |
| 396 |
*/ |
| 397 |
aOther.mNext = &aOther; |
| 398 |
aOther.mPrev = &aOther; |
| 399 |
|
| 400 |
if (!mIsSentinel) { |
| 401 |
Traits::exitList(&aOther); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
LinkedListElement& operator=(const LinkedListElement<T>& aOther) = delete; |
| 406 |
LinkedListElement(const LinkedListElement<T>& aOther) = delete; |
| 407 |
}; |
| 408 |
|
| 409 |
template <typename T> |
| 410 |
class LinkedList { |
| 411 |
private: |
| 412 |
using Traits = typename detail::LinkedListElementTraits<T>; |
| 413 |
using RawType = typename Traits::RawType; |
| 414 |
using ConstRawType = typename Traits::ConstRawType; |
| 415 |
using ClientType = typename Traits::ClientType; |
| 416 |
using ConstClientType = typename Traits::ConstClientType; |
| 417 |
using ElementType = LinkedListElement<T>*; |
| 418 |
using ConstElementType = const LinkedListElement<T>*; |
| 419 |
|
| 420 |
LinkedListElement<T> sentinel; |
| 421 |
|
| 422 |
public: |
| 423 |
template <typename Type, typename Element> |
| 424 |
class Iterator { |
| 425 |
Type mCurrent; |
| 426 |
|
| 427 |
public: |
| 428 |
using iterator_category = std::forward_iterator_tag; |
| 429 |
using value_type = T; |
| 430 |
using difference_type = std::ptrdiff_t; |
| 431 |
using pointer = T*; |
| 432 |
using reference = T&; |
| 433 |
|
| 434 |
explicit Iterator(Type aCurrent) : mCurrent(aCurrent) {} |
| 435 |
|
| 436 |
Type operator*() const { return mCurrent; } |
| 437 |
|
| 438 |
const Iterator& operator++() { |
| 439 |
mCurrent = static_cast<Element>(mCurrent)->getNext(); |
| 440 |
return *this; |
| 441 |
} |
| 442 |
|
| 443 |
bool operator!=(const Iterator& aOther) const { |
| 444 |
return mCurrent != aOther.mCurrent; |
| 445 |
} |
| 446 |
}; |
| 447 |
|
| 448 |
using const_iterator = Iterator<ConstRawType, ConstElementType>; |
| 449 |
using iterator = Iterator<RawType, ElementType>; |
| 450 |
|
| 451 |
LinkedList() : sentinel(LinkedListElement<T>::NodeKind::Sentinel) {} |
| 452 |
|
| 453 |
LinkedList(LinkedList<T>&& aOther) : sentinel(std::move(aOther.sentinel)) {} |
| 454 |
|
| 455 |
LinkedList& operator=(LinkedList<T>&& aOther) { |
| 456 |
MOZ_ASSERT(isEmpty(), |
| 457 |
"Assigning to a non-empty list leaks elements in that list!"); |
| 458 |
sentinel = std::move(aOther.sentinel); |
| 459 |
return *this; |
| 460 |
} |
| 461 |
|
| 462 |
~LinkedList() { |
| 463 |
# ifdef DEBUG |
| 464 |
if (!isEmpty()) { |
| 465 |
MOZ_CRASH_UNSAFE_PRINTF( |
| 466 |
"%s has a buggy user: " |
| 467 |
"it should have removed all this list's elements before " |
| 468 |
"the list's destruction", |
| 469 |
__PRETTY_FUNCTION__); |
| 470 |
} |
| 471 |
# endif |
| 472 |
} |
| 473 |
|
| 474 |
/* |
| 475 |
* Add aElem to the front of the list. |
| 476 |
*/ |
| 477 |
void insertFront(RawType aElem) { |
| 478 |
/* Bypass setNext()'s this->isInList() assertion. */ |
| 479 |
sentinel.setNextUnsafe(aElem); |
| 480 |
} |
| 481 |
|
| 482 |
/* |
| 483 |
* Add aElem to the back of the list. |
| 484 |
*/ |
| 485 |
void insertBack(RawType aElem) { sentinel.setPreviousUnsafe(aElem); } |
| 486 |
|
| 487 |
/* |
| 488 |
* Move all elements from another list to the back |
| 489 |
*/ |
| 490 |
void extendBack(LinkedList<T>&& aOther) { |
| 491 |
MOZ_RELEASE_ASSERT(this != &aOther); |
| 492 |
if (aOther.isEmpty()) { |
| 493 |
return; |
| 494 |
} |
| 495 |
sentinel.transferBeforeUnsafe(**aOther.begin(), aOther.sentinel); |
| 496 |
} |
| 497 |
|
| 498 |
/* |
| 499 |
* Move elements from another list to the specified position |
| 500 |
*/ |
| 501 |
void splice(size_t aDestinationPos, LinkedList<T>& aListFrom, |
| 502 |
size_t aSourceStart, size_t aSourceLen) { |
| 503 |
MOZ_RELEASE_ASSERT(this != &aListFrom); |
| 504 |
if (aListFrom.isEmpty() || !aSourceLen) { |
| 505 |
return; |
| 506 |
} |
| 507 |
|
| 508 |
const auto safeForward = [](LinkedList<T>& aList, |
| 509 |
LinkedListElement<T>& aBegin, |
| 510 |
size_t aPos) -> LinkedListElement<T>& { |
| 511 |
auto* iter = &aBegin; |
| 512 |
for (size_t i = 0; i < aPos; ++i, (iter = iter->mNext)) { |
| 513 |
if (iter->mIsSentinel) { |
| 514 |
break; |
| 515 |
} |
| 516 |
} |
| 517 |
return *iter; |
| 518 |
}; |
| 519 |
|
| 520 |
auto& sourceBegin = |
| 521 |
safeForward(aListFrom, *aListFrom.sentinel.mNext, aSourceStart); |
| 522 |
if (sourceBegin.mIsSentinel) { |
| 523 |
return; |
| 524 |
} |
| 525 |
auto& sourceEnd = safeForward(aListFrom, sourceBegin, aSourceLen); |
| 526 |
auto& destination = safeForward(*this, *sentinel.mNext, aDestinationPos); |
| 527 |
|
| 528 |
destination.transferBeforeUnsafe(sourceBegin, sourceEnd); |
| 529 |
} |
| 530 |
|
| 531 |
/* |
| 532 |
* Get the first element of the list, or nullptr if the list is empty. |
| 533 |
*/ |
| 534 |
RawType getFirst() { return sentinel.getNext(); } |
| 535 |
ConstRawType getFirst() const { return sentinel.getNext(); } |
| 536 |
|
| 537 |
/* |
| 538 |
* Get the last element of the list, or nullptr if the list is empty. |
| 539 |
*/ |
| 540 |
RawType getLast() { return sentinel.getPrevious(); } |
| 541 |
ConstRawType getLast() const { return sentinel.getPrevious(); } |
| 542 |
|
| 543 |
/* |
| 544 |
* Get and remove the first element of the list. If the list is empty, |
| 545 |
* return nullptr. |
| 546 |
*/ |
| 547 |
ClientType popFirst() { |
| 548 |
ClientType ret = sentinel.getNext(); |
| 549 |
if (ret) { |
| 550 |
static_cast<LinkedListElement<T>*>(RawType(ret))->remove(); |
| 551 |
} |
| 552 |
return ret; |
| 553 |
} |
| 554 |
|
| 555 |
/* |
| 556 |
* Get and remove the last element of the list. If the list is empty, |
| 557 |
* return nullptr. |
| 558 |
*/ |
| 559 |
ClientType popLast() { |
| 560 |
ClientType ret = sentinel.getPrevious(); |
| 561 |
if (ret) { |
| 562 |
static_cast<LinkedListElement<T>*>(RawType(ret))->remove(); |
| 563 |
} |
| 564 |
return ret; |
| 565 |
} |
| 566 |
|
| 567 |
/* |
| 568 |
* Return true if the list is empty, or false otherwise. |
| 569 |
*/ |
| 570 |
bool isEmpty() const { return !sentinel.isInList(); } |
| 571 |
|
| 572 |
/** |
| 573 |
* Returns whether the given element is in the list. |
| 574 |
*/ |
| 575 |
bool contains(ConstRawType aElm) const { |
| 576 |
return std::find(begin(), end(), aElm) != end(); |
| 577 |
} |
| 578 |
|
| 579 |
/* |
| 580 |
* Remove all the elements from the list. |
| 581 |
* |
| 582 |
* This runs in time linear to the list's length, because we have to mark |
| 583 |
* each element as not in the list. |
| 584 |
*/ |
| 585 |
void clear() { |
| 586 |
while (popFirst()) { |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
/** |
| 591 |
* Return the length of elements in the list. |
| 592 |
*/ |
| 593 |
size_t length() const { return std::distance(begin(), end()); } |
| 594 |
|
| 595 |
/* |
| 596 |
* Allow range-based iteration: |
| 597 |
* |
| 598 |
* for (MyElementType* elt : myList) { ... } |
| 599 |
*/ |
| 600 |
Iterator<RawType, ElementType> begin() { |
| 601 |
return Iterator<RawType, ElementType>(getFirst()); |
| 602 |
} |
| 603 |
Iterator<ConstRawType, ConstElementType> begin() const { |
| 604 |
return Iterator<ConstRawType, ConstElementType>(getFirst()); |
| 605 |
} |
| 606 |
Iterator<RawType, ElementType> end() { |
| 607 |
return Iterator<RawType, ElementType>(nullptr); |
| 608 |
} |
| 609 |
Iterator<ConstRawType, ConstElementType> end() const { |
| 610 |
return Iterator<ConstRawType, ConstElementType>(nullptr); |
| 611 |
} |
| 612 |
|
| 613 |
/* |
| 614 |
* Measures the memory consumption of the list excluding |this|. Note that |
| 615 |
* it only measures the list elements themselves. If the list elements |
| 616 |
* contain pointers to other memory blocks, those blocks must be measured |
| 617 |
* separately during a subsequent iteration over the list. |
| 618 |
*/ |
| 619 |
size_t sizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const { |
| 620 |
size_t n = 0; |
| 621 |
ConstRawType t = getFirst(); |
| 622 |
while (t) { |
| 623 |
n += aMallocSizeOf(t); |
| 624 |
t = static_cast<const LinkedListElement<T>*>(t)->getNext(); |
| 625 |
} |
| 626 |
return n; |
| 627 |
} |
| 628 |
|
| 629 |
/* |
| 630 |
* Like sizeOfExcludingThis(), but measures |this| as well. |
| 631 |
*/ |
| 632 |
size_t sizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { |
| 633 |
return aMallocSizeOf(this) + sizeOfExcludingThis(aMallocSizeOf); |
| 634 |
} |
| 635 |
|
| 636 |
/* |
| 637 |
* In a debug build, make sure that the list is sane (no cycles, consistent |
| 638 |
* mNext/mPrev pointers, only one sentinel). Has no effect in release builds. |
| 639 |
*/ |
| 640 |
void debugAssertIsSane() const { |
| 641 |
# ifdef DEBUG |
| 642 |
const LinkedListElement<T>* slow; |
| 643 |
const LinkedListElement<T>* fast1; |
| 644 |
const LinkedListElement<T>* fast2; |
| 645 |
|
| 646 |
/* |
| 647 |
* Check for cycles in the forward singly-linked list using the |
| 648 |
* tortoise/hare algorithm. |
| 649 |
*/ |
| 650 |
for (slow = sentinel.mNext, fast1 = sentinel.mNext->mNext, |
| 651 |
fast2 = sentinel.mNext->mNext->mNext; |
| 652 |
slow != &sentinel && fast1 != &sentinel && fast2 != &sentinel; |
| 653 |
slow = slow->mNext, fast1 = fast2->mNext, fast2 = fast1->mNext) { |
| 654 |
MOZ_ASSERT(slow != fast1); |
| 655 |
MOZ_ASSERT(slow != fast2); |
| 656 |
} |
| 657 |
|
| 658 |
/* Check for cycles in the backward singly-linked list. */ |
| 659 |
for (slow = sentinel.mPrev, fast1 = sentinel.mPrev->mPrev, |
| 660 |
fast2 = sentinel.mPrev->mPrev->mPrev; |
| 661 |
slow != &sentinel && fast1 != &sentinel && fast2 != &sentinel; |
| 662 |
slow = slow->mPrev, fast1 = fast2->mPrev, fast2 = fast1->mPrev) { |
| 663 |
MOZ_ASSERT(slow != fast1); |
| 664 |
MOZ_ASSERT(slow != fast2); |
| 665 |
} |
| 666 |
|
| 667 |
/* |
| 668 |
* Check that |sentinel| is the only node in the list with |
| 669 |
* mIsSentinel == true. |
| 670 |
*/ |
| 671 |
for (const LinkedListElement<T>* elem = sentinel.mNext; elem != &sentinel; |
| 672 |
elem = elem->mNext) { |
| 673 |
MOZ_ASSERT(!elem->mIsSentinel); |
| 674 |
} |
| 675 |
|
| 676 |
/* Check that the mNext/mPrev pointers match up. */ |
| 677 |
const LinkedListElement<T>* prev = &sentinel; |
| 678 |
const LinkedListElement<T>* cur = sentinel.mNext; |
| 679 |
do { |
| 680 |
MOZ_ASSERT(cur->mPrev == prev); |
| 681 |
MOZ_ASSERT(prev->mNext == cur); |
| 682 |
|
| 683 |
prev = cur; |
| 684 |
cur = cur->mNext; |
| 685 |
} while (cur != &sentinel); |
| 686 |
# endif /* ifdef DEBUG */ |
| 687 |
} |
| 688 |
|
| 689 |
private: |
| 690 |
friend class LinkedListElement<T>; |
| 691 |
|
| 692 |
void assertContains(const RawType aValue) const { |
| 693 |
# ifdef DEBUG |
| 694 |
for (ConstRawType elem = getFirst(); elem; elem = elem->getNext()) { |
| 695 |
if (elem == aValue) { |
| 696 |
return; |
| 697 |
} |
| 698 |
} |
| 699 |
MOZ_CRASH("element wasn't found in this list!"); |
| 700 |
# endif |
| 701 |
} |
| 702 |
|
| 703 |
LinkedList& operator=(const LinkedList<T>& aOther) = delete; |
| 704 |
LinkedList(const LinkedList<T>& aOther) = delete; |
| 705 |
}; |
| 706 |
|
| 707 |
template <typename T> |
| 708 |
size_t RangeSizeEstimate(const LinkedList<T>&) { |
| 709 |
return 0; |
| 710 |
} |
| 711 |
|
| 712 |
template <typename T> |
| 713 |
inline void ImplCycleCollectionUnlink(LinkedList<RefPtr<T>>& aField) { |
| 714 |
aField.clear(); |
| 715 |
} |
| 716 |
|
| 717 |
template <typename T> |
| 718 |
inline void ImplCycleCollectionTraverse( |
| 719 |
nsCycleCollectionTraversalCallback& aCallback, |
| 720 |
LinkedList<RefPtr<T>>& aField, const char* aName, uint32_t aFlags = 0) { |
| 721 |
typedef typename detail::LinkedListElementTraits<T> Traits; |
| 722 |
typedef typename Traits::RawType RawType; |
| 723 |
for (RawType element : aField) { |
| 724 |
// RefPtr is stored as a raw pointer in LinkedList. |
| 725 |
// So instead of creating a new RefPtr from the raw |
| 726 |
// pointer (which is not allowed), we simply call |
| 727 |
// CycleCollectionNoteChild against the raw pointer |
| 728 |
CycleCollectionNoteChild(aCallback, element, aName, aFlags); |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
template <typename T> |
| 733 |
class AutoCleanLinkedList : public LinkedList<T> { |
| 734 |
private: |
| 735 |
using Traits = detail::LinkedListElementTraits<T>; |
| 736 |
using ClientType = typename detail::LinkedListElementTraits<T>::ClientType; |
| 737 |
|
| 738 |
public: |
| 739 |
AutoCleanLinkedList() = default; |
| 740 |
AutoCleanLinkedList(AutoCleanLinkedList&&) = default; |
| 741 |
~AutoCleanLinkedList() { clear(); } |
| 742 |
|
| 743 |
AutoCleanLinkedList& operator=(AutoCleanLinkedList&& aOther) = default; |
| 744 |
|
| 745 |
void clear() { |
| 746 |
while (ClientType element = this->popFirst()) { |
| 747 |
Traits::cleanElement(element); |
| 748 |
} |
| 749 |
} |
| 750 |
}; |
| 751 |
|
| 752 |
} /* namespace mozilla */ |
| 753 |
|
| 754 |
#endif /* __cplusplus */ |
| 755 |
|
| 756 |
#endif /* mozilla_LinkedList_h */ |