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