Chromium Code Reviews| 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 |
| 11 #define SkRefCnt_DEFINED | 11 #define SkRefCnt_DEFINED |
| 12 | 12 |
| 13 #include "SkThread.h" | 13 #include "SkThread.h" |
| 14 #include "SkInstCnt.h" | 14 #include "SkInstCnt.h" |
| 15 #include "SkTemplates.h" | 15 #include "SkTemplates.h" |
| 16 | 16 |
| 17 #ifdef SK_REF_CNT_BASE_INCLUDE | |
| 18 #include SK_REF_CNT_BASE_INCLUDE | |
| 19 #else | |
| 20 /** \class SkRefCntBase | |
| 21 | |
| 22 Default implementation of SkRefCntBase. The base class' contract is to | |
| 23 provide an implementation of aboutToRef. Embedders of skia can specify | |
| 24 an alternate implementation by setting SK_REF_CNT_BASE_INCLUDE. This is | |
| 25 useful for adding debug run-time checks to enforce certain usage patterns. | |
| 26 */ | |
| 27 class SK_API SkRefCntBase { | |
| 28 public: | |
| 29 void aboutToRef() const {} | |
| 30 }; | |
| 31 #endif | |
| 32 | |
| 17 /** \class SkRefCnt | 33 /** \class SkRefCnt |
| 18 | 34 |
| 19 SkRefCnt is the base class for objects that may be shared by multiple | 35 SkRefCnt is the base class for objects that may be shared by multiple |
| 20 objects. When an existing owner wants to share a reference, it calls ref(). | 36 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 | 37 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() | 38 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 | 39 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 | 40 destructor to be called explicitly (or via the object going out of scope on |
| 25 the stack or calling delete) if getRefCnt() > 1. | 41 the stack or calling delete) if getRefCnt() > 1. |
| 26 */ | 42 */ |
| 27 class SK_API SkRefCnt : SkNoncopyable { | 43 class SK_API SkRefCnt : public SkRefCntBase { |
| 28 public: | 44 public: |
| 29 SK_DECLARE_INST_COUNT_ROOT(SkRefCnt) | 45 SK_DECLARE_INST_COUNT_ROOT(SkRefCnt) |
| 30 | 46 |
| 31 /** Default construct, initializing the reference count to 1. | 47 /** Default construct, initializing the reference count to 1. |
| 32 */ | 48 */ |
| 33 SkRefCnt() : fRefCnt(1) {} | 49 SkRefCnt() : fRefCnt(1) {} |
| 34 | 50 |
| 35 /** Destruct, asserting that the reference count is 1. | 51 /** Destruct, asserting that the reference count is 1. |
| 36 */ | 52 */ |
| 37 virtual ~SkRefCnt() { | 53 virtual ~SkRefCnt() { |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 54 // Prevents user's 'unique' code from happening before decrements. | 70 // Prevents user's 'unique' code from happening before decrements. |
| 55 //TODO: issue the barrier. | 71 //TODO: issue the barrier. |
| 56 } | 72 } |
| 57 return unique; | 73 return unique; |
| 58 } | 74 } |
| 59 | 75 |
| 60 /** Increment the reference count. Must be balanced by a call to unref(). | 76 /** Increment the reference count. Must be balanced by a call to unref(). |
| 61 */ | 77 */ |
| 62 void ref() const { | 78 void ref() const { |
| 63 SkASSERT(fRefCnt > 0); | 79 SkASSERT(fRefCnt > 0); |
| 80 this->INHERITED::aboutToRef(); | |
| 64 sk_atomic_inc(&fRefCnt); // No barrier required. | 81 sk_atomic_inc(&fRefCnt); // No barrier required. |
| 65 } | 82 } |
| 66 | 83 |
| 67 /** Decrement the reference count. If the reference count is 1 before the | 84 /** Decrement the reference count. If the reference count is 1 before the |
| 68 decrement, then delete the object. Note that if this is the case, then | 85 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. | 86 the object needs to have been allocated via new, and not on the stack. |
| 70 */ | 87 */ |
| 71 void unref() const { | 88 void unref() const { |
| 72 SkASSERT(fRefCnt > 0); | 89 SkASSERT(fRefCnt > 0); |
| 73 // Release barrier (SL/S), if not provided below. | 90 // Release barrier (SL/S), if not provided below. |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 99 */ | 116 */ |
| 100 void internal_dispose_restore_refcnt_to_1() const { | 117 void internal_dispose_restore_refcnt_to_1() const { |
| 101 #ifdef SK_DEBUG | 118 #ifdef SK_DEBUG |
| 102 SkASSERT(0 == fRefCnt); | 119 SkASSERT(0 == fRefCnt); |
| 103 fRefCnt = 1; | 120 fRefCnt = 1; |
| 104 #endif | 121 #endif |
| 105 } | 122 } |
| 106 | 123 |
| 107 private: | 124 private: |
| 108 /** | 125 /** |
| 126 * Make, SkRefCnt non-copyable | |
|
bungeman-skia
2013/10/16 14:36:34
Looks like, a magic comma got in here?
| |
| 127 */ | |
| 128 SkRefCnt(const SkRefCnt&); | |
| 129 SkRefCnt& operator=(const SkRefCnt&); | |
| 130 | |
| 131 /** | |
| 109 * Called when the ref count goes to 0. | 132 * Called when the ref count goes to 0. |
| 110 */ | 133 */ |
| 111 virtual void internal_dispose() const { | 134 virtual void internal_dispose() const { |
| 112 this->internal_dispose_restore_refcnt_to_1(); | 135 this->internal_dispose_restore_refcnt_to_1(); |
| 113 SkDELETE(this); | 136 SkDELETE(this); |
| 114 } | 137 } |
| 115 | 138 |
| 116 // The following friends are those which override internal_dispose() | 139 // The following friends are those which override internal_dispose() |
| 117 // and conditionally call SkRefCnt::internal_dispose(). | 140 // and conditionally call SkRefCnt::internal_dispose(). |
| 118 friend class GrTexture; | 141 friend class GrTexture; |
| 119 friend class SkWeakRefCnt; | 142 friend class SkWeakRefCnt; |
| 120 | 143 |
| 121 mutable int32_t fRefCnt; | 144 mutable int32_t fRefCnt; |
| 122 | 145 |
| 123 typedef SkNoncopyable INHERITED; | 146 typedef SkRefCntBase INHERITED; |
| 124 }; | 147 }; |
| 125 | 148 |
| 126 /////////////////////////////////////////////////////////////////////////////// | 149 /////////////////////////////////////////////////////////////////////////////// |
| 127 | 150 |
| 128 /** Helper macro to safely assign one SkRefCnt[TS]* to another, checking for | 151 /** Helper macro to safely assign one SkRefCnt[TS]* to another, checking for |
| 129 null in on each side of the assignment, and ensuring that ref() is called | 152 null in on each side of the assignment, and ensuring that ref() is called |
| 130 before unref(), in case the two pointers point to the same object. | 153 before unref(), in case the two pointers point to the same object. |
| 131 */ | 154 */ |
| 132 #define SkRefCnt_SafeAssign(dst, src) \ | 155 #define SkRefCnt_SafeAssign(dst, src) \ |
| 133 do { \ | 156 do { \ |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 typedef T* SkRefPtr::*unspecified_bool_type; | 297 typedef T* SkRefPtr::*unspecified_bool_type; |
| 275 operator unspecified_bool_type() const { | 298 operator unspecified_bool_type() const { |
| 276 return fObj ? &SkRefPtr::fObj : NULL; | 299 return fObj ? &SkRefPtr::fObj : NULL; |
| 277 } | 300 } |
| 278 | 301 |
| 279 private: | 302 private: |
| 280 T* fObj; | 303 T* fObj; |
| 281 }; | 304 }; |
| 282 | 305 |
| 283 #endif | 306 #endif |
| OLD | NEW |