SkLazyPtr follow ups
- moves test to LazyPtrTest.cpp
- adds the ability to use a Create() method with an argument
BUG=skia:
Review URL: https://codereview.chromium.org/669783002
diff --git a/gyp/tests.gypi b/gyp/tests.gypi
index 422a22e..827d6e3 100644
--- a/gyp/tests.gypi
+++ b/gyp/tests.gypi
@@ -130,7 +130,7 @@
'../tests/LListTest.cpp',
'../tests/LayerDrawLooperTest.cpp',
'../tests/LayerRasterizerTest.cpp',
- '../tests/LazyPtr.cpp',
+ '../tests/LazyPtrTest.cpp',
'../tests/MD5Test.cpp',
'../tests/MallocPixelRefTest.cpp',
'../tests/MathTest.cpp',
diff --git a/src/core/SkLazyPtr.h b/src/core/SkLazyPtr.h
index f9508c5..7273079 100644
--- a/src/core/SkLazyPtr.h
+++ b/src/core/SkLazyPtr.h
@@ -49,10 +49,10 @@
*/
#define SK_DECLARE_STATIC_LAZY_PTR(T, name, ...) \
- namespace {} static Private::SkLazyPtrBase<T, ##__VA_ARGS__> name
+ namespace {} static Private::SkStaticLazyPtr<T, ##__VA_ARGS__> name
#define SK_DECLARE_STATIC_LAZY_PTR_ARRAY(T, name, N, ...) \
- namespace {} static Private::SkLazyPtrArray<T, N, ##__VA_ARGS__> name
+ namespace {} static Private::SkStaticLazyPtrArray<T, N, ##__VA_ARGS__> name
// namespace {} forces these macros to only be legal in global scopes. Chrome has thread-safety
// problems with them in function-local statics because it uses -fno-threadsafe-statics, and even
@@ -102,7 +102,7 @@
// This has no constructor and must be zero-initalized (the macro above does this).
template <typename T, T* (*Create)() = sk_new<T>, void (*Destroy)(T*) = sk_delete<T> >
-class SkLazyPtrBase {
+class SkStaticLazyPtr {
public:
T* get() {
// If fPtr has already been filled, we need a consume barrier when loading it.
@@ -111,7 +111,7 @@
return ptr ? ptr : try_cas<T*, Destroy>(&fPtr, Create());
}
-protected:
+private:
void* fPtr;
};
@@ -119,7 +119,7 @@
// This has no constructor and must be zero-initalized (the macro above does this).
template <typename T, int N, T* (*Create)(int) = sk_new_arg<T>, void (*Destroy)(T*) = sk_delete<T> >
-class SkLazyPtrArray {
+class SkStaticLazyPtrArray {
public:
T* operator[](int i) {
SkASSERT(i >= 0 && i < N);
@@ -136,16 +136,30 @@
} // namespace Private
// This version is suitable for use as a class member.
-// It's the same as above except it has a constructor to zero itself and a destructor to clean up.
-template <typename T,
- T* (*Create)() = Private::sk_new<T>,
- void (*Destroy)(T*) = Private::sk_delete<T> >
-class SkLazyPtr : public Private::SkLazyPtrBase<T, Create, Destroy> {
+// It's much the same as above except:
+// - it has a constructor to zero itself;
+// - it has a destructor to clean up;
+// - get() calls SkNew(T) to create the pointer;
+// - get(functor) calls functor to create the pointer.
+template <typename T, void (*Destroy)(T*) = Private::sk_delete<T> >
+class SkLazyPtr : SkNoncopyable {
public:
- SkLazyPtr() { INHERITED::fPtr = NULL; }
- ~SkLazyPtr() { if (INHERITED::fPtr) { Destroy((T*)INHERITED::fPtr); } }
+ SkLazyPtr() : fPtr(NULL) {}
+ ~SkLazyPtr() { if (fPtr) { Destroy((T*)fPtr); } }
+
+ T* get() {
+ T* ptr = (T*)sk_consume_load(&fPtr);
+ return ptr ? ptr : Private::try_cas<T*, Destroy>(&fPtr, SkNEW(T));
+ }
+
+ template <typename Create>
+ T* get(const Create& create) {
+ T* ptr = (T*)sk_consume_load(&fPtr);
+ return ptr ? ptr : Private::try_cas<T*, Destroy>(&fPtr, create());
+ }
+
private:
- typedef Private::SkLazyPtrBase<T, Create, Destroy> INHERITED;
+ void* fPtr;
};
diff --git a/tests/LazyPtr.cpp b/tests/LazyPtr.cpp
deleted file mode 100644
index ff235e6..0000000
--- a/tests/LazyPtr.cpp
+++ /dev/null
@@ -1,48 +0,0 @@
-#include "Test.h"
-#include "SkLazyPtr.h"
-#include "SkTaskGroup.h"
-
-DEF_TEST(LazyPtr, r) {
- SkLazyPtr<int> lazy;
- int* ptr = lazy.get();
-
- REPORTER_ASSERT(r, ptr);
- REPORTER_ASSERT(r, lazy.get() == ptr);
-
- SkLazyPtr<double> neverRead;
-}
-
-namespace {
-
-struct Racer : public SkRunnable {
- Racer() : fLazy(NULL), fSeen(NULL) {}
-
- virtual void run() SK_OVERRIDE { fSeen = fLazy->get(); }
-
- SkLazyPtr<int>* fLazy;
- int* fSeen;
-};
-
-} // namespace
-
-DEF_TEST(LazyPtr_Threaded, r) {
- static const int kRacers = 321;
-
- SkLazyPtr<int> lazy;
-
- Racer racers[kRacers];
- for (int i = 0; i < kRacers; i++) {
- racers[i].fLazy = &lazy;
- }
-
- SkTaskGroup tg;
- for (int i = 0; i < kRacers; i++) {
- tg.add(racers + i);
- }
- tg.wait();
-
- for (int i = 1; i < kRacers; i++) {
- REPORTER_ASSERT(r, racers[i].fSeen);
- REPORTER_ASSERT(r, racers[i].fSeen == racers[0].fSeen);
- }
-}
diff --git a/tests/LazyPtrTest.cpp b/tests/LazyPtrTest.cpp
new file mode 100644
index 0000000..f719c2e
--- /dev/null
+++ b/tests/LazyPtrTest.cpp
@@ -0,0 +1,79 @@
+#include "Test.h"
+#include "SkLazyPtr.h"
+#include "SkTaskGroup.h"
+
+namespace {
+
+struct CreateIntFromFloat {
+ CreateIntFromFloat(float val) : fVal(val) {}
+ int* operator()() const { return SkNEW_ARGS(int, ((int)fVal)); }
+ float fVal;
+};
+
+// As a template argument this must have external linkage.
+void custom_destroy(int* ptr) { *ptr = 99; }
+
+} // namespace
+
+DEF_TEST(LazyPtr, r) {
+ // Basic usage: calls SkNEW(int).
+ SkLazyPtr<int> lazy;
+ int* ptr = lazy.get();
+ REPORTER_ASSERT(r, ptr);
+ REPORTER_ASSERT(r, lazy.get() == ptr);
+
+ // Advanced usage: calls a functor.
+ SkLazyPtr<int> lazyFunctor;
+ int* six = lazyFunctor.get(CreateIntFromFloat(6.4f));
+ REPORTER_ASSERT(r, six);
+ REPORTER_ASSERT(r, 6 == *six);
+
+ // Just makes sure this is safe.
+ SkLazyPtr<double> neverRead;
+
+ // SkLazyPtr supports custom destroy methods.
+ {
+ SkLazyPtr<int, custom_destroy> customDestroy;
+ ptr = customDestroy.get();
+ // custom_destroy called here.
+ }
+ REPORTER_ASSERT(r, ptr);
+ REPORTER_ASSERT(r, 99 == *ptr);
+ // Since custom_destroy didn't actually delete ptr, we do now.
+ SkDELETE(ptr);
+}
+
+namespace {
+
+struct Racer : public SkRunnable {
+ Racer() : fLazy(NULL), fSeen(NULL) {}
+
+ virtual void run() SK_OVERRIDE { fSeen = fLazy->get(); }
+
+ SkLazyPtr<int>* fLazy;
+ int* fSeen;
+};
+
+} // namespace
+
+DEF_TEST(LazyPtr_Threaded, r) {
+ static const int kRacers = 321;
+
+ SkLazyPtr<int> lazy;
+
+ Racer racers[kRacers];
+ for (int i = 0; i < kRacers; i++) {
+ racers[i].fLazy = &lazy;
+ }
+
+ SkTaskGroup tg;
+ for (int i = 0; i < kRacers; i++) {
+ tg.add(racers + i);
+ }
+ tg.wait();
+
+ for (int i = 1; i < kRacers; i++) {
+ REPORTER_ASSERT(r, racers[i].fSeen);
+ REPORTER_ASSERT(r, racers[i].fSeen == racers[0].fSeen);
+ }
+}