1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/chromium/base/memory/ref_counted.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,301 @@ 1.4 +// Copyright (c) 2012 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#ifndef BASE_MEMORY_REF_COUNTED_H_ 1.9 +#define BASE_MEMORY_REF_COUNTED_H_ 1.10 + 1.11 +#include <cassert> 1.12 + 1.13 +#include "base/atomic_ref_count.h" 1.14 +#include "base/base_export.h" 1.15 +#include "base/compiler_specific.h" 1.16 +#include "base/threading/thread_collision_warner.h" 1.17 + 1.18 +namespace base { 1.19 + 1.20 +namespace subtle { 1.21 + 1.22 +class BASE_EXPORT RefCountedBase { 1.23 + public: 1.24 + bool HasOneRef() const { return ref_count_ == 1; } 1.25 + 1.26 + protected: 1.27 + RefCountedBase(); 1.28 + ~RefCountedBase(); 1.29 + 1.30 + void AddRef() const; 1.31 + 1.32 + // Returns true if the object should self-delete. 1.33 + bool Release() const; 1.34 + 1.35 + private: 1.36 + mutable int ref_count_; 1.37 +#ifndef NDEBUG 1.38 + mutable bool in_dtor_; 1.39 +#endif 1.40 + 1.41 + DFAKE_MUTEX(add_release_); 1.42 + 1.43 + DISALLOW_COPY_AND_ASSIGN(RefCountedBase); 1.44 +}; 1.45 + 1.46 +class BASE_EXPORT RefCountedThreadSafeBase { 1.47 + public: 1.48 + bool HasOneRef() const; 1.49 + 1.50 + protected: 1.51 + RefCountedThreadSafeBase(); 1.52 + ~RefCountedThreadSafeBase(); 1.53 + 1.54 + void AddRef() const; 1.55 + 1.56 + // Returns true if the object should self-delete. 1.57 + bool Release() const; 1.58 + 1.59 + private: 1.60 + mutable AtomicRefCount ref_count_; 1.61 +#ifndef NDEBUG 1.62 + mutable bool in_dtor_; 1.63 +#endif 1.64 + 1.65 + DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafeBase); 1.66 +}; 1.67 + 1.68 +} // namespace subtle 1.69 + 1.70 +// 1.71 +// A base class for reference counted classes. Otherwise, known as a cheap 1.72 +// knock-off of WebKit's RefCounted<T> class. To use this guy just extend your 1.73 +// class from it like so: 1.74 +// 1.75 +// class MyFoo : public base::RefCounted<MyFoo> { 1.76 +// ... 1.77 +// private: 1.78 +// friend class base::RefCounted<MyFoo>; 1.79 +// ~MyFoo(); 1.80 +// }; 1.81 +// 1.82 +// You should always make your destructor private, to avoid any code deleting 1.83 +// the object accidently while there are references to it. 1.84 +template <class T> 1.85 +class RefCounted : public subtle::RefCountedBase { 1.86 + public: 1.87 + RefCounted() {} 1.88 + 1.89 + void AddRef() const { 1.90 + subtle::RefCountedBase::AddRef(); 1.91 + } 1.92 + 1.93 + void Release() const { 1.94 + if (subtle::RefCountedBase::Release()) { 1.95 + delete static_cast<const T*>(this); 1.96 + } 1.97 + } 1.98 + 1.99 + protected: 1.100 + ~RefCounted() {} 1.101 + 1.102 + private: 1.103 + DISALLOW_COPY_AND_ASSIGN(RefCounted<T>); 1.104 +}; 1.105 + 1.106 +// Forward declaration. 1.107 +template <class T, typename Traits> class RefCountedThreadSafe; 1.108 + 1.109 +// Default traits for RefCountedThreadSafe<T>. Deletes the object when its ref 1.110 +// count reaches 0. Overload to delete it on a different thread etc. 1.111 +template<typename T> 1.112 +struct DefaultRefCountedThreadSafeTraits { 1.113 + static void Destruct(const T* x) { 1.114 + // Delete through RefCountedThreadSafe to make child classes only need to be 1.115 + // friend with RefCountedThreadSafe instead of this struct, which is an 1.116 + // implementation detail. 1.117 + RefCountedThreadSafe<T, 1.118 + DefaultRefCountedThreadSafeTraits>::DeleteInternal(x); 1.119 + } 1.120 +}; 1.121 + 1.122 +// 1.123 +// A thread-safe variant of RefCounted<T> 1.124 +// 1.125 +// class MyFoo : public base::RefCountedThreadSafe<MyFoo> { 1.126 +// ... 1.127 +// }; 1.128 +// 1.129 +// If you're using the default trait, then you should add compile time 1.130 +// asserts that no one else is deleting your object. i.e. 1.131 +// private: 1.132 +// friend class base::RefCountedThreadSafe<MyFoo>; 1.133 +// ~MyFoo(); 1.134 +template <class T, typename Traits = DefaultRefCountedThreadSafeTraits<T> > 1.135 +class RefCountedThreadSafe : public subtle::RefCountedThreadSafeBase { 1.136 + public: 1.137 + RefCountedThreadSafe() {} 1.138 + 1.139 + void AddRef() const { 1.140 + subtle::RefCountedThreadSafeBase::AddRef(); 1.141 + } 1.142 + 1.143 + void Release() const { 1.144 + if (subtle::RefCountedThreadSafeBase::Release()) { 1.145 + Traits::Destruct(static_cast<const T*>(this)); 1.146 + } 1.147 + } 1.148 + 1.149 + protected: 1.150 + ~RefCountedThreadSafe() {} 1.151 + 1.152 + private: 1.153 + friend struct DefaultRefCountedThreadSafeTraits<T>; 1.154 + static void DeleteInternal(const T* x) { delete x; } 1.155 + 1.156 + DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafe); 1.157 +}; 1.158 + 1.159 +// 1.160 +// A thread-safe wrapper for some piece of data so we can place other 1.161 +// things in scoped_refptrs<>. 1.162 +// 1.163 +template<typename T> 1.164 +class RefCountedData 1.165 + : public base::RefCountedThreadSafe< base::RefCountedData<T> > { 1.166 + public: 1.167 + RefCountedData() : data() {} 1.168 + RefCountedData(const T& in_value) : data(in_value) {} 1.169 + 1.170 + T data; 1.171 + 1.172 + private: 1.173 + friend class base::RefCountedThreadSafe<base::RefCountedData<T> >; 1.174 + ~RefCountedData() {} 1.175 +}; 1.176 + 1.177 +} // namespace base 1.178 + 1.179 +// 1.180 +// A smart pointer class for reference counted objects. Use this class instead 1.181 +// of calling AddRef and Release manually on a reference counted object to 1.182 +// avoid common memory leaks caused by forgetting to Release an object 1.183 +// reference. Sample usage: 1.184 +// 1.185 +// class MyFoo : public RefCounted<MyFoo> { 1.186 +// ... 1.187 +// }; 1.188 +// 1.189 +// void some_function() { 1.190 +// scoped_refptr<MyFoo> foo = new MyFoo(); 1.191 +// foo->Method(param); 1.192 +// // |foo| is released when this function returns 1.193 +// } 1.194 +// 1.195 +// void some_other_function() { 1.196 +// scoped_refptr<MyFoo> foo = new MyFoo(); 1.197 +// ... 1.198 +// foo = NULL; // explicitly releases |foo| 1.199 +// ... 1.200 +// if (foo) 1.201 +// foo->Method(param); 1.202 +// } 1.203 +// 1.204 +// The above examples show how scoped_refptr<T> acts like a pointer to T. 1.205 +// Given two scoped_refptr<T> classes, it is also possible to exchange 1.206 +// references between the two objects, like so: 1.207 +// 1.208 +// { 1.209 +// scoped_refptr<MyFoo> a = new MyFoo(); 1.210 +// scoped_refptr<MyFoo> b; 1.211 +// 1.212 +// b.swap(a); 1.213 +// // now, |b| references the MyFoo object, and |a| references NULL. 1.214 +// } 1.215 +// 1.216 +// To make both |a| and |b| in the above example reference the same MyFoo 1.217 +// object, simply use the assignment operator: 1.218 +// 1.219 +// { 1.220 +// scoped_refptr<MyFoo> a = new MyFoo(); 1.221 +// scoped_refptr<MyFoo> b; 1.222 +// 1.223 +// b = a; 1.224 +// // now, |a| and |b| each own a reference to the same MyFoo object. 1.225 +// } 1.226 +// 1.227 +template <class T> 1.228 +class scoped_refptr { 1.229 + public: 1.230 + typedef T element_type; 1.231 + 1.232 + scoped_refptr() : ptr_(NULL) { 1.233 + } 1.234 + 1.235 + scoped_refptr(T* p) : ptr_(p) { 1.236 + if (ptr_) 1.237 + ptr_->AddRef(); 1.238 + } 1.239 + 1.240 + scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) { 1.241 + if (ptr_) 1.242 + ptr_->AddRef(); 1.243 + } 1.244 + 1.245 + template <typename U> 1.246 + scoped_refptr(const scoped_refptr<U>& r) : ptr_(r.get()) { 1.247 + if (ptr_) 1.248 + ptr_->AddRef(); 1.249 + } 1.250 + 1.251 + ~scoped_refptr() { 1.252 + if (ptr_) 1.253 + ptr_->Release(); 1.254 + } 1.255 + 1.256 + T* get() const { return ptr_; } 1.257 + operator T*() const { return ptr_; } 1.258 + T* operator->() const { 1.259 + assert(ptr_ != NULL); 1.260 + return ptr_; 1.261 + } 1.262 + 1.263 + scoped_refptr<T>& operator=(T* p) { 1.264 + // AddRef first so that self assignment should work 1.265 + if (p) 1.266 + p->AddRef(); 1.267 + T* old_ptr = ptr_; 1.268 + ptr_ = p; 1.269 + if (old_ptr) 1.270 + old_ptr->Release(); 1.271 + return *this; 1.272 + } 1.273 + 1.274 + scoped_refptr<T>& operator=(const scoped_refptr<T>& r) { 1.275 + return *this = r.ptr_; 1.276 + } 1.277 + 1.278 + template <typename U> 1.279 + scoped_refptr<T>& operator=(const scoped_refptr<U>& r) { 1.280 + return *this = r.get(); 1.281 + } 1.282 + 1.283 + void swap(T** pp) { 1.284 + T* p = ptr_; 1.285 + ptr_ = *pp; 1.286 + *pp = p; 1.287 + } 1.288 + 1.289 + void swap(scoped_refptr<T>& r) { 1.290 + swap(&r.ptr_); 1.291 + } 1.292 + 1.293 + protected: 1.294 + T* ptr_; 1.295 +}; 1.296 + 1.297 +// Handy utility for creating a scoped_refptr<T> out of a T* explicitly without 1.298 +// having to retype all the template arguments 1.299 +template <typename T> 1.300 +scoped_refptr<T> make_scoped_refptr(T* t) { 1.301 + return scoped_refptr<T>(t); 1.302 +} 1.303 + 1.304 +#endif // BASE_MEMORY_REF_COUNTED_H_