Index: include/core/SkRefCnt.h |
diff --git a/include/core/SkRefCnt.h b/include/core/SkRefCnt.h |
index 4da2fbbc936266662a4510b64a8ce3a9de921972..fd49cbea30b85e8b66d6607589e2eac7de325d9e 100644 |
--- a/include/core/SkRefCnt.h |
+++ b/include/core/SkRefCnt.h |
@@ -247,4 +247,21 @@ public: |
}; |
#define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref) |
tomhudson
2014/11/19 18:59:06
Can we put something like a short version of the C
mtklein
2014/11/19 19:19:14
Done.
|
+template <typename Derived> |
+class SkNVRefCnt : SkNoncopyable { |
+public: |
+ SkNVRefCnt() : fRefCnt(1) {} |
+ |
+ // Implementation is pretty much the same as SkRefCntBase. All required barriers are the same: |
+ // - unique() needs acquire when it returns true, and no barrier if it returns false; |
+ // - ref() doesn't need any barrier; |
+ // - unref() needs a release barrier, and an acquire if it's going to call delete. |
+ |
+ bool unique() const { return 1 == sk_acquire_load(&fRefCnt); } |
+ void ref() const { sk_atomic_inc(&fRefCnt); } |
+ void unref() const { if (1 == sk_atomic_dec(&fRefCnt)) { SkDELETE((const Derived*)this); } } |
+private: |
+ mutable int32_t fRefCnt; |
+}; |
+ |
#endif |