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() {} | |
| 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, SkNoncopyable { |
|
reed1
2013/10/16 12:27:23
To avoid multiple inheritance, perhaps we just nee
Justin Novosad
2013/10/16 13:19:57
Done.
| |
| 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 113 SkDELETE(this); | 130 SkDELETE(this); |
| 114 } | 131 } |
| 115 | 132 |
| 116 // The following friends are those which override internal_dispose() | 133 // The following friends are those which override internal_dispose() |
| 117 // and conditionally call SkRefCnt::internal_dispose(). | 134 // and conditionally call SkRefCnt::internal_dispose(). |
| 118 friend class GrTexture; | 135 friend class GrTexture; |
| 119 friend class SkWeakRefCnt; | 136 friend class SkWeakRefCnt; |
| 120 | 137 |
| 121 mutable int32_t fRefCnt; | 138 mutable int32_t fRefCnt; |
| 122 | 139 |
| 123 typedef SkNoncopyable INHERITED; | 140 typedef SkRefCntBase INHERITED; |
| 124 }; | 141 }; |
| 125 | 142 |
| 126 /////////////////////////////////////////////////////////////////////////////// | 143 /////////////////////////////////////////////////////////////////////////////// |
| 127 | 144 |
| 128 /** Helper macro to safely assign one SkRefCnt[TS]* to another, checking for | 145 /** 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 | 146 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. | 147 before unref(), in case the two pointers point to the same object. |
| 131 */ | 148 */ |
| 132 #define SkRefCnt_SafeAssign(dst, src) \ | 149 #define SkRefCnt_SafeAssign(dst, src) \ |
| 133 do { \ | 150 do { \ |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 typedef T* SkRefPtr::*unspecified_bool_type; | 291 typedef T* SkRefPtr::*unspecified_bool_type; |
| 275 operator unspecified_bool_type() const { | 292 operator unspecified_bool_type() const { |
| 276 return fObj ? &SkRefPtr::fObj : NULL; | 293 return fObj ? &SkRefPtr::fObj : NULL; |
| 277 } | 294 } |
| 278 | 295 |
| 279 private: | 296 private: |
| 280 T* fObj; | 297 T* fObj; |
| 281 }; | 298 }; |
| 282 | 299 |
| 283 #endif | 300 #endif |
| OLD | NEW |