2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
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.
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.
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.
24 #include "Assertions.h"
25 #include "FastAllocBase.h"
26 #include "Noncopyable.h"
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).
33 class RefCountedBase
{
37 ASSERT(!m_deletionHasBegun
);
38 ASSERT(!m_adoptionIsRequired
);
42 bool hasOneRef() const
44 ASSERT(!m_deletionHasBegun
);
45 return m_refCount
== 1;
53 void relaxAdoptionRequirement()
56 ASSERT(!m_deletionHasBegun
);
57 ASSERT(m_adoptionIsRequired
);
58 m_adoptionIsRequired
= false;
62 // Helper for generating JIT code. Please do not use for non-JIT purposes.
63 const int* addressOfCount() const
72 , m_deletionHasBegun(false)
73 , m_adoptionIsRequired(true)
80 ASSERT(m_deletionHasBegun
);
81 ASSERT(!m_adoptionIsRequired
);
84 // Returns whether the pointer should be freed or not.
87 ASSERT(!m_deletionHasBegun
);
88 ASSERT(!m_adoptionIsRequired
);
90 ASSERT(m_refCount
> 0);
91 if (m_refCount
== 1) {
93 m_deletionHasBegun
= true;
103 bool deletionHasBegun() const
105 return m_deletionHasBegun
;
110 template<typename T
> friend class CrossThreadRefCounted
;
113 friend void adopted(RefCountedBase
*);
118 bool m_deletionHasBegun
;
119 bool m_adoptionIsRequired
;
125 inline void adopted(RefCountedBase
* object
)
129 ASSERT(!object
->m_deletionHasBegun
);
130 object
->m_adoptionIsRequired
= false;
135 template<typename T
> class RefCounted
: public RefCountedBase
{
136 WTF_MAKE_NONCOPYABLE(RefCounted
); WTF_MAKE_FAST_ALLOCATED
;
141 delete static_cast<T
*>(this);
151 template<typename T
> class RefCountedCustomAllocated
: public RefCountedBase
{
152 WTF_MAKE_NONCOPYABLE(RefCountedCustomAllocated
);
158 delete static_cast<T
*>(this);
162 ~RefCountedCustomAllocated()
169 using WTF::RefCounted
;
170 using WTF::RefCountedCustomAllocated
;
172 #endif // RefCounted_h