| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #ifndef SkRefCnt_DEFINED | 10 #ifndef SkRefCnt_DEFINED |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 SK_DECLARE_INST_COUNT_ROOT(SkRefCntBase) | 30 SK_DECLARE_INST_COUNT_ROOT(SkRefCntBase) |
| 31 | 31 |
| 32 /** Default construct, initializing the reference count to 1. | 32 /** Default construct, initializing the reference count to 1. |
| 33 */ | 33 */ |
| 34 SkRefCntBase() : fRefCnt(1) {} | 34 SkRefCntBase() : fRefCnt(1) {} |
| 35 | 35 |
| 36 /** Destruct, asserting that the reference count is 1. | 36 /** Destruct, asserting that the reference count is 1. |
| 37 */ | 37 */ |
| 38 virtual ~SkRefCntBase() { | 38 virtual ~SkRefCntBase() { |
| 39 #ifdef SK_DEBUG | 39 #ifdef SK_DEBUG |
| 40 SkASSERT(this->unique()); | 40 SkASSERT(fRefCnt == 1); |
| 41 fRefCnt = 0; // illegal value, to catch us if we reuse after delete | 41 fRefCnt = 0; // illegal value, to catch us if we reuse after delete |
| 42 #endif | 42 #endif |
| 43 } | 43 } |
| 44 | 44 |
| 45 /** Return the reference count. Use only for debugging. */ | 45 /** Return the reference count. Use only for debugging. */ |
| 46 int32_t getRefCnt() const { return fRefCnt; } | 46 int32_t getRefCnt() const { return fRefCnt; } |
| 47 | 47 |
| 48 /** May return true if the caller is the only owner. | 48 /** May return true if the caller is the only owner. |
| 49 * Ensures that all previous owner's actions are complete. | 49 * Ensures that all previous owner's actions are complete. |
| 50 */ | 50 */ |
| 51 bool unique() const { | 51 bool unique() const { |
| 52 // We believe we're reading fRefCnt in a safe way here, so we stifle the
TSAN warning about | 52 // We believe we're reading fRefCnt in a safe way here, so we stifle the
TSAN warning about |
| 53 // an unproctected read. Generally, don't read fRefCnt, and don't stifl
e this warning. | 53 // an unproctected read. Generally, don't read fRefCnt, and don't stifl
e this warning. |
| 54 bool const unique = (1 == SK_ANNOTATE_UNPROTECTED_READ(fRefCnt)); | 54 bool const unique = (1 == SK_ANNOTATE_UNPROTECTED_READ(fRefCnt)); |
| 55 if (unique) { | 55 if (unique) { |
| 56 SK_ANNOTATE_HAPPENS_AFTER(this); | |
| 57 // Acquire barrier (L/SL), if not provided by load of fRefCnt. | 56 // Acquire barrier (L/SL), if not provided by load of fRefCnt. |
| 58 // Prevents user's 'unique' code from happening before decrements. | 57 // Prevents user's 'unique' code from happening before decrements. |
| 59 //TODO: issue the barrier. | 58 //TODO: issue the barrier. |
| 60 } | 59 } |
| 61 return unique; | 60 return unique; |
| 62 } | 61 } |
| 63 | 62 |
| 64 /** Increment the reference count. Must be balanced by a call to unref(). | 63 /** Increment the reference count. Must be balanced by a call to unref(). |
| 65 */ | 64 */ |
| 66 void ref() const { | 65 void ref() const { |
| 67 SkASSERT(this->unsafeGetRefCnt() > 0); | 66 SkASSERT(fRefCnt > 0); |
| 68 sk_atomic_inc(&fRefCnt); // No barrier required. | 67 sk_atomic_inc(&fRefCnt); // No barrier required. |
| 69 } | 68 } |
| 70 | 69 |
| 71 /** 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 |
| 72 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 |
| 73 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. |
| 74 */ | 73 */ |
| 75 void unref() const { | 74 void unref() const { |
| 76 SkASSERT(this->unsafeGetRefCnt() > 0); | 75 SkASSERT(fRefCnt > 0); |
| 77 SK_ANNOTATE_HAPPENS_BEFORE(this); | |
| 78 // Release barrier (SL/S), if not provided below. | 76 // Release barrier (SL/S), if not provided below. |
| 79 if (sk_atomic_dec(&fRefCnt) == 1) { | 77 if (sk_atomic_dec(&fRefCnt) == 1) { |
| 80 SK_ANNOTATE_HAPPENS_AFTER(this); | |
| 81 // Acquire barrier (L/SL), if not provided above. | 78 // Acquire barrier (L/SL), if not provided above. |
| 82 // Prevents code in dispose from happening before the decrement. | 79 // Prevents code in dispose from happening before the decrement. |
| 83 sk_membar_acquire__after_atomic_dec(); | 80 sk_membar_acquire__after_atomic_dec(); |
| 84 internal_dispose(); | 81 internal_dispose(); |
| 85 } | 82 } |
| 86 } | 83 } |
| 87 | 84 |
| 88 #ifdef SK_DEBUG | 85 #ifdef SK_DEBUG |
| 89 void validate() const { | 86 void validate() const { |
| 90 SkASSERT(this->unsafeGetRefCnt() > 0); | 87 SkASSERT(fRefCnt > 0); |
| 91 } | 88 } |
| 92 #endif | 89 #endif |
| 93 | 90 |
| 94 protected: | 91 protected: |
| 95 /** | 92 /** |
| 96 * Allow subclasses to call this if they've overridden internal_dispose | 93 * Allow subclasses to call this if they've overridden internal_dispose |
| 97 * so they can reset fRefCnt before the destructor is called. Should only | 94 * so they can reset fRefCnt before the destructor is called. Should only |
| 98 * be called right before calling through to inherited internal_dispose() | 95 * be called right before calling through to inherited internal_dispose() |
| 99 * or before calling the destructor. | 96 * or before calling the destructor. |
| 100 */ | 97 */ |
| 101 void internal_dispose_restore_refcnt_to_1() const { | 98 void internal_dispose_restore_refcnt_to_1() const { |
| 102 #ifdef SK_DEBUG | 99 #ifdef SK_DEBUG |
| 103 SkASSERT(0 == fRefCnt); | 100 SkASSERT(0 == fRefCnt); |
| 104 fRefCnt = 1; | 101 fRefCnt = 1; |
| 105 #endif | 102 #endif |
| 106 } | 103 } |
| 107 | 104 |
| 108 private: | 105 private: |
| 109 // OK for use in asserts, but not much else. | |
| 110 int32_t unsafeGetRefCnt() { return SK_ANNOTATE_UNPROTECTED_READ(fRefCnt); } | |
| 111 | |
| 112 /** | 106 /** |
| 113 * Called when the ref count goes to 0. | 107 * Called when the ref count goes to 0. |
| 114 */ | 108 */ |
| 115 virtual void internal_dispose() const { | 109 virtual void internal_dispose() const { |
| 116 this->internal_dispose_restore_refcnt_to_1(); | 110 this->internal_dispose_restore_refcnt_to_1(); |
| 117 SkDELETE(this); | 111 SkDELETE(this); |
| 118 } | 112 } |
| 119 | 113 |
| 120 // The following friends are those which override internal_dispose() | 114 // The following friends are those which override internal_dispose() |
| 121 // and conditionally call SkRefCnt::internal_dispose(). | 115 // and conditionally call SkRefCnt::internal_dispose(). |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 typedef T* SkRefPtr::*unspecified_bool_type; | 283 typedef T* SkRefPtr::*unspecified_bool_type; |
| 290 operator unspecified_bool_type() const { | 284 operator unspecified_bool_type() const { |
| 291 return fObj ? &SkRefPtr::fObj : NULL; | 285 return fObj ? &SkRefPtr::fObj : NULL; |
| 292 } | 286 } |
| 293 | 287 |
| 294 private: | 288 private: |
| 295 T* fObj; | 289 T* fObj; |
| 296 }; | 290 }; |
| 297 | 291 |
| 298 #endif | 292 #endif |
| OLD | NEW |