|
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/. */ |
|
5 |
|
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. |
|
9 |
|
10 #ifndef MOZILLA_GENERICREFCOUNTED_H_ |
|
11 #define MOZILLA_GENERICREFCOUNTED_H_ |
|
12 |
|
13 #include "mozilla/RefPtr.h" |
|
14 |
|
15 namespace mozilla { |
|
16 |
|
17 /** |
|
18 * Common base class for GenericRefCounted and GenericAtomicRefCounted. |
|
19 * |
|
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. |
|
23 */ |
|
24 class GenericRefCountedBase |
|
25 { |
|
26 protected: |
|
27 virtual ~GenericRefCountedBase() {}; |
|
28 |
|
29 public: |
|
30 // AddRef() and Release() method names are for compatibility with nsRefPtr. |
|
31 virtual void AddRef() = 0; |
|
32 |
|
33 virtual void Release() = 0; |
|
34 |
|
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(); } |
|
40 |
|
41 #ifdef MOZ_REFCOUNTED_LEAK_CHECKING |
|
42 virtual const char* typeName() const = 0; |
|
43 virtual size_t typeSize() const = 0; |
|
44 #endif |
|
45 }; |
|
46 |
|
47 namespace detail { |
|
48 |
|
49 template<RefCountAtomicity Atomicity> |
|
50 class GenericRefCounted : public GenericRefCountedBase |
|
51 { |
|
52 protected: |
|
53 GenericRefCounted() : refCnt(0) { } |
|
54 |
|
55 virtual ~GenericRefCounted() { |
|
56 MOZ_ASSERT(refCnt == detail::DEAD); |
|
57 } |
|
58 |
|
59 public: |
|
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 |
|
64 ++refCnt; |
|
65 #else |
|
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); |
|
71 #endif |
|
72 } |
|
73 |
|
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; |
|
79 #else |
|
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, |
|
84 // except for below. |
|
85 detail::RefCountLogger::logRelease(ptr, cnt, type); |
|
86 #endif |
|
87 if (0 == cnt) { |
|
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. |
|
92 #ifdef DEBUG |
|
93 refCnt = detail::DEAD; |
|
94 #endif |
|
95 delete this; |
|
96 } |
|
97 } |
|
98 |
|
99 MozRefCountType refCount() const { return refCnt; } |
|
100 bool hasOneRef() const { |
|
101 MOZ_ASSERT(refCnt > 0); |
|
102 return refCnt == 1; |
|
103 } |
|
104 |
|
105 private: |
|
106 typename Conditional<Atomicity == AtomicRefCount, Atomic<MozRefCountType>, MozRefCountType>::Type refCnt; |
|
107 }; |
|
108 |
|
109 } // namespace detail |
|
110 |
|
111 /** |
|
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. |
|
116 */ |
|
117 class GenericRefCounted : public detail::GenericRefCounted<detail::NonAtomicRefCount> |
|
118 { |
|
119 }; |
|
120 |
|
121 /** |
|
122 * GenericAtomicRefCounted is like GenericRefCounted, with an atomically updated |
|
123 * reference counter. |
|
124 */ |
|
125 class GenericAtomicRefCounted : public detail::GenericRefCounted<detail::AtomicRefCount> |
|
126 { |
|
127 }; |
|
128 |
|
129 } // namespace mozilla |
|
130 |
|
131 #endif |