Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkRefCnt_DEFINED | 8 #ifndef SkRefCnt_DEFINED |
| 9 #define SkRefCnt_DEFINED | 9 #define SkRefCnt_DEFINED |
| 10 | 10 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 public: | 25 public: |
| 26 /** Default construct, initializing the reference count to 1. | 26 /** Default construct, initializing the reference count to 1. |
| 27 */ | 27 */ |
| 28 SkRefCntBase() : fRefCnt(1) {} | 28 SkRefCntBase() : fRefCnt(1) {} |
| 29 | 29 |
| 30 /** Destruct, asserting that the reference count is 1. | 30 /** Destruct, asserting that the reference count is 1. |
| 31 */ | 31 */ |
| 32 virtual ~SkRefCntBase() { | 32 virtual ~SkRefCntBase() { |
| 33 #ifdef SK_DEBUG | 33 #ifdef SK_DEBUG |
| 34 SkASSERTF(fRefCnt == 1, "fRefCnt was %d", fRefCnt); | 34 SkASSERTF(fRefCnt == 1, "fRefCnt was %d", fRefCnt); |
| 35 fRefCnt = 0; // illegal value, to catch us if we reuse after delete | 35 fRefCnt = 0; // illegal value, to catch us if we reuse after delete |
|
bungeman-skia
2015/08/20 22:18:13
This check may have less weight now that we use va
bungeman-skia
2015/08/21 18:22:33
Changing SkRefCnt to support start at 1, 0 means d
| |
| 36 #endif | 36 #endif |
| 37 } | 37 } |
| 38 | 38 |
| 39 #ifdef SK_DEBUG | 39 #ifdef SK_DEBUG |
| 40 /** Return the reference count. Use only for debugging. */ | 40 /** Return the reference count. Use only for debugging. */ |
| 41 int32_t getRefCnt() const { return fRefCnt; } | 41 int32_t getRefCnt() const { return fRefCnt; } |
| 42 #endif | 42 #endif |
| 43 | 43 |
| 44 /** May return true if the caller is the only owner. | 44 /** May return true if the caller is the only owner. |
| 45 * Ensures that all previous owner's actions are complete. | 45 * Ensures that all previous owner's actions are complete. |
| 46 */ | 46 */ |
| 47 bool unique() const { | 47 bool unique() const { |
| 48 if (1 == sk_atomic_load(&fRefCnt, sk_memory_order_acquire)) { | 48 if (1 == sk_atomic_load(&fRefCnt, sk_memory_order_acquire)) { |
| 49 // The acquire barrier is only really needed if we return true. It | 49 // The acquire barrier is only really needed if we return true. It |
| 50 // prevents code conditioned on the result of unique() from running | 50 // prevents code conditioned on the result of unique() from running |
| 51 // until previous owners are all totally done calling unref(). | 51 // until previous owners are all totally done calling unref(). |
| 52 return true; | 52 return true; |
| 53 } | 53 } |
| 54 return false; | 54 return false; |
| 55 } | 55 } |
| 56 | 56 |
| 57 /** Increment the reference count. Must be balanced by a call to unref(). | 57 /** Increment the reference count. Must be balanced by a call to unref(). |
| 58 */ | 58 */ |
| 59 void ref() const { | 59 void ref() const { |
| 60 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK | |
| 61 // Android employs some special subclasses that enable the fRefCnt to | |
| 62 // go to zero, but not below, prior to reusing the object. | |
|
bungeman-skia
2015/08/21 21:44:35
So after much back and forth and looking into this
djsollen
2015/08/24 12:25:43
Done.
| |
| 63 SkASSERT(fRefCnt >= 0); | |
| 64 #else | |
| 60 SkASSERT(fRefCnt > 0); | 65 SkASSERT(fRefCnt > 0); |
| 66 #endif | |
| 61 (void)sk_atomic_fetch_add(&fRefCnt, +1, sk_memory_order_relaxed); // No barrier required. | 67 (void)sk_atomic_fetch_add(&fRefCnt, +1, sk_memory_order_relaxed); // No barrier required. |
| 62 } | 68 } |
| 63 | 69 |
| 64 /** Decrement the reference count. If the reference count is 1 before the | 70 /** Decrement the reference count. If the reference count is 1 before the |
| 65 decrement, then delete the object. Note that if this is the case, then | 71 decrement, then delete the object. Note that if this is the case, then |
| 66 the object needs to have been allocated via new, and not on the stack. | 72 the object needs to have been allocated via new, and not on the stack. |
| 67 */ | 73 */ |
| 68 void unref() const { | 74 void unref() const { |
| 69 SkASSERT(fRefCnt > 0); | 75 SkASSERT(fRefCnt > 0); |
| 70 // A release here acts in place of all releases we "should" have been do ing in ref(). | 76 // A release here acts in place of all releases we "should" have been do ing in ref(). |
| 71 if (1 == sk_atomic_fetch_add(&fRefCnt, -1, sk_memory_order_acq_rel)) { | 77 if (1 == sk_atomic_fetch_add(&fRefCnt, -1, sk_memory_order_acq_rel)) { |
|
bungeman-skia
2015/08/20 21:55:13
Not that I'm advocating this, but as a note to the
bungeman-skia
2015/08/21 18:22:33
Note that 'become_unique()' cannot work correctly,
| |
| 72 // Like unique(), the acquire is only needed on success, to make sur e | 78 // Like unique(), the acquire is only needed on success, to make sur e |
| 73 // code in internal_dispose() doesn't happen before the decrement. | 79 // code in internal_dispose() doesn't happen before the decrement. |
| 74 this->internal_dispose(); | 80 this->internal_dispose(); |
| 75 } | 81 } |
| 76 } | 82 } |
| 77 | 83 |
| 78 #ifdef SK_DEBUG | 84 #ifdef SK_DEBUG |
| 79 void validate() const { | 85 void validate() const { |
| 80 SkASSERT(fRefCnt > 0); | 86 SkASSERT(fRefCnt > 0); |
| 81 } | 87 } |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 238 SkDELETE((const Derived*)this); | 244 SkDELETE((const Derived*)this); |
| 239 } | 245 } |
| 240 } | 246 } |
| 241 void deref() const { this->unref(); } | 247 void deref() const { this->unref(); } |
| 242 | 248 |
| 243 private: | 249 private: |
| 244 mutable int32_t fRefCnt; | 250 mutable int32_t fRefCnt; |
| 245 }; | 251 }; |
| 246 | 252 |
| 247 #endif | 253 #endif |
| OLD | NEW |