/home/arjun/llvm-project/llvm/utils/unittest/googletest/include/gtest/internal/gtest-linked_ptr.h
Line | Count | Source (jump to first uncovered line) |
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 | | // Authors: Dan Egnor (egnor@google.com) |
31 | | // |
32 | | // A "smart" pointer type with reference tracking. Every pointer to a |
33 | | // particular object is kept on a circular linked list. When the last pointer |
34 | | // to an object is destroyed or reassigned, the object is deleted. |
35 | | // |
36 | | // Used properly, this deletes the object when the last reference goes away. |
37 | | // There are several caveats: |
38 | | // - Like all reference counting schemes, cycles lead to leaks. |
39 | | // - Each smart pointer is actually two pointers (8 bytes instead of 4). |
40 | | // - Every time a pointer is assigned, the entire list of pointers to that |
41 | | // object is traversed. This class is therefore NOT SUITABLE when there |
42 | | // will often be more than two or three pointers to a particular object. |
43 | | // - References are only tracked as long as linked_ptr<> objects are copied. |
44 | | // If a linked_ptr<> is converted to a raw pointer and back, BAD THINGS |
45 | | // will happen (double deletion). |
46 | | // |
47 | | // A good use of this class is storing object references in STL containers. |
48 | | // You can safely put linked_ptr<> in a vector<>. |
49 | | // Other uses may not be as good. |
50 | | // |
51 | | // Note: If you use an incomplete type with linked_ptr<>, the class |
52 | | // *containing* linked_ptr<> must have a constructor and destructor (even |
53 | | // if they do nothing!). |
54 | | // |
55 | | // Bill Gibbons suggested we use something like this. |
56 | | // |
57 | | // Thread Safety: |
58 | | // Unlike other linked_ptr implementations, in this implementation |
59 | | // a linked_ptr object is thread-safe in the sense that: |
60 | | // - it's safe to copy linked_ptr objects concurrently, |
61 | | // - it's safe to copy *from* a linked_ptr and read its underlying |
62 | | // raw pointer (e.g. via get()) concurrently, and |
63 | | // - it's safe to write to two linked_ptrs that point to the same |
64 | | // shared object concurrently. |
65 | | // TODO(wan@google.com): rename this to safe_linked_ptr to avoid |
66 | | // confusion with normal linked_ptr. |
67 | | |
68 | | #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_ |
69 | | #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_ |
70 | | |
71 | | #include <stdlib.h> |
72 | | #include <assert.h> |
73 | | |
74 | | #include "gtest/internal/gtest-port.h" |
75 | | |
76 | | namespace testing { |
77 | | namespace internal { |
78 | | |
79 | | // Protects copying of all linked_ptr objects. |
80 | | GTEST_API_ GTEST_DECLARE_STATIC_MUTEX_(g_linked_ptr_mutex); |
81 | | |
82 | | // This is used internally by all instances of linked_ptr<>. It needs to be |
83 | | // a non-template class because different types of linked_ptr<> can refer to |
84 | | // the same object (linked_ptr<Superclass>(obj) vs linked_ptr<Subclass>(obj)). |
85 | | // So, it needs to be possible for different types of linked_ptr to participate |
86 | | // in the same circular linked list, so we need a single class type here. |
87 | | // |
88 | | // DO NOT USE THIS CLASS DIRECTLY YOURSELF. Use linked_ptr<T>. |
89 | | class linked_ptr_internal { |
90 | | public: |
91 | | // Create a new circle that includes only this instance. |
92 | 10 | void join_new() { |
93 | 10 | next_ = this; |
94 | 10 | } |
95 | | |
96 | | // Many linked_ptr operations may change p.link_ for some linked_ptr |
97 | | // variable p in the same circle as this object. Therefore we need |
98 | | // to prevent two such operations from occurring concurrently. |
99 | | // |
100 | | // Note that different types of linked_ptr objects can coexist in a |
101 | | // circle (e.g. linked_ptr<Base>, linked_ptr<Derived1>, and |
102 | | // linked_ptr<Derived2>). Therefore we must use a single mutex to |
103 | | // protect all linked_ptr objects. This can create serious |
104 | | // contention in production code, but is acceptable in a testing |
105 | | // framework. |
106 | | |
107 | | // Join an existing circle. |
108 | | void join(linked_ptr_internal const* ptr) |
109 | 18 | GTEST_LOCK_EXCLUDED_(g_linked_ptr_mutex) { |
110 | 18 | MutexLock lock(&g_linked_ptr_mutex); |
111 | 18 | |
112 | 18 | linked_ptr_internal const* p = ptr; |
113 | 24 | while (p->next_ != ptr) { |
114 | 6 | assert(p->next_ != this && |
115 | 6 | "Trying to join() a linked ring we are already in. " |
116 | 6 | "Is GMock thread safety enabled?"); |
117 | 6 | p = p->next_; |
118 | 6 | } |
119 | 18 | p->next_ = this; |
120 | 18 | next_ = ptr; |
121 | 18 | } |
122 | | |
123 | | // Leave whatever circle we're part of. Returns true if we were the |
124 | | // last member of the circle. Once this is done, you can join() another. |
125 | | bool depart() |
126 | 28 | GTEST_LOCK_EXCLUDED_(g_linked_ptr_mutex) { |
127 | 28 | MutexLock lock(&g_linked_ptr_mutex); |
128 | 28 | |
129 | 28 | if (next_ == this) return true; |
130 | 18 | linked_ptr_internal const* p = next_; |
131 | 24 | while (p->next_ != this) { |
132 | 6 | assert(p->next_ != next_ && |
133 | 6 | "Trying to depart() a linked ring we are not in. " |
134 | 6 | "Is GMock thread safety enabled?"); |
135 | 6 | p = p->next_; |
136 | 6 | } |
137 | 18 | p->next_ = next_; |
138 | 18 | return false; |
139 | 18 | } |
140 | | |
141 | | private: |
142 | | mutable linked_ptr_internal const* next_; |
143 | | }; |
144 | | |
145 | | template <typename T> |
146 | | class linked_ptr { |
147 | | public: |
148 | | typedef T element_type; |
149 | | |
150 | | // Take over ownership of a raw pointer. This should happen as soon as |
151 | | // possible after the object is created. |
152 | 0 | explicit linked_ptr(T* ptr = NULL) { capture(ptr); } Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS_20CardinalityInterfaceEEC2EPS3_ Unexecuted instantiation: _ZN7testing8internal10linked_ptrINS_11ExpectationEEC2EPS2_ Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS_16MatcherInterfaceIRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEEC2EPSC_ Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS_16MatcherInterfaceINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEEC2EPSA_ Unexecuted instantiation: _ZN7testing8internal10linked_ptrINS0_15ExpectationBaseEEC2EPS2_ |
153 | 0 | ~linked_ptr() { depart(); } Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS_16MatcherInterfaceIRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEED2Ev Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS_16MatcherInterfaceINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEED2Ev Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS_20CardinalityInterfaceEED2Ev Unexecuted instantiation: _ZN7testing8internal10linked_ptrINS_11ExpectationEED2Ev Unexecuted instantiation: _ZN7testing8internal10linked_ptrINS0_15ExpectationBaseEED2Ev |
154 | | |
155 | | // Copy an existing linked_ptr<>, adding ourselves to the list of references. |
156 | | template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); } |
157 | 0 | linked_ptr(linked_ptr const& ptr) { // NOLINT |
158 | 0 | assert(&ptr != this); |
159 | 0 | copy(&ptr); |
160 | 0 | } |
161 | | |
162 | | // Assignment releases the old value and acquires the new. |
163 | | template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) { |
164 | | depart(); |
165 | | copy(&ptr); |
166 | | return *this; |
167 | | } |
168 | | |
169 | 0 | linked_ptr& operator=(linked_ptr const& ptr) { |
170 | 0 | if (&ptr != this) { |
171 | 0 | depart(); |
172 | 0 | copy(&ptr); |
173 | 0 | } |
174 | 0 | return *this; |
175 | 0 | } Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS_20CardinalityInterfaceEEaSERKS4_ Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS_16MatcherInterfaceIRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEEaSERKSD_ Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS_16MatcherInterfaceINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEEaSERKSB_ Unexecuted instantiation: _ZN7testing8internal10linked_ptrINS0_15ExpectationBaseEEaSERKS3_ |
176 | | |
177 | | // Smart pointer members. |
178 | | void reset(T* ptr = NULL) { |
179 | | depart(); |
180 | | capture(ptr); |
181 | | } |
182 | 0 | T* get() const { return value_; } Unexecuted instantiation: _ZNK7testing8internal10linked_ptrIKNS0_23ParamGeneratorInterfaceIbEEE3getEv Unexecuted instantiation: _ZNK7testing8internal10linked_ptrIKNS_20CardinalityInterfaceEE3getEv Unexecuted instantiation: _ZNK7testing8internal10linked_ptrIKNS0_2REEE3getEv Unexecuted instantiation: _ZNK7testing8internal10linked_ptrINS0_15ExpectationBaseEE3getEv Unexecuted instantiation: _ZNK7testing8internal10linked_ptrIKNS_16MatcherInterfaceIRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEE3getEv Unexecuted instantiation: _ZNK7testing8internal10linked_ptrIKNS_16MatcherInterfaceINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEE3getEv |
183 | 0 | T* operator->() const { return value_; } Unexecuted instantiation: _ZNK7testing8internal10linked_ptrIKNS_20CardinalityInterfaceEEptEv Unexecuted instantiation: _ZNK7testing8internal10linked_ptrIKNS0_2REEEptEv Unexecuted instantiation: _ZNK7testing8internal10linked_ptrINS0_15ExpectationBaseEEptEv Unexecuted instantiation: _ZNK7testing8internal10linked_ptrINS_11ExpectationEEptEv |
184 | 0 | T& operator*() const { return *value_; } |
185 | | |
186 | | bool operator==(T* p) const { return value_ == p; } |
187 | 0 | bool operator!=(T* p) const { return value_ != p; } |
188 | | template <typename U> |
189 | 0 | bool operator==(linked_ptr<U> const& ptr) const { |
190 | 0 | return value_ == ptr.get(); |
191 | 0 | } |
192 | | template <typename U> |
193 | | bool operator!=(linked_ptr<U> const& ptr) const { |
194 | | return value_ != ptr.get(); |
195 | | } |
196 | | |
197 | | private: |
198 | | template <typename U> |
199 | | friend class linked_ptr; |
200 | | |
201 | | T* value_; |
202 | | linked_ptr_internal link_; |
203 | | |
204 | 0 | void depart() { |
205 | 0 | if (link_.depart()) delete value_; |
206 | 0 | } Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS0_23ParamGeneratorInterfaceIbEEE6departEv Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS_20CardinalityInterfaceEE6departEv Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS_16MatcherInterfaceIRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEE6departEv Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS_16MatcherInterfaceINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEE6departEv Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS0_2REEE6departEv Unexecuted instantiation: _ZN7testing8internal10linked_ptrINS_11ExpectationEE6departEv Unexecuted instantiation: _ZN7testing8internal10linked_ptrINS0_15ExpectationBaseEE6departEv |
207 | | |
208 | 0 | void capture(T* ptr) { |
209 | 0 | value_ = ptr; |
210 | 0 | link_.join_new(); |
211 | 0 | } Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS0_23ParamGeneratorInterfaceIbEEE7captureEPS4_ Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS_20CardinalityInterfaceEE7captureEPS3_ Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS_16MatcherInterfaceIRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEE7captureEPSC_ Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS_16MatcherInterfaceINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEE7captureEPSA_ Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS0_2REEE7captureEPS3_ Unexecuted instantiation: _ZN7testing8internal10linked_ptrINS_11ExpectationEE7captureEPS2_ Unexecuted instantiation: _ZN7testing8internal10linked_ptrINS0_15ExpectationBaseEE7captureEPS2_ |
212 | | |
213 | 0 | template <typename U> void copy(linked_ptr<U> const* ptr) { |
214 | 0 | value_ = ptr->get(); |
215 | 0 | if (value_) |
216 | 0 | link_.join(&ptr->link_); |
217 | 0 | else |
218 | 0 | link_.join_new(); |
219 | 0 | } Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS0_23ParamGeneratorInterfaceIbEEE4copyIS4_EEvPKNS1_IT_EE Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS_20CardinalityInterfaceEE4copyIS3_EEvPKNS1_IT_EE Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS0_2REEE4copyIS3_EEvPKNS1_IT_EE Unexecuted instantiation: _ZN7testing8internal10linked_ptrINS0_15ExpectationBaseEE4copyIS2_EEvPKNS1_IT_EE Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS_16MatcherInterfaceIRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEE4copyISC_EEvPKNS1_IT_EE Unexecuted instantiation: _ZN7testing8internal10linked_ptrIKNS_16MatcherInterfaceINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEE4copyISA_EEvPKNS1_IT_EE |
220 | | }; |
221 | | |
222 | | template<typename T> inline |
223 | | bool operator==(T* ptr, const linked_ptr<T>& x) { |
224 | | return ptr == x.get(); |
225 | | } |
226 | | |
227 | | template<typename T> inline |
228 | | bool operator!=(T* ptr, const linked_ptr<T>& x) { |
229 | | return ptr != x.get(); |
230 | | } |
231 | | |
232 | | // A function to convert T* into linked_ptr<T> |
233 | | // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation |
234 | | // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) |
235 | | template <typename T> |
236 | | linked_ptr<T> make_linked_ptr(T* ptr) { |
237 | | return linked_ptr<T>(ptr); |
238 | | } |
239 | | |
240 | | } // namespace internal |
241 | | } // namespace testing |
242 | | |
243 | | #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_ |