gtest-linked_ptr.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright 2003 Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // A "smart" pointer type with reference tracking. Every pointer to a
  31. // particular object is kept on a circular linked list. When the last pointer
  32. // to an object is destroyed or reassigned, the object is deleted.
  33. //
  34. // Used properly, this deletes the object when the last reference goes away.
  35. // There are several caveats:
  36. // - Like all reference counting schemes, cycles lead to leaks.
  37. // - Each smart pointer is actually two pointers (8 bytes instead of 4).
  38. // - Every time a pointer is assigned, the entire list of pointers to that
  39. // object is traversed. This class is therefore NOT SUITABLE when there
  40. // will often be more than two or three pointers to a particular object.
  41. // - References are only tracked as long as linked_ptr<> objects are copied.
  42. // If a linked_ptr<> is converted to a raw pointer and back, BAD THINGS
  43. // will happen (double deletion).
  44. //
  45. // A good use of this class is storing object references in STL containers.
  46. // You can safely put linked_ptr<> in a vector<>.
  47. // Other uses may not be as good.
  48. //
  49. // Note: If you use an incomplete type with linked_ptr<>, the class
  50. // *containing* linked_ptr<> must have a constructor and destructor (even
  51. // if they do nothing!).
  52. //
  53. // Bill Gibbons suggested we use something like this.
  54. //
  55. // Thread Safety:
  56. // Unlike other linked_ptr implementations, in this implementation
  57. // a linked_ptr object is thread-safe in the sense that:
  58. // - it's safe to copy linked_ptr objects concurrently,
  59. // - it's safe to copy *from* a linked_ptr and read its underlying
  60. // raw pointer (e.g. via get()) concurrently, and
  61. // - it's safe to write to two linked_ptrs that point to the same
  62. // shared object concurrently.
  63. // FIXME: rename this to safe_linked_ptr to avoid
  64. // confusion with normal linked_ptr.
  65. // GOOGLETEST_CM0001 DO NOT DELETE
  66. #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_
  67. #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_
  68. #include <stdlib.h>
  69. #include <assert.h>
  70. #include "gtest/internal/gtest-port.h"
  71. namespace testing {
  72. namespace internal {
  73. // Protects copying of all linked_ptr objects.
  74. GTEST_API_ GTEST_DECLARE_STATIC_MUTEX_(g_linked_ptr_mutex);
  75. // This is used internally by all instances of linked_ptr<>. It needs to be
  76. // a non-template class because different types of linked_ptr<> can refer to
  77. // the same object (linked_ptr<Superclass>(obj) vs linked_ptr<Subclass>(obj)).
  78. // So, it needs to be possible for different types of linked_ptr to participate
  79. // in the same circular linked list, so we need a single class type here.
  80. //
  81. // DO NOT USE THIS CLASS DIRECTLY YOURSELF. Use linked_ptr<T>.
  82. class linked_ptr_internal {
  83. public:
  84. // Create a new circle that includes only this instance.
  85. void join_new() {
  86. next_ = this;
  87. }
  88. // Many linked_ptr operations may change p.link_ for some linked_ptr
  89. // variable p in the same circle as this object. Therefore we need
  90. // to prevent two such operations from occurring concurrently.
  91. //
  92. // Note that different types of linked_ptr objects can coexist in a
  93. // circle (e.g. linked_ptr<Base>, linked_ptr<Derived1>, and
  94. // linked_ptr<Derived2>). Therefore we must use a single mutex to
  95. // protect all linked_ptr objects. This can create serious
  96. // contention in production code, but is acceptable in a testing
  97. // framework.
  98. // Join an existing circle.
  99. void join(linked_ptr_internal const* ptr)
  100. GTEST_LOCK_EXCLUDED_(g_linked_ptr_mutex) {
  101. MutexLock lock(&g_linked_ptr_mutex);
  102. linked_ptr_internal const* p = ptr;
  103. while (p->next_ != ptr) {
  104. assert(p->next_ != this &&
  105. "Trying to join() a linked ring we are already in. "
  106. "Is GMock thread safety enabled?");
  107. p = p->next_;
  108. }
  109. p->next_ = this;
  110. next_ = ptr;
  111. }
  112. // Leave whatever circle we're part of. Returns true if we were the
  113. // last member of the circle. Once this is done, you can join() another.
  114. bool depart()
  115. GTEST_LOCK_EXCLUDED_(g_linked_ptr_mutex) {
  116. MutexLock lock(&g_linked_ptr_mutex);
  117. if (next_ == this) return true;
  118. linked_ptr_internal const* p = next_;
  119. while (p->next_ != this) {
  120. assert(p->next_ != next_ &&
  121. "Trying to depart() a linked ring we are not in. "
  122. "Is GMock thread safety enabled?");
  123. p = p->next_;
  124. }
  125. p->next_ = next_;
  126. return false;
  127. }
  128. private:
  129. mutable linked_ptr_internal const* next_;
  130. };
  131. template <typename T>
  132. class linked_ptr {
  133. public:
  134. typedef T element_type;
  135. // Take over ownership of a raw pointer. This should happen as soon as
  136. // possible after the object is created.
  137. explicit linked_ptr(T* ptr = NULL) { capture(ptr); }
  138. ~linked_ptr() { depart(); }
  139. // Copy an existing linked_ptr<>, adding ourselves to the list of references.
  140. template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); }
  141. linked_ptr(linked_ptr const& ptr) { // NOLINT
  142. assert(&ptr != this);
  143. copy(&ptr);
  144. }
  145. // Assignment releases the old value and acquires the new.
  146. template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) {
  147. depart();
  148. copy(&ptr);
  149. return *this;
  150. }
  151. linked_ptr& operator=(linked_ptr const& ptr) {
  152. if (&ptr != this) {
  153. depart();
  154. copy(&ptr);
  155. }
  156. return *this;
  157. }
  158. // Smart pointer members.
  159. void reset(T* ptr = NULL) {
  160. depart();
  161. capture(ptr);
  162. }
  163. T* get() const { return value_; }
  164. T* operator->() const { return value_; }
  165. T& operator*() const { return *value_; }
  166. bool operator==(T* p) const { return value_ == p; }
  167. bool operator!=(T* p) const { return value_ != p; }
  168. template <typename U>
  169. bool operator==(linked_ptr<U> const& ptr) const {
  170. return value_ == ptr.get();
  171. }
  172. template <typename U>
  173. bool operator!=(linked_ptr<U> const& ptr) const {
  174. return value_ != ptr.get();
  175. }
  176. private:
  177. template <typename U>
  178. friend class linked_ptr;
  179. T* value_;
  180. linked_ptr_internal link_;
  181. void depart() {
  182. if (link_.depart()) delete value_;
  183. }
  184. void capture(T* ptr) {
  185. value_ = ptr;
  186. link_.join_new();
  187. }
  188. template <typename U> void copy(linked_ptr<U> const* ptr) {
  189. value_ = ptr->get();
  190. if (value_)
  191. link_.join(&ptr->link_);
  192. else
  193. link_.join_new();
  194. }
  195. };
  196. template<typename T> inline
  197. bool operator==(T* ptr, const linked_ptr<T>& x) {
  198. return ptr == x.get();
  199. }
  200. template<typename T> inline
  201. bool operator!=(T* ptr, const linked_ptr<T>& x) {
  202. return ptr != x.get();
  203. }
  204. // A function to convert T* into linked_ptr<T>
  205. // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation
  206. // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
  207. template <typename T>
  208. linked_ptr<T> make_linked_ptr(T* ptr) {
  209. return linked_ptr<T>(ptr);
  210. }
  211. } // namespace internal
  212. } // namespace testing
  213. #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_