| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // This is a copy of base/linked_ptr.h with CHECKS/DCHECKS replaced with |
| 6 // PP_DCHECKs. |
| 7 // |
| 5 // A "smart" pointer type with reference tracking. Every pointer to a | 8 // 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 | 9 // 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. | 10 // to an object is destroyed or reassigned, the object is deleted. |
| 8 // | 11 // |
| 9 // Used properly, this deletes the object when the last reference goes away. | 12 // Used properly, this deletes the object when the last reference goes away. |
| 10 // There are several caveats: | 13 // There are several caveats: |
| 11 // - Like all reference counting schemes, cycles lead to leaks. | 14 // - Like all reference counting schemes, cycles lead to leaks. |
| 12 // - Each smart pointer is actually two pointers (8 bytes instead of 4). | 15 // - 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 | 16 // - 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 | 17 // object is traversed. This class is therefore NOT SUITABLE when there |
| (...skipping 12 matching lines...) Expand all Loading... |
| 27 // | 30 // |
| 28 // Thread Safety: | 31 // Thread Safety: |
| 29 // A linked_ptr is NOT thread safe. Copying a linked_ptr object is | 32 // A linked_ptr is NOT thread safe. Copying a linked_ptr object is |
| 30 // effectively a read-write operation. | 33 // effectively a read-write operation. |
| 31 // | 34 // |
| 32 // Alternative: to linked_ptr is shared_ptr, which | 35 // Alternative: to linked_ptr is shared_ptr, which |
| 33 // - is also two pointers in size (8 bytes for 32 bit addresses) | 36 // - is also two pointers in size (8 bytes for 32 bit addresses) |
| 34 // - is thread safe for copying and deletion | 37 // - is thread safe for copying and deletion |
| 35 // - supports weak_ptrs | 38 // - supports weak_ptrs |
| 36 | 39 |
| 37 #ifndef BASE_MEMORY_LINKED_PTR_H_ | 40 #ifndef WEBKIT_MEDIA_CRYPTO_PPAPI_LINKED_PTR_H_ |
| 38 #define BASE_MEMORY_LINKED_PTR_H_ | 41 #define WEBKIT_MEDIA_CRYPTO_PPAPI_LINKED_PTR_H_ |
| 39 | 42 |
| 40 #include "base/logging.h" // for CHECK macros | 43 #include "ppapi/cpp/logging.h" |
| 41 | 44 |
| 42 // This is used internally by all instances of linked_ptr<>. It needs to be | 45 // 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 | 46 // 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)). | 47 // 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 | 48 // 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. | 49 // in the same circular linked list, so we need a single class type here. |
| 47 // | 50 // |
| 48 // DO NOT USE THIS CLASS DIRECTLY YOURSELF. Use linked_ptr<T>. | 51 // DO NOT USE THIS CLASS DIRECTLY YOURSELF. Use linked_ptr<T>. |
| 49 class linked_ptr_internal { | 52 class linked_ptr_internal { |
| 50 public: | 53 public: |
| (...skipping 29 matching lines...) Expand all Loading... |
| 80 | 83 |
| 81 // Take over ownership of a raw pointer. This should happen as soon as | 84 // Take over ownership of a raw pointer. This should happen as soon as |
| 82 // possible after the object is created. | 85 // possible after the object is created. |
| 83 explicit linked_ptr(T* ptr = NULL) { capture(ptr); } | 86 explicit linked_ptr(T* ptr = NULL) { capture(ptr); } |
| 84 ~linked_ptr() { depart(); } | 87 ~linked_ptr() { depart(); } |
| 85 | 88 |
| 86 // Copy an existing linked_ptr<>, adding ourselves to the list of references. | 89 // 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); } | 90 template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); } |
| 88 | 91 |
| 89 linked_ptr(linked_ptr const& ptr) { | 92 linked_ptr(linked_ptr const& ptr) { |
| 90 DCHECK_NE(&ptr, this); | 93 PP_DCHECK(&ptr != this); |
| 91 copy(&ptr); | 94 copy(&ptr); |
| 92 } | 95 } |
| 93 | 96 |
| 94 // Assignment releases the old value and acquires the new. | 97 // Assignment releases the old value and acquires the new. |
| 95 template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) { | 98 template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) { |
| 96 depart(); | 99 depart(); |
| 97 copy(&ptr); | 100 copy(&ptr); |
| 98 return *this; | 101 return *this; |
| 99 } | 102 } |
| 100 | 103 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 111 depart(); | 114 depart(); |
| 112 capture(ptr); | 115 capture(ptr); |
| 113 } | 116 } |
| 114 T* get() const { return value_; } | 117 T* get() const { return value_; } |
| 115 T* operator->() const { return value_; } | 118 T* operator->() const { return value_; } |
| 116 T& operator*() const { return *value_; } | 119 T& operator*() const { return *value_; } |
| 117 // Release ownership of the pointed object and returns it. | 120 // Release ownership of the pointed object and returns it. |
| 118 // Sole ownership by this linked_ptr object is required. | 121 // Sole ownership by this linked_ptr object is required. |
| 119 T* release() { | 122 T* release() { |
| 120 bool last = link_.depart(); | 123 bool last = link_.depart(); |
| 121 CHECK(last); | 124 PP_DCHECK(last); |
| 125 (void)last; |
| 122 T* v = value_; | 126 T* v = value_; |
| 123 value_ = NULL; | 127 value_ = NULL; |
| 124 return v; | 128 return v; |
| 125 } | 129 } |
| 126 | 130 |
| 127 bool operator==(const T* p) const { return value_ == p; } | 131 bool operator==(const T* p) const { return value_ == p; } |
| 128 bool operator!=(const T* p) const { return value_ != p; } | 132 bool operator!=(const T* p) const { return value_ != p; } |
| 129 template <typename U> | 133 template <typename U> |
| 130 bool operator==(linked_ptr<U> const& ptr) const { | 134 bool operator==(linked_ptr<U> const& ptr) const { |
| 131 return value_ == ptr.get(); | 135 return value_ == ptr.get(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 } | 175 } |
| 172 | 176 |
| 173 // A function to convert T* into linked_ptr<T> | 177 // A function to convert T* into linked_ptr<T> |
| 174 // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation | 178 // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation |
| 175 // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) | 179 // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) |
| 176 template <typename T> | 180 template <typename T> |
| 177 linked_ptr<T> make_linked_ptr(T* ptr) { | 181 linked_ptr<T> make_linked_ptr(T* ptr) { |
| 178 return linked_ptr<T>(ptr); | 182 return linked_ptr<T>(ptr); |
| 179 } | 183 } |
| 180 | 184 |
| 181 #endif // BASE_MEMORY_LINKED_PTR_H_ | 185 #endif // WEBKIT_MEDIA_CRYPTO_PPAPI_LINKED_PTR_H_ |
| OLD | NEW |