2012-06-12 00:32:28 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2012-06-05 19:35:09 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef SkInstCnt_DEFINED
|
|
|
|
#define SkInstCnt_DEFINED
|
|
|
|
|
|
|
|
/*
|
2012-08-23 18:09:54 +00:00
|
|
|
* The instance counting system consists of three macros that create the
|
2012-06-05 19:35:09 +00:00
|
|
|
* instance counting machinery. A class is added to the system by adding:
|
2012-06-13 18:54:08 +00:00
|
|
|
* 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
|
|
|
|
* At the end of an application a call to all the "root" objects'
|
|
|
|
* CheckInstanceCount methods should be made
|
2012-06-05 19:35:09 +00:00
|
|
|
*/
|
2013-01-15 20:37:04 +00:00
|
|
|
#include "SkTypes.h"
|
2012-09-24 19:33:57 +00:00
|
|
|
|
2013-01-15 20:37:04 +00:00
|
|
|
#if SK_ENABLE_INST_COUNT
|
2012-06-13 18:54:08 +00:00
|
|
|
#include "SkTArray.h"
|
2012-08-29 18:52:07 +00:00
|
|
|
#include "SkThread_platform.h"
|
2012-06-13 18:54:08 +00:00
|
|
|
|
2012-06-27 19:41:42 +00:00
|
|
|
extern bool gPrintInstCount;
|
|
|
|
|
2012-06-27 19:52:20 +00:00
|
|
|
// The non-root classes just register themselves with their parent
|
2012-06-13 18:54:08 +00:00
|
|
|
#define SK_DECLARE_INST_COUNT(className) \
|
|
|
|
SK_DECLARE_INST_COUNT_INTERNAL(className, \
|
2013-12-05 12:08:12 +00:00
|
|
|
INHERITED::AddInstChild(CheckInstanceCount);)
|
2012-06-13 18:54:08 +00:00
|
|
|
|
2012-06-27 19:52:20 +00:00
|
|
|
// The root classes registers a function to print out the memory stats when
|
|
|
|
// the app ends
|
2012-06-13 18:54:08 +00:00
|
|
|
#define SK_DECLARE_INST_COUNT_ROOT(className) \
|
2013-12-05 12:08:12 +00:00
|
|
|
SK_DECLARE_INST_COUNT_INTERNAL(className, atexit(exitPrint);)
|
2012-06-13 18:54:08 +00:00
|
|
|
|
2013-12-05 12:08:12 +00:00
|
|
|
#define SK_DECLARE_INST_COUNT_INTERNAL(className, initStep) \
|
2012-06-13 18:54:08 +00:00
|
|
|
class SkInstanceCountHelper { \
|
|
|
|
public: \
|
2012-06-28 21:30:45 +00:00
|
|
|
typedef int (*PFCheckInstCnt)(int level, bool cleanUp); \
|
2012-06-13 18:54:08 +00:00
|
|
|
SkInstanceCountHelper() { \
|
2013-12-12 14:24:20 +00:00
|
|
|
static bool gInited; \
|
|
|
|
if (!gInited) { \
|
|
|
|
initStep \
|
|
|
|
GetChildren() = new SkTArray<PFCheckInstCnt>; \
|
|
|
|
gInited = true; \
|
|
|
|
} \
|
2013-12-05 12:08:12 +00:00
|
|
|
sk_atomic_inc(GetInstanceCountPtr()); \
|
2012-06-13 18:54:08 +00:00
|
|
|
} \
|
|
|
|
\
|
2013-03-11 18:50:03 +00:00
|
|
|
SkInstanceCountHelper(const SkInstanceCountHelper&) { \
|
2013-12-05 12:08:12 +00:00
|
|
|
sk_atomic_inc(GetInstanceCountPtr()); \
|
2012-06-13 18:54:08 +00:00
|
|
|
} \
|
|
|
|
\
|
|
|
|
~SkInstanceCountHelper() { \
|
2013-12-05 12:08:12 +00:00
|
|
|
sk_atomic_dec(GetInstanceCountPtr()); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
static int32_t* GetInstanceCountPtr() { \
|
|
|
|
static int32_t gInstanceCount; \
|
|
|
|
return &gInstanceCount; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
static SkTArray<PFCheckInstCnt>*& GetChildren() { \
|
|
|
|
static SkTArray<PFCheckInstCnt>* gChildren; \
|
|
|
|
return gChildren; \
|
2012-06-13 18:54:08 +00:00
|
|
|
} \
|
|
|
|
\
|
|
|
|
} fInstanceCountHelper; \
|
|
|
|
\
|
2012-06-19 15:40:27 +00:00
|
|
|
static int32_t GetInstanceCount() { \
|
2013-12-05 12:08:12 +00:00
|
|
|
return *SkInstanceCountHelper::GetInstanceCountPtr(); \
|
2012-06-19 15:40:27 +00:00
|
|
|
} \
|
|
|
|
\
|
2012-06-27 19:52:20 +00:00
|
|
|
static void exitPrint() { \
|
2012-06-28 21:30:45 +00:00
|
|
|
CheckInstanceCount(0, true); \
|
2012-06-27 19:52:20 +00:00
|
|
|
} \
|
|
|
|
\
|
2012-06-28 21:30:45 +00:00
|
|
|
static int CheckInstanceCount(int level = 0, bool cleanUp = false) { \
|
2013-12-05 12:08:12 +00:00
|
|
|
if (gPrintInstCount && 0 != GetInstanceCount()) { \
|
2012-06-26 18:58:03 +00:00
|
|
|
SkDebugf("%*c Leaked %s: %d\n", \
|
2012-06-21 20:25:03 +00:00
|
|
|
4*level, ' ', #className, \
|
2013-12-05 12:08:12 +00:00
|
|
|
GetInstanceCount()); \
|
2012-06-13 18:54:08 +00:00
|
|
|
} \
|
2013-12-05 12:08:12 +00:00
|
|
|
if (NULL == SkInstanceCountHelper::GetChildren()) { \
|
|
|
|
return GetInstanceCount(); \
|
2012-06-28 21:30:45 +00:00
|
|
|
} \
|
2013-12-12 14:24:20 +00:00
|
|
|
SkTArray<int (*)(int, bool)>* children = \
|
2013-12-05 12:08:12 +00:00
|
|
|
SkInstanceCountHelper::GetChildren(); \
|
|
|
|
int childCount = children->count(); \
|
|
|
|
int count = GetInstanceCount(); \
|
2012-06-26 18:58:03 +00:00
|
|
|
for (int i = 0; i < childCount; ++i) { \
|
2013-12-05 12:08:12 +00:00
|
|
|
count -= (*(*children)[i])(level+1, cleanUp); \
|
2012-06-13 18:54:08 +00:00
|
|
|
} \
|
2012-06-26 18:58:03 +00:00
|
|
|
SkASSERT(count >= 0); \
|
2012-06-27 19:41:42 +00:00
|
|
|
if (gPrintInstCount && childCount > 0 && count > 0) { \
|
2012-06-26 18:58:03 +00:00
|
|
|
SkDebugf("%*c Leaked ???: %d\n", 4*(level + 1), ' ', count); \
|
|
|
|
} \
|
2012-06-28 21:30:45 +00:00
|
|
|
if (cleanUp) { \
|
2013-12-05 12:08:12 +00:00
|
|
|
delete children; \
|
|
|
|
SkInstanceCountHelper::GetChildren() = NULL; \
|
2012-06-28 21:30:45 +00:00
|
|
|
} \
|
2013-12-05 12:08:12 +00:00
|
|
|
return GetInstanceCount(); \
|
2012-06-13 18:54:08 +00:00
|
|
|
} \
|
|
|
|
\
|
2013-12-05 12:08:12 +00:00
|
|
|
static void AddInstChild(int (*childCheckInstCnt)(int, bool)) { \
|
2013-12-12 14:24:20 +00:00
|
|
|
if (CheckInstanceCount != childCheckInstCnt && \
|
|
|
|
NULL != SkInstanceCountHelper::GetChildren()) { \
|
2013-12-05 12:08:12 +00:00
|
|
|
SkInstanceCountHelper::GetChildren()->push_back(childCheckInstCnt); \
|
2012-06-13 18:54:08 +00:00
|
|
|
} \
|
2012-06-05 19:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2013-12-05 12:08:12 +00:00
|
|
|
// Typically SK_ENABLE_INST_COUNT=0. Make sure the class declares public typedef INHERITED by
|
|
|
|
// causing a compile-time error if the typedef is missing. This way SK_ENABLE_INST_COUNT=1 stays
|
|
|
|
// compiling.
|
|
|
|
#define SK_DECLARE_INST_COUNT(className) static void AddInstChild() { INHERITED::AddInstChild(); }
|
|
|
|
#define SK_DECLARE_INST_COUNT_ROOT(className) static void AddInstChild() { }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Following are deprecated. They are defined only for backwards API compatibility.
|
|
|
|
#define SK_DECLARE_INST_COUNT_TEMPLATE(className) SK_DECLARE_INST_COUNT(className)
|
2012-06-13 18:54:08 +00:00
|
|
|
#define SK_DEFINE_INST_COUNT(className)
|
2012-08-22 17:31:22 +00:00
|
|
|
#define SK_DEFINE_INST_COUNT_TEMPLATE(templateInfo, className)
|
2012-06-05 19:35:09 +00:00
|
|
|
|
|
|
|
#endif // SkInstCnt_DEFINED
|