| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 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 SkLazyPtr_DEFINED | 8 #ifndef SkLazyPtr_DEFINED |
| 9 #define SkLazyPtr_DEFINED | 9 #define SkLazyPtr_DEFINED |
| 10 | 10 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 template <typename T, T* (*Create)(), void (*Destroy)(T*) = sk_delete<T> > | 90 template <typename T, T* (*Create)(), void (*Destroy)(T*) = sk_delete<T> > |
| 91 class SkLazyPtr { | 91 class SkLazyPtr { |
| 92 public: | 92 public: |
| 93 T* get() { | 93 T* get() { |
| 94 // If fPtr has already been filled, we need an acquire barrier when load
ing it. | 94 // If fPtr has already been filled, we need an acquire barrier when load
ing it. |
| 95 // If not, we need a release barrier when setting it. try_cas will do t
hat. | 95 // If not, we need a release barrier when setting it. try_cas will do t
hat. |
| 96 T* ptr = (T*)sk_acquire_load(&fPtr); | 96 T* ptr = (T*)sk_acquire_load(&fPtr); |
| 97 return ptr ? ptr : try_cas<T*, Destroy>(&fPtr, Create()); | 97 return ptr ? ptr : try_cas<T*, Destroy>(&fPtr, Create()); |
| 98 } | 98 } |
| 99 | 99 |
| 100 #ifdef SK_DEBUG | 100 #ifdef SK_DEVELOPER |
| 101 // FIXME: We know we leak refs on some classes. For now, let them leak. | 101 // FIXME: We know we leak refs on some classes. For now, let them leak. |
| 102 void cleanup(SkFontConfigInterface*) {} | 102 void cleanup(SkFontConfigInterface*) {} |
| 103 void cleanup(SkTypeface*) {} | 103 void cleanup(SkTypeface*) {} |
| 104 template <typename U> void cleanup(U* ptr) { Destroy(ptr); } | 104 template <typename U> void cleanup(U* ptr) { Destroy(ptr); } |
| 105 | 105 |
| 106 ~SkLazyPtr() { | 106 ~SkLazyPtr() { |
| 107 this->cleanup((T*)fPtr); | 107 this->cleanup((T*)fPtr); |
| 108 fPtr = NULL; | 108 fPtr = NULL; |
| 109 } | 109 } |
| 110 #endif | 110 #endif |
| 111 | 111 |
| 112 private: | 112 private: |
| 113 void* fPtr; | 113 void* fPtr; |
| 114 }; | 114 }; |
| 115 | 115 |
| 116 // This has no constructor and must be zero-initalized (the macro above does thi
s). | 116 // This has no constructor and must be zero-initalized (the macro above does thi
s). |
| 117 template <typename T, int N, T* (*Create)(int), void (*Destroy)(T*) = sk_delete<
T> > | 117 template <typename T, int N, T* (*Create)(int), void (*Destroy)(T*) = sk_delete<
T> > |
| 118 class SkLazyPtrArray { | 118 class SkLazyPtrArray { |
| 119 public: | 119 public: |
| 120 T* operator[](int i) { | 120 T* operator[](int i) { |
| 121 SkASSERT(i >= 0 && i < N); | 121 SkASSERT(i >= 0 && i < N); |
| 122 // If fPtr has already been filled, we need an acquire barrier when load
ing it. | 122 // If fPtr has already been filled, we need an acquire barrier when load
ing it. |
| 123 // If not, we need a release barrier when setting it. try_cas will do t
hat. | 123 // If not, we need a release barrier when setting it. try_cas will do t
hat. |
| 124 T* ptr = (T*)sk_acquire_load(&fArray[i]); | 124 T* ptr = (T*)sk_acquire_load(&fArray[i]); |
| 125 return ptr ? ptr : try_cas<T*, Destroy>(&fArray[i], Create(i)); | 125 return ptr ? ptr : try_cas<T*, Destroy>(&fArray[i], Create(i)); |
| 126 } | 126 } |
| 127 | 127 |
| 128 #ifdef SK_DEBUG | 128 #ifdef SK_DEVELOPER |
| 129 ~SkLazyPtrArray() { | 129 ~SkLazyPtrArray() { |
| 130 for (int i = 0; i < N; i++) { | 130 for (int i = 0; i < N; i++) { |
| 131 Destroy((T*)fArray[i]); | 131 Destroy((T*)fArray[i]); |
| 132 fArray[i] = NULL; | 132 fArray[i] = NULL; |
| 133 } | 133 } |
| 134 } | 134 } |
| 135 #endif | 135 #endif |
| 136 | 136 |
| 137 private: | 137 private: |
| 138 void* fArray[N]; | 138 void* fArray[N]; |
| 139 }; | 139 }; |
| 140 | 140 |
| 141 } // namespace Private | 141 } // namespace Private |
| 142 | 142 |
| 143 #endif//SkLazyPtr_DEFINED | 143 #endif//SkLazyPtr_DEFINED |
| OLD | NEW |