1 /*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkLazyPtr_DEFINED
9 #define SkLazyPtr_DEFINED
10
11 /** Declare a lazily-chosen static pointer (or array of pointers) of type F.
12 *
13 * Example usage:
14 *
15 * Foo* GetSingletonFoo() {
16 * SK_DECLARE_STATIC_LAZY_PTR(Foo, singleton); // Created with SkNEW, destroyed with SkDELETE.
17 * return singleton.get();
18 * }
19 *
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.
22 * These options are most useful when T doesn't have a public constructor or destructor.
23 * Create comes first, so you may use a custom Create with a default Destroy, but not vice versa.
24 *
25 * Foo* CustomCreate() { return ...; }
26 * void CustomDestroy(Foo* ptr) { ... }
27 * Foo* GetSingletonFooWithCustomCleanup() {
28 * SK_DECLARE_STATIC_LAZY_PTR(Foo, singleton, CustomCreate, CustomDestroy);
29 * return singleton.get();
30 * }
31 *
32 * If you have a bunch of related static pointers of the same type, you can
33 * declare an array of lazy pointers together, and we'll pass the index to Create().
34 *
35 * Foo* CreateFoo(int i) { return ...; }
36 * Foo* GetCachedFoo(Foo::Enum enumVal) {
37 * SK_DECLARE_STATIC_LAZY_PTR_ARRAY(Foo, Foo::kEnumCount, cachedFoos, CreateFoo);
38 * return cachedFoos[enumVal];
39 * }
40 *
41 *
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.
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 *
47 * 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.
49 *
50 * These macros must be used in a global or function scope, not as a class member.
51 */
52
53 #define SK_DECLARE_STATIC_LAZY_PTR(T, name, ...) \
54 static Private::SkLazyPtr<T, ##__VA_ARGS__> name
55
56 #define SK_DECLARE_STATIC_LAZY_PTR_ARRAY(T, name, N, ...) \
57 static Private::SkLazyPtrArray<T, N, ##__VA_ARGS__> name
58
59
60
61 // Everything below here is private implementation details. Don't touch, don't even look.
62
63 #include "SkDynamicAnnotations.h"
64 #include "SkThread.h"
65 #include "SkThreadPriv.h"
66
67 // See FIXME below.
68 class SkFontConfigInterfaceDirect;
69
70 namespace Private {
71
72 // Set *dst to ptr if *dst is NULL. Returns value of *dst, destroying ptr if not swapped in.
73 // Issues the same memory barriers as sk_atomic_cas: acquire on failure, release on success.
74 template <typename P, void (*Destroy)(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);
77
78 if (prev) {
79 // We need an acquire barrier before returning prev, which sk_atomic_cas provided.
80 Destroy(ptr);
81 return prev;
82 } else {
83 // We need a release barrier before returning ptr, which sk_atomic_cas provided.
84 return ptr;
85 }
86 }
87
sk_new()88 template <typename T> T* sk_new() { return SkNEW(T); }
sk_delete(T * ptr)89 template <typename T> void sk_delete(T* ptr) { SkDELETE(ptr); }
90
91 // This has no constructor and must be zero-initalized (the macro above does this).
92 template <typename T, T* (*Create)() = sk_new<T>, void (*Destroy)(T*) = sk_delete<T> >
93 class SkLazyPtr {
94 public:
get()95 T* get() {
96 // If fPtr has already been filled, we need an acquire barrier when loading it.
97 // If not, we need a release barrier when setting it. try_cas will do that.
98 T* ptr = (T*)sk_acquire_load(&fPtr);
99 return ptr ? ptr : try_cas<T*, Destroy>(&fPtr, Create());
100 }
101
102 #ifdef SK_DEVELOPER
103 // FIXME: We know we leak refs on some classes. For now, let them leak.
cleanup(SkFontConfigInterfaceDirect *)104 void cleanup(SkFontConfigInterfaceDirect*) {}
cleanup(U * ptr)105 template <typename U> void cleanup(U* ptr) { Destroy(ptr); }
106
~SkLazyPtr()107 ~SkLazyPtr() {
108 this->cleanup((T*)fPtr);
109 fPtr = NULL;
110 }
111 #endif
112
113 private:
114 void* fPtr;
115 };
116
sk_new_arg(int i)117 template <typename T> T* sk_new_arg(int i) { return SkNEW_ARGS(T, (i)); }
118
119 // This has no constructor and must be zero-initalized (the macro above does this).
120 template <typename T, int N, T* (*Create)(int) = sk_new_arg<T>, void (*Destroy)(T*) = sk_delete<T> >
121 class SkLazyPtrArray {
122 public:
123 T* operator[](int i) {
124 SkASSERT(i >= 0 && i < N);
125 // If fPtr has already been filled, we need an acquire barrier when loading it.
126 // If not, we need a release barrier when setting it. try_cas will do that.
127 T* ptr = (T*)sk_acquire_load(&fArray[i]);
128 return ptr ? ptr : try_cas<T*, Destroy>(&fArray[i], Create(i));
129 }
130
131 #ifdef SK_DEVELOPER
~SkLazyPtrArray()132 ~SkLazyPtrArray() {
133 for (int i = 0; i < N; i++) {
134 Destroy((T*)fArray[i]);
135 fArray[i] = NULL;
136 }
137 }
138 #endif
139
140 private:
141 void* fArray[N];
142 };
143
144 } // namespace Private
145
146 #endif//SkLazyPtr_DEFINED
147