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; | |
68 | 67 |
69 namespace Private { | 68 namespace Private { |
70 | 69 |
71 template <typename T> void sk_delete(T* ptr) { SkDELETE(ptr); } | 70 template <typename T> void sk_delete(T* ptr) { SkDELETE(ptr); } |
72 | 71 |
73 // Set *dst to ptr if *dst is NULL. Returns value of *dst, destroying ptr if no
t swapped in. | 72 // Set *dst to ptr if *dst is NULL. Returns value of *dst, destroying ptr if no
t swapped in. |
74 // Issues the same memory barriers as sk_atomic_cas: acquire on failure, release
on success. | 73 // Issues the same memory barriers as sk_atomic_cas: acquire on failure, release
on success. |
75 template <typename P, void (*Destroy)(P)> | 74 template <typename P, void (*Destroy)(P)> |
76 static P try_cas(void** dst, P ptr) { | 75 static P try_cas(void** dst, P ptr) { |
77 P prev = (P)sk_atomic_cas(dst, NULL, ptr); | 76 P prev = (P)sk_atomic_cas(dst, NULL, ptr); |
(...skipping 15 matching lines...) Expand all Loading... |
93 T* get() { | 92 T* get() { |
94 // If fPtr has already been filled, we need an acquire barrier when load
ing it. | 93 // 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. | 94 // If not, we need a release barrier when setting it. try_cas will do t
hat. |
96 T* ptr = (T*)sk_acquire_load(&fPtr); | 95 T* ptr = (T*)sk_acquire_load(&fPtr); |
97 return ptr ? ptr : try_cas<T*, Destroy>(&fPtr, Create()); | 96 return ptr ? ptr : try_cas<T*, Destroy>(&fPtr, Create()); |
98 } | 97 } |
99 | 98 |
100 #ifdef SK_DEVELOPER | 99 #ifdef SK_DEVELOPER |
101 // FIXME: We know we leak refs on some classes. For now, let them leak. | 100 // FIXME: We know we leak refs on some classes. For now, let them leak. |
102 void cleanup(SkFontConfigInterface*) {} | 101 void cleanup(SkFontConfigInterface*) {} |
103 void cleanup(SkTypeface*) {} | |
104 template <typename U> void cleanup(U* ptr) { Destroy(ptr); } | 102 template <typename U> void cleanup(U* ptr) { Destroy(ptr); } |
105 | 103 |
106 ~SkLazyPtr() { | 104 ~SkLazyPtr() { |
107 this->cleanup((T*)fPtr); | 105 this->cleanup((T*)fPtr); |
108 fPtr = NULL; | 106 fPtr = NULL; |
109 } | 107 } |
110 #endif | 108 #endif |
111 | 109 |
112 private: | 110 private: |
113 void* fPtr; | 111 void* fPtr; |
(...skipping 20 matching lines...) Expand all Loading... |
134 } | 132 } |
135 #endif | 133 #endif |
136 | 134 |
137 private: | 135 private: |
138 void* fArray[N]; | 136 void* fArray[N]; |
139 }; | 137 }; |
140 | 138 |
141 } // namespace Private | 139 } // namespace Private |
142 | 140 |
143 #endif//SkLazyPtr_DEFINED | 141 #endif//SkLazyPtr_DEFINED |
OLD | NEW |