1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 // This header provides virtual, non-templated alternatives to MFBT's RefCounted<T>.
7 // It intentionally uses MFBT coding style with the intention of moving there
8 // should there be other use cases for it.
10 #ifndef MOZILLA_GENERICREFCOUNTED_H_
11 #define MOZILLA_GENERICREFCOUNTED_H_
13 #include "mozilla/RefPtr.h"
18 * Common base class for GenericRefCounted and GenericAtomicRefCounted.
20 * Having this shared base class, common to both the atomic and non-atomic
21 * cases, allows to have RefPtr's that don't care about whether the
22 * objects they're managing have atomic refcounts or not.
24 class GenericRefCountedBase
27 virtual ~GenericRefCountedBase() {};
30 // AddRef() and Release() method names are for compatibility with nsRefPtr.
31 virtual void AddRef() = 0;
33 virtual void Release() = 0;
35 // ref() and deref() method names are for compatibility with wtf::RefPtr.
36 // No virtual keywords here: if a subclass wants to override the refcounting
37 // mechanism, it is welcome to do so by overriding AddRef() and Release().
38 void ref() { AddRef(); }
39 void deref() { Release(); }
41 #ifdef MOZ_REFCOUNTED_LEAK_CHECKING
42 virtual const char* typeName() const = 0;
43 virtual size_t typeSize() const = 0;
49 template<RefCountAtomicity Atomicity
>
50 class GenericRefCounted
: public GenericRefCountedBase
53 GenericRefCounted() : refCnt(0) { }
55 virtual ~GenericRefCounted() {
56 MOZ_ASSERT(refCnt
== detail::DEAD
);
60 virtual void AddRef() {
61 // Note: this method must be thread safe for GenericAtomicRefCounted.
62 MOZ_ASSERT(int32_t(refCnt
) >= 0);
63 #ifndef MOZ_REFCOUNTED_LEAK_CHECKING
66 const char* type
= typeName();
67 uint32_t size
= typeSize();
68 const void* ptr
= this;
69 MozRefCountType cnt
= ++refCnt
;
70 detail::RefCountLogger::logAddRef(ptr
, cnt
, type
, size
);
74 virtual void Release() {
75 // Note: this method must be thread safe for GenericAtomicRefCounted.
76 MOZ_ASSERT(int32_t(refCnt
) > 0);
77 #ifndef MOZ_REFCOUNTED_LEAK_CHECKING
78 MozRefCountType cnt
= --refCnt
;
80 const char* type
= typeName();
81 const void* ptr
= this;
82 MozRefCountType cnt
= --refCnt
;
83 // Note: it's not safe to touch |this| after decrementing the refcount,
85 detail::RefCountLogger::logRelease(ptr
, cnt
, type
);
88 // Because we have atomically decremented the refcount above, only
89 // one thread can get a 0 count here, so as long as we can assume that
90 // everything else in the system is accessing this object through
91 // RefPtrs, it's safe to access |this| here.
93 refCnt
= detail::DEAD
;
99 MozRefCountType
refCount() const { return refCnt
; }
100 bool hasOneRef() const {
101 MOZ_ASSERT(refCnt
> 0);
106 typename Conditional
<Atomicity
== AtomicRefCount
, Atomic
<MozRefCountType
>, MozRefCountType
>::Type refCnt
;
109 } // namespace detail
112 * This reference-counting base class is virtual instead of
113 * being templated, which is useful in cases where one needs
114 * genericity at binary code level, but comes at the cost
115 * of a moderate performance and size overhead, like anything virtual.
117 class GenericRefCounted
: public detail::GenericRefCounted
<detail::NonAtomicRefCount
>
122 * GenericAtomicRefCounted is like GenericRefCounted, with an atomically updated
125 class GenericAtomicRefCounted
: public detail::GenericRefCounted
<detail::AtomicRefCount
>
129 } // namespace mozilla