epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkRefCnt_DEFINED |
| 11 | #define SkRefCnt_DEFINED |
| 12 | |
| 13 | #include "SkThread.h" |
robertphillips@google.com | 977b9c8 | 2012-06-05 19:35:09 +0000 | [diff] [blame] | 14 | #include "SkInstCnt.h" |
bungeman@google.com | 9120892 | 2012-07-30 15:03:59 +0000 | [diff] [blame] | 15 | #include "SkTemplates.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | |
| 17 | /** \class SkRefCnt |
| 18 | |
| 19 | SkRefCnt is the base class for objects that may be shared by multiple |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 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. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 27 | class SK_API SkRefCnt : SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | public: |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 29 | SK_DECLARE_INST_COUNT_ROOT(SkRefCnt) |
robertphillips@google.com | 977b9c8 | 2012-06-05 19:35:09 +0000 | [diff] [blame] | 30 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | /** Default construct, initializing the reference count to 1. |
| 32 | */ |
| 33 | SkRefCnt() : fRefCnt(1) {} |
| 34 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 35 | /** Destruct, asserting that the reference count is 1. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | */ |
reed@google.com | 4c888aa | 2011-09-12 19:54:12 +0000 | [diff] [blame] | 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 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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); |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 52 | sk_atomic_inc(&fRefCnt); // No barrier required. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /** Decrement the reference count. If the reference count is 1 before the |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 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. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | */ |
| 59 | void unref() const { |
| 60 | SkASSERT(fRefCnt > 0); |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 61 | // Release barrier (SL/S), if not provided below. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | if (sk_atomic_dec(&fRefCnt) == 1) { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 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(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 70 | void validate() const { |
| 71 | SkASSERT(fRefCnt > 0); |
| 72 | } |
| 73 | |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame] | 74 | protected: |
| 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 { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 82 | #ifdef SK_DEBUG |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame] | 83 | SkASSERT(0 == fRefCnt); |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 84 | fRefCnt = 1; |
| 85 | #endif |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | private: |
| 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(); |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 94 | SkDELETE(this); |
| 95 | } |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 96 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 97 | friend class SkWeakRefCnt; |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 98 | friend class GrTexture; // to allow GrTexture's internal_dispose to |
| 99 | // call SkRefCnt's & directly set fRefCnt (to 1) |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 100 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | mutable int32_t fRefCnt; |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 102 | |
| 103 | typedef SkNoncopyable INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 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 | |
bungeman@google.com | 1fd201b | 2012-08-22 18:56:56 +0000 | [diff] [blame] | 120 | /** Call obj->ref() and return obj. The obj must not be NULL. |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 121 | */ |
bungeman@google.com | 1fd201b | 2012-08-22 18:56:56 +0000 | [diff] [blame] | 122 | template <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 | */ |
| 130 | template <typename T> static inline T* SkSafeRef(T* obj) { |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 131 | if (obj) { |
| 132 | obj->ref(); |
| 133 | } |
bungeman@google.com | 1fd201b | 2012-08-22 18:56:56 +0000 | [diff] [blame] | 134 | return obj; |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /** Check if the argument is non-null, and if so, call obj->unref() |
| 138 | */ |
| 139 | template <typename T> static inline void SkSafeUnref(T* obj) { |
| 140 | if (obj) { |
| 141 | obj->unref(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /////////////////////////////////////////////////////////////////////////////// |
| 146 | |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 147 | /** |
bungeman@google.com | 9120892 | 2012-07-30 15:03:59 +0000 | [diff] [blame] | 148 | * Utility class that simply unref's its argument in the destructor. |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 149 | */ |
bungeman@google.com | 9120892 | 2012-07-30 15:03:59 +0000 | [diff] [blame] | 150 | template <typename T> class SkAutoTUnref : SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | public: |
bungeman@google.com | 9120892 | 2012-07-30 15:03:59 +0000 | [diff] [blame] | 152 | explicit SkAutoTUnref(T* obj = NULL) : fObj(obj) {} |
| 153 | ~SkAutoTUnref() { SkSafeUnref(fObj); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 154 | |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 155 | T* get() const { return fObj; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 156 | |
bsalomon@google.com | a44f700 | 2011-08-09 15:30:41 +0000 | [diff] [blame] | 157 | void reset(T* obj) { |
reed@google.com | 68d6bb0 | 2012-11-27 18:37:52 +0000 | [diff] [blame^] | 158 | SkSafeUnref(fObj); |
| 159 | fObj = obj; |
bsalomon@google.com | a44f700 | 2011-08-09 15:30:41 +0000 | [diff] [blame] | 160 | } |
| 161 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 162 | void swap(SkAutoTUnref* other) { |
| 163 | T* tmp = fObj; |
| 164 | fObj = other->fObj; |
| 165 | other->fObj = tmp; |
| 166 | } |
| 167 | |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 168 | /** |
| 169 | * Return the hosted object (which may be null), transferring ownership. |
| 170 | * The reference count is not modified, and the internal ptr is set to NULL |
| 171 | * so unref() will not be called in our destructor. A subsequent call to |
| 172 | * detach() will do nothing and return null. |
| 173 | */ |
| 174 | T* detach() { |
| 175 | T* obj = fObj; |
| 176 | fObj = NULL; |
| 177 | return obj; |
| 178 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 179 | |
bungeman@google.com | e70f798 | 2012-06-01 19:38:19 +0000 | [diff] [blame] | 180 | /** |
bungeman@google.com | 9120892 | 2012-07-30 15:03:59 +0000 | [diff] [blame] | 181 | * BlockRef<B> is a type which inherits from B, cannot be created, |
| 182 | * and makes ref and unref private. |
bungeman@google.com | e70f798 | 2012-06-01 19:38:19 +0000 | [diff] [blame] | 183 | */ |
bungeman@google.com | 0464029 | 2012-06-01 19:47:51 +0000 | [diff] [blame] | 184 | template<typename B> class BlockRef : public B { |
bungeman@google.com | e70f798 | 2012-06-01 19:38:19 +0000 | [diff] [blame] | 185 | private: |
| 186 | BlockRef(); |
| 187 | void ref() const; |
| 188 | void unref() const; |
| 189 | }; |
bsalomon@google.com | 1448cf8 | 2012-07-27 13:27:35 +0000 | [diff] [blame] | 190 | |
bungeman@google.com | 9120892 | 2012-07-30 15:03:59 +0000 | [diff] [blame] | 191 | /** If T is const, the type returned from operator-> will also be const. */ |
| 192 | typedef typename SkTConstType<BlockRef<T>, SkTIsConst<T>::value>::type BlockRefType; |
| 193 | |
| 194 | /** |
| 195 | * SkAutoTUnref assumes ownership of the ref. As a result, it is an error |
| 196 | * for the user to ref or unref through SkAutoTUnref. Therefore |
| 197 | * SkAutoTUnref::operator-> returns BlockRef<T>*. This prevents use of |
| 198 | * skAutoTUnrefInstance->ref() and skAutoTUnrefInstance->unref(). |
| 199 | */ |
| 200 | BlockRefType *operator->() const { |
| 201 | return static_cast<BlockRefType*>(fObj); |
| 202 | } |
| 203 | operator T*() { return fObj; } |
bsalomon@google.com | 1448cf8 | 2012-07-27 13:27:35 +0000 | [diff] [blame] | 204 | |
| 205 | private: |
bungeman@google.com | 9120892 | 2012-07-30 15:03:59 +0000 | [diff] [blame] | 206 | T* fObj; |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | class SkAutoUnref : public SkAutoTUnref<SkRefCnt> { |
| 210 | public: |
| 211 | SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 212 | }; |
| 213 | |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 214 | class SkAutoRef : SkNoncopyable { |
| 215 | public: |
| 216 | SkAutoRef(SkRefCnt* obj) : fObj(obj) { SkSafeRef(obj); } |
| 217 | ~SkAutoRef() { SkSafeUnref(fObj); } |
| 218 | private: |
| 219 | SkRefCnt* fObj; |
| 220 | }; |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 221 | |
reed@android.com | 756f6dd | 2010-04-16 20:56:00 +0000 | [diff] [blame] | 222 | /** Wrapper class for SkRefCnt pointers. This manages ref/unref of a pointer to |
| 223 | a SkRefCnt (or subclass) object. |
| 224 | */ |
| 225 | template <typename T> class SkRefPtr { |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 226 | public: |
| 227 | SkRefPtr() : fObj(NULL) {} |
| 228 | SkRefPtr(T* obj) : fObj(obj) { SkSafeRef(fObj); } |
| 229 | SkRefPtr(const SkRefPtr& o) : fObj(o.fObj) { SkSafeRef(fObj); } |
| 230 | ~SkRefPtr() { SkSafeUnref(fObj); } |
| 231 | |
| 232 | SkRefPtr& operator=(const SkRefPtr& rp) { |
| 233 | SkRefCnt_SafeAssign(fObj, rp.fObj); |
| 234 | return *this; |
| 235 | } |
| 236 | SkRefPtr& operator=(T* obj) { |
| 237 | SkRefCnt_SafeAssign(fObj, obj); |
| 238 | return *this; |
| 239 | } |
| 240 | |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 241 | T* get() const { return fObj; } |
| 242 | T& operator*() const { return *fObj; } |
| 243 | T* operator->() const { return fObj; } |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 244 | |
| 245 | typedef T* SkRefPtr::*unspecified_bool_type; |
reed@google.com | ddbf4c8 | 2011-03-08 16:06:06 +0000 | [diff] [blame] | 246 | operator unspecified_bool_type() const { |
| 247 | return fObj ? &SkRefPtr::fObj : NULL; |
| 248 | } |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 249 | |
| 250 | private: |
| 251 | T* fObj; |
| 252 | }; |
| 253 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 254 | #endif |
| 255 | |