1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // A "smart" pointer type with reference tracking. Every pointer to a
6 // particular object is kept on a circular linked list. When the last pointer
7 // to an object is destroyed or reassigned, the object is deleted.
8 //
9 // Used properly, this deletes the object when the last reference goes away.
10 // There are several caveats:
11 // - Like all reference counting schemes, cycles lead to leaks.
12 // - Each smart pointer is actually two pointers (8 bytes instead of 4).
13 // - Every time a pointer is released, the entire list of pointers to that
14 // object is traversed. This class is therefore NOT SUITABLE when there
15 // will often be more than two or three pointers to a particular object.
16 // - References are only tracked as long as linked_ptr<> objects are copied.
17 // If a linked_ptr<> is converted to a raw pointer and back, BAD THINGS
18 // will happen (double deletion).
19 //
20 // Note: If you use an incomplete type with linked_ptr<>, the class
21 // *containing* linked_ptr<> must have a constructor and destructor (even
22 // if they do nothing!).
23 //
24 // Thread Safety:
25 // A linked_ptr is NOT thread safe. Copying a linked_ptr object is
26 // effectively a read-write operation.
27 //
28 // Alternative: to linked_ptr is shared_ptr, which
29 // - is also two pointers in size (8 bytes for 32 bit addresses)
30 // - is thread safe for copying and deletion
31 // - supports weak_ptrs
32
33 #ifndef BASE_MEMORY_LINKED_PTR_H_
34 #define BASE_MEMORY_LINKED_PTR_H_
35
36 #include "base/logging.h" // for CHECK macros
37
38 // This is used internally by all instances of linked_ptr<>. It needs to be
39 // a non-template class because different types of linked_ptr<> can refer to
40 // the same object (linked_ptr<Superclass>(obj) vs linked_ptr<Subclass>(obj)).
41 // So, it needs to be possible for different types of linked_ptr to participate
42 // in the same circular linked list, so we need a single class type here.
43 //
44 // DO NOT USE THIS CLASS DIRECTLY YOURSELF. Use linked_ptr<T>.
45 class linked_ptr_internal {
46 public:
47 // Create a new circle that includes only this instance.
join_new()48 void join_new() {
49 next_ = this;
50 }
51
52 // Join an existing circle.
join(linked_ptr_internal const * ptr)53 void join(linked_ptr_internal const* ptr) {
54 next_ = ptr->next_;
55 ptr->next_ = this;
56 }
57
58 // Leave whatever circle we're part of. Returns true iff we were the
59 // last member of the circle. Once this is done, you can join() another.
depart()60 bool depart() {
61 if (next_ == this) return true;
62 linked_ptr_internal const* p = next_;
63 while (p->next_ != this) p = p->next_;
64 p->next_ = next_;
65 return false;
66 }
67
68 private:
69 mutable linked_ptr_internal const* next_;
70 };
71
72 // TODO(http://crbug.com/556939): DEPRECATED: Use unique_ptr instead (now that
73 // we have support for moveable types inside STL containers).
74 template <typename T>
75 class linked_ptr {
76 public:
77 typedef T element_type;
78
79 // Take over ownership of a raw pointer. This should happen as soon as
80 // possible after the object is created.
81 explicit linked_ptr(T* ptr = NULL) { capture(ptr); }
~linked_ptr()82 ~linked_ptr() { depart(); }
83
84 // Copy an existing linked_ptr<>, adding ourselves to the list of references.
linked_ptr(linked_ptr<U> const & ptr)85 template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); }
86
linked_ptr(linked_ptr const & ptr)87 linked_ptr(linked_ptr const& ptr) {
88 DCHECK_NE(&ptr, this);
89 copy(&ptr);
90 }
91
92 // Assignment releases the old value and acquires the new.
93 template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) {
94 depart();
95 copy(&ptr);
96 return *this;
97 }
98
99 linked_ptr& operator=(linked_ptr const& ptr) {
100 if (&ptr != this) {
101 depart();
102 copy(&ptr);
103 }
104 return *this;
105 }
106
107 // Smart pointer members.
108 void reset(T* ptr = NULL) {
109 depart();
110 capture(ptr);
111 }
get()112 T* get() const { return value_; }
113 T* operator->() const { return value_; }
114 T& operator*() const { return *value_; }
115 // Release ownership of the pointed object and returns it.
116 // Sole ownership by this linked_ptr object is required.
release()117 T* release() {
118 bool last = link_.depart();
119 CHECK(last);
120 T* v = value_;
121 value_ = NULL;
122 return v;
123 }
124
125 bool operator==(const T* p) const { return value_ == p; }
126 bool operator!=(const T* p) const { return value_ != p; }
127 template <typename U>
128 bool operator==(linked_ptr<U> const& ptr) const {
129 return value_ == ptr.get();
130 }
131 template <typename U>
132 bool operator!=(linked_ptr<U> const& ptr) const {
133 return value_ != ptr.get();
134 }
135
136 private:
137 template <typename U>
138 friend class linked_ptr;
139
140 T* value_;
141 linked_ptr_internal link_;
142
depart()143 void depart() {
144 if (link_.depart()) delete value_;
145 }
146
capture(T * ptr)147 void capture(T* ptr) {
148 value_ = ptr;
149 link_.join_new();
150 }
151
copy(linked_ptr<U> const * ptr)152 template <typename U> void copy(linked_ptr<U> const* ptr) {
153 value_ = ptr->get();
154 if (value_)
155 link_.join(&ptr->link_);
156 else
157 link_.join_new();
158 }
159 };
160
161 template<typename T> inline
162 bool operator==(T* ptr, const linked_ptr<T>& x) {
163 return ptr == x.get();
164 }
165
166 template<typename T> inline
167 bool operator!=(T* ptr, const linked_ptr<T>& x) {
168 return ptr != x.get();
169 }
170
171 // A function to convert T* into linked_ptr<T>
172 // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation
173 // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
174 template <typename T>
make_linked_ptr(T * ptr)175 linked_ptr<T> make_linked_ptr(T* ptr) {
176 return linked_ptr<T>(ptr);
177 }
178
179 #endif // BASE_MEMORY_LINKED_PTR_H_
180