Version 2 of the Instance Counting system. This one simplifies the print out of infor...
authorrobertphillips@google.com <robertphillips@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Wed, 13 Jun 2012 18:54:08 +0000 (18:54 +0000)
committerrobertphillips@google.com <robertphillips@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Wed, 13 Jun 2012 18:54:08 +0000 (18:54 +0000)
http://codereview.appspot.com/6296069/

git-svn-id: http://skia.googlecode.com/svn/trunk@4255 2bbb7eff-a529-9590-31e7-b0007b416f81

18 files changed:
gm/gmmain.cpp
include/core/SkInstCnt.h
include/core/SkRefCnt.h
include/gpu/GrAARectRenderer.h
include/gpu/GrRenderTarget.h
include/gpu/GrResource.h
include/gpu/GrTexture.h
src/core/SkPathHeap.cpp
src/core/SkPathHeap.h
src/core/SkRefCnt.cpp
src/gpu/GrAARectRenderer.cpp
src/gpu/GrPathRendererChain.cpp
src/gpu/GrPathRendererChain.h
src/gpu/GrRenderTarget.cpp
src/gpu/GrResource.cpp
src/gpu/GrTexture.cpp
src/pdf/SkPDFTypes.cpp
src/pdf/SkPDFTypes.h

index e91ea8f..877d0af 100644 (file)
@@ -1086,9 +1086,9 @@ int main(int argc, char * const argv[]) {
     delete grFactory;
     SkGraphics::Term();
 
-    PRINT_INST_COUNT(SkRefCnt);
-    PRINT_INST_COUNT(GrResource);
-    PRINT_INST_COUNT(GrAARectRenderer);
+#ifdef SK_DEBUG
+    SkRefCnt::CheckInstanceCount();
+#endif
 
     return (0 == testsFailed) ? 0 : -1;
 }
index 3c63ef7..f3519a1 100644 (file)
 /*
  * The instance counting system consists of three macros that create the 
  * instance counting machinery. A class is added to the system by adding:
- *   DECLARE_INST_COUNT at the top of its declaration
- *   DEFINE_INST_COUNT at the top of its .cpp file
- *   and a PRINT_INST_COUNT line at the application's end point
+ *   SK_DECLARE_INST_COUNT at the top of its declaration for derived classes
+ *   SK_DECLARE_INST_COUNT_ROOT at the top of its declaration for a root class
+ *   SK_DEFINE_INST_COUNT at the top of its .cpp file (for both kinds).
+ * At the end of an application a call to all the "root" objects'
+ * CheckInstanceCount methods should be made
  */
 #ifdef SK_DEBUG
