From 4d73ac22a1b99402fc8cff78a4eb4b27aa8fe019 Mon Sep 17 00:00:00 2001 From: "robertphillips@google.com" Date: Wed, 13 Jun 2012 18:54:08 +0000 Subject: [PATCH] Version 2 of the Instance Counting system. This one simplifies the print out of information. http://codereview.appspot.com/6296069/ git-svn-id: http://skia.googlecode.com/svn/trunk@4255 2bbb7eff-a529-9590-31e7-b0007b416f81 --- gm/gmmain.cpp | 6 +-- include/core/SkInstCnt.h | 90 ++++++++++++++++++++++++++++------------- include/core/SkRefCnt.h | 4 +- include/gpu/GrAARectRenderer.h | 4 +- include/gpu/GrRenderTarget.h | 1 + include/gpu/GrResource.h | 2 +- include/gpu/GrTexture.h | 2 + src/core/SkPathHeap.cpp | 2 + src/core/SkPathHeap.h | 8 +++- src/core/SkRefCnt.cpp | 12 +++--- src/gpu/GrAARectRenderer.cpp | 2 +- src/gpu/GrPathRendererChain.cpp | 2 + src/gpu/GrPathRendererChain.h | 3 ++ src/gpu/GrRenderTarget.cpp | 2 + src/gpu/GrResource.cpp | 2 +- src/gpu/GrTexture.cpp | 2 + src/pdf/SkPDFTypes.cpp | 2 + src/pdf/SkPDFTypes.h | 4 ++ 18 files changed, 106 insertions(+), 44 deletions(-) diff --git a/gm/gmmain.cpp b/gm/gmmain.cpp index e91ea8f..877d0af 100644 --- a/gm/gmmain.cpp +++ b/gm/gmmain.cpp @@ -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; } diff --git a/include/core/SkInstCnt.h b/include/core/SkInstCnt.h index 3c63ef7..f3519a1 100644 --- a/include/core/SkInstCnt.h +++ b/include/core/SkInstCnt.h @@ -12,40 +12,74 @@ /* * 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 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::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 diff --git a/include/core/SkRefCnt.h b/include/core/SkRefCnt.h index 761546b..a00bc3e 100644 --- a/include/core/SkRefCnt.h +++ b/include/core/SkRefCnt.h @@ -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; }; /////////////////////////////////////////////////////////////////////////////// diff --git a/include/gpu/GrAARectRenderer.h b/include/gpu/GrAARectRenderer.h index c809091..fdaa30a 100644 --- a/include/gpu/GrAARectRenderer.h +++ b/include/gpu/GrAARectRenderer.h @@ -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 diff --git a/include/gpu/GrRenderTarget.h b/include/gpu/GrRenderTarget.h index 4a2c254..c26ca4b 100644 --- a/include/gpu/GrRenderTarget.h +++ b/include/gpu/GrRenderTarget.h @@ -33,6 +33,7 @@ class GrTexture; */ class GrRenderTarget : public GrResource { public: + SK_DECLARE_INST_COUNT(GrRenderTarget) /** * @return the width of the rendertarget diff --git a/include/gpu/GrResource.h b/include/gpu/GrResource.h index 4c491c7..94d2173 100644 --- a/include/gpu/GrResource.h +++ b/include/gpu/GrResource.h @@ -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 diff --git a/include/gpu/GrTexture.h b/include/gpu/GrTexture.h index 1997f68..704bb28 100644 --- a/include/gpu/GrTexture.h +++ b/include/gpu/GrTexture.h @@ -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. * diff --git a/src/core/SkPathHeap.cpp b/src/core/SkPathHeap.cpp index a9183c3..8713e76 100644 --- a/src/core/SkPathHeap.cpp +++ b/src/core/SkPathHeap.cpp @@ -11,6 +11,8 @@ #include "SkFlattenable.h" #include +SK_DEFINE_INST_COUNT(SkPathHeap) + #define kPathCount 64 SkPathHeap::SkPathHeap() : fHeap(kPathCount * sizeof(SkPath)) { diff --git a/src/core/SkPathHeap.h b/src/core/SkPathHeap.h index 99c2626..32adbc0 100644 --- a/src/core/SkPathHeap.h +++ b/src/core/SkPathHeap.h @@ -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 fPaths; + + typedef SkRefCnt INHERITED; }; #endif diff --git a/src/core/SkRefCnt.cpp b/src/core/SkRefCnt.cpp index 111ecd8..599e7f2 100644 --- a/src/core/SkRefCnt.cpp +++ b/src/core/SkRefCnt.cpp @@ -1,12 +1,12 @@ -/* - * Copyright 2012 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. +/* + * 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) diff --git a/src/gpu/GrAARectRenderer.cpp b/src/gpu/GrAARectRenderer.cpp index 3eca362..a771532 100644 --- a/src/gpu/GrAARectRenderer.cpp +++ b/src/gpu/GrAARectRenderer.cpp @@ -9,7 +9,7 @@ #include "GrRefCnt.h" #include "GrGpu.h" -DEFINE_INST_COUNT(GrAARectRenderer) +SK_DEFINE_INST_COUNT(GrAARectRenderer) namespace { diff --git a/src/gpu/GrPathRendererChain.cpp b/src/gpu/GrPathRendererChain.cpp index 00f0b81..648225e 100644 --- a/src/gpu/GrPathRendererChain.cpp +++ b/src/gpu/GrPathRendererChain.cpp @@ -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) diff --git a/src/gpu/GrPathRendererChain.h b/src/gpu/GrPathRendererChain.h index 529b4cb..e5ccabb 100644 --- a/src/gpu/GrPathRendererChain.h +++ b/src/gpu/GrPathRendererChain.h @@ -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 fChain; + + typedef SkRefCnt INHERITED; }; GR_MAKE_BITFIELD_OPS(GrPathRendererChain::UsageFlags) diff --git a/src/gpu/GrRenderTarget.cpp b/src/gpu/GrRenderTarget.cpp index be08444..071c885 100644 --- a/src/gpu/GrRenderTarget.cpp +++ b/src/gpu/GrRenderTarget.cpp @@ -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) { diff --git a/src/gpu/GrResource.cpp b/src/gpu/GrResource.cpp index a5168c8..7e2d4a8 100644 --- a/src/gpu/GrResource.cpp +++ b/src/gpu/GrResource.cpp @@ -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; diff --git a/src/gpu/GrTexture.cpp b/src/gpu/GrTexture.cpp index 4f4c5f5..67ce629 100644 --- a/src/gpu/GrTexture.cpp +++ b/src/gpu/GrTexture.cpp @@ -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) { diff --git a/src/pdf/SkPDFTypes.cpp b/src/pdf/SkPDFTypes.cpp index 5a0ede8..b367318 100644 --- a/src/pdf/SkPDFTypes.cpp +++ b/src/pdf/SkPDFTypes.cpp @@ -17,6 +17,8 @@ #define SNPRINTF snprintf #endif +SK_DEFINE_INST_COUNT(SkPDFObject) + SkPDFObject::SkPDFObject() {} SkPDFObject::~SkPDFObject() {} diff --git a/src/pdf/SkPDFTypes.h b/src/pdf/SkPDFTypes.h index 6334938..d5fe4ae 100644 --- a/src/pdf/SkPDFTypes.h +++ b/src/pdf/SkPDFTypes.h @@ -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 -- 2.7.4