Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Unified Diff: include/core/SkRefCnt.h

Issue 741793002: Add SkNVRefCnt, prune down SkPicture's size (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: tweak name Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « include/core/SkPicture.h ('k') | src/core/SkRecord.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« no previous file with comments | « include/core/SkPicture.h ('k') | src/core/SkRecord.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698