Index: include/core/SkRefCnt.h |
diff --git a/include/core/SkRefCnt.h b/include/core/SkRefCnt.h |
index 4da2fbbc936266662a4510b64a8ce3a9de921972..9b246f4f5499eeed6bfa12ad596beb275bd0b545 100644 |
--- a/include/core/SkRefCnt.h |
+++ b/include/core/SkRefCnt.h |
@@ -247,4 +247,23 @@ public: |
}; |
#define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref) |
+// This is a variant of SkRefCnt that's Not Virtual, so weighs 4 bytes instead of 8 or 16. |
+// There's only benefit to using this if the deriving class does not otherwise need a vtable. |
+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 |