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-2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef BASE_REF_COUNTED_H_ |
michael@0 | 6 | #define BASE_REF_COUNTED_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include "base/atomic_ref_count.h" |
michael@0 | 9 | #include "base/thread_collision_warner.h" |
michael@0 | 10 | |
michael@0 | 11 | namespace base { |
michael@0 | 12 | |
michael@0 | 13 | namespace subtle { |
michael@0 | 14 | |
michael@0 | 15 | class RefCountedBase { |
michael@0 | 16 | protected: |
michael@0 | 17 | RefCountedBase(); |
michael@0 | 18 | ~RefCountedBase(); |
michael@0 | 19 | |
michael@0 | 20 | void AddRef(); |
michael@0 | 21 | |
michael@0 | 22 | // Returns true if the object should self-delete. |
michael@0 | 23 | bool Release(); |
michael@0 | 24 | |
michael@0 | 25 | private: |
michael@0 | 26 | int ref_count_; |
michael@0 | 27 | #ifndef NDEBUG |
michael@0 | 28 | bool in_dtor_; |
michael@0 | 29 | #endif |
michael@0 | 30 | |
michael@0 | 31 | DFAKE_MUTEX(add_release_) |
michael@0 | 32 | |
michael@0 | 33 | DISALLOW_COPY_AND_ASSIGN(RefCountedBase); |
michael@0 | 34 | }; |
michael@0 | 35 | |
michael@0 | 36 | class RefCountedThreadSafeBase { |
michael@0 | 37 | protected: |
michael@0 | 38 | RefCountedThreadSafeBase(); |
michael@0 | 39 | ~RefCountedThreadSafeBase(); |
michael@0 | 40 | |
michael@0 | 41 | void AddRef(); |
michael@0 | 42 | |
michael@0 | 43 | // Returns true if the object should self-delete. |
michael@0 | 44 | bool Release(); |
michael@0 | 45 | |
michael@0 | 46 | private: |
michael@0 | 47 | AtomicRefCount ref_count_; |
michael@0 | 48 | #ifndef NDEBUG |
michael@0 | 49 | bool in_dtor_; |
michael@0 | 50 | #endif |
michael@0 | 51 | |
michael@0 | 52 | DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafeBase); |
michael@0 | 53 | }; |
michael@0 | 54 | |
michael@0 | 55 | |
michael@0 | 56 | |
michael@0 | 57 | } // namespace subtle |
michael@0 | 58 | |
michael@0 | 59 | // |
michael@0 | 60 | // A base class for reference counted classes. Otherwise, known as a cheap |
michael@0 | 61 | // knock-off of WebKit's RefCounted<T> class. To use this guy just extend your |
michael@0 | 62 | // class from it like so: |
michael@0 | 63 | // |
michael@0 | 64 | // class MyFoo : public base::RefCounted<MyFoo> { |
michael@0 | 65 | // ... |
michael@0 | 66 | // }; |
michael@0 | 67 | // |
michael@0 | 68 | template <class T> |
michael@0 | 69 | class RefCounted : public subtle::RefCountedBase { |
michael@0 | 70 | public: |
michael@0 | 71 | RefCounted() { } |
michael@0 | 72 | ~RefCounted() { } |
michael@0 | 73 | |
michael@0 | 74 | void AddRef() { |
michael@0 | 75 | subtle::RefCountedBase::AddRef(); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | void Release() { |
michael@0 | 79 | if (subtle::RefCountedBase::Release()) { |
michael@0 | 80 | delete static_cast<T*>(this); |
michael@0 | 81 | } |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | private: |
michael@0 | 85 | DISALLOW_COPY_AND_ASSIGN(RefCounted<T>); |
michael@0 | 86 | }; |
michael@0 | 87 | |
michael@0 | 88 | // |
michael@0 | 89 | // A thread-safe variant of RefCounted<T> |
michael@0 | 90 | // |
michael@0 | 91 | // class MyFoo : public base::RefCountedThreadSafe<MyFoo> { |
michael@0 | 92 | // ... |
michael@0 | 93 | // }; |
michael@0 | 94 | // |
michael@0 | 95 | template <class T> |
michael@0 | 96 | class RefCountedThreadSafe : public subtle::RefCountedThreadSafeBase { |
michael@0 | 97 | public: |
michael@0 | 98 | RefCountedThreadSafe() { } |
michael@0 | 99 | ~RefCountedThreadSafe() { } |
michael@0 | 100 | |
michael@0 | 101 | void AddRef() { |
michael@0 | 102 | subtle::RefCountedThreadSafeBase::AddRef(); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | void Release() { |
michael@0 | 106 | if (subtle::RefCountedThreadSafeBase::Release()) { |
michael@0 | 107 | delete static_cast<T*>(this); |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | private: |
michael@0 | 112 | DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafe<T>); |
michael@0 | 113 | }; |
michael@0 | 114 | |
michael@0 | 115 | // |
michael@0 | 116 | // A wrapper for some piece of data so we can place other things in |
michael@0 | 117 | // scoped_refptrs<>. |
michael@0 | 118 | // |
michael@0 | 119 | template<typename T> |
michael@0 | 120 | class RefCountedData : public base::RefCounted< base::RefCountedData<T> > { |
michael@0 | 121 | public: |
michael@0 | 122 | RefCountedData() : data() {} |
michael@0 | 123 | RefCountedData(const T& in_value) : data(in_value) {} |
michael@0 | 124 | |
michael@0 | 125 | T data; |
michael@0 | 126 | }; |
michael@0 | 127 | |
michael@0 | 128 | } // namespace base |
michael@0 | 129 | |
michael@0 | 130 | // |
michael@0 | 131 | // A smart pointer class for reference counted objects. Use this class instead |
michael@0 | 132 | // of calling AddRef and Release manually on a reference counted object to |
michael@0 | 133 | // avoid common memory leaks caused by forgetting to Release an object |
michael@0 | 134 | // reference. Sample usage: |
michael@0 | 135 | // |
michael@0 | 136 | // class MyFoo : public RefCounted<MyFoo> { |
michael@0 | 137 | // ... |
michael@0 | 138 | // }; |
michael@0 | 139 | // |
michael@0 | 140 | // void some_function() { |
michael@0 | 141 | // scoped_refptr<MyFoo> foo = new MyFoo(); |
michael@0 | 142 | // foo->Method(param); |
michael@0 | 143 | // // |foo| is released when this function returns |
michael@0 | 144 | // } |
michael@0 | 145 | // |
michael@0 | 146 | // void some_other_function() { |
michael@0 | 147 | // scoped_refptr<MyFoo> foo = new MyFoo(); |
michael@0 | 148 | // ... |
michael@0 | 149 | // foo = NULL; // explicitly releases |foo| |
michael@0 | 150 | // ... |
michael@0 | 151 | // if (foo) |
michael@0 | 152 | // foo->Method(param); |
michael@0 | 153 | // } |
michael@0 | 154 | // |
michael@0 | 155 | // The above examples show how scoped_refptr<T> acts like a pointer to T. |
michael@0 | 156 | // Given two scoped_refptr<T> classes, it is also possible to exchange |
michael@0 | 157 | // references between the two objects, like so: |
michael@0 | 158 | // |
michael@0 | 159 | // { |
michael@0 | 160 | // scoped_refptr<MyFoo> a = new MyFoo(); |
michael@0 | 161 | // scoped_refptr<MyFoo> b; |
michael@0 | 162 | // |
michael@0 | 163 | // b.swap(a); |
michael@0 | 164 | // // now, |b| references the MyFoo object, and |a| references NULL. |
michael@0 | 165 | // } |
michael@0 | 166 | // |
michael@0 | 167 | // To make both |a| and |b| in the above example reference the same MyFoo |
michael@0 | 168 | // object, simply use the assignment operator: |
michael@0 | 169 | // |
michael@0 | 170 | // { |
michael@0 | 171 | // scoped_refptr<MyFoo> a = new MyFoo(); |
michael@0 | 172 | // scoped_refptr<MyFoo> b; |
michael@0 | 173 | // |
michael@0 | 174 | // b = a; |
michael@0 | 175 | // // now, |a| and |b| each own a reference to the same MyFoo object. |
michael@0 | 176 | // } |
michael@0 | 177 | // |
michael@0 | 178 | template <class T> |
michael@0 | 179 | class scoped_refptr { |
michael@0 | 180 | public: |
michael@0 | 181 | scoped_refptr() : ptr_(NULL) { |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | scoped_refptr(T* p) : ptr_(p) { |
michael@0 | 185 | if (ptr_) |
michael@0 | 186 | ptr_->AddRef(); |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) { |
michael@0 | 190 | if (ptr_) |
michael@0 | 191 | ptr_->AddRef(); |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | ~scoped_refptr() { |
michael@0 | 195 | if (ptr_) |
michael@0 | 196 | ptr_->Release(); |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | T* get() const { return ptr_; } |
michael@0 | 200 | operator T*() const { return ptr_; } |
michael@0 | 201 | T* operator->() const { return ptr_; } |
michael@0 | 202 | |
michael@0 | 203 | scoped_refptr<T>& operator=(T* p) { |
michael@0 | 204 | // AddRef first so that self assignment should work |
michael@0 | 205 | if (p) |
michael@0 | 206 | p->AddRef(); |
michael@0 | 207 | if (ptr_ ) |
michael@0 | 208 | ptr_ ->Release(); |
michael@0 | 209 | ptr_ = p; |
michael@0 | 210 | return *this; |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | scoped_refptr<T>& operator=(const scoped_refptr<T>& r) { |
michael@0 | 214 | return *this = r.ptr_; |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | void swap(T** pp) { |
michael@0 | 218 | T* p = ptr_; |
michael@0 | 219 | ptr_ = *pp; |
michael@0 | 220 | *pp = p; |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | void swap(scoped_refptr<T>& r) { |
michael@0 | 224 | swap(&r.ptr_); |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | protected: |
michael@0 | 228 | T* ptr_; |
michael@0 | 229 | }; |
michael@0 | 230 | |
michael@0 | 231 | #endif // BASE_REF_COUNTED_H_ |