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