]>
Commit | Line | Data |
---|---|---|
b37bf2e1 | 1 | /* |
14957cd0 | 2 | * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. |
b37bf2e1 A |
3 | * |
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 | ||
21 | #ifndef RefCounted_h | |
22 | #define RefCounted_h | |
23 | ||
14957cd0 A |
24 | #include "Assertions.h" |
25 | #include "FastAllocBase.h" | |
26 | #include "Noncopyable.h" | |
b37bf2e1 A |
27 | |
28 | namespace WTF { | |
29 | ||
9dae56ea A |
30 | // This base class holds the non-template methods and attributes. |
31 | // The RefCounted class inherits from it reducing the template bloat | |
32 | // generated by the compiler (technique called template hoisting). | |
f9bf01c6 | 33 | class RefCountedBase { |
b37bf2e1 | 34 | public: |
9dae56ea A |
35 | void ref() |
36 | { | |
37 | ASSERT(!m_deletionHasBegun); | |
14957cd0 | 38 | ASSERT(!m_adoptionIsRequired); |
9dae56ea A |
39 | ++m_refCount; |
40 | } | |
41 | ||
42 | bool hasOneRef() const | |
43 | { | |
44 | ASSERT(!m_deletionHasBegun); | |
45 | return m_refCount == 1; | |
46 | } | |
47 | ||
48 | int refCount() const | |
49 | { | |
50 | return m_refCount; | |
51 | } | |
52 | ||
14957cd0 A |
53 | void relaxAdoptionRequirement() |
54 | { | |
55 | #ifndef NDEBUG | |
56 | ASSERT(!m_deletionHasBegun); | |
57 | ASSERT(m_adoptionIsRequired); | |
58 | m_adoptionIsRequired = false; | |
59 | #endif | |
60 | } | |
61 | ||
62 | // Helper for generating JIT code. Please do not use for non-JIT purposes. | |
63 | const int* addressOfCount() const | |
64 | { | |
65 | return &m_refCount; | |
66 | } | |
67 | ||
9dae56ea | 68 | protected: |
ba379fdc A |
69 | RefCountedBase() |
70 | : m_refCount(1) | |
b37bf2e1 A |
71 | #ifndef NDEBUG |
72 | , m_deletionHasBegun(false) | |
14957cd0 | 73 | , m_adoptionIsRequired(true) |
b37bf2e1 A |
74 | #endif |
75 | { | |
76 | } | |
77 | ||
ba379fdc A |
78 | ~RefCountedBase() |
79 | { | |
14957cd0 A |
80 | ASSERT(m_deletionHasBegun); |
81 | ASSERT(!m_adoptionIsRequired); | |
ba379fdc | 82 | } |
b37bf2e1 | 83 | |
9dae56ea A |
84 | // Returns whether the pointer should be freed or not. |
85 | bool derefBase() | |
b37bf2e1 A |
86 | { |
87 | ASSERT(!m_deletionHasBegun); | |
14957cd0 A |
88 | ASSERT(!m_adoptionIsRequired); |
89 | ||
b37bf2e1 A |
90 | ASSERT(m_refCount > 0); |
91 | if (m_refCount == 1) { | |
92 | #ifndef NDEBUG | |
93 | m_deletionHasBegun = true; | |
94 | #endif | |
9dae56ea A |
95 | return true; |
96 | } | |
97 | ||
98 | --m_refCount; | |
99 | return false; | |
b37bf2e1 A |
100 | } |
101 | ||
ba379fdc A |
102 | #ifndef NDEBUG |
103 | bool deletionHasBegun() const | |
104 | { | |
105 | return m_deletionHasBegun; | |
106 | } | |
107 | #endif | |
108 | ||
109 | private: | |
14957cd0 A |
110 | template<typename T> friend class CrossThreadRefCounted; |
111 | ||
112 | #ifndef NDEBUG | |
113 | friend void adopted(RefCountedBase*); | |
114 | #endif | |
ba379fdc | 115 | |
9dae56ea A |
116 | int m_refCount; |
117 | #ifndef NDEBUG | |
118 | bool m_deletionHasBegun; | |
14957cd0 | 119 | bool m_adoptionIsRequired; |
9dae56ea A |
120 | #endif |
121 | }; | |
122 | ||
14957cd0 A |
123 | #ifndef NDEBUG |
124 | ||
125 | inline void adopted(RefCountedBase* object) | |
126 | { | |
127 | if (!object) | |
128 | return; | |
129 | ASSERT(!object->m_deletionHasBegun); | |
130 | object->m_adoptionIsRequired = false; | |
131 | } | |
9dae56ea | 132 | |
14957cd0 A |
133 | #endif |
134 | ||
135 | template<typename T> class RefCounted : public RefCountedBase { | |
136 | WTF_MAKE_NONCOPYABLE(RefCounted); WTF_MAKE_FAST_ALLOCATED; | |
9dae56ea | 137 | public: |
9dae56ea | 138 | void deref() |
b37bf2e1 | 139 | { |
9dae56ea A |
140 | if (derefBase()) |
141 | delete static_cast<T*>(this); | |
b37bf2e1 A |
142 | } |
143 | ||
9dae56ea | 144 | protected: |
14957cd0 | 145 | RefCounted() { } |
ba379fdc A |
146 | ~RefCounted() |
147 | { | |
148 | } | |
b37bf2e1 A |
149 | }; |
150 | ||
14957cd0 A |
151 | template<typename T> class RefCountedCustomAllocated : public RefCountedBase { |
152 | WTF_MAKE_NONCOPYABLE(RefCountedCustomAllocated); | |
153 | ||
f9bf01c6 A |
154 | public: |
155 | void deref() | |
156 | { | |
157 | if (derefBase()) | |
158 | delete static_cast<T*>(this); | |
159 | } | |
160 | ||
161 | protected: | |
162 | ~RefCountedCustomAllocated() | |
163 | { | |
164 | } | |
165 | }; | |
166 | ||
b37bf2e1 A |
167 | } // namespace WTF |
168 | ||
169 | using WTF::RefCounted; | |
f9bf01c6 | 170 | using WTF::RefCountedCustomAllocated; |
b37bf2e1 A |
171 | |
172 | #endif // RefCounted_h |