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 | |
commit-bot@chromium.org | ea6e14a | 2014-02-04 18:00:23 +0000 | [diff] [blame] | 13 | #include "SkDynamicAnnotations.h" |
commit-bot@chromium.org | 86b0de4 | 2014-05-28 20:02:17 +0000 | [diff] [blame] | 14 | #include "SkThread.h" |
robertphillips@google.com | 977b9c8 | 2012-06-05 19:35:09 +0000 | [diff] [blame] | 15 | #include "SkInstCnt.h" |
bungeman@google.com | 9120892 | 2012-07-30 15:03:59 +0000 | [diff] [blame] | 16 | #include "SkTemplates.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | |
commit-bot@chromium.org | 6d2533e | 2013-10-16 15:15:58 +0000 | [diff] [blame] | 18 | /** \class SkRefCntBase |
| 19 | |
bungeman@google.com | 10ba006 | 2013-10-25 18:40:24 +0000 | [diff] [blame] | 20 | 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] | 21 | objects. When an existing owner wants to share a reference, it calls ref(). |
| 22 | When an owner wants to release its reference, it calls unref(). When the |
| 23 | shared object's reference count goes to zero as the result of an unref() |
| 24 | call, its (virtual) destructor is called. It is an error for the |
| 25 | destructor to be called explicitly (or via the object going out of scope on |
| 26 | the stack or calling delete) if getRefCnt() > 1. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | */ |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 28 | class SK_API SkRefCntBase : SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | public: |
bungeman@google.com | 10ba006 | 2013-10-25 18:40:24 +0000 | [diff] [blame] | 30 | SK_DECLARE_INST_COUNT_ROOT(SkRefCntBase) |
robertphillips@google.com | 977b9c8 | 2012-06-05 19:35:09 +0000 | [diff] [blame] | 31 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | /** Default construct, initializing the reference count to 1. |
| 33 | */ |
bungeman@google.com | 10ba006 | 2013-10-25 18:40:24 +0000 | [diff] [blame] | 34 | SkRefCntBase() : fRefCnt(1) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 36 | /** Destruct, asserting that the reference count is 1. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | */ |
bungeman@google.com | 10ba006 | 2013-10-25 18:40:24 +0000 | [diff] [blame] | 38 | virtual ~SkRefCntBase() { |
reed@google.com | 4c888aa | 2011-09-12 19:54:12 +0000 | [diff] [blame] | 39 | #ifdef SK_DEBUG |
mtklein | b59161f | 2014-06-18 07:54:47 -0700 | [diff] [blame] | 40 | SkASSERTF(fRefCnt == 1, "fRefCnt was %d", fRefCnt); |
reed@google.com | 4c888aa | 2011-09-12 19:54:12 +0000 | [diff] [blame] | 41 | fRefCnt = 0; // illegal value, to catch us if we reuse after delete |
| 42 | #endif |
| 43 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | |
Mike Klein | fa14113 | 2014-11-24 16:26:15 -0500 | [diff] [blame] | 45 | //#ifdef SK_DEBUG |
bungeman@google.com | f64c684 | 2013-07-19 23:18:52 +0000 | [diff] [blame] | 46 | /** Return the reference count. Use only for debugging. */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | int32_t getRefCnt() const { return fRefCnt; } |
Mike Klein | fa14113 | 2014-11-24 16:26:15 -0500 | [diff] [blame] | 48 | //#endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | |
commit-bot@chromium.org | ea6e14a | 2014-02-04 18:00:23 +0000 | [diff] [blame] | 50 | /** May return true if the caller is the only owner. |
bungeman@google.com | f64c684 | 2013-07-19 23:18:52 +0000 | [diff] [blame] | 51 | * Ensures that all previous owner's actions are complete. |
| 52 | */ |
| 53 | bool unique() const { |
commit-bot@chromium.org | ea6e14a | 2014-02-04 18:00:23 +0000 | [diff] [blame] | 54 | // We believe we're reading fRefCnt in a safe way here, so we stifle the TSAN warning about |
| 55 | // an unproctected read. Generally, don't read fRefCnt, and don't stifle this warning. |
bungeman | a5cf665 | 2014-10-29 12:31:33 -0700 | [diff] [blame] | 56 | bool const unique = (1 == sk_acquire_load(&fRefCnt)); |
bungeman@google.com | f64c684 | 2013-07-19 23:18:52 +0000 | [diff] [blame] | 57 | if (unique) { |
bungeman@google.com | d9947f6 | 2013-12-18 15:27:39 +0000 | [diff] [blame] | 58 | // Acquire barrier (L/SL), if not provided by load of fRefCnt. |
bungeman@google.com | f64c684 | 2013-07-19 23:18:52 +0000 | [diff] [blame] | 59 | // Prevents user's 'unique' code from happening before decrements. |
bungeman | a5cf665 | 2014-10-29 12:31:33 -0700 | [diff] [blame] | 60 | //TODO: issue the barrier only when unique is true |
bungeman@google.com | f64c684 | 2013-07-19 23:18:52 +0000 | [diff] [blame] | 61 | } |
| 62 | return unique; |
| 63 | } |
| 64 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | /** Increment the reference count. Must be balanced by a call to unref(). |
| 66 | */ |
| 67 | void ref() const { |
commit-bot@chromium.org | f672cea | 2014-04-27 19:21:51 +0000 | [diff] [blame] | 68 | SkASSERT(fRefCnt > 0); |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 69 | sk_atomic_inc(&fRefCnt); // No barrier required. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** 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] | 73 | decrement, then delete the object. Note that if this is the case, then |
| 74 | 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] | 75 | */ |
| 76 | void unref() const { |
commit-bot@chromium.org | f672cea | 2014-04-27 19:21:51 +0000 | [diff] [blame] | 77 | SkASSERT(fRefCnt > 0); |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 78 | // Release barrier (SL/S), if not provided below. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | if (sk_atomic_dec(&fRefCnt) == 1) { |
bungeman@google.com | d9947f6 | 2013-12-18 15:27:39 +0000 | [diff] [blame] | 80 | // Acquire barrier (L/SL), if not provided above. |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 81 | // Prevents code in dispose from happening before the decrement. |
bungeman@google.com | d9947f6 | 2013-12-18 15:27:39 +0000 | [diff] [blame] | 82 | sk_membar_acquire__after_atomic_dec(); |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 83 | internal_dispose(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 87 | #ifdef SK_DEBUG |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 88 | void validate() const { |
commit-bot@chromium.org | f672cea | 2014-04-27 19:21:51 +0000 | [diff] [blame] | 89 | SkASSERT(fRefCnt > 0); |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 90 | } |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 91 | #endif |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 92 | |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame] | 93 | protected: |
| 94 | /** |
| 95 | * Allow subclasses to call this if they've overridden internal_dispose |
| 96 | * so they can reset fRefCnt before the destructor is called. Should only |
| 97 | * be called right before calling through to inherited internal_dispose() |
| 98 | * or before calling the destructor. |
| 99 | */ |
| 100 | void internal_dispose_restore_refcnt_to_1() const { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 101 | #ifdef SK_DEBUG |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame] | 102 | SkASSERT(0 == fRefCnt); |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 103 | fRefCnt = 1; |
| 104 | #endif |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | private: |
| 108 | /** |
| 109 | * Called when the ref count goes to 0. |
| 110 | */ |
| 111 | virtual void internal_dispose() const { |
| 112 | this->internal_dispose_restore_refcnt_to_1(); |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 113 | SkDELETE(this); |
| 114 | } |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 115 | |
bungeman@google.com | f64c684 | 2013-07-19 23:18:52 +0000 | [diff] [blame] | 116 | // The following friends are those which override internal_dispose() |
| 117 | // and conditionally call SkRefCnt::internal_dispose(). |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 118 | friend class SkWeakRefCnt; |
| 119 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 120 | mutable int32_t fRefCnt; |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 121 | |
bungeman@google.com | 10ba006 | 2013-10-25 18:40:24 +0000 | [diff] [blame] | 122 | typedef SkNoncopyable INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
bungeman@google.com | 10ba006 | 2013-10-25 18:40:24 +0000 | [diff] [blame] | 125 | #ifdef SK_REF_CNT_MIXIN_INCLUDE |
| 126 | // It is the responsibility of the following include to define the type SkRefCnt. |
| 127 | // This SkRefCnt should normally derive from SkRefCntBase. |
| 128 | #include SK_REF_CNT_MIXIN_INCLUDE |
| 129 | #else |
| 130 | class SK_API SkRefCnt : public SkRefCntBase { }; |
| 131 | #endif |
| 132 | |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 133 | /////////////////////////////////////////////////////////////////////////////// |
| 134 | |
| 135 | /** Helper macro to safely assign one SkRefCnt[TS]* to another, checking for |
| 136 | null in on each side of the assignment, and ensuring that ref() is called |
| 137 | before unref(), in case the two pointers point to the same object. |
| 138 | */ |
| 139 | #define SkRefCnt_SafeAssign(dst, src) \ |
| 140 | do { \ |
| 141 | if (src) src->ref(); \ |
| 142 | if (dst) dst->unref(); \ |
| 143 | dst = src; \ |
| 144 | } while (0) |
| 145 | |
| 146 | |
bungeman@google.com | 1fd201b | 2012-08-22 18:56:56 +0000 | [diff] [blame] | 147 | /** 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] | 148 | */ |
bungeman@google.com | 1fd201b | 2012-08-22 18:56:56 +0000 | [diff] [blame] | 149 | template <typename T> static inline T* SkRef(T* obj) { |
| 150 | SkASSERT(obj); |
| 151 | obj->ref(); |
| 152 | return obj; |
| 153 | } |
| 154 | |
| 155 | /** Check if the argument is non-null, and if so, call obj->ref() and return obj. |
| 156 | */ |
| 157 | template <typename T> static inline T* SkSafeRef(T* obj) { |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 158 | if (obj) { |
| 159 | obj->ref(); |
| 160 | } |
bungeman@google.com | 1fd201b | 2012-08-22 18:56:56 +0000 | [diff] [blame] | 161 | return obj; |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** Check if the argument is non-null, and if so, call obj->unref() |
| 165 | */ |
| 166 | template <typename T> static inline void SkSafeUnref(T* obj) { |
| 167 | if (obj) { |
| 168 | obj->unref(); |
| 169 | } |
| 170 | } |
| 171 | |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 172 | template<typename T> static inline void SkSafeSetNull(T*& obj) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 173 | if (obj) { |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 174 | obj->unref(); |
| 175 | obj = NULL; |
| 176 | } |
| 177 | } |
| 178 | |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 179 | /////////////////////////////////////////////////////////////////////////////// |
| 180 | |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 181 | /** |
bungeman@google.com | 9120892 | 2012-07-30 15:03:59 +0000 | [diff] [blame] | 182 | * Utility class that simply unref's its argument in the destructor. |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 183 | */ |
bungeman@google.com | 9120892 | 2012-07-30 15:03:59 +0000 | [diff] [blame] | 184 | template <typename T> class SkAutoTUnref : SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 185 | public: |
bungeman@google.com | 9120892 | 2012-07-30 15:03:59 +0000 | [diff] [blame] | 186 | explicit SkAutoTUnref(T* obj = NULL) : fObj(obj) {} |
| 187 | ~SkAutoTUnref() { SkSafeUnref(fObj); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 188 | |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 189 | T* get() const { return fObj; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 190 | |
reed@google.com | 2863f08 | 2013-03-13 21:28:44 +0000 | [diff] [blame] | 191 | T* reset(T* obj) { |
reed@google.com | 68d6bb0 | 2012-11-27 18:37:52 +0000 | [diff] [blame] | 192 | SkSafeUnref(fObj); |
| 193 | fObj = obj; |
reed@google.com | 2863f08 | 2013-03-13 21:28:44 +0000 | [diff] [blame] | 194 | return obj; |
bsalomon@google.com | a44f700 | 2011-08-09 15:30:41 +0000 | [diff] [blame] | 195 | } |
| 196 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 197 | void swap(SkAutoTUnref* other) { |
| 198 | T* tmp = fObj; |
| 199 | fObj = other->fObj; |
| 200 | other->fObj = tmp; |
| 201 | } |
| 202 | |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 203 | /** |
| 204 | * Return the hosted object (which may be null), transferring ownership. |
| 205 | * The reference count is not modified, and the internal ptr is set to NULL |
| 206 | * so unref() will not be called in our destructor. A subsequent call to |
| 207 | * detach() will do nothing and return null. |
| 208 | */ |
| 209 | T* detach() { |
| 210 | T* obj = fObj; |
| 211 | fObj = NULL; |
| 212 | return obj; |
| 213 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 214 | |
bungeman@google.com | e70f798 | 2012-06-01 19:38:19 +0000 | [diff] [blame] | 215 | /** |
bungeman@google.com | 6f4cf2a | 2013-04-16 15:24:31 +0000 | [diff] [blame] | 216 | * BlockRef<B> is a type which inherits from B, cannot be created, |
| 217 | * cannot be deleted, and makes ref and unref private. |
bungeman@google.com | e70f798 | 2012-06-01 19:38:19 +0000 | [diff] [blame] | 218 | */ |
bungeman@google.com | 0464029 | 2012-06-01 19:47:51 +0000 | [diff] [blame] | 219 | template<typename B> class BlockRef : public B { |
bungeman@google.com | e70f798 | 2012-06-01 19:38:19 +0000 | [diff] [blame] | 220 | private: |
| 221 | BlockRef(); |
bungeman@google.com | 6f4cf2a | 2013-04-16 15:24:31 +0000 | [diff] [blame] | 222 | ~BlockRef(); |
bungeman@google.com | e70f798 | 2012-06-01 19:38:19 +0000 | [diff] [blame] | 223 | void ref() const; |
| 224 | void unref() const; |
| 225 | }; |
bsalomon@google.com | 1448cf8 | 2012-07-27 13:27:35 +0000 | [diff] [blame] | 226 | |
bungeman@google.com | 9120892 | 2012-07-30 15:03:59 +0000 | [diff] [blame] | 227 | /** If T is const, the type returned from operator-> will also be const. */ |
| 228 | typedef typename SkTConstType<BlockRef<T>, SkTIsConst<T>::value>::type BlockRefType; |
| 229 | |
| 230 | /** |
| 231 | * SkAutoTUnref assumes ownership of the ref. As a result, it is an error |
| 232 | * for the user to ref or unref through SkAutoTUnref. Therefore |
| 233 | * SkAutoTUnref::operator-> returns BlockRef<T>*. This prevents use of |
| 234 | * skAutoTUnrefInstance->ref() and skAutoTUnrefInstance->unref(). |
| 235 | */ |
| 236 | BlockRefType *operator->() const { |
| 237 | return static_cast<BlockRefType*>(fObj); |
| 238 | } |
bungeman | d71b757 | 2014-09-18 10:55:32 -0700 | [diff] [blame] | 239 | operator T*() const { return fObj; } |
bsalomon@google.com | 1448cf8 | 2012-07-27 13:27:35 +0000 | [diff] [blame] | 240 | |
| 241 | private: |
bungeman@google.com | 9120892 | 2012-07-30 15:03:59 +0000 | [diff] [blame] | 242 | T* fObj; |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 243 | }; |
commit-bot@chromium.org | e61a86c | 2013-11-18 16:03:59 +0000 | [diff] [blame] | 244 | // 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] | 245 | |
| 246 | class SkAutoUnref : public SkAutoTUnref<SkRefCnt> { |
| 247 | public: |
| 248 | SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 249 | }; |
commit-bot@chromium.org | e61a86c | 2013-11-18 16:03:59 +0000 | [diff] [blame] | 250 | #define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 251 | |
mtklein | 08d1fcc | 2014-11-20 09:18:31 -0800 | [diff] [blame] | 252 | // This is a variant of SkRefCnt that's Not Virtual, so weighs 4 bytes instead of 8 or 16. |
| 253 | // There's only benefit to using this if the deriving class does not otherwise need a vtable. |
| 254 | template <typename Derived> |
| 255 | class SkNVRefCnt : SkNoncopyable { |
| 256 | public: |
| 257 | SkNVRefCnt() : fRefCnt(1) {} |
reed | 3c850c5 | 2014-11-24 14:13:55 -0800 | [diff] [blame^] | 258 | ~SkNVRefCnt() { SkASSERTF(1 == fRefCnt, "NVRefCnt was %d", fRefCnt); } |
mtklein | 08d1fcc | 2014-11-20 09:18:31 -0800 | [diff] [blame] | 259 | |
| 260 | // Implementation is pretty much the same as SkRefCntBase. All required barriers are the same: |
| 261 | // - unique() needs acquire when it returns true, and no barrier if it returns false; |
| 262 | // - ref() doesn't need any barrier; |
| 263 | // - unref() needs a release barrier, and an acquire if it's going to call delete. |
| 264 | |
| 265 | bool unique() const { return 1 == sk_acquire_load(&fRefCnt); } |
| 266 | void ref() const { sk_atomic_inc(&fRefCnt); } |
reed | 90d0ff0 | 2014-11-24 12:02:31 -0800 | [diff] [blame] | 267 | void unref() const { |
| 268 | int32_t prevValue = sk_atomic_dec(&fRefCnt); |
| 269 | SkASSERT(prevValue >= 1); |
| 270 | if (1 == prevValue) { |
reed | 3c850c5 | 2014-11-24 14:13:55 -0800 | [diff] [blame^] | 271 | SkDEBUGCODE(fRefCnt = 1;) // restore the 1 for our destructor's assert |
reed | 90d0ff0 | 2014-11-24 12:02:31 -0800 | [diff] [blame] | 272 | SkDELETE((const Derived*)this); |
| 273 | } |
| 274 | } |
mtklein | d6ab2a8 | 2014-11-20 10:07:54 -0800 | [diff] [blame] | 275 | void deref() const { this->unref(); } // Chrome prefers to call deref(). |
Florin Malita | 844aa33 | 2014-11-20 16:56:22 -0500 | [diff] [blame] | 276 | |
mtklein | 08d1fcc | 2014-11-20 09:18:31 -0800 | [diff] [blame] | 277 | private: |
| 278 | mutable int32_t fRefCnt; |
| 279 | }; |
| 280 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 281 | #endif |