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