SkRefCnt.h revision 1fd201b9dfc739b7a615fdc1eb64dbe136e7cf76
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 SkRefCnt 18 19 SkRefCnt 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*/ 27class SK_API SkRefCnt : SkNoncopyable { 28public: 29 SK_DECLARE_INST_COUNT_ROOT(SkRefCnt) 30 31 /** Default construct, initializing the reference count to 1. 32 */ 33 SkRefCnt() : fRefCnt(1) {} 34 35 /** Destruct, asserting that the reference count is 1. 36 */ 37 virtual ~SkRefCnt() { 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. 45 */ 46 int32_t getRefCnt() const { return fRefCnt; } 47 48 /** Increment the reference count. Must be balanced by a call to unref(). 49 */ 50 void ref() const { 51 SkASSERT(fRefCnt > 0); 52 sk_atomic_inc(&fRefCnt); // No barrier required. 53 } 54 55 /** Decrement the reference count. If the reference count is 1 before the 56 decrement, then delete the object. Note that if this is the case, then 57 the object needs to have been allocated via new, and not on the stack. 58 */ 59 void unref() const { 60 SkASSERT(fRefCnt > 0); 61 // Release barrier (SL/S), if not provided below. 62 if (sk_atomic_dec(&fRefCnt) == 1) { 63 // Aquire barrier (L/SL), if not provided above. 64 // Prevents code in dispose from happening before the decrement. 65 sk_membar_aquire__after_atomic_dec(); 66 internal_dispose(); 67 } 68 } 69 70 void validate() const { 71 SkASSERT(fRefCnt > 0); 72 } 73 74protected: 75 /** 76 * Allow subclasses to call this if they've overridden internal_dispose 77 * so they can reset fRefCnt before the destructor is called. Should only 78 * be called right before calling through to inherited internal_dispose() 79 * or before calling the destructor. 80 */ 81 void internal_dispose_restore_refcnt_to_1() const { 82#ifdef SK_DEBUG 83 SkASSERT(0 == fRefCnt); 84 fRefCnt = 1; 85#endif 86 } 87 88private: 89 /** 90 * Called when the ref count goes to 0. 91 */ 92 virtual void internal_dispose() const { 93 this->internal_dispose_restore_refcnt_to_1(); 94 SkDELETE(this); 95 } 96 97 friend class SkWeakRefCnt; 98 friend class GrTexture; // to allow GrTexture's internal_dispose to 99 // call SkRefCnt's & directly set fRefCnt (to 1) 100 101 mutable int32_t fRefCnt; 102 103 typedef SkNoncopyable INHERITED; 104}; 105 106/////////////////////////////////////////////////////////////////////////////// 107 108/** Helper macro to safely assign one SkRefCnt[TS]* to another, checking for 109 null in on each side of the assignment, and ensuring that ref() is called 110 before unref(), in case the two pointers point to the same object. 111 */ 112#define SkRefCnt_SafeAssign(dst, src) \ 113 do { \ 114 if (src) src->ref(); \ 115 if (dst) dst->unref(); \ 116 dst = src; \ 117 } while (0) 118 119 120/** Call obj->ref() and return obj. The obj must not be NULL. 121 */ 122template <typename T> static inline T* SkRef(T* obj) { 123 SkASSERT(obj); 124 obj->ref(); 125 return obj; 126} 127 128/** Check if the argument is non-null, and if so, call obj->ref() and return obj. 129 */ 130template <typename T> static inline T* SkSafeRef(T* obj) { 131 if (obj) { 132 obj->ref(); 133 } 134 return obj; 135} 136 137/** Check if the argument is non-null, and if so, call obj->unref() 138 */ 139template <typename T> static inline void SkSafeUnref(T* obj) { 140 if (obj) { 141 obj->unref(); 142 } 143} 144 145/////////////////////////////////////////////////////////////////////////////// 146 147/** 148 * Utility class that simply unref's its argument in the destructor. 149 */ 150template <typename T> class SkAutoTUnref : SkNoncopyable { 151public: 152 explicit SkAutoTUnref(T* obj = NULL) : fObj(obj) {} 153 ~SkAutoTUnref() { SkSafeUnref(fObj); } 154 155 T* get() const { return fObj; } 156 157 void reset(T* obj) { 158 SkSafeUnref(fObj); 159 fObj = obj; 160 } 161 162 /** 163 * Return the hosted object (which may be null), transferring ownership. 164 * The reference count is not modified, and the internal ptr is set to NULL 165 * so unref() will not be called in our destructor. A subsequent call to 166 * detach() will do nothing and return null. 167 */ 168 T* detach() { 169 T* obj = fObj; 170 fObj = NULL; 171 return obj; 172 } 173 174 /** 175 * BlockRef<B> is a type which inherits from B, cannot be created, 176 * and makes ref and unref private. 177 */ 178 template<typename B> class BlockRef : public B { 179 private: 180 BlockRef(); 181 void ref() const; 182 void unref() const; 183 }; 184 185 /** If T is const, the type returned from operator-> will also be const. */ 186 typedef typename SkTConstType<BlockRef<T>, SkTIsConst<T>::value>::type BlockRefType; 187 188 /** 189 * SkAutoTUnref assumes ownership of the ref. As a result, it is an error 190 * for the user to ref or unref through SkAutoTUnref. Therefore 191 * SkAutoTUnref::operator-> returns BlockRef<T>*. This prevents use of 192 * skAutoTUnrefInstance->ref() and skAutoTUnrefInstance->unref(). 193 */ 194 BlockRefType *operator->() const { 195 return static_cast<BlockRefType*>(fObj); 196 } 197 operator T*() { return fObj; } 198 199private: 200 T* fObj; 201}; 202 203class SkAutoUnref : public SkAutoTUnref<SkRefCnt> { 204public: 205 SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} 206}; 207 208class SkAutoRef : SkNoncopyable { 209public: 210 SkAutoRef(SkRefCnt* obj) : fObj(obj) { SkSafeRef(obj); } 211 ~SkAutoRef() { SkSafeUnref(fObj); } 212private: 213 SkRefCnt* fObj; 214}; 215 216/** Wrapper class for SkRefCnt pointers. This manages ref/unref of a pointer to 217 a SkRefCnt (or subclass) object. 218 */ 219template <typename T> class SkRefPtr { 220public: 221 SkRefPtr() : fObj(NULL) {} 222 SkRefPtr(T* obj) : fObj(obj) { SkSafeRef(fObj); } 223 SkRefPtr(const SkRefPtr& o) : fObj(o.fObj) { SkSafeRef(fObj); } 224 ~SkRefPtr() { SkSafeUnref(fObj); } 225 226 SkRefPtr& operator=(const SkRefPtr& rp) { 227 SkRefCnt_SafeAssign(fObj, rp.fObj); 228 return *this; 229 } 230 SkRefPtr& operator=(T* obj) { 231 SkRefCnt_SafeAssign(fObj, obj); 232 return *this; 233 } 234 235 T* get() const { return fObj; } 236 T& operator*() const { return *fObj; } 237 T* operator->() const { return fObj; } 238 239 typedef T* SkRefPtr::*unspecified_bool_type; 240 operator unspecified_bool_type() const { 241 return fObj ? &SkRefPtr::fObj : NULL; 242 } 243 244private: 245 T* fObj; 246}; 247 248#endif 249 250