OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 | 8 |
9 #ifndef SkInstCnt_DEFINED | 9 #ifndef SkInstCnt_DEFINED |
10 #define SkInstCnt_DEFINED | 10 #define SkInstCnt_DEFINED |
11 | 11 |
12 /* | 12 /* |
13 * The instance counting system consists of three macros that create the | 13 * The instance counting system consists of three macros that create the |
14 * instance counting machinery. A class is added to the system by adding: | 14 * instance counting machinery. A class is added to the system by adding: |
15 * SK_DECLARE_INST_COUNT at the top of its declaration for derived classes | 15 * SK_DECLARE_INST_COUNT at the top of its declaration for derived classes |
16 * SK_DECLARE_INST_COUNT_ROOT at the top of its declaration for a root class | 16 * SK_DECLARE_INST_COUNT_ROOT at the top of its declaration for a root class |
17 * SK_DEFINE_INST_COUNT at the top of its .cpp file (for both kinds). | |
18 * At the end of an application a call to all the "root" objects' | 17 * At the end of an application a call to all the "root" objects' |
19 * CheckInstanceCount methods should be made | 18 * CheckInstanceCount methods should be made |
20 */ | 19 */ |
21 #include "SkTypes.h" | 20 #include "SkTypes.h" |
22 | 21 |
23 #if SK_ENABLE_INST_COUNT | 22 #if SK_ENABLE_INST_COUNT |
24 #include "SkTArray.h" | 23 #include "SkTArray.h" |
25 #include "SkThread_platform.h" | 24 #include "SkThread_platform.h" |
26 | 25 |
27 extern bool gPrintInstCount; | 26 extern bool gPrintInstCount; |
28 | 27 |
29 // The non-root classes just register themselves with their parent | 28 // The non-root classes just register themselves with their parent |
30 #define SK_DECLARE_INST_COUNT(className) \ | 29 #define SK_DECLARE_INST_COUNT(className) \ |
31 SK_DECLARE_INST_COUNT_INTERNAL(className, \ | 30 SK_DECLARE_INST_COUNT_INTERNAL(className, \ |
32 INHERITED::AddInstChild(CheckInstanceCount);,\ | 31 INHERITED::AddInstChild(CheckInstanceCount);) |
33 /**/) | |
34 | |
35 #define SK_DECLARE_INST_COUNT_TEMPLATE(className) \ | |
36 SK_DECLARE_INST_COUNT_INTERNAL(className, \ | |
37 INHERITED::AddInstChild(CheckInstanceCount);, \ | |
38 typename) | |
39 | 32 |
40 // The root classes registers a function to print out the memory stats when | 33 // The root classes registers a function to print out the memory stats when |
41 // the app ends | 34 // the app ends |
42 #define SK_DECLARE_INST_COUNT_ROOT(className) \ | 35 #define SK_DECLARE_INST_COUNT_ROOT(className) \ |
43 SK_DECLARE_INST_COUNT_INTERNAL(className, atexit(exitPrint);, /**/) | 36 SK_DECLARE_INST_COUNT_INTERNAL(className, atexit(exitPrint);) |
44 | 37 |
45 #define SK_DECLARE_INST_COUNT_INTERNAL(className, initStep, templateType) \ | 38 #define SK_DECLARE_INST_COUNT_INTERNAL(className, initStep) \ |
46 class SkInstanceCountHelper { \ | 39 class SkInstanceCountHelper { \ |
47 public: \ | 40 public: \ |
48 typedef int (*PFCheckInstCnt)(int level, bool cleanUp); \ | 41 typedef int (*PFCheckInstCnt)(int level, bool cleanUp); \ |
49 SkInstanceCountHelper() { \ | 42 SkInstanceCountHelper() { \ |
| 43 static bool gInited; \ |
50 if (!gInited) { \ | 44 if (!gInited) { \ |
51 initStep \ | 45 initStep \ |
52 gChildren = new SkTArray<PFCheckInstCnt>; \ | 46 GetChildren() = new SkTArray<PFCheckInstCnt>; \ |
53 gInited = true; \ | 47 gInited = true; \ |
54 } \ | 48 } \ |
55 sk_atomic_inc(&gInstanceCount); \ | 49 sk_atomic_inc(GetInstanceCountPtr()); \ |
56 } \ | 50 } \ |
57 \ | 51 \ |
58 SkInstanceCountHelper(const SkInstanceCountHelper&) { \ | 52 SkInstanceCountHelper(const SkInstanceCountHelper&) { \ |
59 sk_atomic_inc(&gInstanceCount); \ | 53 sk_atomic_inc(GetInstanceCountPtr()); \ |
60 } \ | 54 } \ |
61 \ | 55 \ |
62 ~SkInstanceCountHelper() { \ | 56 ~SkInstanceCountHelper() { \ |
63 sk_atomic_dec(&gInstanceCount); \ | 57 sk_atomic_dec(GetInstanceCountPtr()); \ |
64 } \ | 58 } \ |
65 \ | 59 \ |
66 static int32_t gInstanceCount; \ | 60 static int32_t* GetInstanceCountPtr() { \ |
67 static bool gInited; \ | 61 static int32_t gInstanceCount; \ |
68 static SkTArray<PFCheckInstCnt>* gChildren; \ | 62 return &gInstanceCount; \ |
| 63 } \ |
| 64 \ |
| 65 static SkTArray<PFCheckInstCnt>*& GetChildren() { \ |
| 66 static SkTArray<PFCheckInstCnt>* gChildren; \ |
| 67 return gChildren; \ |
| 68 } \ |
| 69 \ |
69 } fInstanceCountHelper; \ | 70 } fInstanceCountHelper; \ |
70 \ | 71 \ |
71 static int32_t GetInstanceCount() { \ | 72 static int32_t GetInstanceCount() { \ |
72 return SkInstanceCountHelper::gInstanceCount; \ | 73 return *SkInstanceCountHelper::GetInstanceCountPtr(); \ |
73 } \ | 74 } \ |
74 \ | 75 \ |
75 static void exitPrint() { \ | 76 static void exitPrint() { \ |
76 CheckInstanceCount(0, true); \ | 77 CheckInstanceCount(0, true); \ |
77 } \ | 78 } \ |
78 \ | 79 \ |
79 static int CheckInstanceCount(int level = 0, bool cleanUp = false) { \ | 80 static int CheckInstanceCount(int level = 0, bool cleanUp = false) { \ |
80 if (gPrintInstCount && 0 != SkInstanceCountHelper::gInstanceCount) {\ | 81 if (gPrintInstCount && 0 != GetInstanceCount()) { \ |
81 SkDebugf("%*c Leaked %s: %d\n", \ | 82 SkDebugf("%*c Leaked %s: %d\n", \ |
82 4*level, ' ', #className, \ | 83 4*level, ' ', #className, \ |
83 SkInstanceCountHelper::gInstanceCount); \ | 84 GetInstanceCount()); \ |
84 } \ | 85 } \ |
85 if (NULL == SkInstanceCountHelper::gChildren) { \ | 86 if (NULL == SkInstanceCountHelper::GetChildren()) { \ |
86 return SkInstanceCountHelper::gInstanceCount; \ | 87 return GetInstanceCount(); \ |
87 } \ | 88 } \ |
88 int childCount = SkInstanceCountHelper::gChildren->count(); \ | 89 SkTArray<int (*)(int, bool)>* children = \ |
89 int count = SkInstanceCountHelper::gInstanceCount; \ | 90 SkInstanceCountHelper::GetChildren(); \ |
| 91 int childCount = children->count(); \ |
| 92 int count = GetInstanceCount(); \ |
90 for (int i = 0; i < childCount; ++i) { \ | 93 for (int i = 0; i < childCount; ++i) { \ |
91 count -= (*(*SkInstanceCountHelper::gChildren)[i])(level+1, cleanUp)
; \ | 94 count -= (*(*children)[i])(level+1, cleanUp); \ |
92 } \ | 95 } \ |
93 SkASSERT(count >= 0); \ | 96 SkASSERT(count >= 0); \ |
94 if (gPrintInstCount && childCount > 0 && count > 0) { \ | 97 if (gPrintInstCount && childCount > 0 && count > 0) { \ |
95 SkDebugf("%*c Leaked ???: %d\n", 4*(level + 1), ' ', count); \ | 98 SkDebugf("%*c Leaked ???: %d\n", 4*(level + 1), ' ', count); \ |
96 } \ | 99 } \ |
97 if (cleanUp) { \ | 100 if (cleanUp) { \ |
98 delete SkInstanceCountHelper::gChildren; \ | 101 delete children; \ |
99 SkInstanceCountHelper::gChildren = NULL; \ | 102 SkInstanceCountHelper::GetChildren() = NULL; \ |
100 } \ | 103 } \ |
101 return SkInstanceCountHelper::gInstanceCount; \ | 104 return GetInstanceCount(); \ |
102 } \ | 105 } \ |
103 \ | 106 \ |
104 static void AddInstChild(templateType SkInstanceCountHelper::PFCheckInstCnt
\ | 107 static void AddInstChild(int (*childCheckInstCnt)(int, bool)) { \ |
105 childCheckInstCnt) { \ | |
106 if (CheckInstanceCount != childCheckInstCnt && \ | 108 if (CheckInstanceCount != childCheckInstCnt && \ |
107 NULL != SkInstanceCountHelper::gChildren) { \ | 109 NULL != SkInstanceCountHelper::GetChildren()) { \ |
108 SkInstanceCountHelper::gChildren->push_back(childCheckInstCnt); \ | 110 SkInstanceCountHelper::GetChildren()->push_back(childCheckInstCnt);
\ |
109 } \ | 111 } \ |
110 } | 112 } |
111 | 113 |
112 #define SK_DEFINE_INST_COUNT(className) \ | 114 #else |
113 int32_t className::SkInstanceCountHelper::gInstanceCount = 0; \ | 115 // Typically SK_ENABLE_INST_COUNT=0. Make sure the class declares public typedef
INHERITED by |
114 bool className::SkInstanceCountHelper::gInited = false; \ | 116 // causing a compile-time error if the typedef is missing. This way SK_ENABLE_IN
ST_COUNT=1 stays |
115 SkTArray<className::SkInstanceCountHelper::PFCheckInstCnt>* \ | 117 // compiling. |
116 className::SkInstanceCountHelper::gChildren = NULL; | 118 #define SK_DECLARE_INST_COUNT(className) static void AddInstChild() { INHERITED:
:AddInstChild(); } |
| 119 #define SK_DECLARE_INST_COUNT_ROOT(className) static void AddInstChild() { } |
| 120 #endif |
117 | 121 |
118 #define SK_DEFINE_INST_COUNT_TEMPLATE(templateInfo, className) \ | 122 // Following are deprecated. They are defined only for backwards API compatibili
ty. |
119 templateInfo int32_t className::SkInstanceCountHelper::gInstanceCount = 0;\ | 123 #define SK_DECLARE_INST_COUNT_TEMPLATE(className) SK_DECLARE_INST_COUNT(classNam
e) |
120 templateInfo bool className::SkInstanceCountHelper::gInited = false; \ | |
121 templateInfo \ | |
122 SkTArray<typename className::SkInstanceCountHelper::PFCheckInstCnt>*\ | |
123 className::SkInstanceCountHelper::gChildren = NULL; | |
124 | |
125 #else | |
126 #define SK_DECLARE_INST_COUNT(className) | |
127 #define SK_DECLARE_INST_COUNT_TEMPLATE(className) | |
128 #define SK_DECLARE_INST_COUNT_ROOT(className) | |
129 #define SK_DEFINE_INST_COUNT(className) | 124 #define SK_DEFINE_INST_COUNT(className) |
130 #define SK_DEFINE_INST_COUNT_TEMPLATE(templateInfo, className) | 125 #define SK_DEFINE_INST_COUNT_TEMPLATE(templateInfo, className) |
131 #endif | |
132 | 126 |
133 #endif // SkInstCnt_DEFINED | 127 #endif // SkInstCnt_DEFINED |
OLD | NEW |