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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 static Private::SkLazyPtrArray<T, N, Create, ##__VA_ARGS__> name | 55 static Private::SkLazyPtrArray<T, N, Create, ##__VA_ARGS__> name |
56 | 56 |
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 FIXMEs below. |
66 class SkFontConfigInterface; | 66 class SkFontConfigInterface; |
67 class SkTypeface; | 67 class SkTypeface; |
68 | 68 |
69 namespace Private { | 69 namespace Private { |
70 | 70 |
71 template <typename T> void sk_delete(T* ptr) { SkDELETE(ptr); } | 71 template <typename T> void sk_delete(T* ptr) { SkDELETE(ptr); } |
72 | 72 |
73 // 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. |
74 // 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. |
75 template <typename P, void (*Destroy)(P)> | 75 template <typename P, void (*Destroy)(P)> |
(...skipping 17 matching lines...) Expand all Loading... |
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_DEBUG |
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*) {} | |
104 template <typename U> void cleanup(U* ptr) { Destroy(ptr); } | 103 template <typename U> void cleanup(U* ptr) { Destroy(ptr); } |
105 | 104 |
106 ~SkLazyPtr() { | 105 ~SkLazyPtr() { |
107 this->cleanup((T*)fPtr); | 106 this->cleanup((T*)fPtr); |
108 fPtr = NULL; | 107 fPtr = NULL; |
109 } | 108 } |
110 #endif | 109 #endif |
111 | 110 |
112 private: | 111 private: |
113 void* fPtr; | 112 void* fPtr; |
114 }; | 113 }; |
115 | 114 |
116 // This has no constructor and must be zero-initalized (the macro above does thi
s). | 115 // 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> > | 116 template <typename T, int N, T* (*Create)(int), void (*Destroy)(T*) = sk_delete<
T> > |
118 class SkLazyPtrArray { | 117 class SkLazyPtrArray { |
119 public: | 118 public: |
120 T* operator[](int i) { | 119 T* operator[](int i) { |
121 SkASSERT(i >= 0 && i < N); | 120 SkASSERT(i >= 0 && i < N); |
122 // If fPtr has already been filled, we need an acquire barrier when load
ing it. | 121 // 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. | 122 // 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]); | 123 T* ptr = (T*)sk_acquire_load(&fArray[i]); |
125 return ptr ? ptr : try_cas<T*, Destroy>(&fArray[i], Create(i)); | 124 return ptr ? ptr : try_cas<T*, Destroy>(&fArray[i], Create(i)); |
126 } | 125 } |
127 | 126 |
128 #ifdef SK_DEBUG | 127 #ifdef SK_DEBUG |
| 128 // FIXME: We know we leak refs on some classes. For now, let them leak. |
| 129 void cleanup(SkTypeface*) {} |
| 130 template <typename U> void cleanup(U* ptr) { Destroy(ptr); } |
129 ~SkLazyPtrArray() { | 131 ~SkLazyPtrArray() { |
130 for (int i = 0; i < N; i++) { | 132 for (int i = 0; i < N; i++) { |
131 Destroy((T*)fArray[i]); | 133 this->cleanup((T*)fArray[i]); |
132 fArray[i] = NULL; | 134 fArray[i] = NULL; |
133 } | 135 } |
134 } | 136 } |
135 #endif | 137 #endif |
136 | 138 |
137 private: | 139 private: |
138 void* fArray[N]; | 140 void* fArray[N]; |
139 }; | 141 }; |
140 | 142 |
141 } // namespace Private | 143 } // namespace Private |
142 | 144 |
143 #endif//SkLazyPtr_DEFINED | 145 #endif//SkLazyPtr_DEFINED |
OLD | NEW |