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 |
11 /** Declare a lazily-chosen static pointer (or array of pointers) of type T. | 11 /** Declare a lazily-chosen static pointer (or array of pointers) of type T. |
12 * | 12 * |
13 * Example usage: | 13 * Example usage: |
14 * | 14 * |
15 * Foo* GetSingletonFoo() { | 15 * Foo* GetSingletonFoo() { |
16 * SK_DECLARE_STATIC_LAZY_PTR(Foo, singleton); // Created with SkNEW, dest
royed with SkDELETE. | 16 * SK_DECLARE_STATIC_LAZY_PTR(Foo, singleton); // Created with new, destro
yed with delete. |
17 * return singleton.get(); | 17 * return singleton.get(); |
18 * } | 18 * } |
19 * | 19 * |
20 * These macros take an optional T* (*Create)() and void (*Destroy)(T*) at the
end. | 20 * These macros take an optional T* (*Create)() and void (*Destroy)(T*) at the
end. |
21 * If not given, we'll use SkNEW and SkDELETE. | 21 * If not given, we'll use new and delete. |
22 * These options are most useful when T doesn't have a public constructor or de
structor. | 22 * These options are most useful when T doesn't have a public constructor or de
structor. |
23 * Create comes first, so you may use a custom Create with a default Destroy, b
ut not vice versa. | 23 * Create comes first, so you may use a custom Create with a default Destroy, b
ut not vice versa. |
24 * | 24 * |
25 * Foo* CustomCreate() { return ...; } | 25 * Foo* CustomCreate() { return ...; } |
26 * void CustomDestroy(Foo* ptr) { ... } | 26 * void CustomDestroy(Foo* ptr) { ... } |
27 * Foo* GetSingletonFooWithCustomCleanup() { | 27 * Foo* GetSingletonFooWithCustomCleanup() { |
28 * SK_DECLARE_STATIC_LAZY_PTR(Foo, singleton, CustomCreate, CustomDestroy); | 28 * SK_DECLARE_STATIC_LAZY_PTR(Foo, singleton, CustomCreate, CustomDestroy); |
29 * return singleton.get(); | 29 * return singleton.get(); |
30 * } | 30 * } |
31 * | 31 * |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 SkASSERT(!prev); | 79 SkASSERT(!prev); |
80 return ptr; | 80 return ptr; |
81 } else { | 81 } else { |
82 Destroy(ptr); | 82 Destroy(ptr); |
83 // We need an acquire barrier before returning prev. The compare_exchan
ge provided it. | 83 // We need an acquire barrier before returning prev. The compare_exchan
ge provided it. |
84 SkASSERT(prev); | 84 SkASSERT(prev); |
85 return prev; | 85 return prev; |
86 } | 86 } |
87 } | 87 } |
88 | 88 |
89 template <typename T> T* sk_new() { return SkNEW(T); } | 89 template <typename T> |
90 template <typename T> void sk_delete(T* ptr) { SkDELETE(ptr); } | 90 T* sk_new() { |
| 91 return new T; |
| 92 } |
| 93 template <typename T> |
| 94 void sk_delete(T* ptr) { |
| 95 delete ptr; |
| 96 } |
91 | 97 |
92 // We're basing these implementations here on this article: | 98 // We're basing these implementations here on this article: |
93 // http://preshing.com/20140709/the-purpose-of-memory_order_consume-in-cpp11/ | 99 // http://preshing.com/20140709/the-purpose-of-memory_order_consume-in-cpp11/ |
94 // | 100 // |
95 // Because the users of SkLazyPtr and SkLazyPtrArray will read the pointers | 101 // Because the users of SkLazyPtr and SkLazyPtrArray will read the pointers |
96 // _through_ our atomically set pointer, there is a data dependency between our | 102 // _through_ our atomically set pointer, there is a data dependency between our |
97 // atomic and the guarded data, and so we only need writer-releases / | 103 // atomic and the guarded data, and so we only need writer-releases / |
98 // reader-consumes memory pairing rather than the more general write-releases / | 104 // reader-consumes memory pairing rather than the more general write-releases / |
99 // reader-acquires convention. | 105 // reader-acquires convention. |
100 // | 106 // |
(...skipping 20 matching lines...) Expand all Loading... |
121 // If fPtr has already been filled, we need a consume barrier when loadi
ng it. | 127 // If fPtr has already been filled, we need a consume barrier when loadi
ng it. |
122 // If not, we need a release barrier when setting it. try_cas will do t
hat. | 128 // If not, we need a release barrier when setting it. try_cas will do t
hat. |
123 T* ptr = consume_load(&fPtr); | 129 T* ptr = consume_load(&fPtr); |
124 return ptr ? ptr : try_cas<T*, Destroy>(&fPtr, Create()); | 130 return ptr ? ptr : try_cas<T*, Destroy>(&fPtr, Create()); |
125 } | 131 } |
126 | 132 |
127 private: | 133 private: |
128 T* fPtr; | 134 T* fPtr; |
129 }; | 135 }; |
130 | 136 |
131 template <typename T> T* sk_new_arg(int i) { return SkNEW_ARGS(T, (i)); } | 137 template <typename T> |
| 138 T* sk_new_arg(int i) { |
| 139 return new T(i); |
| 140 } |
132 | 141 |
133 // This has no constructor and must be zero-initalized (the macro above does thi
s). | 142 // This has no constructor and must be zero-initalized (the macro above does thi
s). |
134 template <typename T, int N, T* (*Create)(int) = sk_new_arg<T>, void (*Destroy)(
T*) = sk_delete<T> > | 143 template <typename T, int N, T* (*Create)(int) = sk_new_arg<T>, void (*Destroy)(
T*) = sk_delete<T> > |
135 class SkStaticLazyPtrArray { | 144 class SkStaticLazyPtrArray { |
136 public: | 145 public: |
137 T* operator[](int i) { | 146 T* operator[](int i) { |
138 SkASSERT(i >= 0 && i < N); | 147 SkASSERT(i >= 0 && i < N); |
139 // If fPtr has already been filled, we need an consume barrier when load
ing it. | 148 // If fPtr has already been filled, we need an consume barrier when load
ing it. |
140 // If not, we need a release barrier when setting it. try_cas will do t
hat. | 149 // If not, we need a release barrier when setting it. try_cas will do t
hat. |
141 T* ptr = consume_load(&fArray[i]); | 150 T* ptr = consume_load(&fArray[i]); |
(...skipping 13 matching lines...) Expand all Loading... |
155 // - get() calls SkNew(T) to create the pointer; | 164 // - get() calls SkNew(T) to create the pointer; |
156 // - get(functor) calls functor to create the pointer. | 165 // - get(functor) calls functor to create the pointer. |
157 template <typename T, void (*Destroy)(T*) = Private::sk_delete<T> > | 166 template <typename T, void (*Destroy)(T*) = Private::sk_delete<T> > |
158 class SkLazyPtr : SkNoncopyable { | 167 class SkLazyPtr : SkNoncopyable { |
159 public: | 168 public: |
160 SkLazyPtr() : fPtr(NULL) {} | 169 SkLazyPtr() : fPtr(NULL) {} |
161 ~SkLazyPtr() { if (fPtr) { Destroy((T*)fPtr); } } | 170 ~SkLazyPtr() { if (fPtr) { Destroy((T*)fPtr); } } |
162 | 171 |
163 T* get() const { | 172 T* get() const { |
164 T* ptr = Private::consume_load(&fPtr); | 173 T* ptr = Private::consume_load(&fPtr); |
165 return ptr ? ptr : Private::try_cas<T*, Destroy>(&fPtr, SkNEW(T)); | 174 return ptr ? ptr : Private::try_cas<T*, Destroy>(&fPtr, new T); |
166 } | 175 } |
167 | 176 |
168 template <typename Create> | 177 template <typename Create> |
169 T* get(const Create& create) const { | 178 T* get(const Create& create) const { |
170 T* ptr = Private::consume_load(&fPtr); | 179 T* ptr = Private::consume_load(&fPtr); |
171 return ptr ? ptr : Private::try_cas<T*, Destroy>(&fPtr, create()); | 180 return ptr ? ptr : Private::try_cas<T*, Destroy>(&fPtr, create()); |
172 } | 181 } |
173 | 182 |
174 private: | 183 private: |
175 mutable T* fPtr; | 184 mutable T* fPtr; |
176 }; | 185 }; |
177 | 186 |
178 | 187 |
179 #endif//SkLazyPtr_DEFINED | 188 #endif//SkLazyPtr_DEFINED |
OLD | NEW |