1 // Copyright (c) 2006, 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 #ifndef PROCESSOR_LINKED_PTR_H__
54 #define PROCESSOR_LINKED_PTR_H__
55
56 namespace google_breakpad {
57
58 // This is used internally by all instances of linked_ptr<>. It needs to be
59 // a non-template class because different types of linked_ptr<> can refer to
60 // the same object (linked_ptr<Superclass>(obj) vs linked_ptr<Subclass>(obj)).
61 // So, it needs to be possible for different types of linked_ptr to participate
62 // in the same circular linked list, so we need a single class type here.
63 //
64 // DO NOT USE THIS CLASS DIRECTLY YOURSELF. Use linked_ptr<T>.
65 class linked_ptr_internal {
66 public:
67 // Create a new circle that includes only this instance.
join_new()68 void join_new() {
69 next_ = this;
70 }
71
72 // Join an existing circle.
join(linked_ptr_internal const * ptr)73 void join(linked_ptr_internal const* ptr) {
74 linked_ptr_internal const* p = ptr;
75 while (p->next_ != ptr) p = p->next_;
76 p->next_ = this;
77 next_ = ptr;
78 }
79
80 // Leave whatever circle we're part of. Returns true iff we were the
81 // last member of the circle. Once this is done, you can join() another.
depart()82 bool depart() {
83 if (next_ == this) return true;
84 linked_ptr_internal const* p = next_;
85 while (p->next_ != this) p = p->next_;
86 p->next_ = next_;
87 return false;
88 }
89
90 private:
91 mutable linked_ptr_internal const* next_;
92 };
93
94 template <typename T>
95 class linked_ptr {
96 public:
97 typedef T element_type;
98
99 // Take over ownership of a raw pointer. This should happen as soon as
100 // possible after the object is created.
101 explicit linked_ptr(T* ptr = NULL) { capture(ptr); }
~linked_ptr()102 ~linked_ptr() { depart(); }
103
104 // Copy an existing linked_ptr<>, adding ourselves to the list of references.
linked_ptr(linked_ptr<U> const & ptr)105 template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); }
linked_ptr(linked_ptr const & ptr)106 linked_ptr(linked_ptr const& ptr) { copy(&ptr); }
107
108 // Assignment releases the old value and acquires the new.
109 template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) {
110 depart();
111 copy(&ptr);
112 return *this;
113 }
114
115 linked_ptr& operator=(linked_ptr const& ptr) {
116 if (&ptr != this) {
117 depart();
118 copy(&ptr);
119 }
120 return *this;
121 }
122
123 // Smart pointer members.
124 void reset(T* ptr = NULL) { depart(); capture(ptr); }
get()125 T* get() const { return value_; }
126 T* operator->() const { return value_; }
127 T& operator*() const { return *value_; }
128 // Release ownership of the pointed object and returns it.
129 // Sole ownership by this linked_ptr object is required.
release()130 T* release() {
131 link_.depart();
132 T* v = value_;
133 value_ = NULL;
134 return v;
135 }
136
137 bool operator==(T* p) const { return value_ == p; }
138 bool operator!=(T* p) const { return value_ != p; }
139 template <typename U>
140 bool operator==(linked_ptr<U> const& ptr) const {
141 return value_ == ptr.get();
142 }
143 template <typename U>
144 bool operator!=(linked_ptr<U> const& ptr) const {
145 return value_ != ptr.get();
146 }
147
148 private:
149 template <typename U>
150 friend class linked_ptr;
151
152 T* value_;
153 linked_ptr_internal link_;
154
depart()155 void depart() {
156 if (link_.depart()) delete value_;
157 }
158
capture(T * ptr)159 void capture(T* ptr) {
160 value_ = ptr;
161 link_.join_new();
162 }
163
copy(linked_ptr<U> const * ptr)164 template <typename U> void copy(linked_ptr<U> const* ptr) {
165 value_ = ptr->get();
166 if (value_)
167 link_.join(&ptr->link_);
168 else
169 link_.join_new();
170 }
171 };
172
173 template<typename T> inline
174 bool operator==(T* ptr, const linked_ptr<T>& x) {
175 return ptr == x.get();
176 }
177
178 template<typename T> inline
179 bool operator!=(T* ptr, const linked_ptr<T>& x) {
180 return ptr != x.get();
181 }
182
183 // A function to convert T* into linked_ptr<T>
184 // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation
185 // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
186 template <typename T>
make_linked_ptr(T * ptr)187 linked_ptr<T> make_linked_ptr(T* ptr) {
188 return linked_ptr<T>(ptr);
189 }
190
191 } // namespace google_breakpad
192
193 #endif // PROCESSOR_LINKED_PTR_H__
194