SkRefCnt.h revision 91208922687a33df1d5253928b8d5d7d4685c7ac
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/** Check if the argument is non-null, and if so, call obj->ref() 121 */ 122template <typename T> static inline void SkSafeRef(T* obj) { 123 if (obj) { 124 obj->ref(); 125 } 126} 127 128/** Check if the argument is non-null, and if so, call obj->unref() 129 */ 130template <typename T> static inline void SkSafeUnref(T* obj) { 131 if (obj) { 132 obj->unref(); 133 } 134} 135 136/////////////////////////////////////////////////////////////////////////////// 137 138/** 139 * Utility class that simply unref's its argument in the destructor. 140 */ 141template <typename T> class SkAutoTUnref : SkNoncopyable { 142public: 143 explicit SkAutoTUnref(T* obj = NULL) : fObj(obj) {} 144 ~SkAutoTUnref() { SkSafeUnref(fObj); } 145 146 T* get() const { return fObj; } 147 148 void reset(T* obj) { 149 SkSafeUnref(fObj); 150 fObj = obj; 151 } 152 153 /** 154 * Return the hosted object (which may be null), transferring ownership. 155 * The reference count is not modified, and the internal ptr is set to NULL 156 * so unref() will not be called in our destructor. A subsequent call to 157 * detach() will do nothing and return null. 158 */ 159 T* detach() { 160 T* obj = fObj; 161 fObj = NULL; 162 return obj; 163 } 164 165 /** 166 * BlockRef<B> is a type which inherits from B, cannot be created, 167 * and makes ref and unref private. 168 */ 169 template<typename B> class BlockRef : public B { 170 private: 171 BlockRef(); 172 void ref() const; 173 void unref() const; 174 }; 175 176 /** If T is const, the type returned from operator-> will also be const. */ 177 typedef typename SkTConstType<BlockRef<T>, SkTIsConst<T>::value>::type BlockRefType; 178 179 /** 180 * SkAutoTUnref assumes ownership of the ref. As a result, it is an error 181 * for the user to ref or unref through SkAutoTUnref. Therefore 182 * SkAutoTUnref::operator-> returns BlockRef<T>*. This prevents use of 183 * skAutoTUnrefInstance->ref() and skAutoTUnrefInstance->unref(). 184 */ 185 BlockRefType *operator->() const { 186 return static_cast<BlockRefType*>(fObj); 187 } 188 operator T*() { return fObj; } 189 190private: 191 T* fObj; 192}; 193 194class SkAutoUnref : public SkAutoTUnref<SkRefCnt> { 195public: 196 SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} 197}; 198 199class SkAutoRef : SkNoncopyable { 200public: 201 SkAutoRef(SkRefCnt* obj) : fObj(obj) { SkSafeRef(obj); } 202 ~SkAutoRef() { SkSafeUnref(fObj); } 203private: 204 SkRefCnt* fObj; 205}; 206 207/** Wrapper class for SkRefCnt pointers. This manages ref/unref of a pointer to 208 a SkRefCnt (or subclass) object. 209 */ 210template <typename T> class SkRefPtr { 211public: 212 SkRefPtr() : fObj(NULL) {} 213 SkRefPtr(T* obj) : fObj(obj) { SkSafeRef(fObj); } 214 SkRefPtr(const SkRefPtr& o) : fObj(o.fObj) { SkSafeRef(fObj); } 215 ~SkRefPtr() { SkSafeUnref(fObj); } 216 217 SkRefPtr& operator=(const SkRefPtr& rp) { 218 SkRefCnt_SafeAssign(fObj, rp.fObj); 219 return *this; 220 } 221 SkRefPtr& operator=(T* obj) { 222 SkRefCnt_SafeAssign(fObj, obj); 223 return *this; 224 } 225 226 T* get() const { return fObj; } 227 T& operator*() const { return *fObj; } 228 T* operator->() const { return fObj; } 229 230 typedef T* SkRefPtr::*unspecified_bool_type; 231 operator unspecified_bool_type() const { 232 return fObj ? &SkRefPtr::fObj : NULL; 233 } 234 235private: 236 T* fObj; 237}; 238 239#endif 240 241