-#define DECLARE_INST_COUNT                  \
-    class SkInstanceCountHelper {           \
-    public:                                 \
-        SkInstanceCountHelper() {           \
-            gInstanceCount++;               \
-        }                                   \
-                                            \
-        ~SkInstanceCountHelper() {          \
-            gInstanceCount--;               \
-        }                                   \
-                                            \
-        static int32_t gInstanceCount;      \
-    } fInstanceCountHelper;                 \
-                                            \
-    static int32_t GetInstanceCount() {     \
-        return SkInstanceCountHelper::gInstanceCount;   \
+#include "SkTArray.h"
+
+#define SK_DECLARE_INST_COUNT(className)                                    \
+    SK_DECLARE_INST_COUNT_INTERNAL(className,                               \
+                                INHERITED::AddInstChild(CheckInstanceCount);)
+
+#define SK_DECLARE_INST_COUNT_ROOT(className)                               \
+    SK_DECLARE_INST_COUNT_INTERNAL(className, ;)
+
+#define SK_DECLARE_INST_COUNT_INTERNAL(className, initStep)                 \
+    class SkInstanceCountHelper {                                           \
+    public:                                                                 \
+        typedef void (*PFCheckInstCnt)();                                   \
+        SkInstanceCountHelper() {                                           \
+            if (!gInited) {                                                 \
+                initStep                                                    \
+                gInited = true;                                             \
+            }                                                               \
+            gInstanceCount++;                                               \
+        }                                                                   \
+                                                                            \
+        SkInstanceCountHelper(const SkInstanceCountHelper& other) {         \
+            gInstanceCount++;                                               \
+        }                                                                   \
+                                                                            \
+        ~SkInstanceCountHelper() {                                          \
+            gInstanceCount--;                                               \
+        }                                                                   \
+                                                                            \
+        static int32_t gInstanceCount;                                      \
+        static bool gInited;                                                \
+        static SkTArray<PFCheckInstCnt> gChildren;                          \
+    } fInstanceCountHelper;                                                 \
+                                                                            \
+    static void CheckInstanceCount() {                                      \
+        if (0 != SkInstanceCountHelper::gInstanceCount) {                   \
+            SkDebugf("Leaked %s objects: %d\n", #className,                 \
+                     SkInstanceCountHelper::gInstanceCount);                \
+        }                                                                   \
+        for (int i = 0; i < SkInstanceCountHelper::gChildren.count(); ++i) { \
+            (*SkInstanceCountHelper::gChildren[i])();                       \
+        }                                                                   \
+    }                                                                       \
+                                                                            \
+    static void AddInstChild(SkInstanceCountHelper::PFCheckInstCnt          \
+                                                       childCheckInstCnt) { \
+        if (CheckInstanceCount != childCheckInstCnt) {                      \
+            SkInstanceCountHelper::gChildren.push_back(childCheckInstCnt);  \
+        }                                                                   \
     }
 
-#define DEFINE_INST_COUNT(className)        \
-    int32_t className::SkInstanceCountHelper::gInstanceCount = 0;
+#define SK_DEFINE_INST_COUNT(className)                                     \
+    int32_t className::SkInstanceCountHelper::gInstanceCount = 0;           \
+    bool className::SkInstanceCountHelper::gInited = false;                 \
+    SkTArray<className::SkInstanceCountHelper::PFCheckInstCnt>              \
+                        className::SkInstanceCountHelper::gChildren;
 
-#define PRINT_INST_COUNT(className)         \
-    SkDebugf("Leaked %s objects: %d\n",     \
-                  #className,               \
-                  className::GetInstanceCount());
 #else
-#define DECLARE_INST_COUNT
-#define DEFINE_INST_COUNT(className)
-#define PRINT_INST_COUNT(className)
+#define SK_DECLARE_INST_COUNT(className)
+#define SK_DECLARE_INST_COUNT_ROOT(className)
+#define SK_DEFINE_INST_COUNT(className)
 #endif
 
 #endif // SkInstCnt_DEFINED
index 761546b..a00bc3e 100644 (file)
@@ -25,7 +25,7 @@
 */
 class SK_API SkRefCnt : SkNoncopyable {
 public:
-    DECLARE_INST_COUNT
+    SK_DECLARE_INST_COUNT_ROOT(SkRefCnt)
 
     /** Default construct, initializing the reference count to 1.
     */
@@ -83,6 +83,8 @@ private:
     friend class SkWeakRefCnt;
 
     mutable int32_t fRefCnt;
+
+    typedef SkNoncopyable INHERITED;
 };
 
 ///////////////////////////////////////////////////////////////////////////////
index c809091..fdaa30a 100644 (file)
@@ -21,7 +21,7 @@ class GrIndexBuffer;
  */
 class GrAARectRenderer : public GrRefCnt {
 public:
-    DECLARE_INST_COUNT
+    SK_DECLARE_INST_COUNT(GrAARectRenderer)
 
     GrAARectRenderer() 
     : fAAFillRectIndexBuffer(NULL)
@@ -62,6 +62,8 @@ private:
 
     static int aaStrokeRectIndexCount();
     GrIndexBuffer* aaStrokeRectIndexBuffer(GrGpu* gpu);
+
+    typedef GrRefCnt INHERITED;
 };
 
 #endif // GrAARectRenderer_DEFINED
index 4a2c254..c26ca4b 100644 (file)
@@ -33,6 +33,7 @@ class GrTexture;
  */
 class GrRenderTarget : public GrResource {
 public:
+    SK_DECLARE_INST_COUNT(GrRenderTarget)
 
     /**
      * @return the width of the rendertarget
index 4c491c7..94d2173 100644 (file)
@@ -20,7 +20,7 @@ class GrContext;
  */
 class GrResource : public GrRefCnt {
 public:
-    DECLARE_INST_COUNT
+    SK_DECLARE_INST_COUNT(GrResource)
 
     /**
      * Frees the resource in the underlying 3D API. It must be safe to call this
index 1997f68..704bb28 100644 (file)
@@ -31,6 +31,8 @@ static const uint64_t kScratch_CacheID = 0xBBBBBBBB;
 class GrTexture : public GrResource {
 
 public:
+    SK_DECLARE_INST_COUNT(GrTexture)
+
     /**
      * Retrieves the width of the texture.
      *
index a9183c3..8713e76 100644 (file)
@@ -11,6 +11,8 @@
 #include "SkFlattenable.h"
 #include <new>
 
+SK_DEFINE_INST_COUNT(SkPathHeap)
+
 #define kPathCount  64
 
 SkPathHeap::SkPathHeap() : fHeap(kPathCount * sizeof(SkPath)) {
index 99c2626..32adbc0 100644 (file)
@@ -18,8 +18,10 @@ class SkFlattenableWriteBuffer;
 
 class SkPathHeap : public SkRefCnt {
 public:
-            SkPathHeap();
-            SkPathHeap(SkFlattenableReadBuffer&);
+    SK_DECLARE_INST_COUNT(SkPathHeap)
+
+    SkPathHeap();
+    SkPathHeap(SkFlattenableReadBuffer&);
     virtual ~SkPathHeap();
 
     /** Copy the path into the heap, and return the new total number of paths.
@@ -41,6 +43,8 @@ private:
     SkChunkAlloc        fHeap;
     // we just store ptrs into fHeap here
     SkTDArray<SkPath*>  fPaths;
+
+    typedef SkRefCnt INHERITED;
 };
 
 #endif
index 111ecd8..599e7f2 100644 (file)
@@ -1,12 +1,12 @@
-/*\r
- * Copyright 2012 Google Inc.\r
- *\r
- * Use of this source code is governed by a BSD-style license that can be\r
- * found in the LICENSE file.\r
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
  */
 
 
 #include "SkRefCnt.h"
 
-DEFINE_INST_COUNT(SkRefCnt)
+SK_DEFINE_INST_COUNT(SkRefCnt)
 
index 3eca362..a771532 100644 (file)
@@ -9,7 +9,7 @@
 #include "GrRefCnt.h"
 #include "GrGpu.h"
 
-DEFINE_INST_COUNT(GrAARectRenderer)
+SK_DEFINE_INST_COUNT(GrAARectRenderer)
 
 namespace {
 
index 00f0b81..648225e 100644 (file)
@@ -13,6 +13,8 @@
 #include "GrDefaultPathRenderer.h"
 #include "GrGpu.h"
 
+SK_DEFINE_INST_COUNT(GrPathRendererChain)
+
 GrPathRendererChain::GrPathRendererChain(GrContext* context, UsageFlags flags)
     : fInit(false)
     , fOwner(context)
index 529b4cb..e5ccabb 100644 (file)
@@ -27,6 +27,7 @@ class GrPathRenderer;
  */
 class GrPathRendererChain : public SkRefCnt {
 public:
+    SK_DECLARE_INST_COUNT(GrPathRendererChain)
 
     enum UsageFlags {
         kNone_UsageFlag      = 0,
@@ -58,6 +59,8 @@ private:
     GrContext*                                          fOwner;
     UsageFlags                                          fFlags;
     SkSTArray<kPreAllocCount, GrPathRenderer*, true>    fChain;
+
+    typedef SkRefCnt INHERITED;
 };
 
 GR_MAKE_BITFIELD_OPS(GrPathRendererChain::UsageFlags)
index be08444..071c885 100644 (file)
@@ -13,6 +13,8 @@
 #include "GrGpu.h"
 #include "GrStencilBuffer.h"
 
+SK_DEFINE_INST_COUNT(GrRenderTarget)
+
 bool GrRenderTarget::readPixels(int left, int top, int width, int height,
                                 GrPixelConfig config, void* buffer,
                                 size_t rowBytes) {
index a5168c8..7e2d4a8 100644 (file)
@@ -10,7 +10,7 @@
 #include "GrResource.h"
 #include "GrGpu.h"
 
-DEFINE_INST_COUNT(GrResource)
+SK_DEFINE_INST_COUNT(GrResource)
 
 GrResource::GrResource(GrGpu* gpu) {
     fGpu        = gpu;
index 4f4c5f5..67ce629 100644 (file)
@@ -14,6 +14,8 @@
 #include "GrRenderTarget.h"
 #include "GrResourceCache.h"
 
+SK_DEFINE_INST_COUNT(GrTexture)
+
 bool GrTexture::readPixels(int left, int top, int width, int height,
                            GrPixelConfig config, void* buffer,
                            size_t rowBytes) {
index 5a0ede8..b367318 100644 (file)
@@ -17,6 +17,8 @@
     #define SNPRINTF    snprintf
 #endif
 
+SK_DEFINE_INST_COUNT(SkPDFObject)
+
 SkPDFObject::SkPDFObject() {}
 SkPDFObject::~SkPDFObject() {}
 
index 6334938..d5fe4ae 100644 (file)
@@ -27,6 +27,8 @@ class SkWStream;
 */
 class SkPDFObject : public SkRefCnt {
 public:
+    SK_DECLARE_INST_COUNT(SkPDFObject)
+
     /** Create a PDF object.
      */
     SkPDFObject();
@@ -91,6 +93,8 @@ protected:
      */
     virtual void emitObject(SkWStream* stream, SkPDFCatalog* catalog,
                             bool indirect) = 0;
+
+        typedef SkRefCnt INHERITED;
 };
 
 /** \class SkPDFObjRef