SkRefCnt.h revision 3c850c561fcad1ac35bff4ec2875a40ef2309148
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 "SkDynamicAnnotations.h" 14#include "SkThread.h" 15#include "SkInstCnt.h" 16#include "SkTemplates.h" 17 18/** \class SkRefCntBase 19 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. 27*/ 28class SK_API SkRefCntBase : SkNoncopyable { 29public: 30 SK_DECLARE_INST_COUNT_ROOT(SkRefCntBase) 31 32 /** Default construct, initializing the reference count to 1. 33 */ 34 SkRefCntBase() : fRefCnt(1) {} 35 36 /** Destruct, asserting that the reference count is 1. 37 */ 38 virtual ~SkRefCntBase() { 39#ifdef SK_DEBUG 40 SkASSERTF(fRefCnt == 1, "fRefCnt was %d", fRefCnt); 41 fRefCnt = 0; // illegal value, to catch us if we reuse after delete 42#endif 43 } 44 45//#ifdef SK_DEBUG 46 /** Return the reference count. Use only for debugging. */ 47 int32_t getRefCnt() const { return fRefCnt; } 48//#endif 49 50 /** May return true if the caller is the only owner. 51 * Ensures that all previous owner's actions are complete. 52 */ 53 bool unique() const { 54 // We believe we're reading fRefCnt in a safe way here, so we stifle the TSAN warning about 55 // an unproctected read. Generally, don't read fRefCnt, and don't stifle this warning. 56 bool const unique = (1 == sk_acquire_load(&fRefCnt)); 57 if (unique) { 58 // Acquire barrier (L/SL), if not provided by load of fRefCnt. 59 // Prevents user's 'unique' code from happening before decrements. 60 //TODO: issue the barrier only when unique is true 61 } 62 return unique; 63 } 64 65 /** Increment the reference count. Must be balanced by a call to unref(). 66 */ 67 void ref() const { 68 SkASSERT(fRefCnt > 0); 69 sk_atomic_inc(&fRefCnt); // 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 // Release barrier (SL/S), if not provided below. 79 if (sk_atomic_dec(&fRefCnt) == 1) { 80 // Acquire barrier (L/SL), if not provided above. 81 // Prevents code in dispose from happening before the decrement. 82 sk_membar_acquire__after_atomic_dec(); 83 internal_dispose(); 84 } 85 } 86 87#ifdef SK_DEBUG 88 void validate() const { 89 SkASSERT(fRefCnt > 0); 90 } 91#endif 92 93protected: 94 /** 95 * Allow subclasses to call this if they've overridden internal_dispose 96 * so they can reset fRefCnt before the destructor is called. Should only 97 * be called right before calling through to inherited internal_dispose() 98 * or before calling the destructor. 99 */ 100 void internal_dispose_restore_refcnt_to_1() const { 101#ifdef SK_DEBUG 102 SkASSERT(0 == fRefCnt); 103 fRefCnt = 1; 104#endif 105 } 106 107private: 108 /** 109 * Called when the ref count goes to 0. 110 */ 111 virtual void internal_dispose() const { 112 this->internal_dispose_restore_refcnt_to_1(); 113 SkDELETE(this); 114 } 115 116 // The following friends are those which override internal_dispose() 117 // and conditionally call SkRefCnt::internal_dispose(). 118 friend class SkWeakRefCnt; 119 120 mutable int32_t fRefCnt; 121 122 typedef SkNoncopyable INHERITED; 123}; 124 125#ifdef SK_REF_CNT_MIXIN_INCLUDE 126// It is the responsibility of the following include to define the type SkRefCnt. 127// This SkRefCnt should normally derive from SkRefCntBase. 128#include SK_REF_CNT_MIXIN_INCLUDE 129#else 130class SK_API SkRefCnt : public SkRefCntBase { }; 131#endif 132 133/////////////////////////////////////////////////////////////////////////////// 134 135/** Helper macro to safely assign one SkRefCnt[TS]* to another, checking for 136 null in on each side of the assignment, and ensuring that ref() is called 137 before unref(), in case the two pointers point to the same object. 138 */ 139#define SkRefCnt_SafeAssign(dst, src) \ 140 do { \ 141 if (src) src->ref(); \ 142 if (dst) dst->unref(); \ 143 dst = src; \ 144 } while (0) 145 146 147/** Call obj->ref() and return obj. The obj must not be NULL. 148 */ 149template <typename T> static inline T* SkRef(T* obj) { 150 SkASSERT(obj); 151 obj->ref(); 152 return obj; 153} 154 155/** Check if the argument is non-null, and if so, call obj->ref() and return obj. 156 */ 157template <typename T> static inline T* SkSafeRef(T* obj) { 158 if (obj) { 159 obj->ref(); 160 } 161 return obj; 162} 163 164/** Check if the argument is non-null, and if so, call obj->unref() 165 */ 166template <typename T> static inline void SkSafeUnref(T* obj) { 167 if (obj) { 168 obj->unref(); 169 } 170} 171 172template<typename T> static inline void SkSafeSetNull(T*& obj) { 173 if (obj) { 174 obj->unref(); 175 obj = NULL; 176 } 177} 178 179/////////////////////////////////////////////////////////////////////////////// 180 181/** 182 * Utility class that simply unref's its argument in the destructor. 183 */ 184template <typename T> class SkAutoTUnref : SkNoncopyable { 185public: 186 explicit SkAutoTUnref(T* obj = NULL) : fObj(obj) {} 187 ~SkAutoTUnref() { SkSafeUnref(fObj); } 188 189 T* get() const { return fObj; } 190 191 T* reset(T* obj) { 192 SkSafeUnref(fObj); 193 fObj = obj; 194 return obj; 195 } 196 197 void swap(SkAutoTUnref* other) { 198 T* tmp = fObj; 199 fObj = other->fObj; 200 other->fObj = tmp; 201 } 202 203 /** 204 * Return the hosted object (which may be null), transferring ownership. 205 * The reference count is not modified, and the internal ptr is set to NULL 206 * so unref() will not be called in our destructor. A subsequent call to 207 * detach() will do nothing and return null. 208 */ 209 T* detach() { 210 T* obj = fObj; 211 fObj = NULL; 212 return obj; 213 } 214 215 /** 216 * BlockRef<B> is a type which inherits from B, cannot be created, 217 * cannot be deleted, and makes ref and unref private. 218 */ 219 template<typename B> class BlockRef : public B { 220 private: 221 BlockRef(); 222 ~BlockRef(); 223 void ref() const; 224 void unref() const; 225 }; 226 227 /** If T is const, the type returned from operator-> will also be const. */ 228 typedef typename SkTConstType<BlockRef<T>, SkTIsConst<T>::value>::type BlockRefType; 229 230 /** 231 * SkAutoTUnref assumes ownership of the ref. As a result, it is an error 232 * for the user to ref or unref through SkAutoTUnref. Therefore 233 * SkAutoTUnref::operator-> returns BlockRef<T>*. This prevents use of 234 * skAutoTUnrefInstance->ref() and skAutoTUnrefInstance->unref(). 235 */ 236 BlockRefType *operator->() const { 237 return static_cast<BlockRefType*>(fObj); 238 } 239 operator T*() const { return fObj; } 240 241private: 242 T* fObj; 243}; 244// Can't use the #define trick below to guard a bare SkAutoTUnref(...) because it's templated. :( 245 246class SkAutoUnref : public SkAutoTUnref<SkRefCnt> { 247public: 248 SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} 249}; 250#define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref) 251 252// This is a variant of SkRefCnt that's Not Virtual, so weighs 4 bytes instead of 8 or 16. 253// There's only benefit to using this if the deriving class does not otherwise need a vtable. 254template <typename Derived> 255class SkNVRefCnt : SkNoncopyable { 256public: 257 SkNVRefCnt() : fRefCnt(1) {} 258 ~SkNVRefCnt() { SkASSERTF(1 == fRefCnt, "NVRefCnt was %d", fRefCnt); } 259 260 // Implementation is pretty much the same as SkRefCntBase. All required barriers are the same: 261 // - unique() needs acquire when it returns true, and no barrier if it returns false; 262 // - ref() doesn't need any barrier; 263 // - unref() needs a release barrier, and an acquire if it's going to call delete. 264 265 bool unique() const { return 1 == sk_acquire_load(&fRefCnt); } 266 void ref() const { sk_atomic_inc(&fRefCnt); } 267 void unref() const { 268 int32_t prevValue = sk_atomic_dec(&fRefCnt); 269 SkASSERT(prevValue >= 1); 270 if (1 == prevValue) { 271 SkDEBUGCODE(fRefCnt = 1;) // restore the 1 for our destructor's assert 272 SkDELETE((const Derived*)this); 273 } 274 } 275 void deref() const { this->unref(); } // Chrome prefers to call deref(). 276 277private: 278 mutable int32_t fRefCnt; 279}; 280 281#endif 282