From 4d73ac22a1b99402fc8cff78a4eb4b27aa8fe019 Mon Sep 17 00:00:00 2001 From: "robertphillips@google.com" Date: Wed, 13 Jun 2012 18:54:08 +0000 Subject: 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 --- 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 + 6 files changed, 72 insertions(+), 31 deletions(-) (limited to 'include') diff --git a/include/core/SkInstCnt.h b/include/core/SkInstCnt.h index 3c63ef7e28..f3519a14dd 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 761546b12b..a00bc3e3f1 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 c809091181..fdaa30a47e 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 4a2c254da2..c26ca4bb7e 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 4c491c7369..94d2173ec2 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 1997f68ddf..704bb28f0b 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. * -- cgit v1.2.3