Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef NS_TPRIORITY_QUEUE_H_ |
michael@0 | 8 | #define NS_TPRIORITY_QUEUE_H_ |
michael@0 | 9 | |
michael@0 | 10 | #include "nsTArray.h" |
michael@0 | 11 | #include "nsDebug.h" |
michael@0 | 12 | |
michael@0 | 13 | /** |
michael@0 | 14 | * A templatized priority queue data structure that uses an nsTArray to serve as |
michael@0 | 15 | * a binary heap. The default comparator causes this to act like a min-heap. |
michael@0 | 16 | * Only the LessThan method of the comparator is used. |
michael@0 | 17 | */ |
michael@0 | 18 | template<class T, class Compare = nsDefaultComparator<T, T> > |
michael@0 | 19 | class nsTPriorityQueue |
michael@0 | 20 | { |
michael@0 | 21 | public: |
michael@0 | 22 | typedef typename nsTArray<T>::size_type size_type; |
michael@0 | 23 | |
michael@0 | 24 | /** |
michael@0 | 25 | * Default constructor also creates a comparator object using the default |
michael@0 | 26 | * constructor for type Compare. |
michael@0 | 27 | */ |
michael@0 | 28 | nsTPriorityQueue() : mCompare(Compare()) { } |
michael@0 | 29 | |
michael@0 | 30 | /** |
michael@0 | 31 | * Constructor to allow a specific instance of a comparator object to be |
michael@0 | 32 | * used. |
michael@0 | 33 | */ |
michael@0 | 34 | nsTPriorityQueue(const Compare& aComp) : mCompare(aComp) { } |
michael@0 | 35 | |
michael@0 | 36 | /** |
michael@0 | 37 | * Copy constructor |
michael@0 | 38 | */ |
michael@0 | 39 | nsTPriorityQueue(const nsTPriorityQueue& aOther) |
michael@0 | 40 | : mElements(aOther.mElements), |
michael@0 | 41 | mCompare(aOther.mCompare) |
michael@0 | 42 | { } |
michael@0 | 43 | |
michael@0 | 44 | /** |
michael@0 | 45 | * @return True if the queue is empty or false otherwise. |
michael@0 | 46 | */ |
michael@0 | 47 | bool IsEmpty() const |
michael@0 | 48 | { |
michael@0 | 49 | return mElements.IsEmpty(); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | /** |
michael@0 | 53 | * @return The number of elements in the queue. |
michael@0 | 54 | */ |
michael@0 | 55 | size_type Length() const |
michael@0 | 56 | { |
michael@0 | 57 | return mElements.Length(); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | /** |
michael@0 | 61 | * @return The topmost element in the queue without changing the queue. This |
michael@0 | 62 | * is the element 'a' such that there is no other element 'b' in the queue for |
michael@0 | 63 | * which Compare(b, a) returns true. (Since this container does not check |
michael@0 | 64 | * for duplicate entries there may exist 'b' for which Compare(a, b) returns |
michael@0 | 65 | * false.) |
michael@0 | 66 | */ |
michael@0 | 67 | const T& Top() const |
michael@0 | 68 | { |
michael@0 | 69 | NS_ABORT_IF_FALSE(!mElements.IsEmpty(), "Empty queue"); |
michael@0 | 70 | return mElements[0]; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | /** |
michael@0 | 74 | * Adds an element to the queue |
michael@0 | 75 | * @param aElement The element to add |
michael@0 | 76 | * @return true on success, false on out of memory. |
michael@0 | 77 | */ |
michael@0 | 78 | bool Push(const T& aElement) |
michael@0 | 79 | { |
michael@0 | 80 | T* elem = mElements.AppendElement(aElement); |
michael@0 | 81 | if (!elem) |
michael@0 | 82 | return false; // Out of memory |
michael@0 | 83 | |
michael@0 | 84 | // Sift up |
michael@0 | 85 | size_type i = mElements.Length() - 1; |
michael@0 | 86 | while (i) { |
michael@0 | 87 | size_type parent = (size_type)((i - 1) / 2); |
michael@0 | 88 | if (mCompare.LessThan(mElements[parent], mElements[i])) { |
michael@0 | 89 | break; |
michael@0 | 90 | } |
michael@0 | 91 | Swap(i, parent); |
michael@0 | 92 | i = parent; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | return true; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | /** |
michael@0 | 99 | * Removes and returns the top-most element from the queue. |
michael@0 | 100 | * @return The topmost element, that is, the element 'a' such that there is no |
michael@0 | 101 | * other element 'b' in the queue for which Compare(b, a) returns true. |
michael@0 | 102 | * @see Top() |
michael@0 | 103 | */ |
michael@0 | 104 | T Pop() |
michael@0 | 105 | { |
michael@0 | 106 | NS_ABORT_IF_FALSE(!mElements.IsEmpty(), "Empty queue"); |
michael@0 | 107 | T pop = mElements[0]; |
michael@0 | 108 | |
michael@0 | 109 | // Move last to front |
michael@0 | 110 | mElements[0] = mElements[mElements.Length() - 1]; |
michael@0 | 111 | mElements.TruncateLength(mElements.Length() - 1); |
michael@0 | 112 | |
michael@0 | 113 | // Sift down |
michael@0 | 114 | size_type i = 0; |
michael@0 | 115 | while (2*i + 1 < mElements.Length()) { |
michael@0 | 116 | size_type swap = i; |
michael@0 | 117 | size_type l_child = 2*i + 1; |
michael@0 | 118 | if (mCompare.LessThan(mElements[l_child], mElements[i])) { |
michael@0 | 119 | swap = l_child; |
michael@0 | 120 | } |
michael@0 | 121 | size_type r_child = l_child + 1; |
michael@0 | 122 | if (r_child < mElements.Length() && |
michael@0 | 123 | mCompare.LessThan(mElements[r_child], mElements[swap])) { |
michael@0 | 124 | swap = r_child; |
michael@0 | 125 | } |
michael@0 | 126 | if (swap == i) { |
michael@0 | 127 | break; |
michael@0 | 128 | } |
michael@0 | 129 | Swap(i, swap); |
michael@0 | 130 | i = swap; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | return pop; |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | /** |
michael@0 | 137 | * Removes all elements from the queue. |
michael@0 | 138 | */ |
michael@0 | 139 | void Clear() |
michael@0 | 140 | { |
michael@0 | 141 | mElements.Clear(); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | /** |
michael@0 | 145 | * Provides readonly access to the queue elements as an array. Generally this |
michael@0 | 146 | * should be avoided but may be needed in some situations such as when the |
michael@0 | 147 | * elements contained in the queue need to be enumerated for cycle-collection. |
michael@0 | 148 | * @return A pointer to the first element of the array. If the array is |
michael@0 | 149 | * empty, then this pointer must not be dereferenced. |
michael@0 | 150 | */ |
michael@0 | 151 | const T* Elements() const |
michael@0 | 152 | { |
michael@0 | 153 | return mElements.Elements(); |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | protected: |
michael@0 | 157 | /** |
michael@0 | 158 | * Swaps the elements at the specified indices. |
michael@0 | 159 | */ |
michael@0 | 160 | void Swap(size_type aIndexA, size_type aIndexB) |
michael@0 | 161 | { |
michael@0 | 162 | T temp = mElements[aIndexA]; |
michael@0 | 163 | mElements[aIndexA] = mElements[aIndexB]; |
michael@0 | 164 | mElements[aIndexB] = temp; |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | nsTArray<T> mElements; |
michael@0 | 168 | Compare mCompare; // Comparator object |
michael@0 | 169 | }; |
michael@0 | 170 | |
michael@0 | 171 | #endif // NS_TPRIORITY_QUEUE_H_ |