toolkit/crashreporter/google-breakpad/src/processor/linked_ptr.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/crashreporter/google-breakpad/src/processor/linked_ptr.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,193 @@
     1.4 +// Copyright (c) 2006, Google Inc.
     1.5 +// All rights reserved.
     1.6 +//
     1.7 +// Redistribution and use in source and binary forms, with or without
     1.8 +// modification, are permitted provided that the following conditions are
     1.9 +// met:
    1.10 +//
    1.11 +//     * Redistributions of source code must retain the above copyright
    1.12 +// notice, this list of conditions and the following disclaimer.
    1.13 +//     * Redistributions in binary form must reproduce the above
    1.14 +// copyright notice, this list of conditions and the following disclaimer
    1.15 +// in the documentation and/or other materials provided with the
    1.16 +// distribution.
    1.17 +//     * Neither the name of Google Inc. nor the names of its
    1.18 +// contributors may be used to endorse or promote products derived from
    1.19 +// this software without specific prior written permission.
    1.20 +//
    1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.32 +
    1.33 +// A "smart" pointer type with reference tracking.  Every pointer to a
    1.34 +// particular object is kept on a circular linked list.  When the last pointer
    1.35 +// to an object is destroyed or reassigned, the object is deleted.
    1.36 +//
    1.37 +// Used properly, this deletes the object when the last reference goes away.
    1.38 +// There are several caveats:
    1.39 +// - Like all reference counting schemes, cycles lead to leaks.
    1.40 +// - Each smart pointer is actually two pointers (8 bytes instead of 4).
    1.41 +// - Every time a pointer is assigned, the entire list of pointers to that
    1.42 +//   object is traversed.  This class is therefore NOT SUITABLE when there
    1.43 +//   will often be more than two or three pointers to a particular object.
    1.44 +// - References are only tracked as long as linked_ptr<> objects are copied.
    1.45 +//   If a linked_ptr<> is converted to a raw pointer and back, BAD THINGS
    1.46 +//   will happen (double deletion).
    1.47 +//
    1.48 +// A good use of this class is storing object references in STL containers.
    1.49 +// You can safely put linked_ptr<> in a vector<>.
    1.50 +// Other uses may not be as good.
    1.51 +//
    1.52 +// Note: If you use an incomplete type with linked_ptr<>, the class
    1.53 +// *containing* linked_ptr<> must have a constructor and destructor (even
    1.54 +// if they do nothing!).
    1.55 +
    1.56 +#ifndef PROCESSOR_LINKED_PTR_H__
    1.57 +#define PROCESSOR_LINKED_PTR_H__
    1.58 +
    1.59 +namespace google_breakpad {
    1.60 +
    1.61 +// This is used internally by all instances of linked_ptr<>.  It needs to be
    1.62 +// a non-template class because different types of linked_ptr<> can refer to
    1.63 +// the same object (linked_ptr<Superclass>(obj) vs linked_ptr<Subclass>(obj)).
    1.64 +// So, it needs to be possible for different types of linked_ptr to participate
    1.65 +// in the same circular linked list, so we need a single class type here.
    1.66 +//
    1.67 +// DO NOT USE THIS CLASS DIRECTLY YOURSELF.  Use linked_ptr<T>.
    1.68 +class linked_ptr_internal {
    1.69 + public:
    1.70 +  // Create a new circle that includes only this instance.
    1.71 +  void join_new() {
    1.72 +    next_ = this;
    1.73 +  }
    1.74 +
    1.75 +  // Join an existing circle.
    1.76 +  void join(linked_ptr_internal const* ptr) {
    1.77 +    linked_ptr_internal const* p = ptr;
    1.78 +    while (p->next_ != ptr) p = p->next_;
    1.79 +    p->next_ = this;
    1.80 +    next_ = ptr;
    1.81 +  }
    1.82 +
    1.83 +  // Leave whatever circle we're part of.  Returns true iff we were the
    1.84 +  // last member of the circle.  Once this is done, you can join() another.
    1.85 +  bool depart() {
    1.86 +    if (next_ == this) return true;
    1.87 +    linked_ptr_internal const* p = next_;
    1.88 +    while (p->next_ != this) p = p->next_;
    1.89 +    p->next_ = next_;
    1.90 +    return false;
    1.91 +  }
    1.92 +
    1.93 + private:
    1.94 +  mutable linked_ptr_internal const* next_;
    1.95 +};
    1.96 +
    1.97 +template <typename T>
    1.98 +class linked_ptr {
    1.99 + public:
   1.100 +  typedef T element_type;
   1.101 +
   1.102 +  // Take over ownership of a raw pointer.  This should happen as soon as
   1.103 +  // possible after the object is created.
   1.104 +  explicit linked_ptr(T* ptr = NULL) { capture(ptr); }
   1.105 +  ~linked_ptr() { depart(); }
   1.106 +
   1.107 +  // Copy an existing linked_ptr<>, adding ourselves to the list of references.
   1.108 +  template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); }
   1.109 +  linked_ptr(linked_ptr const& ptr) { copy(&ptr); }
   1.110 +
   1.111 +  // Assignment releases the old value and acquires the new.
   1.112 +  template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) {
   1.113 +    depart();
   1.114 +    copy(&ptr);
   1.115 +    return *this;
   1.116 +  }
   1.117 +
   1.118 +  linked_ptr& operator=(linked_ptr const& ptr) {
   1.119 +    if (&ptr != this) {
   1.120 +      depart();
   1.121 +      copy(&ptr);
   1.122 +    }
   1.123 +    return *this;
   1.124 +  }
   1.125 +
   1.126 +  // Smart pointer members.
   1.127 +  void reset(T* ptr = NULL) { depart(); capture(ptr); }
   1.128 +  T* get() const { return value_; }
   1.129 +  T* operator->() const { return value_; }
   1.130 +  T& operator*() const { return *value_; }
   1.131 +  // Release ownership of the pointed object and returns it.
   1.132 +  // Sole ownership by this linked_ptr object is required.
   1.133 +  T* release() {
   1.134 +    link_.depart();
   1.135 +    T* v = value_;
   1.136 +    value_ = NULL;
   1.137 +    return v;
   1.138 +  }
   1.139 +
   1.140 +  bool operator==(T* p) const { return value_ == p; }
   1.141 +  bool operator!=(T* p) const { return value_ != p; }
   1.142 +  template <typename U>
   1.143 +  bool operator==(linked_ptr<U> const& ptr) const {
   1.144 +    return value_ == ptr.get();
   1.145 +  }
   1.146 +  template <typename U>
   1.147 +  bool operator!=(linked_ptr<U> const& ptr) const {
   1.148 +    return value_ != ptr.get();
   1.149 +  }
   1.150 +
   1.151 + private:
   1.152 +  template <typename U>
   1.153 +  friend class linked_ptr;
   1.154 +
   1.155 +  T* value_;
   1.156 +  linked_ptr_internal link_;
   1.157 +
   1.158 +  void depart() {
   1.159 +    if (link_.depart()) delete value_;
   1.160 +  }
   1.161 +
   1.162 +  void capture(T* ptr) {
   1.163 +    value_ = ptr;
   1.164 +    link_.join_new();
   1.165 +  }
   1.166 +
   1.167 +  template <typename U> void copy(linked_ptr<U> const* ptr) {
   1.168 +    value_ = ptr->get();
   1.169 +    if (value_)
   1.170 +      link_.join(&ptr->link_);
   1.171 +    else
   1.172 +      link_.join_new();
   1.173 +  }
   1.174 +};
   1.175 +
   1.176 +template<typename T> inline
   1.177 +bool operator==(T* ptr, const linked_ptr<T>& x) {
   1.178 +  return ptr == x.get();
   1.179 +}
   1.180 +
   1.181 +template<typename T> inline
   1.182 +bool operator!=(T* ptr, const linked_ptr<T>& x) {
   1.183 +  return ptr != x.get();
   1.184 +}
   1.185 +
   1.186 +// A function to convert T* into linked_ptr<T>
   1.187 +// Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation
   1.188 +// for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
   1.189 +template <typename T>
   1.190 +linked_ptr<T> make_linked_ptr(T* ptr) {
   1.191 +  return linked_ptr<T>(ptr);
   1.192 +}
   1.193 +
   1.194 +}  // namespace google_breakpad
   1.195 +
   1.196 +#endif  // PROCESSOR_LINKED_PTR_H__

mercurial