michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_MEMORY_REF_COUNTED_H_ michael@0: #define BASE_MEMORY_REF_COUNTED_H_ michael@0: michael@0: #include michael@0: michael@0: #include "base/atomic_ref_count.h" michael@0: #include "base/base_export.h" michael@0: #include "base/compiler_specific.h" michael@0: #include "base/threading/thread_collision_warner.h" michael@0: michael@0: namespace base { michael@0: michael@0: namespace subtle { michael@0: michael@0: class BASE_EXPORT RefCountedBase { michael@0: public: michael@0: bool HasOneRef() const { return ref_count_ == 1; } michael@0: michael@0: protected: michael@0: RefCountedBase(); michael@0: ~RefCountedBase(); michael@0: michael@0: void AddRef() const; michael@0: michael@0: // Returns true if the object should self-delete. michael@0: bool Release() const; michael@0: michael@0: private: michael@0: mutable int ref_count_; michael@0: #ifndef NDEBUG michael@0: mutable bool in_dtor_; michael@0: #endif michael@0: michael@0: DFAKE_MUTEX(add_release_); michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(RefCountedBase); michael@0: }; michael@0: michael@0: class BASE_EXPORT RefCountedThreadSafeBase { michael@0: public: michael@0: bool HasOneRef() const; michael@0: michael@0: protected: michael@0: RefCountedThreadSafeBase(); michael@0: ~RefCountedThreadSafeBase(); michael@0: michael@0: void AddRef() const; michael@0: michael@0: // Returns true if the object should self-delete. michael@0: bool Release() const; michael@0: michael@0: private: michael@0: mutable AtomicRefCount ref_count_; michael@0: #ifndef NDEBUG michael@0: mutable bool in_dtor_; michael@0: #endif michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafeBase); michael@0: }; michael@0: michael@0: } // namespace subtle michael@0: michael@0: // michael@0: // A base class for reference counted classes. Otherwise, known as a cheap michael@0: // knock-off of WebKit's RefCounted class. To use this guy just extend your michael@0: // class from it like so: michael@0: // michael@0: // class MyFoo : public base::RefCounted { michael@0: // ... michael@0: // private: michael@0: // friend class base::RefCounted; michael@0: // ~MyFoo(); michael@0: // }; michael@0: // michael@0: // You should always make your destructor private, to avoid any code deleting michael@0: // the object accidently while there are references to it. michael@0: template michael@0: class RefCounted : public subtle::RefCountedBase { michael@0: public: michael@0: RefCounted() {} michael@0: michael@0: void AddRef() const { michael@0: subtle::RefCountedBase::AddRef(); michael@0: } michael@0: michael@0: void Release() const { michael@0: if (subtle::RefCountedBase::Release()) { michael@0: delete static_cast(this); michael@0: } michael@0: } michael@0: michael@0: protected: michael@0: ~RefCounted() {} michael@0: michael@0: private: michael@0: DISALLOW_COPY_AND_ASSIGN(RefCounted); michael@0: }; michael@0: michael@0: // Forward declaration. michael@0: template class RefCountedThreadSafe; michael@0: michael@0: // Default traits for RefCountedThreadSafe. Deletes the object when its ref michael@0: // count reaches 0. Overload to delete it on a different thread etc. michael@0: template michael@0: struct DefaultRefCountedThreadSafeTraits { michael@0: static void Destruct(const T* x) { michael@0: // Delete through RefCountedThreadSafe to make child classes only need to be michael@0: // friend with RefCountedThreadSafe instead of this struct, which is an michael@0: // implementation detail. michael@0: RefCountedThreadSafe::DeleteInternal(x); michael@0: } michael@0: }; michael@0: michael@0: // michael@0: // A thread-safe variant of RefCounted michael@0: // michael@0: // class MyFoo : public base::RefCountedThreadSafe { michael@0: // ... michael@0: // }; michael@0: // michael@0: // If you're using the default trait, then you should add compile time michael@0: // asserts that no one else is deleting your object. i.e. michael@0: // private: michael@0: // friend class base::RefCountedThreadSafe; michael@0: // ~MyFoo(); michael@0: template > michael@0: class RefCountedThreadSafe : public subtle::RefCountedThreadSafeBase { michael@0: public: michael@0: RefCountedThreadSafe() {} michael@0: michael@0: void AddRef() const { michael@0: subtle::RefCountedThreadSafeBase::AddRef(); michael@0: } michael@0: michael@0: void Release() const { michael@0: if (subtle::RefCountedThreadSafeBase::Release()) { michael@0: Traits::Destruct(static_cast(this)); michael@0: } michael@0: } michael@0: michael@0: protected: michael@0: ~RefCountedThreadSafe() {} michael@0: michael@0: private: michael@0: friend struct DefaultRefCountedThreadSafeTraits; michael@0: static void DeleteInternal(const T* x) { delete x; } michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafe); michael@0: }; michael@0: michael@0: // michael@0: // A thread-safe wrapper for some piece of data so we can place other michael@0: // things in scoped_refptrs<>. michael@0: // michael@0: template michael@0: class RefCountedData michael@0: : public base::RefCountedThreadSafe< base::RefCountedData > { michael@0: public: michael@0: RefCountedData() : data() {} michael@0: RefCountedData(const T& in_value) : data(in_value) {} michael@0: michael@0: T data; michael@0: michael@0: private: michael@0: friend class base::RefCountedThreadSafe >; michael@0: ~RefCountedData() {} michael@0: }; michael@0: michael@0: } // namespace base michael@0: michael@0: // michael@0: // A smart pointer class for reference counted objects. Use this class instead michael@0: // of calling AddRef and Release manually on a reference counted object to michael@0: // avoid common memory leaks caused by forgetting to Release an object michael@0: // reference. Sample usage: michael@0: // michael@0: // class MyFoo : public RefCounted { michael@0: // ... michael@0: // }; michael@0: // michael@0: // void some_function() { michael@0: // scoped_refptr foo = new MyFoo(); michael@0: // foo->Method(param); michael@0: // // |foo| is released when this function returns michael@0: // } michael@0: // michael@0: // void some_other_function() { michael@0: // scoped_refptr foo = new MyFoo(); michael@0: // ... michael@0: // foo = NULL; // explicitly releases |foo| michael@0: // ... michael@0: // if (foo) michael@0: // foo->Method(param); michael@0: // } michael@0: // michael@0: // The above examples show how scoped_refptr acts like a pointer to T. michael@0: // Given two scoped_refptr classes, it is also possible to exchange michael@0: // references between the two objects, like so: michael@0: // michael@0: // { michael@0: // scoped_refptr a = new MyFoo(); michael@0: // scoped_refptr b; michael@0: // michael@0: // b.swap(a); michael@0: // // now, |b| references the MyFoo object, and |a| references NULL. michael@0: // } michael@0: // michael@0: // To make both |a| and |b| in the above example reference the same MyFoo michael@0: // object, simply use the assignment operator: michael@0: // michael@0: // { michael@0: // scoped_refptr a = new MyFoo(); michael@0: // scoped_refptr b; michael@0: // michael@0: // b = a; michael@0: // // now, |a| and |b| each own a reference to the same MyFoo object. michael@0: // } michael@0: // michael@0: template michael@0: class scoped_refptr { michael@0: public: michael@0: typedef T element_type; michael@0: michael@0: scoped_refptr() : ptr_(NULL) { michael@0: } michael@0: michael@0: scoped_refptr(T* p) : ptr_(p) { michael@0: if (ptr_) michael@0: ptr_->AddRef(); michael@0: } michael@0: michael@0: scoped_refptr(const scoped_refptr& r) : ptr_(r.ptr_) { michael@0: if (ptr_) michael@0: ptr_->AddRef(); michael@0: } michael@0: michael@0: template michael@0: scoped_refptr(const scoped_refptr& r) : ptr_(r.get()) { michael@0: if (ptr_) michael@0: ptr_->AddRef(); michael@0: } michael@0: michael@0: ~scoped_refptr() { michael@0: if (ptr_) michael@0: ptr_->Release(); michael@0: } michael@0: michael@0: T* get() const { return ptr_; } michael@0: operator T*() const { return ptr_; } michael@0: T* operator->() const { michael@0: assert(ptr_ != NULL); michael@0: return ptr_; michael@0: } michael@0: michael@0: scoped_refptr& operator=(T* p) { michael@0: // AddRef first so that self assignment should work michael@0: if (p) michael@0: p->AddRef(); michael@0: T* old_ptr = ptr_; michael@0: ptr_ = p; michael@0: if (old_ptr) michael@0: old_ptr->Release(); michael@0: return *this; michael@0: } michael@0: michael@0: scoped_refptr& operator=(const scoped_refptr& r) { michael@0: return *this = r.ptr_; michael@0: } michael@0: michael@0: template michael@0: scoped_refptr& operator=(const scoped_refptr& r) { michael@0: return *this = r.get(); michael@0: } michael@0: michael@0: void swap(T** pp) { michael@0: T* p = ptr_; michael@0: ptr_ = *pp; michael@0: *pp = p; michael@0: } michael@0: michael@0: void swap(scoped_refptr& r) { michael@0: swap(&r.ptr_); michael@0: } michael@0: michael@0: protected: michael@0: T* ptr_; michael@0: }; michael@0: michael@0: // Handy utility for creating a scoped_refptr out of a T* explicitly without michael@0: // having to retype all the template arguments michael@0: template michael@0: scoped_refptr make_scoped_refptr(T* t) { michael@0: return scoped_refptr(t); michael@0: } michael@0: michael@0: #endif // BASE_MEMORY_REF_COUNTED_H_