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// A good use of this class is storing object references in STL containers. 21// You can safely put linked_ptr<> in a vector<>. 22// Other uses may not be as good. 23// 24// Note: If you use an incomplete type with linked_ptr<>, the class 25// *containing* linked_ptr<> must have a constructor and destructor (even 26// if they do nothing!). 27// 28// Thread Safety: 29// A linked_ptr is NOT thread safe. Copying a linked_ptr object is 30// effectively a read-write operation. 31// 32// Alternative: to linked_ptr is shared_ptr, which 33// - is also two pointers in size (8 bytes for 32 bit addresses) 34// - is thread safe for copying and deletion 35// - supports weak_ptrs 36 37#ifndef BASE_MEMORY_LINKED_PTR_H_ 38#define BASE_MEMORY_LINKED_PTR_H_ 39#pragma once 40 41#include "base/logging.h" // for CHECK macros 42 43// This is used internally by all instances of linked_ptr<>. It needs to be 44// a non-template class because different types of linked_ptr<> can refer to 45// the same object (linked_ptr<Superclass>(obj) vs linked_ptr<Subclass>(obj)). 46// So, it needs to be possible for different types of linked_ptr to participate 47// in the same circular linked list, so we need a single class type here. 48// 49// DO NOT USE THIS CLASS DIRECTLY YOURSELF. Use linked_ptr<T>. 50class linked_ptr_internal { 51 public: 52 // Create a new circle that includes only this instance. 53 void join_new() { 54 next_ = this; 55 } 56 57 // Join an existing circle. 58 void join(linked_ptr_internal const* ptr) { 59 next_ = ptr->next_; 60 ptr->next_ = this; 61 } 62 63 // Leave whatever circle we're part of. Returns true iff we were the 64 // last member of the circle. Once this is done, you can join() another. 65 bool depart() { 66 if (next_ == this) return true; 67 linked_ptr_internal const* p = next_; 68 while (p->next_ != this) p = p->next_; 69 p->next_ = next_; 70 return false; 71 } 72 73 private: 74 mutable linked_ptr_internal const* next_; 75}; 76 77template <typename T> 78class linked_ptr { 79 public: 80 typedef T element_type; 81 82 // Take over ownership of a raw pointer. This should happen as soon as 83 // possible after the object is created. 84 explicit linked_ptr(T* ptr = NULL) { capture(ptr); } 85 ~linked_ptr() { depart(); } 86 87 // Copy an existing linked_ptr<>, adding ourselves to the list of references. 88 template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); } 89 90 linked_ptr(linked_ptr const& ptr) { 91 DCHECK_NE(&ptr, this); 92 copy(&ptr); 93 } 94 95 // Assignment releases the old value and acquires the new. 96 template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) { 97 depart(); 98 copy(&ptr); 99 return *this; 100 } 101 102 linked_ptr& operator=(linked_ptr const& ptr) { 103 if (&ptr != this) { 104 depart(); 105 copy(&ptr); 106 } 107 return *this; 108 } 109 110 // Smart pointer members. 111 void reset(T* ptr = NULL) { 112 depart(); 113 capture(ptr); 114 } 115 T* get() const { return value_; } 116 T* operator->() const { return value_; } 117 T& operator*() const { return *value_; } 118 // Release ownership of the pointed object and returns it. 119 // Sole ownership by this linked_ptr object is required. 120 T* release() { 121 bool last = link_.depart(); 122 CHECK(last); 123 T* v = value_; 124 value_ = NULL; 125 return v; 126 } 127 128 bool operator==(const T* p) const { return value_ == p; } 129 bool operator!=(const T* p) const { return value_ != p; } 130 template <typename U> 131 bool operator==(linked_ptr<U> const& ptr) const { 132 return value_ == ptr.get(); 133 } 134 template <typename U> 135 bool operator!=(linked_ptr<U> const& ptr) const { 136 return value_ != ptr.get(); 137 } 138 139 private: 140 template <typename U> 141 friend class linked_ptr; 142 143 T* value_; 144 linked_ptr_internal link_; 145 146 void depart() { 147 if (link_.depart()) delete value_; 148 } 149 150 void capture(T* ptr) { 151 value_ = ptr; 152 link_.join_new(); 153 } 154 155 template <typename U> void copy(linked_ptr<U> const* ptr) { 156 value_ = ptr->get(); 157 if (value_) 158 link_.join(&ptr->link_); 159 else 160 link_.join_new(); 161 } 162}; 163 164template<typename T> inline 165bool operator==(T* ptr, const linked_ptr<T>& x) { 166 return ptr == x.get(); 167} 168 169template<typename T> inline 170bool operator!=(T* ptr, const linked_ptr<T>& x) { 171 return ptr != x.get(); 172} 173 174// A function to convert T* into linked_ptr<T> 175// Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation 176// for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) 177template <typename T> 178linked_ptr<T> make_linked_ptr(T* ptr) { 179 return linked_ptr<T>(ptr); 180} 181 182#endif // BASE_MEMORY_LINKED_PTR_H_ 183