michael@0: // Copyright (c) 2006, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // A "smart" pointer type with reference tracking. Every pointer to a michael@0: // particular object is kept on a circular linked list. When the last pointer michael@0: // to an object is destroyed or reassigned, the object is deleted. michael@0: // michael@0: // Used properly, this deletes the object when the last reference goes away. michael@0: // There are several caveats: michael@0: // - Like all reference counting schemes, cycles lead to leaks. michael@0: // - Each smart pointer is actually two pointers (8 bytes instead of 4). michael@0: // - Every time a pointer is assigned, the entire list of pointers to that michael@0: // object is traversed. This class is therefore NOT SUITABLE when there michael@0: // will often be more than two or three pointers to a particular object. michael@0: // - References are only tracked as long as linked_ptr<> objects are copied. michael@0: // If a linked_ptr<> is converted to a raw pointer and back, BAD THINGS michael@0: // will happen (double deletion). michael@0: // michael@0: // A good use of this class is storing object references in STL containers. michael@0: // You can safely put linked_ptr<> in a vector<>. michael@0: // Other uses may not be as good. michael@0: // michael@0: // Note: If you use an incomplete type with linked_ptr<>, the class michael@0: // *containing* linked_ptr<> must have a constructor and destructor (even michael@0: // if they do nothing!). michael@0: michael@0: #ifndef PROCESSOR_LINKED_PTR_H__ michael@0: #define PROCESSOR_LINKED_PTR_H__ michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: // This is used internally by all instances of linked_ptr<>. It needs to be michael@0: // a non-template class because different types of linked_ptr<> can refer to michael@0: // the same object (linked_ptr(obj) vs linked_ptr(obj)). michael@0: // So, it needs to be possible for different types of linked_ptr to participate michael@0: // in the same circular linked list, so we need a single class type here. michael@0: // michael@0: // DO NOT USE THIS CLASS DIRECTLY YOURSELF. Use linked_ptr. michael@0: class linked_ptr_internal { michael@0: public: michael@0: // Create a new circle that includes only this instance. michael@0: void join_new() { michael@0: next_ = this; michael@0: } michael@0: michael@0: // Join an existing circle. michael@0: void join(linked_ptr_internal const* ptr) { michael@0: linked_ptr_internal const* p = ptr; michael@0: while (p->next_ != ptr) p = p->next_; michael@0: p->next_ = this; michael@0: next_ = ptr; michael@0: } michael@0: michael@0: // Leave whatever circle we're part of. Returns true iff we were the michael@0: // last member of the circle. Once this is done, you can join() another. michael@0: bool depart() { michael@0: if (next_ == this) return true; michael@0: linked_ptr_internal const* p = next_; michael@0: while (p->next_ != this) p = p->next_; michael@0: p->next_ = next_; michael@0: return false; michael@0: } michael@0: michael@0: private: michael@0: mutable linked_ptr_internal const* next_; michael@0: }; michael@0: michael@0: template michael@0: class linked_ptr { michael@0: public: michael@0: typedef T element_type; michael@0: michael@0: // Take over ownership of a raw pointer. This should happen as soon as michael@0: // possible after the object is created. michael@0: explicit linked_ptr(T* ptr = NULL) { capture(ptr); } michael@0: ~linked_ptr() { depart(); } michael@0: michael@0: // Copy an existing linked_ptr<>, adding ourselves to the list of references. michael@0: template linked_ptr(linked_ptr const& ptr) { copy(&ptr); } michael@0: linked_ptr(linked_ptr const& ptr) { copy(&ptr); } michael@0: michael@0: // Assignment releases the old value and acquires the new. michael@0: template linked_ptr& operator=(linked_ptr const& ptr) { michael@0: depart(); michael@0: copy(&ptr); michael@0: return *this; michael@0: } michael@0: michael@0: linked_ptr& operator=(linked_ptr const& ptr) { michael@0: if (&ptr != this) { michael@0: depart(); michael@0: copy(&ptr); michael@0: } michael@0: return *this; michael@0: } michael@0: michael@0: // Smart pointer members. michael@0: void reset(T* ptr = NULL) { depart(); capture(ptr); } michael@0: T* get() const { return value_; } michael@0: T* operator->() const { return value_; } michael@0: T& operator*() const { return *value_; } michael@0: // Release ownership of the pointed object and returns it. michael@0: // Sole ownership by this linked_ptr object is required. michael@0: T* release() { michael@0: link_.depart(); michael@0: T* v = value_; michael@0: value_ = NULL; michael@0: return v; michael@0: } michael@0: michael@0: bool operator==(T* p) const { return value_ == p; } michael@0: bool operator!=(T* p) const { return value_ != p; } michael@0: template michael@0: bool operator==(linked_ptr const& ptr) const { michael@0: return value_ == ptr.get(); michael@0: } michael@0: template michael@0: bool operator!=(linked_ptr const& ptr) const { michael@0: return value_ != ptr.get(); michael@0: } michael@0: michael@0: private: michael@0: template michael@0: friend class linked_ptr; michael@0: michael@0: T* value_; michael@0: linked_ptr_internal link_; michael@0: michael@0: void depart() { michael@0: if (link_.depart()) delete value_; michael@0: } michael@0: michael@0: void capture(T* ptr) { michael@0: value_ = ptr; michael@0: link_.join_new(); michael@0: } michael@0: michael@0: template void copy(linked_ptr const* ptr) { michael@0: value_ = ptr->get(); michael@0: if (value_) michael@0: link_.join(&ptr->link_); michael@0: else michael@0: link_.join_new(); michael@0: } michael@0: }; michael@0: michael@0: template inline michael@0: bool operator==(T* ptr, const linked_ptr& x) { michael@0: return ptr == x.get(); michael@0: } michael@0: michael@0: template inline michael@0: bool operator!=(T* ptr, const linked_ptr& x) { michael@0: return ptr != x.get(); michael@0: } michael@0: michael@0: // A function to convert T* into linked_ptr michael@0: // Doing e.g. make_linked_ptr(new FooBarBaz(arg)) is a shorter notation michael@0: // for linked_ptr >(new FooBarBaz(arg)) michael@0: template michael@0: linked_ptr make_linked_ptr(T* ptr) { michael@0: return linked_ptr(ptr); michael@0: } michael@0: michael@0: } // namespace google_breakpad michael@0: michael@0: #endif // PROCESSOR_LINKED_PTR_H__