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 void join(linked_ptr_internal const* ptr) 00109 GTEST_LOCK_EXCLUDED_(g_linked_ptr_mutex) { 00110 MutexLock lock(&g_linked_ptr_mutex); 00111 00112 linked_ptr_internal const* p = ptr; 00113 while (p->next_ != ptr) { 00114 assert(p->next_ != this && 00115 "Trying to join() a linked ring we are already in. " 00116 "Is GMock thread safety enabled?"); 00117 p = p->next_; 00118 } 00119 p->next_ = this; 00120 next_ = ptr; 00121 } 00122 00123 // Leave whatever circle we're part of. Returns true if we were the 00124 // last member of the circle. Once this is done, you can join() another. 00125 bool depart() 00126 GTEST_LOCK_EXCLUDED_(g_linked_ptr_mutex) { 00127 MutexLock lock(&g_linked_ptr_mutex); 00128 00129 if (next_ == this) return true; 00130 linked_ptr_internal const* p = next_; 00131 while (p->next_ != this) { 00132 assert(p->next_ != next_ && 00133 "Trying to depart() a linked ring we are not in. " 00134 "Is GMock thread safety enabled?"); 00135 p = p->next_; 00136 } 00137 p->next_ = next_; 00138 return false; 00139 } 00140 00141 private: 00142 mutable linked_ptr_internal const* next_; 00143 }; 00144 00145 template <typename T> 00146 class linked_ptr { 00147 public: 00148 typedef T element_type; 00149 00150 // Take over ownership of a raw pointer. This should happen as soon as 00151 // possible after the object is created. 00152 explicit linked_ptr(T* ptr = NULL) { capture(ptr); } 00153 ~linked_ptr() { depart(); } 00154 00155 // Copy an existing linked_ptr<>, adding ourselves to the list of references. 00156 template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); } 00157 linked_ptr(linked_ptr const& ptr) { // NOLINT 00158 assert(&ptr != this); 00159 copy(&ptr); 00160 } 00161 00162 // Assignment releases the old value and acquires the new. 00163 template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) { 00164 depart(); 00165 copy(&ptr); 00166 return *this; 00167 } 00168 00169 linked_ptr& operator=(linked_ptr const& ptr) { 00170 if (&ptr != this) { 00171 depart(); 00172 copy(&ptr); 00173 } 00174 return *this; 00175 } 00176 00177 // Smart pointer members. 00178 void reset(T* ptr = NULL) { 00179 depart(); 00180 capture(ptr); 00181 } 00182 T* get() const { return value_; } 00183 T* operator->() const { return value_; } 00184 T& operator*() const { return *value_; } 00185 00186 bool operator==(T* p) const { return value_ == p; } 00187 bool operator!=(T* p) const { return value_ != p; } 00188 template <typename U> 00189 bool operator==(linked_ptr<U> const& ptr) const { 00190 return value_ == ptr.get(); 00191 } 00192 template <typename U> 00193 bool operator!=(linked_ptr<U> const& ptr) const { 00194 return value_ != ptr.get(); 00195 } 00196 00197 private: 00198 template <typename U> 00199 friend class linked_ptr; 00200 00201 T* value_; 00202 linked_ptr_internal link_; 00203 00204 void depart() { 00205 if (link_.depart()) delete value_; 00206 } 00207 00208 void capture(T* ptr) { 00209 value_ = ptr; 00210 link_.join_new(); 00211 } 00212 00213 template <typename U> void copy(linked_ptr<U> const* ptr) { 00214 value_ = ptr->get(); 00215 if (value_) 00216 link_.join(&ptr->link_); 00217 else 00218 link_.join_new(); 00219 } 00220 }; 00221 00222 template<typename T> inline 00223 bool operator==(T* ptr, const linked_ptr<T>& x) { 00224 return ptr == x.get(); 00225 } 00226 00227 template<typename T> inline 00228 bool operator!=(T* ptr, const linked_ptr<T>& x) { 00229 return ptr != x.get(); 00230 } 00231 00232 // A function to convert T* into linked_ptr<T> 00233 // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation 00234 // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) 00235 template <typename T> 00236 linked_ptr<T> make_linked_ptr(T* ptr) { 00237 return linked_ptr<T>(ptr); 00238 } 00239 00240 } // namespace internal 00241 } // namespace testing 00242 00243 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_