| 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 23 matching lines...) Expand all Loading... |
| 34 | 34 |
| 35 /** Destruct, asserting that the reference count is 1. | 35 /** Destruct, asserting that the reference count is 1. |
| 36 */ | 36 */ |
| 37 virtual ~SkRefCnt() { | 37 virtual ~SkRefCnt() { |
| 38 #ifdef SK_DEBUG | 38 #ifdef SK_DEBUG |
| 39 SkASSERT(fRefCnt == 1); | 39 SkASSERT(fRefCnt == 1); |
| 40 fRefCnt = 0; // illegal value, to catch us if we reuse after delete | 40 fRefCnt = 0; // illegal value, to catch us if we reuse after delete |
| 41 #endif | 41 #endif |
| 42 } | 42 } |
| 43 | 43 |
| 44 /** Return the reference count. | 44 /** Return the reference count. Use only for debugging. */ |
| 45 */ | |
| 46 int32_t getRefCnt() const { return fRefCnt; } | 45 int32_t getRefCnt() const { return fRefCnt; } |
| 47 | 46 |
| 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) { |
| 53 // Aquire barrier (L/SL), if not provided by load of fRefCnt. |
| 54 // Prevents user's 'unique' code from happening before decrements. |
| 55 //TODO: issue the barrier. |
| 56 } |
| 57 return unique; |
| 58 } |
| 59 |
| 48 /** Increment the reference count. Must be balanced by a call to unref(). | 60 /** Increment the reference count. Must be balanced by a call to unref(). |
| 49 */ | 61 */ |
| 50 void ref() const { | 62 void ref() const { |
| 51 SkASSERT(fRefCnt > 0); | 63 SkASSERT(fRefCnt > 0); |
| 52 sk_atomic_inc(&fRefCnt); // No barrier required. | 64 sk_atomic_inc(&fRefCnt); // No barrier required. |
| 53 } | 65 } |
| 54 | 66 |
| 55 /** Decrement the reference count. If the reference count is 1 before the | 67 /** Decrement the reference count. If the reference count is 1 before the |
| 56 decrement, then delete the object. Note that if this is the case, then | 68 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. | 69 the object needs to have been allocated via new, and not on the stack. |
| 58 */ | 70 */ |
| 59 void unref() const { | 71 void unref() const { |
| 60 SkASSERT(fRefCnt > 0); | 72 SkASSERT(fRefCnt > 0); |
| 61 // Release barrier (SL/S), if not provided below. | 73 // Release barrier (SL/S), if not provided below. |
| 62 if (sk_atomic_dec(&fRefCnt) == 1) { | 74 if (sk_atomic_dec(&fRefCnt) == 1) { |
| 63 // Aquire barrier (L/SL), if not provided above. | 75 // Aquire barrier (L/SL), if not provided above. |
| 64 // Prevents code in dispose from happening before the decrement. | 76 // Prevents code in dispose from happening before the decrement. |
| 65 sk_membar_aquire__after_atomic_dec(); | 77 sk_membar_aquire__after_atomic_dec(); |
| 66 internal_dispose(); | 78 internal_dispose(); |
| 67 } | 79 } |
| 68 } | 80 } |
| 69 | 81 |
| 70 void validate() const { | 82 void validate() const { |
| 71 SkASSERT(fRefCnt > 0); | 83 SkASSERT(fRefCnt > 0); |
| 72 } | 84 } |
| 73 | 85 |
| 74 /** | 86 /** |
| 75 * Alias for ref(), for compatibility with scoped_refptr. | |
| 76 */ | |
| 77 void AddRef() { this->ref(); } | |
| 78 | |
| 79 /** | |
| 80 * Alias for unref(), for compatibility with scoped_refptr. | |
| 81 */ | |
| 82 void Release() { this->unref(); } | |
| 83 | |
| 84 /** | |
| 85 * Alias for unref(), for compatibility with WTF::RefPtr. | 87 * Alias for unref(), for compatibility with WTF::RefPtr. |
| 86 */ | 88 */ |
| 87 void deref() { this->unref(); } | 89 void deref() { this->unref(); } |
| 88 | 90 |
| 89 protected: | 91 protected: |
| 90 /** | 92 /** |
| 91 * Allow subclasses to call this if they've overridden internal_dispose | 93 * Allow subclasses to call this if they've overridden internal_dispose |
| 92 * 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 |
| 93 * be called right before calling through to inherited internal_dispose() | 95 * be called right before calling through to inherited internal_dispose() |
| 94 * or before calling the destructor. | 96 * or before calling the destructor. |
| 95 */ | 97 */ |
| 96 void internal_dispose_restore_refcnt_to_1() const { | 98 void internal_dispose_restore_refcnt_to_1() const { |
| 97 #ifdef SK_DEBUG | 99 #ifdef SK_DEBUG |
| 98 SkASSERT(0 == fRefCnt); | 100 SkASSERT(0 == fRefCnt); |
| 99 fRefCnt = 1; | 101 fRefCnt = 1; |
| 100 #endif | 102 #endif |
| 101 } | 103 } |
| 102 | 104 |
| 103 private: | 105 private: |
| 104 /** | 106 /** |
| 105 * Called when the ref count goes to 0. | 107 * Called when the ref count goes to 0. |
| 106 */ | 108 */ |
| 107 virtual void internal_dispose() const { | 109 virtual void internal_dispose() const { |
| 108 this->internal_dispose_restore_refcnt_to_1(); | 110 this->internal_dispose_restore_refcnt_to_1(); |
| 109 SkDELETE(this); | 111 SkDELETE(this); |
| 110 } | 112 } |
| 111 | 113 |
| 114 // The following friends are those which override internal_dispose() |
| 115 // and conditionally call SkRefCnt::internal_dispose(). |
| 116 friend class GrTexture; |
| 112 friend class SkWeakRefCnt; | 117 friend class SkWeakRefCnt; |
| 113 friend class GrTexture; // to allow GrTexture's internal_dispose to | |
| 114 // call SkRefCnt's & directly set fRefCnt (to 1) | |
| 115 | 118 |
| 116 mutable int32_t fRefCnt; | 119 mutable int32_t fRefCnt; |
| 117 | 120 |
| 118 typedef SkNoncopyable INHERITED; | 121 typedef SkNoncopyable INHERITED; |
| 119 }; | 122 }; |
| 120 | 123 |
| 121 /////////////////////////////////////////////////////////////////////////////// | 124 /////////////////////////////////////////////////////////////////////////////// |
| 122 | 125 |
| 123 /** Helper macro to safely assign one SkRefCnt[TS]* to another, checking for | 126 /** Helper macro to safely assign one SkRefCnt[TS]* to another, checking for |
| 124 null in on each side of the assignment, and ensuring that ref() is called | 127 null in on each side of the assignment, and ensuring that ref() is called |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 typedef T* SkRefPtr::*unspecified_bool_type; | 265 typedef T* SkRefPtr::*unspecified_bool_type; |
| 263 operator unspecified_bool_type() const { | 266 operator unspecified_bool_type() const { |
| 264 return fObj ? &SkRefPtr::fObj : NULL; | 267 return fObj ? &SkRefPtr::fObj : NULL; |
| 265 } | 268 } |
| 266 | 269 |
| 267 private: | 270 private: |
| 268 T* fObj; | 271 T* fObj; |
| 269 }; | 272 }; |
| 270 | 273 |
| 271 #endif | 274 #endif |
| OLD | NEW |