Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1151)

Side by Side Diff: src/core/SkLazyPtr.h

Issue 606013004: Stop doing at-exit cleanup of lazy pointers. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: re-rebase Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 23 matching lines...) Expand all
34 * 34 *
35 * Foo* CreateFoo(int i) { return ...; } 35 * Foo* CreateFoo(int i) { return ...; }
36 * Foo* GetCachedFoo(Foo::Enum enumVal) { 36 * Foo* GetCachedFoo(Foo::Enum enumVal) {
37 * SK_DECLARE_STATIC_LAZY_PTR_ARRAY(Foo, Foo::kEnumCount, cachedFoos, Creat eFoo); 37 * SK_DECLARE_STATIC_LAZY_PTR_ARRAY(Foo, Foo::kEnumCount, cachedFoos, Creat eFoo);
38 * return cachedFoos[enumVal]; 38 * return cachedFoos[enumVal];
39 * } 39 * }
40 * 40 *
41 * 41 *
42 * You can think of SK_DECLARE_STATIC_LAZY_PTR as a cheaper specialization of 42 * You can think of SK_DECLARE_STATIC_LAZY_PTR as a cheaper specialization of
43 * SkOnce. There is no mutex or extra storage used past the pointer itself. 43 * SkOnce. There is no mutex or extra storage used past the pointer itself.
44 * In debug mode, each lazy pointer will be cleaned up at process exit so we
45 * can check that we've not leaked or freed them early.
46 * 44 *
47 * We may call Create more than once, but all threads will see the same pointer 45 * We may call Create more than once, but all threads will see the same pointer
48 * returned from get(). Any extra calls to Create will be cleaned up. 46 * returned from get(). Any extra calls to Create will be cleaned up.
49 * 47 *
50 * These macros must be used in a global scope, not in function scope or as a c lass member. 48 * These macros must be used in a global scope, not in function scope or as a c lass member.
51 */ 49 */
52 50
53 #define SK_DECLARE_STATIC_LAZY_PTR(T, name, ...) \ 51 #define SK_DECLARE_STATIC_LAZY_PTR(T, name, ...) \
54 namespace {} static Private::SkLazyPtr<T, ##__VA_ARGS__> name 52 namespace {} static Private::SkLazyPtr<T, ##__VA_ARGS__> name
55 53
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 template <typename T, T* (*Create)() = sk_new<T>, void (*Destroy)(T*) = sk_delet e<T> > 105 template <typename T, T* (*Create)() = sk_new<T>, void (*Destroy)(T*) = sk_delet e<T> >
108 class SkLazyPtr { 106 class SkLazyPtr {
109 public: 107 public:
110 T* get() { 108 T* get() {
111 // If fPtr has already been filled, we need a consume barrier when loadi ng it. 109 // If fPtr has already been filled, we need a consume barrier when loadi ng it.
112 // If not, we need a release barrier when setting it. try_cas will do t hat. 110 // If not, we need a release barrier when setting it. try_cas will do t hat.
113 T* ptr = (T*)sk_consume_load(&fPtr); 111 T* ptr = (T*)sk_consume_load(&fPtr);
114 return ptr ? ptr : try_cas<T*, Destroy>(&fPtr, Create()); 112 return ptr ? ptr : try_cas<T*, Destroy>(&fPtr, Create());
115 } 113 }
116 114
117 #ifdef SK_DEVELOPER
118 // FIXME: We know we leak refs on some classes. For now, let them leak.
119 void cleanup(SkFontConfigInterfaceDirect*) {}
120 template <typename U> void cleanup(U* ptr) { Destroy(ptr); }
121
122 ~SkLazyPtr() {
123 this->cleanup((T*)fPtr);
124 fPtr = NULL;
125 }
126 #endif
127
128 private: 115 private:
129 void* fPtr; 116 void* fPtr;
130 }; 117 };
131 118
132 template <typename T> T* sk_new_arg(int i) { return SkNEW_ARGS(T, (i)); } 119 template <typename T> T* sk_new_arg(int i) { return SkNEW_ARGS(T, (i)); }
133 120
134 // This has no constructor and must be zero-initalized (the macro above does thi s). 121 // This has no constructor and must be zero-initalized (the macro above does thi s).
135 template <typename T, int N, T* (*Create)(int) = sk_new_arg<T>, void (*Destroy)( T*) = sk_delete<T> > 122 template <typename T, int N, T* (*Create)(int) = sk_new_arg<T>, void (*Destroy)( T*) = sk_delete<T> >
136 class SkLazyPtrArray { 123 class SkLazyPtrArray {
137 public: 124 public:
138 T* operator[](int i) { 125 T* operator[](int i) {
139 SkASSERT(i >= 0 && i < N); 126 SkASSERT(i >= 0 && i < N);
140 // If fPtr has already been filled, we need an consume barrier when load ing it. 127 // If fPtr has already been filled, we need an consume barrier when load ing it.
141 // 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.
142 T* ptr = (T*)sk_consume_load(&fArray[i]); 129 T* ptr = (T*)sk_consume_load(&fArray[i]);
143 return ptr ? ptr : try_cas<T*, Destroy>(&fArray[i], Create(i)); 130 return ptr ? ptr : try_cas<T*, Destroy>(&fArray[i], Create(i));
144 } 131 }
145 132
146 #ifdef SK_DEVELOPER
147 ~SkLazyPtrArray() {
148 for (int i = 0; i < N; i++) {
149 Destroy((T*)fArray[i]);
150 fArray[i] = NULL;
151 }
152 }
153 #endif
154
155 private: 133 private:
156 void* fArray[N]; 134 void* fArray[N];
157 }; 135 };
158 136
159 } // namespace Private 137 } // namespace Private
160 138
161 #endif//SkLazyPtr_DEFINED 139 #endif//SkLazyPtr_DEFINED
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698