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