SkRefCnt.h revision e6e41a8a19976a822de36379db23184ff2f28601
1/* 2 * Copyright 2006 The Android Open Source Project 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8#ifndef SkRefCnt_DEFINED 9#define SkRefCnt_DEFINED 10 11#include "../private/SkAtomics.h" 12#include "SkTypes.h" 13 14/** \class SkRefCntBase 15 16 SkRefCntBase is the base class for objects that may be shared by multiple 17 objects. When an existing owner wants to share a reference, it calls ref(). 18 When an owner wants to release its reference, it calls unref(). When the 19 shared object's reference count goes to zero as the result of an unref() 20 call, its (virtual) destructor is called. It is an error for the 21 destructor to be called explicitly (or via the object going out of scope on 22 the stack or calling delete) if getRefCnt() > 1. 23*/ 24class SK_API SkRefCntBase : SkNoncopyable { 25public: 26 /** Default construct, initializing the reference count to 1. 27 */ 28 SkRefCntBase() : fRefCnt(1) {} 29 30 /** Destruct, asserting that the reference count is 1. 31 */ 32 virtual ~SkRefCntBase() { 33#ifdef SK_DEBUG 34 SkASSERTF(fRefCnt == 1, "fRefCnt was %d", fRefCnt); 35 fRefCnt = 0; // illegal value, to catch us if we reuse after delete 36#endif 37 } 38 39#ifdef SK_DEBUG 40 /** Return the reference count. Use only for debugging. */ 41 int32_t getRefCnt() const { return fRefCnt; } 42#endif 43 44 /** May return true if the caller is the only owner. 45 * Ensures that all previous owner's actions are complete. 46 */ 47 bool unique() const { 48 if (1 == sk_atomic_load(&fRefCnt, sk_memory_order_acquire)) { 49 // The acquire barrier is only really needed if we return true. It 50 // prevents code conditioned on the result of unique() from running 51 // until previous owners are all totally done calling unref(). 52 return true; 53 } 54 return false; 55 } 56 57 /** Increment the reference count. Must be balanced by a call to unref(). 58 */ 59 void ref() const { 60#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK 61 // Android employs some special subclasses that enable the fRefCnt to 62 // go to zero, but not below, prior to reusing the object. This breaks 63 // the use of unique() on such objects and as such should be removed 64 // once the Android code is fixed. 65 SkASSERT(fRefCnt >= 0); 66#else 67 SkASSERT(fRefCnt > 0); 68#endif 69 (void)sk_atomic_fetch_add(&fRefCnt, +1, sk_memory_order_relaxed); // No barrier required. 70 } 71 72 /** Decrement the reference count. If the reference count is 1 before the 73 decrement, then delete the object. Note that if this is the case, then 74 the object needs to have been allocated via new, and not on the stack. 75 */ 76 void unref() const { 77 SkASSERT(fRefCnt > 0); 78 // A release here acts in place of all releases we "should" have been doing in ref(). 79 if (1 == sk_atomic_fetch_add(&fRefCnt, -1, sk_memory_order_acq_rel)) { 80 // Like unique(), the acquire is only needed on success, to make sure 81 // code in internal_dispose() doesn't happen before the decrement. 82 this->internal_dispose(); 83 } 84 } 85 86#ifdef SK_DEBUG 87 void validate() const { 88 SkASSERT(fRefCnt > 0); 89 } 90#endif 91 92protected: 93 /** 94 * Allow subclasses to call this if they've overridden internal_dispose 95 * so they can reset fRefCnt before the destructor is called. Should only 96 * be called right before calling through to inherited internal_dispose() 97 * or before calling the destructor. 98 */ 99 void internal_dispose_restore_refcnt_to_1() const { 100#ifdef SK_DEBUG 101 SkASSERT(0 == fRefCnt); 102 fRefCnt = 1; 103#endif 104 } 105 106private: 107 /** 108 * Called when the ref count goes to 0. 109 */ 110 virtual void internal_dispose() const { 111 this->internal_dispose_restore_refcnt_to_1(); 112 delete this; 113 } 114 115 // The following friends are those which override internal_dispose() 116 // and conditionally call SkRefCnt::internal_dispose(). 117 friend class SkWeakRefCnt; 118 119 mutable int32_t fRefCnt; 120 121 typedef SkNoncopyable INHERITED; 122}; 123 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 128#else 129class SK_API SkRefCnt : public SkRefCntBase { }; 130#endif 131 132/////////////////////////////////////////////////////////////////////////////// 133 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. 137 */ 138#define SkRefCnt_SafeAssign(dst, src) \ 139 do { \ 140 if (src) src->ref(); \ 141 if (dst) dst->unref(); \ 142 dst = src; \ 143 } while (0) 144 145 146/** Call obj->ref() and return obj. The obj must not be NULL. 147 */ 148template <typename T> static inline T* SkRef(T* obj) { 149 SkASSERT(obj); 150 obj->ref(); 151 return obj; 152} 153 154/** Check if the argument is non-null, and if so, call obj->ref() and return obj. 155 */ 156template <typename T> static inline T* SkSafeRef(T* obj) { 157 if (obj) { 158 obj->ref(); 159 } 160 return obj; 161} 162 163/** Check if the argument is non-null, and if so, call obj->unref() 164 */ 165template <typename T> static inline void SkSafeUnref(T* obj) { 166 if (obj) { 167 obj->unref(); 168 } 169} 170 171template<typename T> static inline void SkSafeSetNull(T*& obj) { 172 if (obj) { 173 obj->unref(); 174 obj = NULL; 175 } 176} 177 178/////////////////////////////////////////////////////////////////////////////// 179 180/** 181 * Utility class that simply unref's its argument in the destructor. 182 */ 183template <typename T> class SkAutoTUnref : SkNoncopyable { 184public: 185 explicit SkAutoTUnref(T* obj = NULL) : fObj(obj) {} 186 ~SkAutoTUnref() { SkSafeUnref(fObj); } 187 188 T* get() const { return fObj; } 189 190 T* reset(T* obj) { 191 SkSafeUnref(fObj); 192 fObj = obj; 193 return obj; 194 } 195 196 void swap(SkAutoTUnref* other) { 197 T* tmp = fObj; 198 fObj = other->fObj; 199 other->fObj = tmp; 200 } 201 202 /** 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. 207 */ 208 T* detach() { 209 T* obj = fObj; 210 fObj = NULL; 211 return obj; 212 } 213 214 T* operator->() const { return fObj; } 215 operator T*() const { return fObj; } 216 217private: 218 T* fObj; 219}; 220// Can't use the #define trick below to guard a bare SkAutoTUnref(...) because it's templated. :( 221 222class SkAutoUnref : public SkAutoTUnref<SkRefCnt> { 223public: 224 SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} 225}; 226#define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref) 227 228// This is a variant of SkRefCnt that's Not Virtual, so weighs 4 bytes instead of 8 or 16. 229// There's only benefit to using this if the deriving class does not otherwise need a vtable. 230template <typename Derived> 231class SkNVRefCnt : SkNoncopyable { 232public: 233 SkNVRefCnt() : fRefCnt(1) {} 234 ~SkNVRefCnt() { SkASSERTF(1 == fRefCnt, "NVRefCnt was %d", fRefCnt); } 235 236 // Implementation is pretty much the same as SkRefCntBase. All required barriers are the same: 237 // - unique() needs acquire when it returns true, and no barrier if it returns false; 238 // - ref() doesn't need any barrier; 239 // - unref() needs a release barrier, and an acquire if it's going to call delete. 240 241 bool unique() const { return 1 == sk_atomic_load(&fRefCnt, sk_memory_order_acquire); } 242 void ref() const { (void)sk_atomic_fetch_add(&fRefCnt, +1, sk_memory_order_relaxed); } 243 void unref() const { 244 if (1 == sk_atomic_fetch_add(&fRefCnt, -1, sk_memory_order_acq_rel)) { 245 SkDEBUGCODE(fRefCnt = 1;) // restore the 1 for our destructor's assert 246 delete (const Derived*)this; 247 } 248 } 249 void deref() const { this->unref(); } 250 251private: 252 mutable int32_t fRefCnt; 253}; 254 255#endif 256