Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006, Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | |
michael@0 | 30 | // A "smart" pointer type with reference tracking. Every pointer to a |
michael@0 | 31 | // particular object is kept on a circular linked list. When the last pointer |
michael@0 | 32 | // to an object is destroyed or reassigned, the object is deleted. |
michael@0 | 33 | // |
michael@0 | 34 | // Used properly, this deletes the object when the last reference goes away. |
michael@0 | 35 | // There are several caveats: |
michael@0 | 36 | // - Like all reference counting schemes, cycles lead to leaks. |
michael@0 | 37 | // - Each smart pointer is actually two pointers (8 bytes instead of 4). |
michael@0 | 38 | // - Every time a pointer is assigned, the entire list of pointers to that |
michael@0 | 39 | // object is traversed. This class is therefore NOT SUITABLE when there |
michael@0 | 40 | // will often be more than two or three pointers to a particular object. |
michael@0 | 41 | // - References are only tracked as long as linked_ptr<> objects are copied. |
michael@0 | 42 | // If a linked_ptr<> is converted to a raw pointer and back, BAD THINGS |
michael@0 | 43 | // will happen (double deletion). |
michael@0 | 44 | // |
michael@0 | 45 | // A good use of this class is storing object references in STL containers. |
michael@0 | 46 | // You can safely put linked_ptr<> in a vector<>. |
michael@0 | 47 | // Other uses may not be as good. |
michael@0 | 48 | // |
michael@0 | 49 | // Note: If you use an incomplete type with linked_ptr<>, the class |
michael@0 | 50 | // *containing* linked_ptr<> must have a constructor and destructor (even |
michael@0 | 51 | // if they do nothing!). |
michael@0 | 52 | |
michael@0 | 53 | #ifndef PROCESSOR_LINKED_PTR_H__ |
michael@0 | 54 | #define PROCESSOR_LINKED_PTR_H__ |
michael@0 | 55 | |
michael@0 | 56 | namespace google_breakpad { |
michael@0 | 57 | |
michael@0 | 58 | // This is used internally by all instances of linked_ptr<>. It needs to be |
michael@0 | 59 | // a non-template class because different types of linked_ptr<> can refer to |
michael@0 | 60 | // the same object (linked_ptr<Superclass>(obj) vs linked_ptr<Subclass>(obj)). |
michael@0 | 61 | // So, it needs to be possible for different types of linked_ptr to participate |
michael@0 | 62 | // in the same circular linked list, so we need a single class type here. |
michael@0 | 63 | // |
michael@0 | 64 | // DO NOT USE THIS CLASS DIRECTLY YOURSELF. Use linked_ptr<T>. |
michael@0 | 65 | class linked_ptr_internal { |
michael@0 | 66 | public: |
michael@0 | 67 | // Create a new circle that includes only this instance. |
michael@0 | 68 | void join_new() { |
michael@0 | 69 | next_ = this; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | // Join an existing circle. |
michael@0 | 73 | void join(linked_ptr_internal const* ptr) { |
michael@0 | 74 | linked_ptr_internal const* p = ptr; |
michael@0 | 75 | while (p->next_ != ptr) p = p->next_; |
michael@0 | 76 | p->next_ = this; |
michael@0 | 77 | next_ = ptr; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | // Leave whatever circle we're part of. Returns true iff we were the |
michael@0 | 81 | // last member of the circle. Once this is done, you can join() another. |
michael@0 | 82 | bool depart() { |
michael@0 | 83 | if (next_ == this) return true; |
michael@0 | 84 | linked_ptr_internal const* p = next_; |
michael@0 | 85 | while (p->next_ != this) p = p->next_; |
michael@0 | 86 | p->next_ = next_; |
michael@0 | 87 | return false; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | private: |
michael@0 | 91 | mutable linked_ptr_internal const* next_; |
michael@0 | 92 | }; |
michael@0 | 93 | |
michael@0 | 94 | template <typename T> |
michael@0 | 95 | class linked_ptr { |
michael@0 | 96 | public: |
michael@0 | 97 | typedef T element_type; |
michael@0 | 98 | |
michael@0 | 99 | // Take over ownership of a raw pointer. This should happen as soon as |
michael@0 | 100 | // possible after the object is created. |
michael@0 | 101 | explicit linked_ptr(T* ptr = NULL) { capture(ptr); } |
michael@0 | 102 | ~linked_ptr() { depart(); } |
michael@0 | 103 | |
michael@0 | 104 | // Copy an existing linked_ptr<>, adding ourselves to the list of references. |
michael@0 | 105 | template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); } |
michael@0 | 106 | linked_ptr(linked_ptr const& ptr) { copy(&ptr); } |
michael@0 | 107 | |
michael@0 | 108 | // Assignment releases the old value and acquires the new. |
michael@0 | 109 | template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) { |
michael@0 | 110 | depart(); |
michael@0 | 111 | copy(&ptr); |
michael@0 | 112 | return *this; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | linked_ptr& operator=(linked_ptr const& ptr) { |
michael@0 | 116 | if (&ptr != this) { |
michael@0 | 117 | depart(); |
michael@0 | 118 | copy(&ptr); |
michael@0 | 119 | } |
michael@0 | 120 | return *this; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | // Smart pointer members. |
michael@0 | 124 | void reset(T* ptr = NULL) { depart(); capture(ptr); } |
michael@0 | 125 | T* get() const { return value_; } |
michael@0 | 126 | T* operator->() const { return value_; } |
michael@0 | 127 | T& operator*() const { return *value_; } |
michael@0 | 128 | // Release ownership of the pointed object and returns it. |
michael@0 | 129 | // Sole ownership by this linked_ptr object is required. |
michael@0 | 130 | T* release() { |
michael@0 | 131 | link_.depart(); |
michael@0 | 132 | T* v = value_; |
michael@0 | 133 | value_ = NULL; |
michael@0 | 134 | return v; |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | bool operator==(T* p) const { return value_ == p; } |
michael@0 | 138 | bool operator!=(T* p) const { return value_ != p; } |
michael@0 | 139 | template <typename U> |
michael@0 | 140 | bool operator==(linked_ptr<U> const& ptr) const { |
michael@0 | 141 | return value_ == ptr.get(); |
michael@0 | 142 | } |
michael@0 | 143 | template <typename U> |
michael@0 | 144 | bool operator!=(linked_ptr<U> const& ptr) const { |
michael@0 | 145 | return value_ != ptr.get(); |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | private: |
michael@0 | 149 | template <typename U> |
michael@0 | 150 | friend class linked_ptr; |
michael@0 | 151 | |
michael@0 | 152 | T* value_; |
michael@0 | 153 | linked_ptr_internal link_; |
michael@0 | 154 | |
michael@0 | 155 | void depart() { |
michael@0 | 156 | if (link_.depart()) delete value_; |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | void capture(T* ptr) { |
michael@0 | 160 | value_ = ptr; |
michael@0 | 161 | link_.join_new(); |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | template <typename U> void copy(linked_ptr<U> const* ptr) { |
michael@0 | 165 | value_ = ptr->get(); |
michael@0 | 166 | if (value_) |
michael@0 | 167 | link_.join(&ptr->link_); |
michael@0 | 168 | else |
michael@0 | 169 | link_.join_new(); |
michael@0 | 170 | } |
michael@0 | 171 | }; |
michael@0 | 172 | |
michael@0 | 173 | template<typename T> inline |
michael@0 | 174 | bool operator==(T* ptr, const linked_ptr<T>& x) { |
michael@0 | 175 | return ptr == x.get(); |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | template<typename T> inline |
michael@0 | 179 | bool operator!=(T* ptr, const linked_ptr<T>& x) { |
michael@0 | 180 | return ptr != x.get(); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | // A function to convert T* into linked_ptr<T> |
michael@0 | 184 | // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation |
michael@0 | 185 | // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) |
michael@0 | 186 | template <typename T> |
michael@0 | 187 | linked_ptr<T> make_linked_ptr(T* ptr) { |
michael@0 | 188 | return linked_ptr<T>(ptr); |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | } // namespace google_breakpad |
michael@0 | 192 | |
michael@0 | 193 | #endif // PROCESSOR_LINKED_PTR_H__ |