Chromium Code Reviews| 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 * 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' |
| 18 * CheckInstanceCount methods should be made | 18 * CheckInstanceCount methods should be made |
| 19 */ | 19 */ |
| 20 #include "SkTypes.h" | 20 #include "SkTypes.h" |
| 21 | 21 |
| 22 #if SK_ENABLE_INST_COUNT | 22 #if SK_ENABLE_INST_COUNT |
| 23 #include "SkOnce.h" | |
| 23 #include "SkTArray.h" | 24 #include "SkTArray.h" |
| 24 #include "SkThread_platform.h" | 25 #include "SkThread_platform.h" |
| 25 | 26 |
| 26 extern bool gPrintInstCount; | 27 extern bool gPrintInstCount; |
| 27 | 28 |
| 29 SkBaseMutex& SkGetInstCntAddInstChildMutex(); | |
| 30 | |
| 28 // The non-root classes just register themselves with their parent | 31 // The non-root classes just register themselves with their parent |
| 29 #define SK_DECLARE_INST_COUNT(className) \ | 32 #define SK_DECLARE_INST_COUNT(className) \ |
| 30 SK_DECLARE_INST_COUNT_INTERNAL(className, \ | 33 SK_DECLARE_INST_COUNT_INTERNAL(className, \ |
| 31 INHERITED::AddInstChild(CheckInstanceCount);) | 34 INHERITED::AddInstChild(CheckInstanceCount);) |
| 32 | 35 |
| 33 // The root classes registers a function to print out the memory stats when | 36 // The root classes registers a function to print out the memory stats when |
| 34 // the app ends | 37 // the app ends |
| 35 #define SK_DECLARE_INST_COUNT_ROOT(className) \ | 38 #define SK_DECLARE_INST_COUNT_ROOT(className) \ |
| 36 SK_DECLARE_INST_COUNT_INTERNAL(className, atexit(exitPrint);) | 39 SK_DECLARE_INST_COUNT_INTERNAL(className, atexit(exitPrint);) |
| 37 | 40 |
| 38 #define SK_DECLARE_INST_COUNT_INTERNAL(className, initStep) \ | 41 #define SK_DECLARE_INST_COUNT_INTERNAL(className, initStep) \ |
| 39 class SkInstanceCountHelper { \ | 42 class SkInstanceCountHelper { \ |
| 40 public: \ | 43 public: \ |
| 41 typedef int (*PFCheckInstCnt)(int level, bool cleanUp); \ | 44 typedef int (*PFCheckInstCnt)(int level, bool cleanUp); \ |
| 42 SkInstanceCountHelper() { \ | 45 SkInstanceCountHelper() { \ |
| 43 static bool gInited; \ | 46 SK_DECLARE_STATIC_ONCE(once); \ |
| 44 if (!gInited) { \ | 47 SkOnce<void*>(&once, init, NULL); \ |
| 45 initStep \ | |
| 46 GetChildren() = new SkTArray<PFCheckInstCnt>; \ | |
| 47 gInited = true; \ | |
| 48 } \ | |
| 49 sk_atomic_inc(GetInstanceCountPtr()); \ | 48 sk_atomic_inc(GetInstanceCountPtr()); \ |
| 50 } \ | 49 } \ |
| 51 \ | 50 \ |
| 51 static void init(void*) { \ | |
| 52 initStep \ | |
| 53 } \ | |
| 54 \ | |
| 52 SkInstanceCountHelper(const SkInstanceCountHelper&) { \ | 55 SkInstanceCountHelper(const SkInstanceCountHelper&) { \ |
| 53 sk_atomic_inc(GetInstanceCountPtr()); \ | 56 sk_atomic_inc(GetInstanceCountPtr()); \ |
| 54 } \ | 57 } \ |
| 55 \ | 58 \ |
| 56 ~SkInstanceCountHelper() { \ | 59 ~SkInstanceCountHelper() { \ |
| 57 sk_atomic_dec(GetInstanceCountPtr()); \ | 60 sk_atomic_dec(GetInstanceCountPtr()); \ |
| 58 } \ | 61 } \ |
| 59 \ | 62 \ |
| 60 static int32_t* GetInstanceCountPtr() { \ | 63 static int32_t* GetInstanceCountPtr() { \ |
| 61 static int32_t gInstanceCount; \ | 64 static int32_t gInstanceCount; \ |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 79 \ | 82 \ |
| 80 static int CheckInstanceCount(int level = 0, bool cleanUp = false) { \ | 83 static int CheckInstanceCount(int level = 0, bool cleanUp = false) { \ |
| 81 if (gPrintInstCount && 0 != GetInstanceCount()) { \ | 84 if (gPrintInstCount && 0 != GetInstanceCount()) { \ |
| 82 SkDebugf("%*c Leaked %s: %d\n", \ | 85 SkDebugf("%*c Leaked %s: %d\n", \ |
| 83 4*level, ' ', #className, \ | 86 4*level, ' ', #className, \ |
| 84 GetInstanceCount()); \ | 87 GetInstanceCount()); \ |
| 85 } \ | 88 } \ |
| 86 if (NULL == SkInstanceCountHelper::GetChildren()) { \ | 89 if (NULL == SkInstanceCountHelper::GetChildren()) { \ |
| 87 return GetInstanceCount(); \ | 90 return GetInstanceCount(); \ |
| 88 } \ | 91 } \ |
| 89 SkTArray<int (*)(int, bool)>* children = \ | 92 SkTArray<typename SkInstanceCountHelper::PFCheckInstCnt>* children = \ |
| 90 SkInstanceCountHelper::GetChildren(); \ | 93 SkInstanceCountHelper::GetChildren(); \ |
| 91 int childCount = children->count(); \ | 94 int childCount = children->count(); \ |
| 92 int count = GetInstanceCount(); \ | 95 int count = GetInstanceCount(); \ |
| 93 for (int i = 0; i < childCount; ++i) { \ | 96 for (int i = 0; i < childCount; ++i) { \ |
| 94 count -= (*(*children)[i])(level+1, cleanUp); \ | 97 count -= (*(*children)[i])(level+1, cleanUp); \ |
| 95 } \ | 98 } \ |
| 96 SkASSERT(count >= 0); \ | 99 SkASSERT(count >= 0); \ |
| 97 if (gPrintInstCount && childCount > 0 && count > 0) { \ | 100 if (gPrintInstCount && childCount > 0 && count > 0) { \ |
| 98 SkDebugf("%*c Leaked ???: %d\n", 4*(level + 1), ' ', count); \ | 101 SkDebugf("%*c Leaked ???: %d\n", 4*(level + 1), ' ', count); \ |
| 99 } \ | 102 } \ |
| 100 if (cleanUp) { \ | 103 if (cleanUp) { \ |
| 101 delete children; \ | 104 delete children; \ |
| 102 SkInstanceCountHelper::GetChildren() = NULL; \ | 105 SkInstanceCountHelper::GetChildren() = NULL; \ |
| 103 } \ | 106 } \ |
| 104 return GetInstanceCount(); \ | 107 return GetInstanceCount(); \ |
| 105 } \ | 108 } \ |
| 106 \ | 109 \ |
| 107 static void AddInstChild(int (*childCheckInstCnt)(int, bool)) { \ | 110 static void AddInstChild(int (*childCheckInstCnt)(int, bool)) { \ |
| 108 if (CheckInstanceCount != childCheckInstCnt && \ | 111 if (CheckInstanceCount != childCheckInstCnt) { \ |
| 109 NULL != SkInstanceCountHelper::GetChildren()) { \ | 112 SkAutoMutexAcquire ama(SkGetInstCntAddInstChildMutex()); \ |
|
mtklein
2013/12/05 16:06:54
Does this one really need to be a global mutex? S
Kimmo Kinnunen
2013/12/09 07:54:51
Well, they could.. I didn't think they should.
Ch
| |
| 113 if (NULL == SkInstanceCountHelper::GetChildren()) { \ | |
| 114 SkInstanceCountHelper::GetChildren() = \ | |
| 115 new SkTArray<typename SkInstanceCountHelper::PFCheckInstCnt> ; \ | |
| 116 } \ | |
| 110 SkInstanceCountHelper::GetChildren()->push_back(childCheckInstCnt); \ | 117 SkInstanceCountHelper::GetChildren()->push_back(childCheckInstCnt); \ |
| 111 } \ | 118 } \ |
| 112 } | 119 } |
| 113 | 120 |
| 114 #else | 121 #else |
| 115 // Typically SK_ENABLE_INST_COUNT=0. Make sure the class declares public typedef INHERITED by | 122 // Typically SK_ENABLE_INST_COUNT=0. Make sure the class declares public typedef INHERITED by |
| 116 // causing a compile-time error if the typedef is missing. This way SK_ENABLE_IN ST_COUNT=1 stays | 123 // causing a compile-time error if the typedef is missing. This way SK_ENABLE_IN ST_COUNT=1 stays |
| 117 // compiling. | 124 // compiling. |
| 118 #define SK_DECLARE_INST_COUNT(className) static void AddInstChild() { INHERITED: :AddInstChild(); } | 125 #define SK_DECLARE_INST_COUNT(className) static void AddInstChild() { INHERITED: :AddInstChild(); } |
| 119 #define SK_DECLARE_INST_COUNT_ROOT(className) static void AddInstChild() { } | 126 #define SK_DECLARE_INST_COUNT_ROOT(className) static void AddInstChild() { } |
| 120 #endif | 127 #endif |
| 121 | 128 |
| 122 // Following are deprecated. They are defined only for backwards API compatibili ty. | 129 // Following are deprecated. They are defined only for backwards API compatibili ty. |
| 123 #define SK_DECLARE_INST_COUNT_TEMPLATE(className) SK_DECLARE_INST_COUNT(classNam e) | 130 #define SK_DECLARE_INST_COUNT_TEMPLATE(className) SK_DECLARE_INST_COUNT(classNam e) |
| 124 #define SK_DEFINE_INST_COUNT(className) | 131 #define SK_DEFINE_INST_COUNT(className) |
| 125 #define SK_DEFINE_INST_COUNT_TEMPLATE(templateInfo, className) | 132 #define SK_DEFINE_INST_COUNT_TEMPLATE(templateInfo, className) |
| 126 | 133 |
| 127 #endif // SkInstCnt_DEFINED | 134 #endif // SkInstCnt_DEFINED |
| OLD | NEW |