3 * Copyright 2006 The Android Open Source Project
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #ifndef SkRefCnt_DEFINED
11 #define SkRefCnt_DEFINED
13 #include "SkDynamicAnnotations.h"
15 #include "SkInstCnt.h"
16 #include "SkTemplates.h"
18 /** \class SkRefCntBase
20 SkRefCntBase is the base class for objects that may be shared by multiple
21 objects. When an existing owner wants to share a reference, it calls ref().
22 When an owner wants to release its reference, it calls unref(). When the
23 shared object's reference count goes to zero as the result of an unref()
24 call, its (virtual) destructor is called. It is an error for the
25 destructor to be called explicitly (or via the object going out of scope on
26 the stack or calling delete) if getRefCnt() > 1.
28 class SK_API SkRefCntBase : public SkNoncopyable {
30 SK_DECLARE_INST_COUNT_ROOT(SkRefCntBase)
32 /** Default construct, initializing the reference count to 1.
34 SkRefCntBase() : fRefCnt(1) {}
36 /** Destruct, asserting that the reference count is 1.
38 virtual ~SkRefCntBase() {
40 SkASSERT(fRefCnt == 1);
41 fRefCnt = 0; // illegal value, to catch us if we reuse after delete
45 /** Return the reference count. Use only for debugging. */
46 int32_t getRefCnt() const { return fRefCnt; }
48 /** May return true if the caller is the only owner.
49 * Ensures that all previous owner's actions are complete.
52 // We believe we're reading fRefCnt in a safe way here, so we stifle the TSAN warning about
53 // an unproctected read. Generally, don't read fRefCnt, and don't stifle this warning.
54 bool const unique = (1 == SK_ANNOTATE_UNPROTECTED_READ(fRefCnt));
56 // Acquire barrier (L/SL), if not provided by load of fRefCnt.
57 // Prevents user's 'unique' code from happening before decrements.
58 //TODO: issue the barrier.
63 /** Increment the reference count. Must be balanced by a call to unref().
66 SkASSERT(fRefCnt > 0);
67 sk_atomic_inc(&fRefCnt); // No barrier required.
70 /** Decrement the reference count. If the reference count is 1 before the
71 decrement, then delete the object. Note that if this is the case, then
72 the object needs to have been allocated via new, and not on the stack.
75 SkASSERT(fRefCnt > 0);
76 // Release barrier (SL/S), if not provided below.
77 if (sk_atomic_dec(&fRefCnt) == 1) {
78 // Acquire barrier (L/SL), if not provided above.
79 // Prevents code in dispose from happening before the decrement.
80 sk_membar_acquire__after_atomic_dec();
86 void validate() const {
87 SkASSERT(fRefCnt > 0);
93 * Allow subclasses to call this if they've overridden internal_dispose
94 * so they can reset fRefCnt before the destructor is called. Should only
95 * be called right before calling through to inherited internal_dispose()
96 * or before calling the destructor.
98 void internal_dispose_restore_refcnt_to_1() const {
100 SkASSERT(0 == fRefCnt);
107 * Called when the ref count goes to 0.
109 virtual void internal_dispose() const {
110 this->internal_dispose_restore_refcnt_to_1();
114 // The following friends are those which override internal_dispose()
115 // and conditionally call SkRefCnt::internal_dispose().
116 friend class GrTexture;
117 friend class SkWeakRefCnt;
119 mutable int32_t fRefCnt;
121 typedef SkNoncopyable INHERITED;
124 #ifdef SK_REF_CNT_MIXIN_INCLUDE
125 // It is the responsibility of the following include to define the type SkRefCnt.
126 // This SkRefCnt should normally derive from SkRefCntBase.
127 #include SK_REF_CNT_MIXIN_INCLUDE
129 class SK_API SkRefCnt : public SkRefCntBase { };
132 ///////////////////////////////////////////////////////////////////////////////
134 /** Helper macro to safely assign one SkRefCnt[TS]* to another, checking for
135 null in on each side of the assignment, and ensuring that ref() is called
136 before unref(), in case the two pointers point to the same object.
138 #define SkRefCnt_SafeAssign(dst, src) \
140 if (src) src->ref(); \
141 if (dst) dst->unref(); \
146 /** Call obj->ref() and return obj. The obj must not be NULL.
148 template <typename T> static inline T* SkRef(T* obj) {
154 /** Check if the argument is non-null, and if so, call obj->ref() and return obj.
156 template <typename T> static inline T* SkSafeRef(T* obj) {
163 /** Check if the argument is non-null, and if so, call obj->unref()
165 template <typename T> static inline void SkSafeUnref(T* obj) {
171 template<typename T> static inline void SkSafeSetNull(T*& obj) {
178 ///////////////////////////////////////////////////////////////////////////////
181 * Utility class that simply unref's its argument in the destructor.
183 template <typename T> class SkAutoTUnref : SkNoncopyable {
185 explicit SkAutoTUnref(T* obj = NULL) : fObj(obj) {}
186 ~SkAutoTUnref() { SkSafeUnref(fObj); }
188 T* get() const { return fObj; }
196 void swap(SkAutoTUnref* other) {
203 * Return the hosted object (which may be null), transferring ownership.
204 * The reference count is not modified, and the internal ptr is set to NULL
205 * so unref() will not be called in our destructor. A subsequent call to
206 * detach() will do nothing and return null.
215 * BlockRef<B> is a type which inherits from B, cannot be created,
216 * cannot be deleted, and makes ref and unref private.
218 template<typename B> class BlockRef : public B {
226 /** If T is const, the type returned from operator-> will also be const. */
227 typedef typename SkTConstType<BlockRef<T>, SkTIsConst<T>::value>::type BlockRefType;
230 * SkAutoTUnref assumes ownership of the ref. As a result, it is an error
231 * for the user to ref or unref through SkAutoTUnref. Therefore
232 * SkAutoTUnref::operator-> returns BlockRef<T>*. This prevents use of
233 * skAutoTUnrefInstance->ref() and skAutoTUnrefInstance->unref().
235 BlockRefType *operator->() const {
236 return static_cast<BlockRefType*>(fObj);
238 operator T*() { return fObj; }
243 // Can't use the #define trick below to guard a bare SkAutoTUnref(...) because it's templated. :(
245 class SkAutoUnref : public SkAutoTUnref<SkRefCnt> {
247 SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {}
249 #define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref)
251 class SkAutoRef : SkNoncopyable {
253 SkAutoRef(SkRefCnt* obj) : fObj(obj) { SkSafeRef(obj); }
254 ~SkAutoRef() { SkSafeUnref(fObj); }
258 #define SkAutoRef(...) SK_REQUIRE_LOCAL_VAR(SkAutoRef)
260 /** Wrapper class for SkRefCnt pointers. This manages ref/unref of a pointer to
261 a SkRefCnt (or subclass) object.
263 template <typename T> class SkRefPtr {
265 SkRefPtr() : fObj(NULL) {}
266 SkRefPtr(T* obj) : fObj(obj) { SkSafeRef(fObj); }
267 SkRefPtr(const SkRefPtr& o) : fObj(o.fObj) { SkSafeRef(fObj); }
268 ~SkRefPtr() { SkSafeUnref(fObj); }
270 SkRefPtr& operator=(const SkRefPtr& rp) {
271 SkRefCnt_SafeAssign(fObj, rp.fObj);
274 SkRefPtr& operator=(T* obj) {
275 SkRefCnt_SafeAssign(fObj, obj);
279 T* get() const { return fObj; }
280 T& operator*() const { return *fObj; }
281 T* operator->() const { return fObj; }
283 typedef T* SkRefPtr::*unspecified_bool_type;
284 operator unspecified_bool_type() const {
285 return fObj ? &SkRefPtr::fObj : NULL;