00001 // Copyright 2003 Google Inc. 00002 // All rights reserved. 00003 // 00004 // Redistribution and use in source and binary forms, with or without 00005 // modification, are permitted provided that the following conditions are 00006 // met: 00007 // 00008 // * Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // * Redistributions in binary form must reproduce the above 00011 // copyright notice, this list of conditions and the following disclaimer 00012 // in the documentation and/or other materials provided with the 00013 // distribution. 00014 // * Neither the name of Google Inc. nor the names of its 00015 // contributors may be used to endorse or promote products derived from 00016 // this software without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 // 00030 // Authors: Dan Egnor (egnor@google.com) 00031 // 00032 // A "smart" pointer type with reference tracking. Every pointer to a 00033 // particular object is kept on a circular linked list. When the last pointer 00034 // to an object is destroyed or reassigned, the object is deleted. 00035 // 00036 // Used properly, this deletes the object when the last reference goes away. 00037 // There are several caveats: 00038 // - Like all reference counting schemes, cycles lead to leaks. 00039 // - Each smart pointer is actually two pointers (8 bytes instead of 4). 00040 // - Every time a pointer is assigned, the entire list of pointers to that 00041 // object is traversed. This class is therefore NOT SUITABLE when there 00042 // will often be more than two or three pointers to a particular object. 00043 // - References are only tracked as long as linked_ptr<> objects are copied. 00044 // If a linked_ptr<> is converted to a raw pointer and back, BAD THINGS 00045 // will happen (double deletion). 00046 // 00047 // A good use of this class is storing object references in STL containers. 00048 // You can safely put linked_ptr<> in a vector<>. 00049 // Other uses may not be as good. 00050 // 00051 // Note: If you use an incomplete type with linked_ptr<>, the class 00052 // *containing* linked_ptr<> must have a constructor and destructor (even 00053 // if they do nothing!). 00054 // 00055 // Bill Gibbons suggested we use something like this. 00056 // 00057 // Thread Safety: 00058 // Unlike other linked_ptr implementations, in this implementation 00059 // a linked_ptr object is thread-safe in the sense that: 00060 // - it's safe to copy linked_ptr objects concurrently, 00061 // - it's safe to copy *from* a linked_ptr and read its underlying 00062 // raw pointer (e.g. via get()) concurrently, and 00063 // - it's safe to write to two linked_ptrs that point to the same 00064 // shared object concurrently. 00065 // TODO(wan@google.com): rename this to safe_linked_ptr to avoid 00066 // confusion with normal linked_ptr. 00067 00068 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_ 00069 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_ 00070 00071 #include <stdlib.h> 00072 #include <assert.h> 00073 00074 #include "gtest/internal/gtest-port.h" 00075 00076 namespace testing { 00077 namespace internal { 00078 00079 // Protects copying of all linked_ptr objects. 00080 GTEST_API_ GTEST_DECLARE_STATIC_MUTEX_(g_linked_ptr_mutex); 00081 00082 // This is used internally by all instances of linked_ptr<>. It needs to be 00083 // a non-template class because different types of linked_ptr<> can refer to 00084 // the same object (linked_ptr<Superclass>(obj) vs linked_ptr<Subclass>(obj)). 00085 // So, it needs to be possible for different types of linked_ptr to participate 00086 // in the same circular linked list, so we need a single class type here. 00087 // 00088 // DO NOT USE THIS CLASS DIRECTLY YOURSELF. Use linked_ptr<T>. 00089 class linked_ptr_internal { 00090 public: 00091 // Create a new circle that includes only this instance. 00092 void join_new() { 00093 next_ = this; 00094 } 00095 00096 // Many linked_ptr operations may change p.link_ for some linked_ptr 00097 // variable p in the same circle as this object. Therefore we need 00098 // to prevent two such operations from occurring concurrently. 00099 // 00100 // Note that different types of linked_ptr objects can coexist in a 00101 // circle (e.g. linked_ptr<Base>, linked_ptr<Derived1>, and 00102 // linked_ptr<Derived2>). Therefore we must use a single mutex to 00103 // protect all linked_ptr objects. This can create serious 00104 // contention in production code, but is acceptable in a testing 00105 // framework. 00106 00107 // Join an existing circle. 00108 // L < g_linked_ptr_mutex 00109 void join(linked_ptr_internal const* ptr) { 00110 MutexLock lock(&g_linked_ptr_mutex); 00111 00112 linked_ptr_internal const* p = ptr; 00113 while (p->next_ != ptr) p = p->next_; 00114 p->next_ = this; 00115 next_ = ptr; 00116 } 00117 00118 // Leave whatever circle we're part of. Returns true if we were the 00119 // last member of the circle. Once this is done, you can join() another. 00120 // L < g_linked_ptr_mutex 00121 bool depart() { 00122 MutexLock lock(&g_linked_ptr_mutex); 00123 00124 if (next_ == this) return true; 00125 linked_ptr_internal const* p = next_; 00126 while (p->next_ != this) p = p->next_; 00127 p->next_ = next_; 00128 return false; 00129 } 00130 00131 private: 00132 mutable linked_ptr_internal const* next_; 00133 }; 00134 00135 template <typename T> 00136 class linked_ptr { 00137 public: 00138 typedef T element_type; 00139 00140 // Take over ownership of a raw pointer. This should happen as soon as 00141 // possible after the object is created. 00142 explicit linked_ptr(T* ptr = NULL) { capture(ptr); } 00143 ~linked_ptr() { depart(); } 00144 00145 // Copy an existing linked_ptr<>, adding ourselves to the list of references. 00146 template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); } 00147 linked_ptr(linked_ptr const& ptr) { // NOLINT 00148 assert(&ptr != this); 00149 copy(&ptr); 00150 } 00151 00152 // Assignment releases the old value and acquires the new. 00153 template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) { 00154 depart(); 00155 copy(&ptr); 00156 return *this; 00157 } 00158 00159 linked_ptr& operator=(linked_ptr const& ptr) { 00160 if (&ptr != this) { 00161 depart(); 00162 copy(&ptr); 00163 } 00164 return *this; 00165 } 00166 00167 // Smart pointer members. 00168 void reset(T* ptr = NULL) { 00169 depart(); 00170 capture(ptr); 00171 } 00172 T* get() const { return value_; } 00173 T* operator->() const { return value_; } 00174 T& operator*() const { return *value_; } 00175 00176 bool operator==(T* p) const { return value_ == p; } 00177 bool operator!=(T* p) const { return value_ != p; } 00178 template <typename U> 00179 bool operator==(linked_ptr<U> const& ptr) const { 00180 return value_ == ptr.get(); 00181 } 00182 template <typename U> 00183 bool operator!=(linked_ptr<U> const& ptr) const { 00184 return value_ != ptr.get(); 00185 } 00186 00187 private: 00188 template <typename U> 00189 friend class linked_ptr; 00190 00191 T* value_; 00192 linked_ptr_internal link_; 00193 00194 void depart() { 00195 if (link_.depart()) delete value_; 00196 } 00197 00198 void capture(T* ptr) { 00199 value_ = ptr; 00200 link_.join_new(); 00201 } 00202 00203 template <typename U> void copy(linked_ptr<U> const* ptr) { 00204 value_ = ptr->get(); 00205 if (value_) 00206 link_.join(&ptr->link_); 00207 else 00208 link_.join_new(); 00209 } 00210 }; 00211 00212 template<typename T> inline 00213 bool operator==(T* ptr, const linked_ptr<T>& x) { 00214 return ptr == x.get(); 00215 } 00216 00217 template<typename T> inline 00218 bool operator!=(T* ptr, const linked_ptr<T>& x) { 00219 return ptr != x.get(); 00220 } 00221 00222 // A function to convert T* into linked_ptr<T> 00223 // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation 00224 // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) 00225 template <typename T> 00226 linked_ptr<T> make_linked_ptr(T* ptr) { 00227 return linked_ptr<T>(ptr); 00228 } 00229 00230 } // namespace internal 00231 } // namespace testing 00232 00233 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_