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

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

Issue 326413003: SkNEW as default Create() for SkLazyPtr macros. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: update exemption Created 6 years, 6 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 | src/ports/SkFontConfigInterface_direct.cpp » ('j') | 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
11 /** Declare a lazily-chosen static pointer (or array of pointers) of type F. 11 /** Declare a lazily-chosen static pointer (or array of pointers) of type F.
12 * 12 *
13 * Example usage: 13 * Example usage:
14 * 14 *
15 * Foo* CreateFoo() { return SkNEW(Foo); }
16 * Foo* GetSingletonFoo() { 15 * Foo* GetSingletonFoo() {
17 * SK_DECLARE_STATIC_LAZY_PTR(Foo, singleton, CreateFoo); // Clean up with SkDELETE. 16 * SK_DECLARE_STATIC_LAZY_PTR(Foo, singleton); // Created with SkNEW, dest royed with SkDELETE.
18 * return singleton.get(); 17 * return singleton.get();
19 * } 18 * }
20 * 19 *
21 * These macros take an optional void (*Destroy)(T*) at the end. If not given, we'll use SkDELETE. 20 * These macros take an optional T* (*Create)() and void (*Destroy)(T*) at the end.
22 * This option is most useful when T doesn't have a public destructor. 21 * If not given, we'll use SkNEW and SkDELETE.
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 * 24 *
24 * void CustomCleanup(Foo* ptr) { ... } 25 * Foo* CustomCreate() { return ...; }
26 * void CustomDestroy(Foo* ptr) { ... }
25 * Foo* GetSingletonFooWithCustomCleanup() { 27 * Foo* GetSingletonFooWithCustomCleanup() {
26 * SK_DECLARE_STATIC_LAZY_PTR(Foo, singleton, CreateFoo, CustomCleanup); 28 * SK_DECLARE_STATIC_LAZY_PTR(Foo, singleton, CustomCreate, CustomDestroy);
27 * return singleton.get(); 29 * return singleton.get();
28 * } 30 * }
29 * 31 *
30 * If you have a bunch of related static pointers of the same type, you can 32 * If you have a bunch of related static pointers of the same type, you can
31 * declare an array of lazy pointers together: 33 * declare an array of lazy pointers together, and we'll pass the index to Crea te().
32 * 34 *
33 * Foo* CreateFoo(int i) { return ...; } 35 * Foo* CreateFoo(int i) { return ...; }
34 * Foo* GetCachedFoo(Foo::Enum enumVal) { 36 * Foo* GetCachedFoo(Foo::Enum enumVal) {
35 * 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);
36 * return cachedFoos[enumVal]; 38 * return cachedFoos[enumVal];
37 * } 39 * }
38 * 40 *
39 * 41 *
40 * 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
41 * 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.
42 * In debug mode, each lazy pointer will be cleaned up at process exit so we 44 * In debug mode, each lazy pointer will be cleaned up at process exit so we
43 * can check that we've not leaked or freed them early. 45 * can check that we've not leaked or freed them early.
44 * 46 *
45 * We may call Create more than once, but all threads will see the same pointer 47 * We may call Create more than once, but all threads will see the same pointer
46 * returned from get(). Any extra calls to Create will be cleaned up. 48 * returned from get(). Any extra calls to Create will be cleaned up.
47 * 49 *
48 * These macros must be used in a global or function scope, not as a class memb er. 50 * These macros must be used in a global or function scope, not as a class memb er.
49 */ 51 */
50 52
51 #define SK_DECLARE_STATIC_LAZY_PTR(T, name, Create, ...) \ 53 #define SK_DECLARE_STATIC_LAZY_PTR(T, name, ...) \
52 static Private::SkLazyPtr<T, Create, ##__VA_ARGS__> name 54 static Private::SkLazyPtr<T, ##__VA_ARGS__> name
53 55
54 #define SK_DECLARE_STATIC_LAZY_PTR_ARRAY(T, name, N, Create, ...) \ 56 #define SK_DECLARE_STATIC_LAZY_PTR_ARRAY(T, name, N, ...) \
55 static Private::SkLazyPtrArray<T, N, Create, ##__VA_ARGS__> name 57 static Private::SkLazyPtrArray<T, N, ##__VA_ARGS__> name
56 58
57 59
58 60
59 // Everything below here is private implementation details. Don't touch, don't even look. 61 // Everything below here is private implementation details. Don't touch, don't even look.
60 62
61 #include "SkDynamicAnnotations.h" 63 #include "SkDynamicAnnotations.h"
62 #include "SkThread.h" 64 #include "SkThread.h"
63 #include "SkThreadPriv.h" 65 #include "SkThreadPriv.h"
64 66
65 // See FIXME below. 67 // See FIXME below.
66 class SkFontConfigInterface; 68 class SkFontConfigInterfaceDirect;
67 69
68 namespace Private { 70 namespace Private {
69 71
70 template <typename T> void sk_delete(T* ptr) { SkDELETE(ptr); }
71
72 // 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.
73 // 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.
74 template <typename P, void (*Destroy)(P)> 74 template <typename P, void (*Destroy)(P)>
75 static P try_cas(void** dst, P ptr) { 75 static P try_cas(void** dst, P ptr) {
76 P prev = (P)sk_atomic_cas(dst, NULL, ptr); 76 P prev = (P)sk_atomic_cas(dst, NULL, ptr);
77 77
78 if (prev) { 78 if (prev) {
79 // We need an acquire barrier before returning prev, which sk_atomic_cas provided. 79 // We need an acquire barrier before returning prev, which sk_atomic_cas provided.
80 Destroy(ptr); 80 Destroy(ptr);
81 return prev; 81 return prev;
82 } else { 82 } else {
83 // We need a release barrier before returning ptr, which sk_atomic_cas p rovided. 83 // We need a release barrier before returning ptr, which sk_atomic_cas p rovided.
84 return ptr; 84 return ptr;
85 } 85 }
86 } 86 }
87 87
88 template <typename T> T* sk_new() { return SkNEW(T); }
89 template <typename T> void sk_delete(T* ptr) { SkDELETE(ptr); }
90
88 // This has no constructor and must be zero-initalized (the macro above does thi s). 91 // This has no constructor and must be zero-initalized (the macro above does thi s).
89 template <typename T, T* (*Create)(), void (*Destroy)(T*) = sk_delete<T> > 92 template <typename T, T* (*Create)() = sk_new<T>, void (*Destroy)(T*) = sk_delet e<T> >
90 class SkLazyPtr { 93 class SkLazyPtr {
91 public: 94 public:
92 T* get() { 95 T* get() {
93 // If fPtr has already been filled, we need an acquire barrier when load ing it. 96 // 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. 97 // If not, we need a release barrier when setting it. try_cas will do t hat.
95 T* ptr = (T*)sk_acquire_load(&fPtr); 98 T* ptr = (T*)sk_acquire_load(&fPtr);
96 return ptr ? ptr : try_cas<T*, Destroy>(&fPtr, Create()); 99 return ptr ? ptr : try_cas<T*, Destroy>(&fPtr, Create());
97 } 100 }
98 101
99 #ifdef SK_DEVELOPER 102 #ifdef SK_DEVELOPER
100 // FIXME: We know we leak refs on some classes. For now, let them leak. 103 // FIXME: We know we leak refs on some classes. For now, let them leak.
101 void cleanup(SkFontConfigInterface*) {} 104 void cleanup(SkFontConfigInterfaceDirect*) {}
102 template <typename U> void cleanup(U* ptr) { Destroy(ptr); } 105 template <typename U> void cleanup(U* ptr) { Destroy(ptr); }
103 106
104 ~SkLazyPtr() { 107 ~SkLazyPtr() {
105 this->cleanup((T*)fPtr); 108 this->cleanup((T*)fPtr);
106 fPtr = NULL; 109 fPtr = NULL;
107 } 110 }
108 #endif 111 #endif
109 112
110 private: 113 private:
111 void* fPtr; 114 void* fPtr;
112 }; 115 };
113 116
117 template <typename T> T* sk_new_arg(int i) { return SkNEW_ARGS(T, (i)); }
118
114 // This has no constructor and must be zero-initalized (the macro above does thi s). 119 // This has no constructor and must be zero-initalized (the macro above does thi s).
115 template <typename T, int N, T* (*Create)(int), void (*Destroy)(T*) = sk_delete< T> > 120 template <typename T, int N, T* (*Create)(int) = sk_new_arg<T>, void (*Destroy)( T*) = sk_delete<T> >
116 class SkLazyPtrArray { 121 class SkLazyPtrArray {
117 public: 122 public:
118 T* operator[](int i) { 123 T* operator[](int i) {
119 SkASSERT(i >= 0 && i < N); 124 SkASSERT(i >= 0 && i < N);
120 // If fPtr has already been filled, we need an acquire barrier when load ing it. 125 // If fPtr has already been filled, we need an acquire barrier when load ing it.
121 // If not, we need a release barrier when setting it. try_cas will do t hat. 126 // If not, we need a release barrier when setting it. try_cas will do t hat.
122 T* ptr = (T*)sk_acquire_load(&fArray[i]); 127 T* ptr = (T*)sk_acquire_load(&fArray[i]);
123 return ptr ? ptr : try_cas<T*, Destroy>(&fArray[i], Create(i)); 128 return ptr ? ptr : try_cas<T*, Destroy>(&fArray[i], Create(i));
124 } 129 }
125 130
126 #ifdef SK_DEVELOPER 131 #ifdef SK_DEVELOPER
127 ~SkLazyPtrArray() { 132 ~SkLazyPtrArray() {
128 for (int i = 0; i < N; i++) { 133 for (int i = 0; i < N; i++) {
129 Destroy((T*)fArray[i]); 134 Destroy((T*)fArray[i]);
130 fArray[i] = NULL; 135 fArray[i] = NULL;
131 } 136 }
132 } 137 }
133 #endif 138 #endif
134 139
135 private: 140 private:
136 void* fArray[N]; 141 void* fArray[N];
137 }; 142 };
138 143
139 } // namespace Private 144 } // namespace Private
140 145
141 #endif//SkLazyPtr_DEFINED 146 #endif//SkLazyPtr_DEFINED
OLDNEW
« no previous file with comments | « no previous file | src/ports/SkFontConfigInterface_direct.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698