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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 | 57 |
58 | 58 |
59 // Everything below here is private implementation details. Don't touch, don't
even look. | 59 // Everything below here is private implementation details. Don't touch, don't
even look. |
60 | 60 |
61 #include "SkDynamicAnnotations.h" | 61 #include "SkDynamicAnnotations.h" |
62 #include "SkThread.h" | 62 #include "SkThread.h" |
63 #include "SkThreadPriv.h" | 63 #include "SkThreadPriv.h" |
64 | 64 |
65 // See FIXME below. | 65 // See FIXME below. |
66 class SkFontConfigInterface; | 66 class SkFontConfigInterface; |
| 67 class SkTypeface; |
67 | 68 |
68 namespace Private { | 69 namespace Private { |
69 | 70 |
70 template <typename T> void sk_delete(T* ptr) { SkDELETE(ptr); } | 71 template <typename T> void sk_delete(T* ptr) { SkDELETE(ptr); } |
71 | 72 |
72 // Set *dst to ptr if *dst is NULL. Returns value of *dst, destroying ptr if no
t swapped in. | 73 // Set *dst to ptr if *dst is NULL. Returns value of *dst, destroying ptr if no
t swapped in. |
73 // Issues the same memory barriers as sk_atomic_cas: acquire on failure, release
on success. | 74 // Issues the same memory barriers as sk_atomic_cas: acquire on failure, release
on success. |
74 template <typename P, void (*Destroy)(P)> | 75 template <typename P, void (*Destroy)(P)> |
75 static P try_cas(void** dst, P ptr) { | 76 static P try_cas(void** dst, P ptr) { |
76 P prev = (P)sk_atomic_cas(dst, NULL, ptr); | 77 P prev = (P)sk_atomic_cas(dst, NULL, ptr); |
(...skipping 15 matching lines...) Expand all Loading... |
92 T* get() { | 93 T* get() { |
93 // 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. |
94 // 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. |
95 T* ptr = (T*)sk_acquire_load(&fPtr); | 96 T* ptr = (T*)sk_acquire_load(&fPtr); |
96 return ptr ? ptr : try_cas<T*, Destroy>(&fPtr, Create()); | 97 return ptr ? ptr : try_cas<T*, Destroy>(&fPtr, Create()); |
97 } | 98 } |
98 | 99 |
99 #ifdef SK_DEBUG | 100 #ifdef SK_DEBUG |
100 // 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. |
101 void cleanup(SkFontConfigInterface*) {} | 102 void cleanup(SkFontConfigInterface*) {} |
| 103 void cleanup(SkTypeface*) {} |
102 template <typename U> void cleanup(U* ptr) { Destroy(ptr); } | 104 template <typename U> void cleanup(U* ptr) { Destroy(ptr); } |
103 | 105 |
104 ~SkLazyPtr() { | 106 ~SkLazyPtr() { |
105 this->cleanup((T*)fPtr); | 107 this->cleanup((T*)fPtr); |
106 fPtr = NULL; | 108 fPtr = NULL; |
107 } | 109 } |
108 #endif | 110 #endif |
109 | 111 |
110 private: | 112 private: |
111 void* fPtr; | 113 void* fPtr; |
(...skipping 20 matching lines...) Expand all Loading... |
132 } | 134 } |
133 #endif | 135 #endif |
134 | 136 |
135 private: | 137 private: |
136 void* fArray[N]; | 138 void* fArray[N]; |
137 }; | 139 }; |
138 | 140 |
139 } // namespace Private | 141 } // namespace Private |
140 | 142 |
141 #endif//SkLazyPtr_DEFINED | 143 #endif//SkLazyPtr_DEFINED |
OLD | NEW |