virtual ~SkData();
// Called the first time someone calls NewEmpty to initialize the singleton.
- static void NewEmptyImpl(SkData**);
+ static void NewEmptyImpl(int/*unused*/);
typedef SkRefCnt INHERITED;
};
// }
//
// OnceTest.cpp also should serve as a few other simple examples.
+//
+// You may optionally pass SkOnce a second function to be called at exit for cleanup.
#include "SkThread.h"
#include "SkTypes.h"
struct SkOnceFlag; // If manually created, initialize with SkOnceFlag once = SK_ONCE_INIT
template <typename Func, typename Arg>
-inline void SkOnce(SkOnceFlag* once, Func f, Arg arg);
+inline void SkOnce(SkOnceFlag* once, Func f, Arg arg, void(*atExit)() = NULL);
// ---------------------- Implementation details below here. -----------------------------
// This should be rarely called, so we separate it from SkOnce and don't mark it as inline.
// (We don't mind if this is an actual function call, but odds are it'll be inlined anyway.)
template <typename Func, typename Arg>
-static void sk_once_slow(SkOnceFlag* once, Func f, Arg arg) {
+static void sk_once_slow(SkOnceFlag* once, Func f, Arg arg, void (*atExit)()) {
const SkAutoSpinlock lock(&once->lock);
if (!once->done) {
f(arg);
+ if (atExit != NULL) {
+ atexit(atExit);
+ }
// Also known as a store-store/load-store barrier, this makes sure that the writes
// done before here---in particular, those done by calling f(arg)---are observable
// before the writes after the line, *done = true.
// This is our fast path, called all the time. We do really want it to be inlined.
template <typename Func, typename Arg>
-inline void SkOnce(SkOnceFlag* once, Func f, Arg arg) {
+inline void SkOnce(SkOnceFlag* once, Func f, Arg arg, void(*atExit)()) {
SK_ANNOTATE_BENIGN_RACE(&(once->done), "Don't worry TSAN, we're sure this is safe.");
if (!once->done) {
- sk_once_slow(once, f, arg);
+ sk_once_slow(once, f, arg, atExit);
}
// Also known as a load-load/load-store barrier, this acquire barrier makes
// sure that anything we read from memory---in particular, memory written by
/**
* Called the first time someone calls CreateEmpty to actually create the singleton.
*/
- static void CreateEmptyImpl(SkPathRef** empty);
+ static void CreateEmptyImpl(int/*unused*/);
void setIsOval(bool isOval) { fIsOval = isOval; }
///////////////////////////////////////////////////////////////////////////////
-void SkData::NewEmptyImpl(SkData** empty) {
- *empty = new SkData(NULL, 0, NULL, NULL);
+static SkData* gEmptyDataRef = NULL;
+static void cleanup_gEmptyDataRef() { gEmptyDataRef->unref(); }
+
+void SkData::NewEmptyImpl(int) {
+ gEmptyDataRef = new SkData(NULL, 0, NULL, NULL);
}
SkData* SkData::NewEmpty() {
- static SkData* gEmptyRef;
SK_DECLARE_STATIC_ONCE(once);
- SkOnce(&once, SkData::NewEmptyImpl, &gEmptyRef);
- gEmptyRef->ref();
- return gEmptyRef;
+ SkOnce(&once, SkData::NewEmptyImpl, 0, cleanup_gEmptyDataRef);
+ gEmptyDataRef->ref();
+ return gEmptyDataRef;
}
// assumes fPtr was allocated via sk_malloc
}
//////////////////////////////////////////////////////////////////////////////
-void SkPathRef::CreateEmptyImpl(SkPathRef** empty) {
- *empty = SkNEW(SkPathRef);
- (*empty)->computeBounds(); // Preemptively avoid a race to clear fBoundsIsDirty.
+static SkPathRef* gEmptyPathRef = NULL;
+static void cleanup_gEmptyPathRef() { gEmptyPathRef->unref(); }
+
+void SkPathRef::CreateEmptyImpl(int) {
+ gEmptyPathRef = SkNEW(SkPathRef);
+ gEmptyPathRef->computeBounds(); // Preemptively avoid a race to clear fBoundsIsDirty.
}
SkPathRef* SkPathRef::CreateEmpty() {
- static SkPathRef* gEmptyPathRef;
SK_DECLARE_STATIC_ONCE(once);
- SkOnce(&once, SkPathRef::CreateEmptyImpl, &gEmptyPathRef);
+ SkOnce(&once, SkPathRef::CreateEmptyImpl, 0, cleanup_gEmptyPathRef);
return SkRef(gEmptyPathRef);
}
#include "SkThread.h"
SK_DECLARE_STATIC_MUTEX(gMutex);
+static SkScaledImageCache* gScaledImageCache = NULL;
+static void cleanup_gScaledImageCache() { SkDELETE(gScaledImageCache); }
-static void create_cache(SkScaledImageCache** cache) {
+static void create_cache(int) {
#ifdef SK_USE_DISCARDABLE_SCALEDIMAGECACHE
- *cache = SkNEW_ARGS(SkScaledImageCache, (SkDiscardableMemory::Create));
+ gScaledImageCache = SkNEW_ARGS(SkScaledImageCache, (SkDiscardableMemory::Create));
#else
- *cache = SkNEW_ARGS(SkScaledImageCache, (SK_DEFAULT_IMAGE_CACHE_LIMIT));
+ gScaledImageCache = SkNEW_ARGS(SkScaledImageCache, (SK_DEFAULT_IMAGE_CACHE_LIMIT));
#endif
}
static SkScaledImageCache* get_cache() {
- static SkScaledImageCache* gCache(NULL);
- SK_DECLARE_STATIC_ONCE(create_cache_once);
- SkOnce(&create_cache_once, create_cache, &gCache);
- SkASSERT(NULL != gCache);
- return gCache;
+ SK_DECLARE_STATIC_ONCE(once);
+ SkOnce(&once, create_cache, 0, cleanup_gScaledImageCache);
+ SkASSERT(NULL != gScaledImageCache);
+ return gScaledImageCache;
}
}
}
-static void purge_global_scaled_image_cache() {
- size_t byteLimit = SkScaledImageCache::GetByteLimit();
- SkScaledImageCache::SetByteLimit(0);
- SkScaledImageCache::SetByteLimit(byteLimit);
-}
-
////////////////////////////////////////////////////////////////////////////////
static bool install_skCachingPixelRef(SkData* encoded, SkBitmap* dst) {
return SkCachingPixelRef::Install(
*/
DEF_TEST(DecodingImageGenerator, reporter) {
test_three_encodings(reporter, install_skCachingPixelRef);
- purge_global_scaled_image_cache();
test_three_encodings(reporter, install_skDiscardablePixelRef);
}
check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
reporter, kSkCaching_PixelRefType, NULL);
- purge_global_scaled_image_cache();
-
check_pixelref(TestImageGenerator::kFailGetInfo_TestType,
reporter, kSkDiscardable_PixelRefType, NULL);
check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
// Only one should have done the +=.
REPORTER_ASSERT(r, 6 == x);
}
+
+// Test that the atExit option works.
+static int gToDecrement = 1;
+static void noop(int) {}
+static void decrement() { gToDecrement--; }
+static void checkDecremented() { SkASSERT(gToDecrement == 0); }
+
+DEF_TEST(SkOnce_atExit, r) {
+ atexit(checkDecremented);
+ SK_DECLARE_STATIC_ONCE(once);
+ SkOnce(&once, noop, 0, decrement);
+}