1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Helpers for defining and using refcounted objects. */
9 #ifndef mozilla_RefPtr_h
10 #define mozilla_RefPtr_h
12 #include "mozilla/Assertions.h"
13 #include "mozilla/Atomics.h"
14 #include "mozilla/Attributes.h"
15 #include "mozilla/TypeTraits.h"
19 template<typename T
> class RefCounted
;
20 template<typename T
> class RefPtr
;
21 template<typename T
> class TemporaryRef
;
22 template<typename T
> class OutParamRef
;
23 template<typename T
> OutParamRef
<T
> byRef(RefPtr
<T
>&);
26 * RefCounted<T> is a sort of a "mixin" for a class T. RefCounted
27 * manages, well, refcounting for T, and because RefCounted is
28 * parameterized on T, RefCounted<T> can call T's destructor directly.
29 * This means T doesn't need to have a virtual dtor and so doesn't
32 * RefCounted<T> is created with refcount == 0. Newly-allocated
33 * RefCounted<T> must immediately be assigned to a RefPtr to make the
34 * refcount > 0. It's an error to allocate and free a bare
35 * RefCounted<T>, i.e. outside of the RefPtr machinery. Attempts to
36 * do so will abort DEBUG builds.
38 * Live RefCounted<T> have refcount > 0. The lifetime (refcounts) of
39 * live RefCounted<T> are controlled by RefPtr<T> and
40 * RefPtr<super/subclass of T>. Upon a transition from refcounted==1
41 * to 0, the RefCounted<T> "dies" and is destroyed. The "destroyed"
42 * state is represented in DEBUG builds by refcount==0xffffdead. This
43 * state distinguishes use-before-ref (refcount==0) from
44 * use-after-destroy (refcount==0xffffdead).
48 static const int DEAD
= 0xffffdead;
51 // This is used WeakPtr.h as well as this file.
52 enum RefCountAtomicity
58 template<typename T
, RefCountAtomicity Atomicity
>
61 friend class RefPtr
<T
>;
64 RefCounted() : refCnt(0) { }
66 MOZ_ASSERT(refCnt
== detail::DEAD
);
70 // Compatibility with nsRefPtr.
72 MOZ_ASSERT(refCnt
>= 0);
76 void Release() const {
77 MOZ_ASSERT(refCnt
> 0);
80 refCnt
= detail::DEAD
;
82 delete static_cast<const T
*>(this);
86 // Compatibility with wtf::RefPtr.
87 void ref() { AddRef(); }
88 void deref() { Release(); }
89 int refCount() const { return refCnt
; }
90 bool hasOneRef() const {
91 MOZ_ASSERT(refCnt
> 0);
96 mutable typename Conditional
<Atomicity
== AtomicRefCount
, Atomic
<int>, int>::Type refCnt
;
102 class RefCounted
: public detail::RefCounted
<T
, detail::NonAtomicRefCount
>
106 static_assert(IsBaseOf
<RefCounted
, T
>::value
,
107 "T must derive from RefCounted<T>");
112 * AtomicRefCounted<T> is like RefCounted<T>, with an atomically updated
116 class AtomicRefCounted
: public detail::RefCounted
<T
, detail::AtomicRefCount
>
119 ~AtomicRefCounted() {
120 static_assert(IsBaseOf
<AtomicRefCounted
, T
>::value
,
121 "T must derive from AtomicRefCounted<T>");
126 * RefPtr points to a refcounted thing that has AddRef and Release
127 * methods to increase/decrease the refcount, respectively. After a
128 * RefPtr<T> is assigned a T*, the T* can be used through the RefPtr
129 * as if it were a T*.
131 * A RefPtr can forget its underlying T*, which results in the T*
132 * being wrapped in a temporary object until the T* is either
133 * re-adopted from or released by the temporary.
138 // To allow them to use unref()
139 friend class TemporaryRef
<T
>;
140 friend class OutParamRef
<T
>;
145 RefPtr() : ptr(0) { }
146 RefPtr(const RefPtr
& o
) : ptr(ref(o
.ptr
)) {}
147 RefPtr(const TemporaryRef
<T
>& o
) : ptr(o
.drop()) {}
148 RefPtr(T
* t
) : ptr(ref(t
)) {}
151 RefPtr(const RefPtr
<U
>& o
) : ptr(ref(o
.get())) {}
153 ~RefPtr() { unref(ptr
); }
155 RefPtr
& operator=(const RefPtr
& o
) {
159 RefPtr
& operator=(const TemporaryRef
<T
>& o
) {
163 RefPtr
& operator=(T
* t
) {
169 RefPtr
& operator=(const RefPtr
<U
>& o
) {
170 assign(ref(o
.get()));
174 TemporaryRef
<T
> forget() {
177 return TemporaryRef
<T
>(tmp
, DontRef());
180 T
* get() const { return ptr
; }
181 operator T
*() const { return ptr
; }
182 T
* operator->() const { return ptr
; }
183 T
& operator*() const { return *ptr
; }
185 operator TemporaryRef
<U
>() { return TemporaryRef
<U
>(ptr
); }
195 static MOZ_ALWAYS_INLINE T
* ref(T
* t
) {
201 static MOZ_ALWAYS_INLINE
void unref(T
* t
) {
208 * TemporaryRef<T> represents an object that holds a temporary
209 * reference to a T. TemporaryRef objects can't be manually ref'd or
210 * unref'd (being temporaries, not lvalues), so can only relinquish
211 * references to other objects, or unref on destruction.
216 // To allow it to construct TemporaryRef from a bare T*
217 friend class RefPtr
<T
>;
219 typedef typename RefPtr
<T
>::DontRef DontRef
;
222 TemporaryRef(T
* t
) : ptr(RefPtr
<T
>::ref(t
)) {}
223 TemporaryRef(const TemporaryRef
& o
) : ptr(o
.drop()) {}
226 TemporaryRef(const TemporaryRef
<U
>& o
) : ptr(o
.drop()) {}
228 ~TemporaryRef() { RefPtr
<T
>::unref(ptr
); }
237 TemporaryRef(T
* t
, const DontRef
&) : ptr(t
) {}
241 TemporaryRef() MOZ_DELETE
;
242 void operator=(const TemporaryRef
&) MOZ_DELETE
;
246 * OutParamRef is a wrapper that tracks a refcounted pointer passed as
247 * an outparam argument to a function. OutParamRef implements COM T**
248 * outparam semantics: this requires the callee to AddRef() the T*
249 * returned through the T** outparam on behalf of the caller. This
250 * means the caller (through OutParamRef) must Release() the old
251 * object contained in the tracked RefPtr. It's OK if the callee
252 * returns the same T* passed to it through the T** outparam, as long
253 * as the callee obeys the COM discipline.
255 * Prefer returning TemporaryRef<T> from functions over creating T**
256 * outparams and passing OutParamRef<T> to T**. Prefer RefPtr<T>*
257 * outparams over T** outparams.
262 friend OutParamRef byRef
<T
>(RefPtr
<T
>&);
266 RefPtr
<T
>::unref(refPtr
.ptr
);
270 operator T
**() { return &tmp
; }
273 OutParamRef(RefPtr
<T
>& p
) : refPtr(p
), tmp(p
.get()) {}
278 OutParamRef() MOZ_DELETE
;
279 OutParamRef
& operator=(const OutParamRef
&) MOZ_DELETE
;
283 * byRef cooperates with OutParamRef to implement COM outparam semantics.
287 byRef(RefPtr
<T
>& ptr
)
289 return OutParamRef
<T
>(ptr
);
292 } // namespace mozilla
296 // Command line that builds these tests
298 // cp RefPtr.h test.cc && g++ -g -Wall -pedantic -DDEBUG -o test test.cc && ./test
300 using namespace mozilla
;
302 struct Foo
: public RefCounted
<Foo
>
304 Foo() : dead(false) { }
312 static int numDestroyed
;
314 int Foo::numDestroyed
;
316 struct Bar
: public Foo
{ };
321 return RefPtr
<Foo
>(new Foo());
334 // Kids, don't try this at home
339 GetPassedFoo(Foo
** f
)
341 // Kids, don't try this at home
346 GetNewFoo(RefPtr
<Foo
>* f
)
352 GetPassedFoo(RefPtr
<Foo
>* f
)
362 main(int argc
, char** argv
)
364 // This should blow up
365 // Foo* f = new Foo(); delete f;
367 MOZ_ASSERT(0 == Foo::numDestroyed
);
369 RefPtr
<Foo
> f
= new Foo();
370 MOZ_ASSERT(f
->refCount() == 1);
372 MOZ_ASSERT(1 == Foo::numDestroyed
);
375 RefPtr
<Foo
> f1
= NewFoo();
376 RefPtr
<Foo
> f2(NewFoo());
377 MOZ_ASSERT(1 == Foo::numDestroyed
);
379 MOZ_ASSERT(3 == Foo::numDestroyed
);
382 RefPtr
<Foo
> b
= NewBar();
383 MOZ_ASSERT(3 == Foo::numDestroyed
);
385 MOZ_ASSERT(4 == Foo::numDestroyed
);
393 MOZ_ASSERT(4 == Foo::numDestroyed
);
395 MOZ_ASSERT(4 == Foo::numDestroyed
);
397 MOZ_ASSERT(5 == Foo::numDestroyed
);
400 RefPtr
<Foo
> f
= new Foo();
402 MOZ_ASSERT(6 == Foo::numDestroyed
);
406 RefPtr
<Foo
> f
= new Foo();
408 MOZ_ASSERT(7 == Foo::numDestroyed
);
410 MOZ_ASSERT(8 == Foo::numDestroyed
);
413 RefPtr
<Foo
> f
= new Foo();
414 GetPassedFoo(byRef(f
));
415 MOZ_ASSERT(8 == Foo::numDestroyed
);
417 MOZ_ASSERT(9 == Foo::numDestroyed
);
420 RefPtr
<Foo
> f
= new Foo();
422 MOZ_ASSERT(10 == Foo::numDestroyed
);
424 MOZ_ASSERT(11 == Foo::numDestroyed
);
427 RefPtr
<Foo
> f
= new Foo();
429 MOZ_ASSERT(11 == Foo::numDestroyed
);
431 MOZ_ASSERT(12 == Foo::numDestroyed
);
434 RefPtr
<Foo
> f1
= new Bar();
436 MOZ_ASSERT(13 == Foo::numDestroyed
);
439 RefPtr
<Foo
> f
= GetNullFoo();
440 MOZ_ASSERT(13 == Foo::numDestroyed
);
442 MOZ_ASSERT(13 == Foo::numDestroyed
);
449 #endif /* mozilla_RefPtr_h */