OLD | NEW |
1 /* | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. | 2 // Use of this source code is governed by a BSD-style license that can be |
3 * | 3 // found in the LICENSE file. |
4 * This library is free software; you can redistribute it and/or | |
5 * modify it under the terms of the GNU Library General Public | |
6 * License as published by the Free Software Foundation; either | |
7 * version 2 of the License, or (at your option) any later version. | |
8 * | |
9 * This library is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 * Library General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU Library General Public License | |
15 * along with this library; see the file COPYING.LIB. If not, write to | |
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
17 * Boston, MA 02110-1301, USA. | |
18 * | |
19 */ | |
20 | 4 |
21 #ifndef RefCounted_h | 5 #include "platform/wtf/RefCounted.h" |
22 #define RefCounted_h | |
23 | 6 |
24 #include "wtf/Allocator.h" | 7 // The contents of this header was moved to platform/wtf as part of |
25 #include "wtf/Assertions.h" | 8 // WTF migration project. See the following post for details: |
26 #include "wtf/Noncopyable.h" | 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY
CAAJ |
27 #include "wtf/WTFExport.h" | |
28 | |
29 #if ENABLE(INSTANCE_COUNTER) | |
30 #include "wtf/InstanceCounter.h" | |
31 #endif | |
32 | |
33 #if DCHECK_IS_ON() | |
34 #define CHECK_REF_COUNTED_LIFECYCLE 1 | |
35 #include "wtf/ThreadRestrictionVerifier.h" | |
36 #else | |
37 #define CHECK_REF_COUNTED_LIFECYCLE 0 | |
38 #endif | |
39 | |
40 namespace WTF { | |
41 | |
42 // This base class holds the non-template methods and attributes. | |
43 // The RefCounted class inherits from it reducing the template bloat | |
44 // generated by the compiler (technique called template hoisting). | |
45 class WTF_EXPORT RefCountedBase { | |
46 public: | |
47 void ref() const { | |
48 #if CHECK_REF_COUNTED_LIFECYCLE | |
49 SECURITY_DCHECK(m_verifier.onRef(m_refCount)); | |
50 DCHECK(!m_adoptionIsRequired); | |
51 #endif | |
52 SECURITY_DCHECK(!m_deletionHasBegun); | |
53 ++m_refCount; | |
54 } | |
55 | |
56 bool hasOneRef() const { | |
57 SECURITY_DCHECK(!m_deletionHasBegun); | |
58 #if CHECK_REF_COUNTED_LIFECYCLE | |
59 SECURITY_DCHECK(m_verifier.isSafeToUse()); | |
60 #endif | |
61 return m_refCount == 1; | |
62 } | |
63 | |
64 int refCount() const { | |
65 #if CHECK_REF_COUNTED_LIFECYCLE | |
66 SECURITY_DCHECK(m_verifier.isSafeToUse()); | |
67 #endif | |
68 return m_refCount; | |
69 } | |
70 | |
71 protected: | |
72 RefCountedBase() | |
73 : m_refCount(1) | |
74 #if ENABLE(SECURITY_ASSERT) | |
75 , | |
76 m_deletionHasBegun(false) | |
77 #endif | |
78 #if CHECK_REF_COUNTED_LIFECYCLE | |
79 , | |
80 m_adoptionIsRequired(true) | |
81 #endif | |
82 { | |
83 } | |
84 | |
85 ~RefCountedBase() { | |
86 SECURITY_DCHECK(m_deletionHasBegun); | |
87 #if CHECK_REF_COUNTED_LIFECYCLE | |
88 DCHECK(!m_adoptionIsRequired); | |
89 #endif | |
90 } | |
91 | |
92 // Returns whether the pointer should be freed or not. | |
93 bool derefBase() const { | |
94 SECURITY_DCHECK(!m_deletionHasBegun); | |
95 #if CHECK_REF_COUNTED_LIFECYCLE | |
96 SECURITY_DCHECK(m_verifier.onDeref(m_refCount)); | |
97 DCHECK(!m_adoptionIsRequired); | |
98 #endif | |
99 | |
100 DCHECK_GT(m_refCount, 0); | |
101 --m_refCount; | |
102 if (!m_refCount) { | |
103 #if ENABLE(SECURITY_ASSERT) | |
104 m_deletionHasBegun = true; | |
105 #endif | |
106 return true; | |
107 } | |
108 | |
109 return false; | |
110 } | |
111 | |
112 #if CHECK_REF_COUNTED_LIFECYCLE | |
113 bool deletionHasBegun() const { return m_deletionHasBegun; } | |
114 #endif | |
115 | |
116 private: | |
117 #if CHECK_REF_COUNTED_LIFECYCLE || ENABLE(SECURITY_ASSERT) | |
118 friend void adopted(RefCountedBase*); | |
119 #endif | |
120 | |
121 mutable int m_refCount; | |
122 #if ENABLE(SECURITY_ASSERT) | |
123 mutable bool m_deletionHasBegun; | |
124 #endif | |
125 #if CHECK_REF_COUNTED_LIFECYCLE | |
126 mutable bool m_adoptionIsRequired; | |
127 mutable ThreadRestrictionVerifier m_verifier; | |
128 #endif | |
129 }; | |
130 | |
131 #if CHECK_REF_COUNTED_LIFECYCLE || ENABLE(SECURITY_ASSERT) | |
132 inline void adopted(RefCountedBase* object) { | |
133 if (!object) | |
134 return; | |
135 SECURITY_DCHECK(!object->m_deletionHasBegun); | |
136 #if CHECK_REF_COUNTED_LIFECYCLE | |
137 object->m_adoptionIsRequired = false; | |
138 #endif | |
139 } | |
140 #endif | |
141 | |
142 template <typename T> | |
143 class RefCounted : public RefCountedBase { | |
144 WTF_MAKE_NONCOPYABLE(RefCounted); | |
145 | |
146 // Put |T| in here instead of |RefCounted| so the heap profiler reports |T| | |
147 // instead of |RefCounted<T>|. This does not affect overloading of operator | |
148 // new. | |
149 USING_FAST_MALLOC(T); | |
150 | |
151 public: | |
152 void deref() const { | |
153 if (derefBase()) | |
154 delete static_cast<const T*>(this); | |
155 } | |
156 | |
157 protected: | |
158 #if ENABLE(INSTANCE_COUNTER) | |
159 RefCounted() { incrementInstanceCount<T>(static_cast<T*>(this)); } | |
160 | |
161 ~RefCounted() { decrementInstanceCount<T>(static_cast<T*>(this)); } | |
162 #else | |
163 RefCounted() {} | |
164 #endif | |
165 }; | |
166 | |
167 } // namespace WTF | |
168 | |
169 using WTF::RefCounted; | |
170 | |
171 #endif // RefCounted_h | |
OLD | NEW |