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 | |
commit-bot@chromium.org | 6d2533e | 2013-10-16 15:15:58 +0000 | [diff] [blame] | 17 | /** \class SkRefCntBase |
| 18 | |
bungeman@google.com | 10ba006 | 2013-10-25 18:40:24 +0000 | [diff] [blame] | 19 | SkRefCntBase 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 | */ |
bungeman@google.com | 10ba006 | 2013-10-25 18:40:24 +0000 | [diff] [blame] | 27 | class SK_API SkRefCntBase : public SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | public: |
bungeman@google.com | 10ba006 | 2013-10-25 18:40:24 +0000 | [diff] [blame] | 29 | SK_DECLARE_INST_COUNT_ROOT(SkRefCntBase) |
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 | */ |
bungeman@google.com | 10ba006 | 2013-10-25 18:40:24 +0000 | [diff] [blame] | 33 | SkRefCntBase() : fRefCnt(1) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | */ |
bungeman@google.com | 10ba006 | 2013-10-25 18:40:24 +0000 | [diff] [blame] | 37 | virtual ~SkRefCntBase() { |
reed@google.com | 4c888aa | 2011-09-12 19:54:12 +0000 | [diff] [blame] | 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 | |
bungeman@google.com | f64c684 | 2013-07-19 23:18:52 +0000 | [diff] [blame] | 44 | /** Return the reference count. Use only for debugging. */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | int32_t getRefCnt() const { return fRefCnt; } |
| 46 | |
bungeman@google.com | f64c684 | 2013-07-19 23:18:52 +0000 | [diff] [blame] | 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) { |
bungeman@google.com | d9947f6 | 2013-12-18 15:27:39 +0000 | [diff] [blame^] | 53 | // Acquire barrier (L/SL), if not provided by load of fRefCnt. |
bungeman@google.com | f64c684 | 2013-07-19 23:18:52 +0000 | [diff] [blame] | 54 | // Prevents user's 'unique' code from happening before decrements. |
| 55 | //TODO: issue the barrier. |
| 56 | } |
| 57 | return unique; |
| 58 | } |
| 59 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | /** Increment the reference count. Must be balanced by a call to unref(). |
| 61 | */ |
| 62 | void ref() const { |
| 63 | SkASSERT(fRefCnt > 0); |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 64 | sk_atomic_inc(&fRefCnt); // No barrier required. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** 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] | 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. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | */ |
| 71 | void unref() const { |
| 72 | SkASSERT(fRefCnt > 0); |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 73 | // Release barrier (SL/S), if not provided below. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | if (sk_atomic_dec(&fRefCnt) == 1) { |
bungeman@google.com | d9947f6 | 2013-12-18 15:27:39 +0000 | [diff] [blame^] | 75 | // Acquire barrier (L/SL), if not provided above. |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 76 | // Prevents code in dispose from happening before the decrement. |
bungeman@google.com | d9947f6 | 2013-12-18 15:27:39 +0000 | [diff] [blame^] | 77 | sk_membar_acquire__after_atomic_dec(); |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 78 | internal_dispose(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 82 | #ifdef SK_DEBUG |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 83 | void validate() const { |
| 84 | SkASSERT(fRefCnt > 0); |
| 85 | } |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 86 | #endif |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 87 | |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame] | 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 { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 96 | #ifdef SK_DEBUG |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame] | 97 | SkASSERT(0 == fRefCnt); |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 98 | fRefCnt = 1; |
| 99 | #endif |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame] | 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(); |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 108 | SkDELETE(this); |
| 109 | } |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 110 | |
bungeman@google.com | f64c684 | 2013-07-19 23:18:52 +0000 | [diff] [blame] | 111 | // The following friends are those which override internal_dispose() |
| 112 | // and conditionally call SkRefCnt::internal_dispose(). |
| 113 | friend class GrTexture; |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 114 | friend class SkWeakRefCnt; |
| 115 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 116 | mutable int32_t fRefCnt; |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 117 | |
bungeman@google.com | 10ba006 | 2013-10-25 18:40:24 +0000 | [diff] [blame] | 118 | typedef SkNoncopyable INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
bungeman@google.com | 10ba006 | 2013-10-25 18:40:24 +0000 | [diff] [blame] | 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 | |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 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 | |
bungeman@google.com | 1fd201b | 2012-08-22 18:56:56 +0000 | [diff] [blame] | 143 | /** 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] | 144 | */ |
bungeman@google.com | 1fd201b | 2012-08-22 18:56:56 +0000 | [diff] [blame] | 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) { |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 154 | if (obj) { |
| 155 | obj->ref(); |
| 156 | } |
bungeman@google.com | 1fd201b | 2012-08-22 18:56:56 +0000 | [diff] [blame] | 157 | return obj; |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 168 | template<typename T> static inline void SkSafeSetNull(T*& obj) { |
| 169 | if (NULL != obj) { |
| 170 | obj->unref(); |
| 171 | obj = NULL; |
| 172 | } |
| 173 | } |
| 174 | |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 175 | /////////////////////////////////////////////////////////////////////////////// |
| 176 | |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 177 | /** |
bungeman@google.com | 9120892 | 2012-07-30 15:03:59 +0000 | [diff] [blame] | 178 | * Utility class that simply unref's its argument in the destructor. |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 179 | */ |
bungeman@google.com | 9120892 | 2012-07-30 15:03:59 +0000 | [diff] [blame] | 180 | template <typename T> class SkAutoTUnref : SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 181 | public: |
bungeman@google.com | 9120892 | 2012-07-30 15:03:59 +0000 | [diff] [blame] | 182 | explicit SkAutoTUnref(T* obj = NULL) : fObj(obj) {} |
| 183 | ~SkAutoTUnref() { SkSafeUnref(fObj); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 184 | |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 185 | T* get() const { return fObj; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 186 | |
reed@google.com | 2863f08 | 2013-03-13 21:28:44 +0000 | [diff] [blame] | 187 | T* reset(T* obj) { |
reed@google.com | 68d6bb0 | 2012-11-27 18:37:52 +0000 | [diff] [blame] | 188 | SkSafeUnref(fObj); |
| 189 | fObj = obj; |
reed@google.com | 2863f08 | 2013-03-13 21:28:44 +0000 | [diff] [blame] | 190 | return obj; |
bsalomon@google.com | a44f700 | 2011-08-09 15:30:41 +0000 | [diff] [blame] | 191 | } |
| 192 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 193 | void swap(SkAutoTUnref* other) { |
| 194 | T* tmp = fObj; |
| 195 | fObj = other->fObj; |
| 196 | other->fObj = tmp; |
| 197 | } |
| 198 | |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 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 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 210 | |
bungeman@google.com | e70f798 | 2012-06-01 19:38:19 +0000 | [diff] [blame] | 211 | /** |
bungeman@google.com | 6f4cf2a | 2013-04-16 15:24:31 +0000 | [diff] [blame] | 212 | * BlockRef<B> is a type which inherits from B, cannot be created, |
| 213 | * cannot be deleted, and makes ref and unref private. |
bungeman@google.com | e70f798 | 2012-06-01 19:38:19 +0000 | [diff] [blame] | 214 | */ |
bungeman@google.com | 0464029 | 2012-06-01 19:47:51 +0000 | [diff] [blame] | 215 | template<typename B> class BlockRef : public B { |
bungeman@google.com | e70f798 | 2012-06-01 19:38:19 +0000 | [diff] [blame] | 216 | private: |
| 217 | BlockRef(); |
bungeman@google.com | 6f4cf2a | 2013-04-16 15:24:31 +0000 | [diff] [blame] | 218 | ~BlockRef(); |
bungeman@google.com | e70f798 | 2012-06-01 19:38:19 +0000 | [diff] [blame] | 219 | void ref() const; |
| 220 | void unref() const; |
| 221 | }; |
bsalomon@google.com | 1448cf8 | 2012-07-27 13:27:35 +0000 | [diff] [blame] | 222 | |
bungeman@google.com | 9120892 | 2012-07-30 15:03:59 +0000 | [diff] [blame] | 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; } |
bsalomon@google.com | 1448cf8 | 2012-07-27 13:27:35 +0000 | [diff] [blame] | 236 | |
| 237 | private: |
bungeman@google.com | 9120892 | 2012-07-30 15:03:59 +0000 | [diff] [blame] | 238 | T* fObj; |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 239 | }; |
commit-bot@chromium.org | e61a86c | 2013-11-18 16:03:59 +0000 | [diff] [blame] | 240 | // Can't use the #define trick below to guard a bare SkAutoTUnref(...) because it's templated. :( |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 241 | |
| 242 | class SkAutoUnref : public SkAutoTUnref<SkRefCnt> { |
| 243 | public: |
| 244 | SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 245 | }; |
commit-bot@chromium.org | e61a86c | 2013-11-18 16:03:59 +0000 | [diff] [blame] | 246 | #define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 247 | |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 248 | class SkAutoRef : SkNoncopyable { |
| 249 | public: |
| 250 | SkAutoRef(SkRefCnt* obj) : fObj(obj) { SkSafeRef(obj); } |
| 251 | ~SkAutoRef() { SkSafeUnref(fObj); } |
| 252 | private: |
| 253 | SkRefCnt* fObj; |
| 254 | }; |
commit-bot@chromium.org | e61a86c | 2013-11-18 16:03:59 +0000 | [diff] [blame] | 255 | #define SkAutoRef(...) SK_REQUIRE_LOCAL_VAR(SkAutoRef) |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 256 | |
robertphillips@google.com | 2b4e544 | 2013-01-07 00:58:00 +0000 | [diff] [blame] | 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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 289 | #endif |