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