| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * This library is free software; you can redistribute it and/or | 4 * This library is free software; you can redistribute it and/or |
| 5 * modify it under the terms of the GNU Library General Public | 5 * modify it under the terms of the GNU Library General Public |
| 6 * License as published by the Free Software Foundation; either | 6 * License as published by the Free Software Foundation; either |
| 7 * version 2 of the License, or (at your option) any later version. | 7 * version 2 of the License, or (at your option) any later version. |
| 8 * | 8 * |
| 9 * This library is distributed in the hope that it will be useful, | 9 * This library is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 #else | 33 #else |
| 34 #define CHECK_REF_COUNTED_LIFECYCLE 0 | 34 #define CHECK_REF_COUNTED_LIFECYCLE 0 |
| 35 #endif | 35 #endif |
| 36 | 36 |
| 37 namespace WTF { | 37 namespace WTF { |
| 38 | 38 |
| 39 // This base class holds the non-template methods and attributes. | 39 // This base class holds the non-template methods and attributes. |
| 40 // The RefCounted class inherits from it reducing the template bloat | 40 // The RefCounted class inherits from it reducing the template bloat |
| 41 // generated by the compiler (technique called template hoisting). | 41 // generated by the compiler (technique called template hoisting). |
| 42 class WTF_EXPORT RefCountedBase { | 42 class WTF_EXPORT RefCountedBase { |
| 43 public: | 43 public: |
| 44 void ref() | 44 void ref() { |
| 45 { | |
| 46 #if CHECK_REF_COUNTED_LIFECYCLE | 45 #if CHECK_REF_COUNTED_LIFECYCLE |
| 47 // Start thread verification as soon as the ref count gets to 2. This | 46 // Start thread verification as soon as the ref count gets to 2. This |
| 48 // heuristic reflects the fact that items are often created on one threa
d | 47 // heuristic reflects the fact that items are often created on one thread |
| 49 // and then given to another thread to be used. | 48 // and then given to another thread to be used. |
| 50 // FIXME: Make this restriction tigher. Especially as we move to more | 49 // FIXME: Make this restriction tigher. Especially as we move to more |
| 51 // common methods for sharing items across threads like CrossThreadCopie
r.h | 50 // common methods for sharing items across threads like CrossThreadCopier.h |
| 52 // We should be able to add a "detachFromThread" method to make this exp
licit. | 51 // We should be able to add a "detachFromThread" method to make this explici
t. |
| 53 if (m_refCount == 1) | 52 if (m_refCount == 1) |
| 54 m_verifier.setShared(true); | 53 m_verifier.setShared(true); |
| 55 // If this assert fires, it either indicates a thread safety issue or | 54 // If this assert fires, it either indicates a thread safety issue or |
| 56 // that the verification needs to change. See ThreadRestrictionVerifier
for | 55 // that the verification needs to change. See ThreadRestrictionVerifier for |
| 57 // the different modes. | 56 // the different modes. |
| 58 ASSERT(m_verifier.isSafeToUse()); | 57 ASSERT(m_verifier.isSafeToUse()); |
| 59 ASSERT(!m_adoptionIsRequired); | 58 ASSERT(!m_adoptionIsRequired); |
| 60 #endif | 59 #endif |
| 61 ASSERT_WITH_SECURITY_IMPLICATION(!m_deletionHasBegun); | 60 ASSERT_WITH_SECURITY_IMPLICATION(!m_deletionHasBegun); |
| 62 ++m_refCount; | 61 ++m_refCount; |
| 63 } | 62 } |
| 64 | 63 |
| 65 bool hasOneRef() const | 64 bool hasOneRef() const { |
| 66 { | 65 ASSERT_WITH_SECURITY_IMPLICATION(!m_deletionHasBegun); |
| 67 ASSERT_WITH_SECURITY_IMPLICATION(!m_deletionHasBegun); | |
| 68 #if CHECK_REF_COUNTED_LIFECYCLE | 66 #if CHECK_REF_COUNTED_LIFECYCLE |
| 69 ASSERT(m_verifier.isSafeToUse()); | 67 ASSERT(m_verifier.isSafeToUse()); |
| 70 #endif | 68 #endif |
| 71 return m_refCount == 1; | 69 return m_refCount == 1; |
| 72 } | 70 } |
| 73 | 71 |
| 74 int refCount() const | 72 int refCount() const { |
| 75 { | |
| 76 #if CHECK_REF_COUNTED_LIFECYCLE | 73 #if CHECK_REF_COUNTED_LIFECYCLE |
| 77 ASSERT(m_verifier.isSafeToUse()); | 74 ASSERT(m_verifier.isSafeToUse()); |
| 78 #endif | 75 #endif |
| 79 return m_refCount; | 76 return m_refCount; |
| 80 } | 77 } |
| 81 | 78 |
| 82 protected: | 79 protected: |
| 83 RefCountedBase() | 80 RefCountedBase() |
| 84 : m_refCount(1) | 81 : m_refCount(1) |
| 85 #if ENABLE(SECURITY_ASSERT) | 82 #if ENABLE(SECURITY_ASSERT) |
| 86 , m_deletionHasBegun(false) | 83 , |
| 84 m_deletionHasBegun(false) |
| 87 #endif | 85 #endif |
| 88 #if CHECK_REF_COUNTED_LIFECYCLE | 86 #if CHECK_REF_COUNTED_LIFECYCLE |
| 89 , m_adoptionIsRequired(true) | 87 , |
| 88 m_adoptionIsRequired(true) |
| 90 #endif | 89 #endif |
| 91 { | 90 { |
| 92 } | 91 } |
| 93 | 92 |
| 94 ~RefCountedBase() | 93 ~RefCountedBase() { |
| 95 { | 94 ASSERT_WITH_SECURITY_IMPLICATION(m_deletionHasBegun); |
| 96 ASSERT_WITH_SECURITY_IMPLICATION(m_deletionHasBegun); | |
| 97 #if CHECK_REF_COUNTED_LIFECYCLE | 95 #if CHECK_REF_COUNTED_LIFECYCLE |
| 98 ASSERT(!m_adoptionIsRequired); | 96 ASSERT(!m_adoptionIsRequired); |
| 99 #endif | 97 #endif |
| 100 } | 98 } |
| 101 | 99 |
| 102 // Returns whether the pointer should be freed or not. | 100 // Returns whether the pointer should be freed or not. |
| 103 bool derefBase() | 101 bool derefBase() { |
| 104 { | 102 ASSERT_WITH_SECURITY_IMPLICATION(!m_deletionHasBegun); |
| 105 ASSERT_WITH_SECURITY_IMPLICATION(!m_deletionHasBegun); | |
| 106 #if CHECK_REF_COUNTED_LIFECYCLE | 103 #if CHECK_REF_COUNTED_LIFECYCLE |
| 107 ASSERT(m_verifier.isSafeToUse()); | 104 ASSERT(m_verifier.isSafeToUse()); |
| 108 ASSERT(!m_adoptionIsRequired); | 105 ASSERT(!m_adoptionIsRequired); |
| 109 #endif | 106 #endif |
| 110 | 107 |
| 111 ASSERT(m_refCount > 0); | 108 ASSERT(m_refCount > 0); |
| 112 --m_refCount; | 109 --m_refCount; |
| 113 if (!m_refCount) { | 110 if (!m_refCount) { |
| 114 #if ENABLE(SECURITY_ASSERT) | 111 #if ENABLE(SECURITY_ASSERT) |
| 115 m_deletionHasBegun = true; | 112 m_deletionHasBegun = true; |
| 116 #endif | 113 #endif |
| 117 return true; | 114 return true; |
| 118 } | |
| 119 | |
| 120 #if CHECK_REF_COUNTED_LIFECYCLE | |
| 121 // Stop thread verification when the ref goes to 1 because it | |
| 122 // is safe to be passed to another thread at this point. | |
| 123 if (m_refCount == 1) | |
| 124 m_verifier.setShared(false); | |
| 125 #endif | |
| 126 return false; | |
| 127 } | 115 } |
| 128 | 116 |
| 129 #if CHECK_REF_COUNTED_LIFECYCLE | 117 #if CHECK_REF_COUNTED_LIFECYCLE |
| 130 bool deletionHasBegun() const | 118 // Stop thread verification when the ref goes to 1 because it |
| 131 { | 119 // is safe to be passed to another thread at this point. |
| 132 return m_deletionHasBegun; | 120 if (m_refCount == 1) |
| 133 } | 121 m_verifier.setShared(false); |
| 122 #endif |
| 123 return false; |
| 124 } |
| 125 |
| 126 #if CHECK_REF_COUNTED_LIFECYCLE |
| 127 bool deletionHasBegun() const { return m_deletionHasBegun; } |
| 134 #endif | 128 #endif |
| 135 | 129 |
| 136 private: | 130 private: |
| 137 | |
| 138 #if CHECK_REF_COUNTED_LIFECYCLE || ENABLE(SECURITY_ASSERT) | 131 #if CHECK_REF_COUNTED_LIFECYCLE || ENABLE(SECURITY_ASSERT) |
| 139 friend void adopted(RefCountedBase*); | 132 friend void adopted(RefCountedBase*); |
| 140 #endif | 133 #endif |
| 141 | 134 |
| 142 int m_refCount; | 135 int m_refCount; |
| 143 #if ENABLE(SECURITY_ASSERT) | 136 #if ENABLE(SECURITY_ASSERT) |
| 144 bool m_deletionHasBegun; | 137 bool m_deletionHasBegun; |
| 145 #endif | 138 #endif |
| 146 #if CHECK_REF_COUNTED_LIFECYCLE | 139 #if CHECK_REF_COUNTED_LIFECYCLE |
| 147 bool m_adoptionIsRequired; | 140 bool m_adoptionIsRequired; |
| 148 ThreadRestrictionVerifier m_verifier; | 141 ThreadRestrictionVerifier m_verifier; |
| 149 #endif | 142 #endif |
| 150 }; | 143 }; |
| 151 | 144 |
| 152 #if CHECK_REF_COUNTED_LIFECYCLE || ENABLE(SECURITY_ASSERT) | 145 #if CHECK_REF_COUNTED_LIFECYCLE || ENABLE(SECURITY_ASSERT) |
| 153 inline void adopted(RefCountedBase* object) | 146 inline void adopted(RefCountedBase* object) { |
| 154 { | 147 if (!object) |
| 155 if (!object) | 148 return; |
| 156 return; | 149 ASSERT_WITH_SECURITY_IMPLICATION(!object->m_deletionHasBegun); |
| 157 ASSERT_WITH_SECURITY_IMPLICATION(!object->m_deletionHasBegun); | |
| 158 #if CHECK_REF_COUNTED_LIFECYCLE | 150 #if CHECK_REF_COUNTED_LIFECYCLE |
| 159 object->m_adoptionIsRequired = false; | 151 object->m_adoptionIsRequired = false; |
| 160 #endif | 152 #endif |
| 161 } | 153 } |
| 162 #endif | 154 #endif |
| 163 | 155 |
| 164 template<typename T> class RefCounted : public RefCountedBase { | 156 template <typename T> |
| 165 WTF_MAKE_NONCOPYABLE(RefCounted); | 157 class RefCounted : public RefCountedBase { |
| 158 WTF_MAKE_NONCOPYABLE(RefCounted); |
| 166 | 159 |
| 167 // Put |T| in here instead of |RefCounted| so the heap profiler reports |T| | 160 // Put |T| in here instead of |RefCounted| so the heap profiler reports |T| |
| 168 // instead of |RefCounted<T>|. This does not affect overloading of operator | 161 // instead of |RefCounted<T>|. This does not affect overloading of operator |
| 169 // new. | 162 // new. |
| 170 USING_FAST_MALLOC(T); | 163 USING_FAST_MALLOC(T); |
| 171 | 164 |
| 172 public: | 165 public: |
| 173 void deref() | 166 void deref() { |
| 174 { | 167 if (derefBase()) |
| 175 if (derefBase()) | 168 delete static_cast<T*>(this); |
| 176 delete static_cast<T*>(this); | 169 } |
| 177 } | |
| 178 | 170 |
| 179 protected: | 171 protected: |
| 180 #ifdef ENABLE_INSTANCE_COUNTER | 172 #ifdef ENABLE_INSTANCE_COUNTER |
| 181 RefCounted() | 173 RefCounted() { incrementInstanceCount<T>(static_cast<T*>(this)); } |
| 182 { | |
| 183 incrementInstanceCount<T>(static_cast<T*>(this)); | |
| 184 } | |
| 185 | 174 |
| 186 ~RefCounted() | 175 ~RefCounted() { decrementInstanceCount<T>(static_cast<T*>(this)); } |
| 187 { | |
| 188 decrementInstanceCount<T>(static_cast<T*>(this)); | |
| 189 } | |
| 190 #else | 176 #else |
| 191 RefCounted() | 177 RefCounted() {} |
| 192 { | |
| 193 } | |
| 194 #endif | 178 #endif |
| 195 }; | 179 }; |
| 196 | 180 |
| 197 } // namespace WTF | 181 } // namespace WTF |
| 198 | 182 |
| 199 using WTF::RefCounted; | 183 using WTF::RefCounted; |
| 200 | 184 |
| 201 #endif // RefCounted_h | 185 #endif // RefCounted_h |
| OLD | NEW |