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